Skip to content

Commit dc2f1cd

Browse files
authored
fix: don't crash when posting the CI link comment fails (#707)
pushStarted() posts a "CI: <url>" (or "V8 CI:" / "Lite-CI:") comment on the pull request when Jenkins reports a pending build, but called createPrComment() without awaiting it or attaching a rejection handler. createPrComment() logs the error and rethrows it, so any GitHub API failure while commenting (5xx, rate limiting, locked PR, timeout) turned into an unhandled promise rejection. server.js treats those as fatal and exits the process, taking down the bot because of a best-effort comment. Route the three comment calls through a small postCiComment() helper that swallows the already-logged rejection, so the commit status update and the rest of the bot keep working. Add a unit test which makes the comment endpoint return 500 and asserts that the status is still created and no unhandled rejection is raised; it fails without this change. Fixes: #704 Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
1 parent 5fdf35d commit dc2f1cd

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎lib/push-jenkins-update.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export function pushStarted (options, build, cb) {
1313
if (build.status === 'pending') {
1414
switch (build.identifier) {
1515
case 'node-test-pull-request':
16-
createPrComment(Object.assign({ issue_number: pr }, options), `CI: ${build.url}`)
16+
postCiComment(options, pr, `CI: ${build.url}`)
1717
break
1818

1919
case 'node-test-commit-v8-linux':
20-
createPrComment(Object.assign({ issue_number: pr }, options), `V8 CI: ${build.url}`)
20+
postCiComment(options, pr, `V8 CI: ${build.url}`)
2121
break
2222

2323
case 'node-test-pull-request-lite-pipeline':
24-
createPrComment(Object.assign({ issue_number: pr }, options), `Lite-CI: ${build.url}`)
24+
postCiComment(options, pr, `Lite-CI: ${build.url}`)
2525
break
2626

2727
default:
@@ -70,6 +70,12 @@ export function pushEnded (options, build, cb) {
7070
})
7171
}
7272

73+
function postCiComment (options, pr, body) {
74+
// posting the CI link is best effort: createPrComment() has already logged any error,
75+
// and letting it surface as an unhandled rejection would terminate the bot process
76+
createPrComment(Object.assign({ issue_number: pr }, options), body).catch(() => {})
77+
}
78+
7379
function findPrInRef (gitRef) {
7480
// refs/pull/12345/head
7581
return parseInt(gitRef.split('/')[2], 10)

‎test/unit/push-jenkins-update.test.js‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'node:test'
22

3-
import { findLatestCommitInPr } from '../../lib/push-jenkins-update.js'
3+
import { findLatestCommitInPr, pushStarted } from '../../lib/push-jenkins-update.js'
44

55
import fetchMock from 'fetch-mock'
66
import readFixture from '../read-fixture.js'
@@ -65,3 +65,27 @@ test('findLatestCommitInPr: paginates results when more than 100 commits in a PR
6565
thirdPageScope.callHistory.called()
6666
fourthPageScope.callHistory.called()
6767
})
68+
69+
test('pushStarted: updates the commit status even when posting the CI comment fails', (t, done) => {
70+
const build = readFixture('jenkins-test-pull-request-success-payload.json')
71+
build.status = 'pending'
72+
const logger = { info: () => {}, error: () => {}, debug: () => {}, child: function () { return this } }
73+
74+
fetchMock.mockGlobal()
75+
76+
const commentUrl = 'https://api.github.com/repos/nodejs/node/issues/12345/comments'
77+
fetchMock.route({ url: commentUrl, method: 'POST' }, 500)
78+
fetchMock.route('https://api.github.com/repos/nodejs/node/pulls/12345/commits', readFixture('pr-commits.json'))
79+
const statusUrl = 'https://api.github.com/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1'
80+
fetchMock.route({ url: statusUrl, method: 'POST' }, 201)
81+
82+
t.plan(3)
83+
84+
pushStarted({ owner: 'nodejs', repo: 'node', logger }, build, (err) => {
85+
t.assert.strictEqual(err, undefined)
86+
t.assert.strictEqual(fetchMock.callHistory.called(commentUrl), true)
87+
t.assert.strictEqual(fetchMock.callHistory.called(statusUrl), true)
88+
// give a rejected comment promise the chance to surface as an unhandled rejection
89+
setImmediate(done)
90+
})
91+
})

0 commit comments

Comments
 (0)