Skip to content

Commit 7be5ef6

Browse files
committed
Use GitHub Status API
1 parent 8824234 commit 7be5ef6

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

lib/pollTravis.js

Lines changed: 33 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,14 @@ 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+
if (checkNumber === 1) {
149+
createGhStatus('pending', lastBuildForCommit.id, 'build in progress')
150+
}
146151
} else {
147152
return console.log(`* ${prInfo} Unknown build state: "${lastState}", stopping polling`)
148153
}
@@ -157,7 +162,7 @@ function pollTravisBuildBySha (options, checkNumber) {
157162
function createGhCommentFn (options) {
158163
const prInfo = prInfoStr(options)
159164

160-
return (message, cb) => {
165+
return (message) => {
161166
githubClient.issues.createComment({
162167
user: options.owner,
163168
repo: options.repoName,
@@ -172,8 +177,27 @@ function createGhCommentFn (options) {
172177
}
173178
}
174179

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

server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ app.all('/hooks/github', (req, res) => {
1616
const repo = req.body.repository
1717

1818
console.log(`* ${repo.owner.login}/${repo.name}/#${req.body.number} Opened, starting build checks!`)
19-
pollTravis.pollThenComment(repo.owner.login, repo.name, parseInt(req.body.number))
19+
pollTravis.pollThenStatus(repo.owner.login, repo.name, parseInt(req.body.number))
2020
}
2121

2222
res.end()
2323
})
2424

2525
// to trigger polling manually
2626
app.get('/pr/:owner/:repo/:prId', (req, res) => {
27-
pollTravis.pollThenComment(req.params.owner, req.params.repo, parseInt(req.params.prId))
27+
pollTravis.pollThenStatus(req.params.owner, req.params.repo, parseInt(req.params.prId))
2828
res.end()
2929
})
3030

0 commit comments

Comments
 (0)