Skip to content

Commit

Permalink
Use GitHub Status API
Browse files Browse the repository at this point in the history
  • Loading branch information
Fishrock123 committed Apr 8, 2016
1 parent 8824234 commit e34cfd3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
45 changes: 35 additions & 10 deletions lib/pollTravis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ githubClient.authenticate({
token: process.env.GITHUB_TOKEN
})

function pollThenComment (owner, repoName, prId) {
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,
Expand All @@ -34,10 +37,10 @@ function pollThenComment (owner, repoName, prId) {

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

if (hasAnyPrBuilds) {
if (false/*hasAnyPrBuilds*/) {
pollByPrThenComment(owner, repoName, prId)
} else {
pollByCommitThenComment(owner, repoName, prId)
pollByCommitThenStatus(owner, repoName, prId)
}
})
}
Expand Down Expand Up @@ -92,7 +95,7 @@ function pollByPrThenComment (owner, repoName, prId, checkNumber) {
*
* This is the case for readable-stream.
*/
function pollByCommitThenComment (owner, repoName, prId) {
function pollByCommitThenStatus (owner, repoName, prId) {
const prInfo = prInfoStr({ owner, repoName, prId })

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

function pollTravisBuildBySha (options, checkNumber) {
const createGhComment = createGhCommentFn(options)
const createGhStatus = createGhStatusFn(options)
const prInfo = prInfoStr(options)
const shaToMatch = options.lastSha

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

if (lastState === 'passed') {
return createGhComment(`[Travis build passed](https://travis-ci.org/${options.owner}/${options.repoName}/builds/${lastBuildForCommit.id}) :+1:`)
return createGhStatus('success', lastBuildForCommit.id, 'all tests passed')
} else if (lastState === 'failed') {
return createGhComment(`[Travis build failed](https://travis-ci.org/${options.owner}/${options.repoName}/builds/${lastBuildForCommit.id}) :-1:`)
return createGhStatus('failure', lastBuildForCommit.id, 'build failure')
} else if (~['created', 'started'].indexOf(lastState)) {
console.log(`* ${prInfo} "${lastState}" build found, will do check #${checkNumber + 1} in 30 seconds`)
if (checkNumber === 1) {
createGhStatus('pending', lastBuildForCommit.id, 'build in progress')
}
} else {
return console.log(`* ${prInfo} Unknown build state: "${lastState}", stopping polling`)
}
Expand All @@ -157,7 +163,7 @@ function pollTravisBuildBySha (options, checkNumber) {
function createGhCommentFn (options) {
const prInfo = prInfoStr(options)

return (message, cb) => {
return (message) => {
githubClient.issues.createComment({
user: options.owner,
repo: options.repoName,
Expand All @@ -172,8 +178,27 @@ function createGhCommentFn (options) {
}
}

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

return (state, travisId, message) => {
githubClient.statuses.create({
user: options.owner,
repo: options.repoName,
sha: options.lastSha,
target_url: `https://travis-ci.org/${options.owner}/${options.repoName}/builds/${travisId}`,
context: "Travis CI via nodejs-github-bot",
state: state,
description: message,
}, (err, res) => {
if (err) {
return console.error(`! ${prInfo} Error while updating GitHub PR status`, err, err.stack)
}
console.log(`* ${prInfo} Github PR status updated`)
})
}
}

function prInfoStr (options) {
return `${options.owner}/${options.repoName}/#${options.prId}`
}

exports.pollThenComment = pollThenComment
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ app.all('/hooks/github', (req, res) => {
const repo = req.body.repository

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

res.end()
})

// to trigger polling manually
app.get('/pr/:owner/:repo/:prId', (req, res) => {
pollTravis.pollThenComment(req.params.owner, req.params.repo, parseInt(req.params.prId))
pollTravis.pollThenStatus(req.params.owner, req.params.repo, parseInt(req.params.prId))
res.end()
})

Expand Down

0 comments on commit e34cfd3

Please sign in to comment.