Skip to content

Commit 4363a1c

Browse files
committed
Use GitHub Status API
1 parent 8824234 commit 4363a1c

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

lib/pollTravis.js

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

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

2831
// we have to figure out what type of Travis polling we should perform,
@@ -37,7 +40,7 @@ function pollThenComment (owner, repoName, prId) {
3740
if (hasAnyPrBuilds) {
3841
pollByPrThenComment(owner, repoName, prId)
3942
} else {
40-
pollByCommitThenComment(owner, repoName, prId)
43+
pollByCommitThenStatus(owner, repoName, prId)
4144
}
4245
})
4346
}
@@ -92,7 +95,7 @@ function pollByPrThenComment (owner, repoName, prId, checkNumber) {
9295
*
9396
* This is the case for readable-stream.
9497
*/
95-
function pollByCommitThenComment (owner, repoName, prId) {
98+
function pollByCommitThenStatus (owner, repoName, prId) {
9699
const prInfo = prInfoStr({ owner, repoName, prId })
97100

98101
githubClient.pullRequests.getCommits({
@@ -111,7 +114,7 @@ function pollByCommitThenComment (owner, repoName, prId) {
111114
}
112115

113116
function pollTravisBuildBySha (options, checkNumber) {
114-
const createGhComment = createGhCommentFn(options)
117+
const createGhStatus = createGhStatusFn(options)
115118
const prInfo = prInfoStr(options)
116119
const shaToMatch = options.lastSha
117120

@@ -138,11 +141,14 @@ function pollTravisBuildBySha (options, checkNumber) {
138141
const lastState = lastBuildForCommit.state
139142

140143
if (lastState === 'passed') {
141-
return createGhComment(`[Travis build passed](https://travis-ci.org/${options.owner}/${options.repoName}/builds/${lastBuildForCommit.id}) :+1:`)
144+
return createGhStatus('success', lastBuildForCommit.id, 'all tests passed')
142145
} else if (lastState === 'failed') {
143-
return createGhComment(`[Travis build failed](https://travis-ci.org/${options.owner}/${options.repoName}/builds/${lastBuildForCommit.id}) :-1:`)
146+
return createGhStatus('failure', lastBuildForCommit.id, 'build failure')
144147
} else if (~['created', 'started'].indexOf(lastState)) {
145148
console.log(`* ${prInfo} "${lastState}" build found, will do check #${checkNumber + 1} in 30 seconds`)
149+
if (checkNumber === 1) {
150+
createGhStatus('pending', lastBuildForCommit.id, 'build in progress')
151+
}
146152
} else {
147153
return console.log(`* ${prInfo} Unknown build state: "${lastState}", stopping polling`)
148154
}
@@ -157,7 +163,7 @@ function pollTravisBuildBySha (options, checkNumber) {
157163
function createGhCommentFn (options) {
158164
const prInfo = prInfoStr(options)
159165

160-
return (message, cb) => {
166+
return (message) => {
161167
githubClient.issues.createComment({
162168
user: options.owner,
163169
repo: options.repoName,
@@ -172,8 +178,27 @@ function createGhCommentFn (options) {
172178
}
173179
}
174180

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