Skip to content

Commit 566ce4c

Browse files
committed
Merge pull request #20 from Fishrock123/revert-#16
Revert "Merge pull request #16 from Fishrock123/no-comment"
2 parents 1535116 + 9d9069a commit 566ce4c

File tree

1 file changed

+87
-2
lines changed

1 file changed

+87
-2
lines changed

lib/pollTravis.js

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

25-
exports.pollThenStatus = pollByCommitThenStatus
25+
exports.pollThenStatus = pollThenStatus
26+
27+
function pollThenStatus (owner, repoName, prId) {
28+
const prInfo = prInfoStr({ owner, repoName, prId })
29+
30+
// we have to figure out what type of Travis polling we should perform,
31+
// either by PR #id (as for nodejs.org) or commit sha (for readable-stream)
32+
travisClient.repos(owner, repoName).builds.get((err, res) => {
33+
if (err) {
34+
return console.error(`! ${prInfo} Error while retrieving initial builds`, err.stack)
35+
}
36+
37+
const hasAnyPrBuilds = res.builds.some((build) => build.pull_request)
38+
39+
if (hasAnyPrBuilds) {
40+
pollByPrThenComment(owner, repoName, prId)
41+
} else {
42+
pollByCommitThenStatus(owner, repoName, prId)
43+
}
44+
})
45+
}
2646

2747
/**
28-
* Poll and match builds by the last commit SHA of the related PR.
48+
* When Travis CI picks up our PR's, we can poll and match builds
49+
* by their related PR #id.
50+
*
51+
* That's the case for nodejs.org.
52+
*/
53+
function pollByPrThenComment (owner, repoName, prId, checkNumber) {
54+
checkNumber = checkNumber || 1
55+
56+
const prInfo = prInfoStr({ owner, repoName, prId })
57+
const createGhComment = createGhCommentFn({ owner, repoName, prId })
58+
59+
if (checkNumber > 100) {
60+
console.warn(`* ${prInfo} Was not able to find matching build for PR, stopping poll now :(`)
61+
return
62+
}
63+
64+
travisClient.repos(owner, repoName).builds.get((err, res) => {
65+
if (err) {
66+
return console.error(`! ${prInfo} Error while retrieving builds`, err.stack)
67+
}
68+
69+
const lastBuildForPr = res.builds.find((build) => build.pull_request_number === prId)
70+
71+
if (lastBuildForPr) {
72+
const lastState = lastBuildForPr.state
73+
74+
if (lastState === 'passed') {
75+
return createGhComment(`[Travis build passed](https://travis-ci.org/${owner}/${repoName}/builds/${lastBuildForPr.id}) :+1:`)
76+
} else if (lastState === 'failed') {
77+
return createGhComment(`[Travis build failed](https://travis-ci.org/${owner}/${repoName}/builds/${lastBuildForPr.id}) :-1:`)
78+
} else if (~['created', 'started'].indexOf(lastState)) {
79+
console.log(`* ${prInfo} "${lastState}" build found, will do check #${checkNumber + 1} in 30 seconds`)
80+
} else {
81+
return console.log(`* ${prInfo} Unknown build state: "${lastState}", stopping polling`)
82+
}
83+
} else {
84+
console.warn(`! ${prInfo} Was not able to find matching build, will do check #${checkNumber + 1} in 30 seconds`)
85+
}
86+
87+
setTimeout(pollByPrThenComment, 30 * 1000, owner, repoName, prId, checkNumber + 1)
88+
})
89+
}
90+
91+
/**
92+
* When Travis CI *doesn't* pick up our PR's, we have to poll and match builds
93+
* by the last commit SHA of the related PR.
94+
*
95+
* This is the case for readable-stream.
2996
*/
3097
function pollByCommitThenStatus (owner, repoName, prId) {
3198
const prInfo = prInfoStr({ owner, repoName, prId })
@@ -90,6 +157,24 @@ function pollTravisBuildBySha (options, checkNumber) {
90157
})
91158
}
92159

160+
function createGhCommentFn (options) {
161+
const prInfo = prInfoStr(options)
162+
163+
return (message) => {
164+
githubClient.issues.createComment({
165+
user: options.owner,
166+
repo: options.repoName,
167+
number: options.prId,
168+
body: message
169+
}, (err, res) => {
170+
if (err) {
171+
return console.error(`! ${prInfo} Error while creating GitHub comment`, err.stack)
172+
}
173+
console.log(`* ${prInfo} Github comment created`)
174+
})
175+
}
176+
}
177+
93178
function createGhStatusFn (options) {
94179
const prInfo = prInfoStr(options)
95180

0 commit comments

Comments
 (0)