Skip to content

Commit 8bb7182

Browse files
committed
Use GitHub Status API
1 parent b54189d commit 8bb7182

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22

33
const debug = require('debug')('display_travis_status')
44
const pollTravis = require('../lib/pollTravis')
5-
const enabledRepos = ['citgm', 'readable-stream', 'nodejs.org']
5+
const enabledRepos = ['citgm', 'readable-stream', 'nodejs.org', 'test-github-bot']
66

77
module.exports = function (app) {
8-
app.on('pull_request.opened', (event) => {
8+
app.on('pull_request.opened', handlePrUpdate)
9+
// Pull Request updates
10+
app.on('pull_request.synchronize', handlePrUpdate)
11+
12+
function handlePrUpdate(event) {
913
const owner = event.repository.owner.login
1014
const repo = event.repository.name
1115
if (!~enabledRepos.indexOf(repo)) return
1216

1317
debug(`/${owner}/${repo}/pull/${event.number} opened`)
14-
pollTravis.pollThenComment(owner, repo, event.number)
15-
})
18+
pollTravis.pollThenStatus(owner, repo, event.number)
19+
}
1620

1721
// to trigger polling manually
1822
app.get('/pr/:owner/:repo/:id', (req, res) => {
1923
const owner = req.params.owner
2024
const repo = req.params.repo
2125
const id = req.params.id
2226
if (~enabledRepos.indexOf(repo)) {
23-
pollTravis.pollThenComment(owner, repo, parseInt(id, 10))
27+
pollTravis.pollThenStatus(owner, repo, parseInt(id, 10))
2428
}
2529
res.end()
2630
})

0 commit comments

Comments
 (0)