-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrigger-build.js
76 lines (63 loc) · 1.67 KB
/
trigger-build.js
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
'use strict'
const hyperquest = require('hyperquest')
const jsonist = require('jsonist')
const bl = require('bl')
const qs = require('querystring')
function triggerBuild (options, callback) {
const url = `${options.jenkinsJobUrl}/build?token=${options.jenkinsToken}`
const auth = `${options.githubAuthUser}:${options.githubAuthToken}`
const data = {
token: options.jenkinsToken,
parameter: [
{
name: 'repository',
value: `${options.githubScheme}${options.githubOrg}/${options.githubRepo}.git`
},
{
name: 'commit',
value: options.commit
},
{
name: 'datestring',
value: options.date
},
{
name: 'disttype',
value: options.type
},
{
name: 'rc',
value: '0'
}
]
}
let cookies
const requestTrigger = (crumb) => {
const postData = {
token: options.jenkinsToken,
json: JSON.stringify(data)
}
postData[crumb.crumbRequestField] = crumb.crumb
const post = qs.encode(postData)
const headers = { 'content-type': 'application/x-www-form-urlencoded' }
if (cookies) {
headers.cookie = cookies
}
const req = hyperquest(url, { method: 'post', auth, headers })
req.end(post)
req.pipe(bl(callback))
}
if (!options.jenkinsCrumbUrl) {
return requestTrigger()
}
const jr = jsonist.get(options.jenkinsCrumbUrl, { auth }, (err, data) => {
if (err) {
return callback(err)
}
cookies = jr.response.headers['set-cookie'] &&
jr.response.headers['set-cookie'].map((c) => c.split(';')[0])
.join(';')
requestTrigger(data)
})
}
module.exports = triggerBuild