Skip to content

Commit 2db3264

Browse files
authored
jenkins: ignore build statuses not related to pull requests (nodejs#177)
We have for a long time seen errors logged when Jenkins tells the bot to report inline pull requests statuses, when the build in question is not related to a pull request, but an ordinary branch like the "v8.x-staging" branch. In short the error raised by github.com said we couldn't fetch commits related to pull request "v8.x-staging". It expects a pull request number to be provided when fetching those commits. These changes checks if the git reference provided by Jenkins does in fact point to a pull request, if not the incoming request from Jenkins gets a 400 Bad Request as response. ``` 13:01:11.244 ERROR bot: Got error when retrieving GitHub commits for PR (req_id=78e986d9-6c52-485c-bac2-64c2797a7a40, pr=v8.x-staging, job=node-test-commit-aix, status=failure) err: { "code": "400", "status": "Bad Request", "message": "Invalid value for parameter 'number': v8.x-staging" } ```
1 parent 828e197 commit 2db3264

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

scripts/jenkins-status.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ function isJenkinsIpWhitelisted (req) {
1616
return true
1717
}
1818

19+
function isRelatedToPullRequest (gitRef) {
20+
// refs/pull/12345/head vs refs/heads/v8.x-staging/head
21+
return gitRef.includes('/pull/')
22+
}
23+
1924
module.exports = function (app) {
2025
app.post('/:repo/jenkins/start', (req, res) => {
2126
const isValid = pushJenkinsUpdate.validate(req.body)
@@ -25,6 +30,10 @@ module.exports = function (app) {
2530
return res.status(400).end('Invalid payload')
2631
}
2732

33+
if (!isRelatedToPullRequest(req.body.ref)) {
34+
return res.status(400).end('Will only push builds related to pull requests')
35+
}
36+
2837
if (!enabledRepos.includes(repo)) {
2938
return res.status(400).end('Invalid repository')
3039
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"identifier": "node-test-commit-aix",
3+
"status": "failure",
4+
"message": "tests failed",
5+
"commit": "084ef6016994a41ffbbe9e48584abe874a197d43",
6+
"url": "https://ci.nodejs.org/job/node-test-commit-aix/15657/",
7+
"ref": "refs/heads/v8.x-staging"
8+
}

test/integration/push-jenkins-update.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ tap.test('Responds with 400 / "Bad request" when incoming request has invalid pa
115115
})
116116
})
117117

118+
tap.test('Responds with 400 / "Bad request" when incoming request is not related to a pull request', (t) => {
119+
const fixture = readFixture('jenkins-staging-failure-payload.json')
120+
121+
// don't care about the results, just want to prevent any HTTP request ever being made
122+
nock('https://api.github.com')
123+
124+
t.plan(1)
125+
126+
supertest(app)
127+
.post('/node/jenkins/start')
128+
.send(fixture)
129+
.expect(400, 'Will only push builds related to pull requests')
130+
.end((err, res) => {
131+
t.equal(err, null)
132+
})
133+
})
134+
118135
tap.test('Responds with 400 / "Bad request" when incoming providing invalid repository name', (t) => {
119136
const fixture = readFixture('pending-payload.json')
120137

0 commit comments

Comments
 (0)