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

Use GitHub Status API #15

Merged
merged 1 commit into from
Apr 10, 2016
Merged
Show file tree
Hide file tree
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
40 changes: 31 additions & 9 deletions lib/pollTravis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ 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 @@ -37,7 +39,7 @@ function pollThenComment (owner, repoName, prId) {
if (hasAnyPrBuilds) {
pollByPrThenComment(owner, repoName, prId)
} else {
pollByCommitThenComment(owner, repoName, prId)
pollByCommitThenStatus(owner, repoName, prId)
}
})
}
Expand Down Expand Up @@ -92,7 +94,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 +113,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 +140,12 @@ 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`)
createGhStatus('pending', lastBuildForCommit.id, 'build in progress')
} else {
return console.log(`* ${prInfo} Unknown build state: "${lastState}", stopping polling`)
}
Expand All @@ -157,7 +160,7 @@ function pollTravisBuildBySha (options, checkNumber) {
function createGhCommentFn (options) {
const prInfo = prInfoStr(options)

return (message, cb) => {
return (message) => {

This comment was marked as off-topic.

githubClient.issues.createComment({
user: options.owner,
repo: options.repoName,
Expand All @@ -172,8 +175,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.stack)
}
console.log(`* ${prInfo} Github PR status updated`)
})
}
}

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

exports.pollThenComment = pollThenComment
14 changes: 9 additions & 5 deletions scripts/display-travis-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

const debug = require('debug')('display_travis_status')
const pollTravis = require('../lib/pollTravis')
const enabledRepos = ['citgm', 'readable-stream', 'nodejs.org']
const enabledRepos = ['citgm', 'readable-stream', 'nodejs.org', 'test-github-bot']

module.exports = function (app) {
app.on('pull_request.opened', (event) => {
app.on('pull_request.opened', handlePrUpdate)
// Pull Request updates
app.on('pull_request.synchronize', handlePrUpdate)

function handlePrUpdate (event) {
const owner = event.repository.owner.login
const repo = event.repository.name
if (!~enabledRepos.indexOf(repo)) return

debug(`/${owner}/${repo}/pull/${event.number} opened`)
pollTravis.pollThenComment(owner, repo, event.number)
})
pollTravis.pollThenStatus(owner, repo, event.number)
}

// to trigger polling manually
app.get('/pr/:owner/:repo/:id', (req, res) => {
const owner = req.params.owner
const repo = req.params.repo
const id = req.params.id
if (~enabledRepos.indexOf(repo)) {
pollTravis.pollThenComment(owner, repo, parseInt(id, 10))
pollTravis.pollThenStatus(owner, repo, parseInt(id, 10))
}
res.end()
})
Expand Down