Skip to content

Commit

Permalink
Merge pull request #20 from Fishrock123/revert-#16
Browse files Browse the repository at this point in the history
Revert "Merge pull request #16 from Fishrock123/no-comment"
  • Loading branch information
phillipj committed Apr 11, 2016
2 parents 1535116 + 9d9069a commit 566ce4c
Showing 1 changed file with 87 additions and 2 deletions.
89 changes: 87 additions & 2 deletions lib/pollTravis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,77 @@ githubClient.authenticate({
token: process.env.GITHUB_TOKEN
})

exports.pollThenStatus = pollByCommitThenStatus
exports.pollThenStatus = pollThenStatus

function pollThenStatus (owner, repoName, prId) {
const prInfo = prInfoStr({ owner, repoName, prId })

// we have to figure out what type of Travis polling we should perform,
// either by PR #id (as for nodejs.org) or commit sha (for readable-stream)
travisClient.repos(owner, repoName).builds.get((err, res) => {
if (err) {
return console.error(`! ${prInfo} Error while retrieving initial builds`, err.stack)
}

const hasAnyPrBuilds = res.builds.some((build) => build.pull_request)

if (hasAnyPrBuilds) {
pollByPrThenComment(owner, repoName, prId)
} else {
pollByCommitThenStatus(owner, repoName, prId)
}
})
}

/**
* Poll and match builds by the last commit SHA of the related PR.
* When Travis CI picks up our PR's, we can poll and match builds
* by their related PR #id.
*
* That's the case for nodejs.org.
*/
function pollByPrThenComment (owner, repoName, prId, checkNumber) {
checkNumber = checkNumber || 1

const prInfo = prInfoStr({ owner, repoName, prId })
const createGhComment = createGhCommentFn({ owner, repoName, prId })

if (checkNumber > 100) {
console.warn(`* ${prInfo} Was not able to find matching build for PR, stopping poll now :(`)
return
}

travisClient.repos(owner, repoName).builds.get((err, res) => {
if (err) {
return console.error(`! ${prInfo} Error while retrieving builds`, err.stack)
}

const lastBuildForPr = res.builds.find((build) => build.pull_request_number === prId)

if (lastBuildForPr) {
const lastState = lastBuildForPr.state

if (lastState === 'passed') {
return createGhComment(`[Travis build passed](https://travis-ci.org/${owner}/${repoName}/builds/${lastBuildForPr.id}) :+1:`)
} else if (lastState === 'failed') {
return createGhComment(`[Travis build failed](https://travis-ci.org/${owner}/${repoName}/builds/${lastBuildForPr.id}) :-1:`)
} else if (~['created', 'started'].indexOf(lastState)) {
console.log(`* ${prInfo} "${lastState}" build found, will do check #${checkNumber + 1} in 30 seconds`)
} else {
return console.log(`* ${prInfo} Unknown build state: "${lastState}", stopping polling`)
}
} else {
console.warn(`! ${prInfo} Was not able to find matching build, will do check #${checkNumber + 1} in 30 seconds`)
}

setTimeout(pollByPrThenComment, 30 * 1000, owner, repoName, prId, checkNumber + 1)
})
}

/**
* When Travis CI *doesn't* pick up our PR's, we have to poll and match builds
* by the last commit SHA of the related PR.
*
* This is the case for readable-stream.
*/
function pollByCommitThenStatus (owner, repoName, prId) {
const prInfo = prInfoStr({ owner, repoName, prId })
Expand Down Expand Up @@ -90,6 +157,24 @@ function pollTravisBuildBySha (options, checkNumber) {
})
}

function createGhCommentFn (options) {
const prInfo = prInfoStr(options)

return (message) => {
githubClient.issues.createComment({
user: options.owner,
repo: options.repoName,
number: options.prId,
body: message
}, (err, res) => {
if (err) {
return console.error(`! ${prInfo} Error while creating GitHub comment`, err.stack)
}
console.log(`* ${prInfo} Github comment created`)
})
}
}

function createGhStatusFn (options) {
const prInfo = prInfoStr(options)

Expand Down

0 comments on commit 566ce4c

Please sign in to comment.