Skip to content

Commit 2100c34

Browse files
authored
Merge pull request #5 from artstorm/feature/github-checkrunner-id
GitHub CheckRunner Automatic ID Handling
2 parents e97bc61 + 82fa1ee commit 2100c34

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ Start a check run for a specific build build.
286286

287287
```groovy
288288
steps {
289-
checkRunIosId = github.createCheckRun("build_ios", "queued")
289+
script {
290+
github.createCheckRun('Build iOS', 'queued')
291+
}
290292
}
291293
```
292294

@@ -295,16 +297,22 @@ steps {
295297
Update a check run.
296298

297299
```groovy
300+
steps {
301+
script {
302+
github.updateCheckRun('Build iOS', 'in_progress')
303+
}
304+
}
305+
298306
post {
299307
failure {
300308
script {
301-
github.updateCheckRun(checkRunIosId, '', 'failure')
309+
github.updateCheckRun('Build iOS', '', 'failure')
302310
}
303311
}
304312
305313
success {
306314
script {
307-
github.updateCheckRun(checkRunIosId, '', 'success')
315+
github.updateCheckRun('Build iOS', '', 'success')
308316
}
309317
}
310318
```

vars/github.groovy

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* - git.commitSha()
1919
*/
2020

21+
// Dictionary that holds the id for each created checkrun. name => id.
22+
@groovy.transform.Field private def checkRuns = [:]
23+
2124
/**
2225
* Creates or updates the Jenkins bot issue comment for the pull request.
2326
*
@@ -177,7 +180,7 @@ def updateIssueComment(int id, String body) {
177180
*
178181
* As we have an GitHub app, we use the checks API instead of using the older commit status API.
179182
*/
180-
def createCheckRun(String name, String status, String url = '') {
183+
def createCheckRun(String name, String status = '', String conclusion = '', String url = '') {
181184
withCredentials([usernamePassword(credentialsId: 'githubapp-jenkins',
182185
usernameVariable: 'GITHUB_APP',
183186
passwordVariable: 'GITHUB_ACCESS_TOKEN')]) {
@@ -187,8 +190,9 @@ def createCheckRun(String name, String status, String url = '') {
187190
'head_sha': sha,
188191
'name': name,
189192
'external_id': env.BUILD_NUMBER,
190-
'status': status
191193
]
194+
if (status.length() > 0) payload['status'] = status
195+
if (conclusion.length() > 0) payload['conclusion'] = conclusion
192196
if (url.length() > 0) payload['details_url'] = url
193197
String json = writeJSON returnText: true, json: payload
194198

@@ -203,7 +207,7 @@ def createCheckRun(String name, String status, String url = '') {
203207
)
204208

205209
def checkRun = readJSON text: response.content
206-
return checkRun.id
210+
this.checkRuns[name] = checkRun.id
207211
}
208212
}
209213

@@ -217,12 +221,13 @@ def createCheckRun(String name, String status, String url = '') {
217221
*
218222
* As we have an GitHub app, we use the checks API instead of using the older commit status API.
219223
*/
220-
def updateCheckRun(id, String status = '', String conclusion = '') {
224+
def updateCheckRun(String name, String status = '', String conclusion = '') {
221225
withCredentials([usernamePassword(credentialsId: 'githubapp-jenkins',
222226
usernameVariable: 'GITHUB_APP',
223227
passwordVariable: 'GITHUB_ACCESS_TOKEN')]) {
224228
def sha = git.commitSha();
225229
def ownerRepo = ownerRepo()
230+
def id = this.checkRuns[name]
226231

227232
def payload = [
228233
'head_sha': sha

0 commit comments

Comments
 (0)