-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWerckerCli.js
110 lines (98 loc) · 2.88 KB
/
WerckerCli.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
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
107
108
109
'use strict'
const request = require('request-promise')
const qs = require('qs')
const WerckerCli = {}
WerckerCli.init = function (werckerConfig) {
this.werckerConfig = werckerConfig
}
WerckerCli.triggerCreateReleaseBranchPipelines = function(createBranchPipelineIds, lastDevelopBuilds) {
request.post({
url: this.werckerConfig.apiUrl + '/api/v3/runs',
json: {
pipelineId: createBranchPipelineIds.pipeline,
sourcePipelineId: lastDevelopBuilds.pipeline,
envVars: [
{'key': 'FROM_BRANCH', 'value': 'develop'},
{'key': 'NEW_BRANCH', 'value': 'test-branch-auto'}
]
},
headers: {
Authorization: 'Bearer ' + this.werckerConfig.token
}
}, function (err, res) {
console.log('res status ', res.statusCode)
console.log(res.body)
})
}
WerckerCli.getAllLastBuildsOnBranch = function (sourcePipelineBuildIds, branch) {
let lastPipelineBuildIds = {}
return Object.entries(sourcePipelineBuildIds)
.reduce((promise, [key, value]) => {
return promise
.then(() => {
return request.get({
url: this.werckerConfig.apiUrl + '/api/v3/runs',
qs: {
pipelineId: value,
status: 'finished',
branch: branch
},
json: true,
resolveWithFullResponse: true,
headers: {
Authorization: 'Bearer ' + this.werckerConfig.token
}
})
.then((res) => {
const lastPipelineBuild = res.body[0] && res.body[0].id
if (!lastPipelineBuild) {
throw new Error('No previous source pipeline found')
}
lastPipelineBuildIds[key] = lastPipelineBuild
})
})
},
Promise.resolve()
)
.then(() => {
return lastPipelineBuildIds
})
.catch((err) => {
console.error(err)
process.exit(1)
})
}
module.exports = WerckerCli
// function getAllLastDevelopBuilds() {
// let requestPromises = []
// let lastPipelineBuildIds = {}
//
// return Promise.all(Object.entries(sourcePipelineBuildIds).reduce((buildIds, [key, value]) => {
// requestPromises.push(request.get({
// url: "https://app.wercker.com/api/v3/runs",
// qs: {
// pipelineId: value,
// status: 'finished',
// branch: 'develop'
// },
// json: true,
// resolveWithFullResponse: true,
// headers: {
// Authorization: "Bearer " + token
// }
// })
// .then(async (res) => {
// const lastPipelineBuild = res.body[0].id
//
// lastPipelineBuildIds[key] = lastPipelineBuild
// })
// .catch((err) => {
// console.error(err)
// process.exit(1)
// }))
//
// return requestPromises
// }, [])).then(() => {
// return lastPipelineBuildIds
// })
// }