Skip to content

Post CI comment in PR for node-test-pull-request builds #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/github-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const githubClient = require('./github-client')

exports.createPrComment = function createPrComment ({ owner, repo, number, logger }, body) {
githubClient.issues.createComment({
owner,
repo,
number,
body
}, (err) => {
if (err) {
logger.error(err, 'Error while creating comment on GitHub')
}
})
}
5 changes: 5 additions & 0 deletions lib/push-jenkins-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const url = require('url')

const githubClient = require('./github-client')
const { createPrComment } = require('./github-comment')

function pushStarted (options, build, cb) {
const pr = findPrInRef(build.ref)
Expand All @@ -13,6 +14,10 @@ function pushStarted (options, build, cb) {

const optsWithPr = Object.assign({ pr }, options)

if (build.identifier === 'node-test-pull-request' && build.status === 'pending') {
createPrComment(Object.assign({ number: pr }, options), `CI: ${build.url}`)
}

findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
if (err) {
logger.error(err, 'Got error when retrieving GitHub commits for PR')
Expand Down
14 changes: 1 addition & 13 deletions scripts/trigger-jenkins-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const request = require('request')

const githubClient = require('../lib/github-client')
const botUsername = require('../lib/bot-username')
const { createPrComment } = require('../lib/github-comment')

const jenkinsApiCredentials = process.env.JENKINS_API_CREDENTIALS || ''

Expand Down Expand Up @@ -85,19 +86,6 @@ function triggerBuild (options, cb) {
})
}

function createPrComment ({ owner, repo, number, logger }, body) {
githubClient.issues.createComment({
owner,
repo,
number,
body
}, (err) => {
if (err) {
logger.error(err, 'Error while creating comment to reply on CI run comment')
}
})
}

function triggerBuildIfValid (options) {
const { owner, repo, author, logger } = options

Expand Down
8 changes: 8 additions & 0 deletions test/_fixtures/jenkins-test-pull-request-success-payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"identifier": "node-test-pull-request",
"status": "pending",
"message": "running tests",
"commit": "8a5fec2a6bade91e544a30314d7cf21f8a200de1",
"url": "https://ci.nodejs.org/job/node-test-pull-request/21633/",
"ref": "refs/pull/12345/head"
}
26 changes: 26 additions & 0 deletions test/integration/push-jenkins-update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ tap.test('Forwards payload provided in incoming POST to GitHub status API', (t)
})
})

tap.test('Posts a CI comment in the related PR when Jenkins build is named node-test-pull-request', (t) => {
const fixture = readFixture('jenkins-test-pull-request-success-payload.json')
const commentScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/issues/12345/comments', { body: 'CI: https://ci.nodejs.org/job/node-test-pull-request/21633/' })
.reply(200)

// we don't care about asserting the scopes below, just want to stop the requests from actually being sent
setupGetCommitsMock('node')
nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1')
.reply(201)

t.plan(1)

supertest(app)
.post('/node/jenkins/start')
.send(fixture)
.expect(201)
.end((err, res) => {
commentScope.done()
t.equal(err, null)
})
})

tap.test('Responds with 400 / "Bad request" when incoming request has invalid payload', (t) => {
const fixture = readFixture('invalid-payload.json')

Expand Down