Skip to content

Commit 10cf185

Browse files
committed
Use GitHub Status API
1 parent b54189d commit 10cf185

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

lib/pollTravis.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ githubClient.authenticate({
2222
token: process.env.GITHUB_TOKEN
2323
})
2424

25-
function pollThenComment (owner, repoName, prId) {
25+
exports.pollThenStatus = pollThenStatus
26+
27+
function pollThenStatus (owner, repoName, prId) {
2628
const prInfo = prInfoStr({ owner, repoName, prId })
2729

2830
// we have to figure out what type of Travis polling we should perform,
@@ -37,7 +39,7 @@ function pollThenComment (owner, repoName, prId) {
3739
if (hasAnyPrBuilds) {
3840
pollByPrThenComment(owner, repoName, prId)
3941
} else {
40-
pollByCommitThenComment(owner, repoName, prId)
42+
pollByCommitThenStatus(owner, repoName, prId)
4143
}
4244
})
4345
}
@@ -92,7 +94,7 @@ function pollByPrThenComment (owner, repoName, prId, checkNumber) {
9294
*
9395
* This is the case for readable-stream.
9496
*/
95-
function pollByCommitThenComment (owner, repoName, prId) {
97+
function pollByCommitThenStatus (owner, repoName, prId) {
9698
const prInfo = prInfoStr({ owner, repoName, prId })
9799

98100
githubClient.pullRequests.getCommits({
@@ -111,7 +113,7 @@ function pollByCommitThenComment (owner, repoName, prId) {
111113
}
112114

113115
function pollTravisBuildBySha (options, checkNumber) {
114-
const createGhComment = createGhCommentFn(options)
116+
const createGhStatus = createGhStatusFn(options)
115117
const prInfo = prInfoStr(options)
116118
const shaToMatch = options.lastSha
117119

@@ -138,11 +140,12 @@ function pollTravisBuildBySha (options, checkNumber) {
138140
const lastState = lastBuildForCommit.state
139141

140142
if (lastState === 'passed') {
141-
return createGhComment(`[Travis build passed](https://travis-ci.org/${options.owner}/${options.repoName}/builds/${lastBuildForCommit.id}) :+1:`)
143+
return createGhStatus('success', lastBuildForCommit.id, 'all tests passed')
142144
} else if (lastState === 'failed') {
143-
return createGhComment(`[Travis build failed](https://travis-ci.org/${options.owner}/${options.repoName}/builds/${lastBuildForCommit.id}) :-1:`)
145+
return createGhStatus('failure', lastBuildForCommit.id, 'build failure')
144146
} else if (~['created', 'started'].indexOf(lastState)) {
145147
console.log(`* ${prInfo} "${lastState}" build found, will do check #${checkNumber + 1} in 30 seconds`)
148+
createGhStatus('pending', lastBuildForCommit.id, 'build in progress')
146149
} else {
147150
return console.log(`* ${prInfo} Unknown build state: "${lastState}", stopping polling`)
148151
}
@@ -157,7 +160,7 @@ function pollTravisBuildBySha (options, checkNumber) {
157160
function createGhCommentFn (options) {
158161
const prInfo = prInfoStr(options)
159162

160-
return (message, cb) => {
163+
return (message) => {
161164
githubClient.issues.createComment({
162165
user: options.owner,
163166
repo: options.repoName,
@@ -172,8 +175,27 @@ function createGhCommentFn (options) {
172175
}
173176
}
174177

178+
function createGhStatusFn (options) {
179+
const prInfo = prInfoStr(options)
180+
181+
return (state, travisId, message) => {
182+
githubClient.statuses.create({
183+
user: options.owner,
184+
repo: options.repoName,
185+
sha: options.lastSha,
186+
target_url: `https://travis-ci.org/${options.owner}/${options.repoName}/builds/${travisId}`,
187+
context: 'Travis CI via nodejs-github-bot',
188+
state: state,
189+
description: message
190+
}, (err, res) => {
191+
if (err) {
192+
return console.error(`! ${prInfo} Error while updating GitHub PR status`, err.stack)
193+
}
194+
console.log(`* ${prInfo} Github PR status updated`)
195+
})
196+
}
197+
}
198+
175199
function prInfoStr (options) {
176200
return `${options.owner}/${options.repoName}/#${options.prId}`
177201
}
178-
179-
exports.pollThenComment = pollThenComment

scripts/display-travis-status.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = function (app) {
1111
if (!~enabledRepos.indexOf(repo)) return
1212

1313
debug(`/${owner}/${repo}/pull/${event.number} opened`)
14-
pollTravis.pollThenComment(owner, repo, event.number)
14+
pollTravis.pollThenStatus(owner, repo, event.number)
1515
})
1616

1717
// to trigger polling manually
@@ -20,7 +20,7 @@ module.exports = function (app) {
2020
const repo = req.params.repo
2121
const id = req.params.id
2222
if (~enabledRepos.indexOf(repo)) {
23-
pollTravis.pollThenComment(owner, repo, parseInt(id, 10))
23+
pollTravis.pollThenStatus(owner, repo, parseInt(id, 10))
2424
}
2525
res.end()
2626
})

0 commit comments

Comments
 (0)