This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·326 lines (313 loc) · 10.9 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env node
require('shelljs/global');
var yargs = require('yargs');
var color = require('colors');
var fetch = require('node-fetch');
var Table = require('easy-table');
var API = 'https://circleci.com/api/v1.1/'
var TOKEN = process.env.CIRCLE_TOKEN
var VCS_TYPE = process.env.CIRCLE_VCS_TYPE
var USERNAME = process.env.CIRCLE_USERNAME
var success = color.green.bold;
var notests = color.yellow.bold;
var running = color.cyan.bold;
var failure = color.red.bold;
var none = color.white.bold;
function getColorFunc(status) {
return {
'no_tests': notests,
'canceled': notests,
'success': success,
'fixed': success,
'failed': failure,
'timedout': failure,
'failure': failure,
'running': running
}[status] || none
}
if (!TOKEN) return console.log('CIRCLE_TOKEN is not set')
yargs
.command('projects', 'Print projects', function(yargs) {
var argv = yargs.reset()
.option("v", {
alias: "verbose",
description: "Show additional information about projects"
})
.alias("h", "help")
.help("h")
.argv;
fetch(API + 'projects?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
data.forEach(function(item) {
console.log(item.username + '/' + item.reponame)
if (argv.v) {
var branches = item.branches
var t = new Table
Object.keys(branches).forEach(function(branch) {
if (branch !== 'dev' && branch !== 'master') return
var recent_builds = branches[branch].recent_builds
if (!recent_builds) return
t.cell('Branch', branch)
t.cell('Status', getColorFunc(recent_builds[0].status)(recent_builds[0].status))
t.newRow()
})
console.log(t.print())
}
})
}).catch(function(err) {
console.log(err)
})
})
.command('ls', 'recent builds', function(yargs) {
var argv = yargs.reset()
.option("b", {
alias: "branch",
description: "branch"
})
.option("r", {
alias: "reponame",
description: "reponame"
})
.alias("h", "help")
.help("h")
.argv;
fetch(API + 'recent-builds?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
var t = new Table
if (argv.r) {
data = data.filter(function(item) {
return argv.r.indexOf(item.reponame) !== -1
})
}
if (argv.b) {
data = data.filter(function(item) {
return argv.b.indexOf(item.branch) !== -1
})
}
data.forEach(function(item) {
t.cell('#', notests(item.build_num))
t.cell('Repo', item.reponame)
t.cell('Branch', item.branch)
t.cell('Status', getColorFunc(item.status)(item.status))
t.cell('Author', item.author_name)
t.cell('Commit Time', new Date(item.committer_date).toLocaleString())
t.cell('Commit', item.subject)
t.newRow()
})
console.log(t.toString())
}).catch(function(err) {
console.log(err)
})
})
.command('project', 'Show project\'s builds', function(yargs) {
var argv = yargs.reset()
.option("t", {
alias: "vcs-type",
description: "vcs-type"
})
.option("u", {
alias: "username",
description: "username"
})
.option("p", {
alias: "project",
description: "project's name"
})
.option("n", {
alias: "build_num",
description: "build_num"
})
.option("a", {
alias: "artifacts",
description: "artifacts"
})
.option("r", {
alias: "retry",
description: "retry"
})
.option("c", {
alias: "cancel",
description: "cancel"
})
.demand(['p'])
.alias("h", "help")
.help("h")
.argv;
if (argv.n) {
if (argv.a) {
fetch(API + 'project/' + (argv.t || VCS_TYPE) + '/' + (argv.u || USERNAME) + '/' + argv.p + '/' + argv.n + '/artifacts?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
data.forEach(function(item) {
console.log(running('- ') + success(item.url))
})
}).catch(function(err) {
console.log(err)
})
return
}
if (argv.r) {
fetch(API + 'project/' + (argv.t || VCS_TYPE) + '/' + (argv.u || USERNAME) + '/' + argv.p + '/' + argv.n + '/retry?circle-token=' + TOKEN, { method: 'post' })
.then(function(response) { return response.json() })
.then(function(data) {
console.log(success(argv.p + ' #' + argv.n + ' retried'))
}).catch(function(err) {
console.log(err)
})
return
}
if (argv.c) {
fetch(API + 'project/' + (argv.t || VCS_TYPE) + '/' + (argv.u || USERNAME) + '/' + argv.p + '/' + argv.n + '/cancel?circle-token=' + TOKEN, { method: 'post' })
.then(function(response) { return response.json() })
.then(function(data) {
console.log(success(argv.p + ' #' + argv.n + ' canceled'))
}).catch(function(err) {
console.log(err)
})
return
}
fetch(API + 'project/' + (argv.t || VCS_TYPE) + '/' + (argv.u || USERNAME) + '/' + argv.p + '/' + argv.n + '?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
var t = new Table
t.cell('key', 'Build')
t.cell('value', running(data.build_num))
t.newRow()
t.cell('key', 'Subject')
t.cell('value', data.subject)
t.newRow()
t.cell('key', 'Author')
t.cell('value', data.author_name)
t.newRow()
t.cell('key', 'Committer')
t.cell('value', data.committer_name)
t.newRow()
t.cell('key', 'Status')
t.cell('value', getColorFunc(data.status)(data.status))
t.newRow()
console.log(t.print())
if (!data.steps) return
data.steps.forEach(function(step) {
console.log(running('- ') + step.name + ' ' + getColorFunc(step.actions[0].status)(step.actions[0].status) + ' ' + failure((step.actions[0].run_time_millis/1000) + 's'))
})
}).catch(function(err) {
console.log(err)
})
return
}
fetch(API + 'project/' + (argv.t || VCS_TYPE) + '/' + (argv.u || USERNAME) + '/' + argv.p + '?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
var t = new Table
data.filter(function(item) {
return (item.branch === 'dev' || item.branch === 'master')
}).forEach(function(item) {
t.cell('#', notests(item.build_num))
t.cell('Repo', item.reponame)
t.cell('Branch', item.branch)
t.cell('Status', getColorFunc(item.status)(item.status))
t.cell('Commit', item.subject)
t.newRow()
})
console.log(t.toString())
}).catch(function(err) {
console.log(err)
})
})
.command('show', 'show recent build', function(yargs) {
fetch(API + 'recent-builds?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
var build = data.filter(function(item) {
return (item.branch === 'dev' || item.branch === 'master')
})[0]
fetch(API + 'project/' + build.vcs_type + '/' + build.username + '/' + build.reponame + '/' + build.build_num + '?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
var t = new Table
t.cell('key', 'Build')
t.cell('value', running(data.build_num))
t.newRow()
t.cell('key', 'Subject')
t.cell('value', data.subject)
t.newRow()
t.cell('key', 'Author')
t.cell('value', data.author_name)
t.newRow()
t.cell('key', 'Committer')
t.cell('value', data.committer_name)
t.newRow()
t.cell('key', 'Status')
t.cell('value', getColorFunc(data.status)(data.status))
t.newRow()
console.log(t.print())
if (!data.steps) return
data.steps.forEach(function(step) {
console.log(running('- ') + step.name + ' ' + getColorFunc(step.actions[0].status)(step.actions[0].status) + ' ' + failure((step.actions[0].run_time_millis/1000) + 's'))
})
}).catch(function(err) {
console.log(err)
})
}).catch(function(err) {
console.log(err)
})
})
.command('artifacts', 'show recent build artifacts', function(yargs) {
fetch(API + 'recent-builds?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
var build = data.filter(function(item) {
return (item.branch === 'dev' || item.branch === 'master')
})[0]
fetch(API + 'project/' + build.vcs_type + '/' + build.username + '/' + build.reponame + '/' + build.build_num + '/artifacts?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
data.forEach(function(item) {
console.log(running('- ') + success(item.url))
})
}).catch(function(err) {
console.log(err)
})
}).catch(function(err) {
console.log(err)
})
})
.command('retry', 'retry recent build', function(yargs) {
fetch(API + 'recent-builds?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
var build = data.filter(function(item) {
return (item.branch === 'dev' || item.branch === 'master')
})[0]
fetch(API + 'project/' + build.vcs_type + '/' + build.username + '/' + build.reponame + '/' + build.build_num + '/retry?circle-token=' + TOKEN, { method: 'post' })
.then(function(response) { return response.json() })
.then(function(data) {
console.log(success(build.reponame + ' #' + build.build_num + ' retried'))
}).catch(function(err) {
console.log(err)
})
}).catch(function(err) {
console.log(err)
})
})
.command('cancel', 'cancel recent build', function(yargs) {
fetch(API + 'recent-builds?circle-token=' + TOKEN)
.then(function(response) { return response.json() })
.then(function(data) {
var build = data.filter(function(item) {
return (item.branch === 'dev' || item.branch === 'master')
})[0]
fetch(API + 'project/' + build.vcs_type + '/' + build.username + '/' + build.reponame + '/' + build.build_num + '/cancel?circle-token=' + TOKEN, { method: 'post' })
.then(function(response) { return response.json() })
.then(function(data) {
console.log(success(build.reponame + ' #' + build.build_num + ' canceled'))
}).catch(function(err) {
console.log(err)
})
}).catch(function(err) {
console.log(err)
})
})
var argv = yargs.argv;