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

Proxy Jenkins build status to GitHub #35

Merged
merged 8 commits into from
May 11, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
80 changes: 80 additions & 0 deletions lib/push-jenkins-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict'

const githubClient = require('./github-client')

const jenkinsToGhStatusMap = {
'SUCCESS': {
state: 'success',
message: 'all tests passed'
},
'FAILURE': {
state: 'failure',
message: 'build failure'
},
'ABORTED': {
state: 'error',
message: 'build aborted'
},
'UNSTABLE': {
state: 'error',
message: 'build unstable'

This comment was marked as off-topic.

This comment was marked as off-topic.

}
}

const rxDisplayName = /node\-test\-commit\-(\w+)/

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.


function pushJenkinsUpdate (options, build) {
const statusOpts = extendWithBuildData(options, build)
const createGhStatus = createGhStatusFn(statusOpts)
const isPending = build.result === null
const matchedGhStatus = jenkinsToGhStatusMap[build.result]

if (isPending) {
createGhStatus('pending', 'build in progress')

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

} else if (matchedGhStatus) {
createGhStatus(matchedGhStatus.state, matchedGhStatus.message)
} else {
console.error(`! ${prInfoStr(options)} Unknown Jenkins build result '${build.result}'`)
}
}

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

return (state, message) => {
githubClient.statuses.create({
user: options.owner,
repo: options.repoName,
sha: options.sha,
target_url: options.url,
context: options.context,
state: state,
description: message
}, (err, res) => {
if (err) {
return console.error(`! ${prInfo} Error while updating Jenkins / GitHub PR status`, err)
}
console.log(`* ${prInfo} Jenkins / Github PR status updated to '${state}'`)
})
}
}

function extendWithBuildData (options, build) {
const lastChangeSet = build.changeSet.items[0]

return Object.assign({
sha: lastChangeSet.commitId,
url: build.url,
context: jobNameToStatusCtx(build.fullDisplayName)
}, options)
}

function jobNameToStatusCtx (displayName) {
return rxDisplayName.exec(displayName)[1]
}

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

module.exports = pushJenkinsUpdate
14 changes: 14 additions & 0 deletions scripts/node-jenkins-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const pushJenkinsUpdate = require('../lib/push-jenkins-update')

module.exports = function (app) {
app.post('/node/jenkins', (req, res) => {
pushJenkinsUpdate({
owner: 'TestOrgPleaseIgnore',
repoName: 'node'
}, req.body)

res.end()
})
}
12 changes: 12 additions & 0 deletions test/_fixtures/error-payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"fullDisplayName": "node-test-commit-arm #3087",

This comment was marked as off-topic.

"result": "FAILURE",

This comment was marked as off-topic.

"changeSet": {
"items": [
{
"commitId": "7190bd23810f0880d95605bf6f785224a345d477"
}
]
},

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

"url": "https://ci.nodejs.org/job/node-test-commit-arm/3087/"
}
12 changes: 12 additions & 0 deletions test/_fixtures/pending-payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"fullDisplayName": "node-test-commit-linux #3176",
"result": null,
"changeSet": {
"items": [
{
"commitId": "7190bd23810f0880d95605bf6f785224a345d477"
}
]
},
"url": "https://ci.nodejs.org/job/node-test-commit-linux/3176/"
}
12 changes: 12 additions & 0 deletions test/_fixtures/success-payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"fullDisplayName": "node-test-commit-osx #3157",
"result": "SUCCESS",
"changeSet": {
"items": [
{
"commitId": "7190bd23810f0880d95605bf6f785224a345d477"
}
]
},
"url": "https://ci.nodejs.org/job/node-test-commit-osx/3157/"
}