Skip to content

Commit 1ad1879

Browse files
authored
[CI] Record Github commit statuses outside of PRs (#69432)
1 parent 1cdeec0 commit 1ad1879

File tree

9 files changed

+229
-5
lines changed

9 files changed

+229
-5
lines changed

.ci/pipeline-library/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
implementation 'org.jenkins-ci.plugins.workflow:workflow-step-api:2.19@jar'
2121
testImplementation 'com.lesfurets:jenkins-pipeline-unit:1.4'
2222
testImplementation 'junit:junit:4.12'
23+
testImplementation 'org.mockito:mockito-core:2.+'
2324
testImplementation 'org.assertj:assertj-core:3.15+' // Temporary https://github.com/jenkinsci/JenkinsPipelineUnit/issues/209
2425
}
2526

.ci/pipeline-library/src/test/KibanaBasePipelineTest.groovy

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class KibanaBasePipelineTest extends BasePipelineTest {
1919
env.BUILD_DISPLAY_NAME = "#${env.BUILD_ID}"
2020

2121
env.JENKINS_URL = 'http://jenkins.localhost:8080'
22-
env.BUILD_URL = "${env.JENKINS_URL}/job/elastic+kibana+${env.BRANCH_NAME}/${env.BUILD_ID}/"
22+
env.BUILD_URL = "${env.JENKINS_URL}/job/elastic+kibana+${env.BRANCH_NAME}/${env.BUILD_ID}/".toString()
2323

24-
env.JOB_BASE_NAME = "elastic / kibana # ${env.BRANCH_NAME}"
24+
env.JOB_BASE_NAME = "elastic / kibana # ${env.BRANCH_NAME}".toString()
2525
env.JOB_NAME = env.JOB_BASE_NAME
2626

2727
env.WORKSPACE = 'WS'
@@ -31,6 +31,9 @@ class KibanaBasePipelineTest extends BasePipelineTest {
3131
getBuildStatus: { 'SUCCESS' },
3232
printStacktrace: { ex -> print ex },
3333
],
34+
githubPr: [
35+
isPr: { false },
36+
],
3437
jenkinsApi: [ getFailedSteps: { [] } ],
3538
testUtils: [ getFailures: { [] } ],
3639
])
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import org.junit.*
2+
import static groovy.test.GroovyAssert.*
3+
4+
class BuildStateTest extends KibanaBasePipelineTest {
5+
def buildState
6+
7+
@Before
8+
void setUp() {
9+
super.setUp()
10+
11+
buildState = loadScript("vars/buildState.groovy")
12+
}
13+
14+
@Test
15+
void 'get() returns existing data'() {
16+
buildState.add('test', 1)
17+
def actual = buildState.get('test')
18+
assertEquals(1, actual)
19+
}
20+
21+
@Test
22+
void 'get() returns null for missing data'() {
23+
def actual = buildState.get('missing_key')
24+
assertEquals(null, actual)
25+
}
26+
27+
@Test
28+
void 'add() does not overwrite existing keys'() {
29+
assertTrue(buildState.add('test', 1))
30+
assertFalse(buildState.add('test', 2))
31+
32+
def actual = buildState.get('test')
33+
34+
assertEquals(1, actual)
35+
}
36+
37+
@Test
38+
void 'set() overwrites existing keys'() {
39+
assertFalse(buildState.has('test'))
40+
buildState.set('test', 1)
41+
assertTrue(buildState.has('test'))
42+
buildState.set('test', 2)
43+
44+
def actual = buildState.get('test')
45+
46+
assertEquals(2, actual)
47+
}
48+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import org.junit.*
2+
import static org.mockito.Mockito.*;
3+
4+
class GithubCommitStatusTest extends KibanaBasePipelineTest {
5+
def githubCommitStatus
6+
def githubApiMock
7+
def buildStateMock
8+
9+
def EXPECTED_STATUS_URL = 'repos/elastic/kibana/statuses/COMMIT_HASH'
10+
def EXPECTED_CONTEXT = 'kibana-ci'
11+
def EXPECTED_BUILD_URL = 'http://jenkins.localhost:8080/job/elastic+kibana+master/1/'
12+
13+
interface BuildState {
14+
Object get(String key)
15+
}
16+
17+
interface GithubApi {
18+
Object post(String url, Map data)
19+
}
20+
21+
@Before
22+
void setUp() {
23+
super.setUp()
24+
25+
buildStateMock = mock(BuildState)
26+
githubApiMock = mock(GithubApi)
27+
28+
when(buildStateMock.get('checkoutInfo')).thenReturn([ commit: 'COMMIT_HASH', ])
29+
when(githubApiMock.post(any(), any())).thenReturn(null)
30+
31+
props([
32+
buildState: buildStateMock,
33+
githubApi: githubApiMock,
34+
])
35+
36+
githubCommitStatus = loadScript("vars/githubCommitStatus.groovy")
37+
}
38+
39+
void verifyStatusCreate(String state, String description) {
40+
verify(githubApiMock).post(
41+
EXPECTED_STATUS_URL,
42+
[
43+
'state': state,
44+
'description': description,
45+
'context': EXPECTED_CONTEXT,
46+
'target_url': EXPECTED_BUILD_URL,
47+
]
48+
)
49+
}
50+
51+
@Test
52+
void 'onStart() should create a pending status'() {
53+
githubCommitStatus.onStart()
54+
verifyStatusCreate('pending', 'Build started.')
55+
}
56+
57+
@Test
58+
void 'onFinish() should create a success status'() {
59+
githubCommitStatus.onFinish()
60+
verifyStatusCreate('success', 'Build completed successfully.')
61+
}
62+
63+
@Test
64+
void 'onFinish() should create an error status for failed builds'() {
65+
mockFailureBuild()
66+
githubCommitStatus.onFinish()
67+
verifyStatusCreate('error', 'Build failed.')
68+
}
69+
70+
@Test
71+
void 'onStart() should exit early for PRs'() {
72+
prop('githubPr', [ isPr: { true } ])
73+
74+
githubCommitStatus.onStart()
75+
verifyZeroInteractions(githubApiMock)
76+
}
77+
78+
@Test
79+
void 'onFinish() should exit early for PRs'() {
80+
prop('githubPr', [ isPr: { true } ])
81+
82+
githubCommitStatus.onFinish()
83+
verifyZeroInteractions(githubApiMock)
84+
}
85+
}

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
library 'kibana-pipeline-library'
44
kibanaLibrary.load()
55

6-
kibanaPipeline(timeoutMinutes: 155, checkPrChanges: true) {
6+
kibanaPipeline(timeoutMinutes: 155, checkPrChanges: true, setCommitStatus: true) {
77
githubPr.withDefaultPrComments {
88
ciStats.trackBuild {
99
catchError {

vars/buildState.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import groovy.transform.Field
2+
3+
public static @Field JENKINS_BUILD_STATE = [:]
4+
5+
def add(key, value) {
6+
if (!buildState.JENKINS_BUILD_STATE.containsKey(key)) {
7+
buildState.JENKINS_BUILD_STATE[key] = value
8+
return true
9+
}
10+
11+
return false
12+
}
13+
14+
def set(key, value) {
15+
buildState.JENKINS_BUILD_STATE[key] = value
16+
}
17+
18+
def get(key) {
19+
return buildState.JENKINS_BUILD_STATE[key]
20+
}
21+
22+
def has(key) {
23+
return buildState.JENKINS_BUILD_STATE.containsKey(key)
24+
}
25+
26+
def get() {
27+
return buildState.JENKINS_BUILD_STATE
28+
}
29+
30+
return this

vars/githubCommitStatus.groovy

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def shouldCreateStatuses() {
2+
return !githubPr.isPr() && buildState.get('checkoutInfo')
3+
}
4+
5+
def onStart() {
6+
catchError {
7+
if (!shouldCreateStatuses()) {
8+
return
9+
}
10+
11+
def checkoutInfo = buildState.get('checkoutInfo')
12+
create(checkoutInfo.commit, 'pending', 'Build started.')
13+
}
14+
}
15+
16+
def onFinish() {
17+
catchError {
18+
if (!shouldCreateStatuses()) {
19+
return
20+
}
21+
22+
def checkoutInfo = buildState.get('checkoutInfo')
23+
def status = buildUtils.getBuildStatus()
24+
25+
if (status == 'SUCCESS' || status == 'UNSTABLE') {
26+
create(checkoutInfo.commit, 'success', 'Build completed successfully.')
27+
} else if(status == 'ABORTED') {
28+
create(checkoutInfo.commit, 'error', 'Build aborted or timed out.')
29+
} else {
30+
create(checkoutInfo.commit, 'error', 'Build failed.')
31+
}
32+
}
33+
}
34+
35+
// state: error|failure|pending|success
36+
def create(sha, state, description, context = 'kibana-ci') {
37+
withGithubCredentials {
38+
return githubApi.post("repos/elastic/kibana/statuses/${sha}", [ state: state, description: description, context: context, target_url: env.BUILD_URL ])
39+
}
40+
}
41+
42+
return this

vars/kibanaPipeline.groovy

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,15 @@ def runErrorReporter() {
214214
}
215215

216216
def call(Map params = [:], Closure closure) {
217-
def config = [timeoutMinutes: 135, checkPrChanges: false] + params
217+
def config = [timeoutMinutes: 135, checkPrChanges: false, setCommitStatus: false] + params
218218

219219
stage("Kibana Pipeline") {
220220
timeout(time: config.timeoutMinutes, unit: 'MINUTES') {
221221
timestamps {
222222
ansiColor('xterm') {
223+
if (config.setCommitStatus) {
224+
buildState.set('shouldSetCommitStatus', true)
225+
}
223226
if (config.checkPrChanges && githubPr.isPr()) {
224227
pipelineLibraryTests()
225228

@@ -230,7 +233,13 @@ def call(Map params = [:], Closure closure) {
230233
return
231234
}
232235
}
233-
closure()
236+
try {
237+
closure()
238+
} finally {
239+
if (config.setCommitStatus) {
240+
githubCommitStatus.onFinish()
241+
}
242+
}
234243
}
235244
}
236245
}

vars/workers.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ def base(Map params, Closure closure) {
6565

6666
dir("kibana") {
6767
checkoutInfo = getCheckoutInfo()
68+
69+
// use `checkoutInfo` as a flag to indicate that we've already reported the pending commit status
70+
if (buildState.get('shouldSetCommitStatus') && !buildState.has('checkoutInfo')) {
71+
buildState.set('checkoutInfo', checkoutInfo)
72+
githubCommitStatus.onStart()
73+
}
6874
}
6975

7076
ciStats.reportGitInfo(

0 commit comments

Comments
 (0)