|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const githubClient = require('./github-client') |
| 4 | + |
| 5 | +const jenkinsToGhStatusMap = { |
| 6 | + 'SUCCESS': { |
| 7 | + state: 'success', |
| 8 | + message: 'all tests passed' |
| 9 | + }, |
| 10 | + 'FAILURE': { |
| 11 | + state: 'failure', |
| 12 | + message: 'build failure' |
| 13 | + }, |
| 14 | + 'ABORTED': { |
| 15 | + state: 'error', |
| 16 | + message: 'build aborted' |
| 17 | + }, |
| 18 | + 'UNSTABLE': { |
| 19 | + state: 'error', |
| 20 | + message: 'build unstable' |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +const rxDisplayName = /node\-test\-commit\-(\w+)/ |
| 25 | + |
| 26 | +function pushJenkinsUpdate (options, build) { |
| 27 | + const statusOpts = extendWithBuildData(options, build) |
| 28 | + const createGhStatus = createGhStatusFn(statusOpts) |
| 29 | + const isPending = build.result === null |
| 30 | + const matchedGhStatus = jenkinsToGhStatusMap[build.result] |
| 31 | + |
| 32 | + if (isPending) { |
| 33 | + createGhStatus('pending', 'build in progress') |
| 34 | + } else if (matchedGhStatus) { |
| 35 | + createGhStatus(matchedGhStatus.state, matchedGhStatus.message) |
| 36 | + } else { |
| 37 | + console.error(`! ${prInfoStr(options)} Unknown Jenkins build result '${build.result}'`) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function createGhStatusFn (options) { |
| 42 | + const prInfo = prInfoStr(options) |
| 43 | + |
| 44 | + return (state, message) => { |
| 45 | + githubClient.statuses.create({ |
| 46 | + user: options.owner, |
| 47 | + repo: options.repoName, |
| 48 | + sha: options.sha, |
| 49 | + target_url: options.url, |
| 50 | + context: options.context, |
| 51 | + state: state, |
| 52 | + description: message |
| 53 | + }, (err, res) => { |
| 54 | + if (err) { |
| 55 | + return console.error(`! ${prInfo} Error while updating Jenkins / GitHub PR status`, err) |
| 56 | + } |
| 57 | + console.log(`* ${prInfo} Jenkins / Github PR status updated to '${state}'`) |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function extendWithBuildData (options, build) { |
| 63 | + const lastChangeSet = build.changeSet.items[0] |
| 64 | + |
| 65 | + return Object.assign({ |
| 66 | + sha: lastChangeSet.commitId, |
| 67 | + url: build.url, |
| 68 | + context: jobNameToStatusCtx(build.fullDisplayName) |
| 69 | + }, options) |
| 70 | +} |
| 71 | + |
| 72 | +function jobNameToStatusCtx (displayName) { |
| 73 | + return rxDisplayName.exec(displayName)[1] |
| 74 | +} |
| 75 | + |
| 76 | +function prInfoStr (options) { |
| 77 | + return `${options.owner}/${options.repoName}` |
| 78 | +} |
| 79 | + |
| 80 | +module.exports = pushJenkinsUpdate |
0 commit comments