forked from nodejs/github-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-jenkins-update.js
More file actions
106 lines (86 loc) · 2.78 KB
/
Copy pathpush-jenkins-update.js
File metadata and controls
106 lines (86 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict'
const githubClient = require('./github-client')
function pushStarted (options, build) {
const pr = findPrInRef(build.ref)
// create unique logger which is easily traceable for every subsequent log statement
const traceFields = { pr, job: build.identifier, status: build.status }
const logger = options.logger.child(traceFields, true)
const optsWithPr = Object.assign({ pr }, options)
findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
if (err) {
return logger.error(err, 'Got error when retrieving GitHub commits for PR')
}
const statusOpts = Object.assign({
sha: latestCommit.sha,
url: build.url,
context: build.identifier,
state: build.status,
message: build.message || 'running tests'
}, options)
createGhStatus(statusOpts, logger)
})
}
function pushEnded (options, build) {
const pr = findPrInRef(build.ref)
// create unique logger which is easily traceable for every subsequent log statement
const traceFields = { pr, job: build.identifier, status: build.status }
const logger = options.logger.child(traceFields, true)
const optsWithPr = Object.assign({ pr }, options)
findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
if (err) {
return logger.error(err, 'Got error when retrieving GitHub commits for PR')
}
const statusOpts = Object.assign({
sha: latestCommit.sha,
url: build.url,
context: build.identifier,
state: build.status,
message: build.message || 'all tests passed'
}, options)
createGhStatus(statusOpts, logger)
})
}
function findPrInRef (gitRef) {
// refs/pull/12345/head
return gitRef.split('/')[2]
}
function findLatestCommitInPr (options, cb) {
githubClient.pullRequests.getCommits({
user: options.owner,
repo: options.repo,
number: options.pr
}, (err, commitMetas) => {
if (err) {
return cb(err)
}
const lastCommitMeta = commitMetas.pop()
const lastCommit = {
sha: lastCommitMeta.sha,
date: lastCommitMeta.commit.committer.date
}
cb(null, lastCommit)
})
}
function createGhStatus (options, logger) {
githubClient.repos.createStatus({
user: options.owner,
repo: options.repo,
sha: options.sha,
target_url: options.url,
context: options.context,
state: options.state,
description: options.message
}, (err, res) => {
if (err) {
return logger.error(err, 'Error while updating Jenkins / GitHub PR status')
}
logger.info('Jenkins / Github PR status updated')
})
}
function validate (payload) {
const isString = (param) => typeof (payload[param]) === 'string'
return ['identifier', 'status', 'message', 'commit', 'url'].every(isString)
}
exports.validate = validate
exports.pushStarted = pushStarted
exports.pushEnded = pushEnded