Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove outdated travis commenting logic #16

Merged
merged 1 commit into from
Apr 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 2 additions & 87 deletions lib/pollTravis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,77 +22,10 @@ githubClient.authenticate({
token: process.env.GITHUB_TOKEN
})

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)
}
})
}
exports.pollThenStatus = pollByCommitThenStatus

/**
* 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.
* Poll and match builds by the last commit SHA of the related PR.
*/
function pollByCommitThenStatus (owner, repoName, prId) {
const prInfo = prInfoStr({ owner, repoName, prId })
Expand Down Expand Up @@ -157,24 +90,6 @@ 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