-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
566 lines (485 loc) · 17.5 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
'use strict';
const fs = require('fs');
const URL = require('url');
const request = require('screwdriver-request');
const Yaml = require('js-yaml');
const SD_API_URL = process.env.SD_API_URL || 'https://api.screwdriver.cd/v4/';
/**
* Loads the yaml configuration from a file
* @method loadYaml
* @param {String} path File path
* @return {Promise} Promise that resolves to the template as a config object
*/
function loadYaml(path) {
return new Promise(resolve => {
resolve(Yaml.load(fs.readFileSync(path, 'utf8')));
});
}
/**
* Validates the jobs and pipeline template yaml by posting to the endpoint
* @method validateTemplate
* @param {Object} config Template config
* @param {String} apiURL endpoint API
* @return {Promise} Resolves if validates successfully
*/
function validateTemplate(config, apiURL) {
const hostname = SD_API_URL;
const url = URL.resolve(hostname, apiURL);
return request({
method: 'POST',
url,
context: {
token: process.env.SD_TOKEN
},
json: {
yaml: JSON.stringify(config)
}
}).then(response => {
const { body } = response;
if (body.errors.length > 0) {
let errorMessage = 'Template is not valid for the following reasons:';
body.errors.forEach(err => {
errorMessage += `\n${JSON.stringify(err, null, 4)},`;
});
throw new Error(errorMessage);
}
return {
valid: true
};
});
}
/**
* Validates the job template yaml by using the validateTemplate method and passing the API endpoint
* @method validateJobTemplate
* @param {Object} config Template config
* @return {Promise} Resolves if validates successfully
*/
function validateJobTemplate(config) {
return validateTemplate(config, 'validator/template');
}
/**
* Validates the pipeline template yaml by using the validateTemplate method and passing the API endpoint
* @method validatePipelineTemplate
* @param {Object} config Template config
* @return {Promise} Resolves if validates successfully
*/
function validatePipelineTemplate(config) {
return validateTemplate(config, 'pipeline/template/validate');
}
/**
* Publishes the jobs and pipeline template yaml by posting to the endpoint
* @method publishTemplate
* @param {Object} config Template config
* @param {String} apiURL endpoint API
* @return {Promise} Resolves if publishes successfully
*/
function publishTemplate(config, apiURL) {
const hostname = SD_API_URL;
const url = URL.resolve(hostname, apiURL);
return request({
method: 'POST',
url,
context: {
token: process.env.SD_TOKEN
},
json: {
yaml: JSON.stringify(config)
}
}).then(response => {
const { body } = response;
if (response.statusCode !== 201) {
throw new Error(`Error publishing template. ${response.statusCode} (${body.error}): ${body.message}`);
}
return body;
});
}
/**
* Publishes the job template yaml by using the publishTemplate method and passing the API endpoint
* @method publishJobTemplate
* @param {Object} config Template config
* @return {Promise} Resolves if publishes successfully
*/
function publishJobTemplate(config) {
return publishTemplate(config, 'templates').then(template => {
let fullTemplateName = template.name;
// Figure out template name
if (template.namespace && template.namespace !== 'default') {
fullTemplateName = `${template.namespace}/${template.name}`;
}
return {
name: fullTemplateName,
version: template.version
};
});
}
/**
* Publishes the pipeline template yaml by using the publishTemplate method and passing the API endpoint
* @method publishPipelineTemplate
* @param {Object} config Template config
* @return {Promise} Resolves if publishes successfully
*/
async function publishPipelineTemplate(config) {
const template = await publishTemplate(config, 'pipeline/template');
return {
namespace: template.namespace,
name: template.name,
version: template.version
};
}
/**
* Removes a template.
* @param {Object} config - The config for removing the template.
* @param {string} config.path - The path of the template.
* @param {string} config.name - The name of the template.
* @returns {Promise<Object>} A promise that resolves to an object containing the name of the removed template.
* @throws {Error} If there is an error removing the template.
*/
function removeTemplate({ path, name }) {
const hostname = SD_API_URL;
const url = URL.resolve(hostname, path);
return request({
method: 'DELETE',
url,
context: {
token: process.env.SD_TOKEN
}
}).then(response => {
const { body } = response;
if (response.statusCode !== 204) {
throw new Error(`Error removing template ${name}. ${response.statusCode} (${body.error}): ${body.message}`);
}
return { name };
});
}
/**
* Removes specified version of a template by sending a delete request to the SDAPI endpoint
* @method removeVersion
* @param {Object} config
* @param {string} config.path - The path of the template.
* @param {String} config.name Template name
* @param {String} config.version Template version to be removed
* @return {Promise} Resolves if removed successfully
*/
function removeVersion({ path, name, version }) {
const hostname = SD_API_URL;
const url = URL.resolve(hostname, path);
return request({
method: 'DELETE',
url,
context: {
token: process.env.SD_TOKEN
}
}).then(response => {
const { body } = response;
if (response.statusCode !== 204) {
throw new Error(
`Error removing version ${version} of template ${name}. ${response.statusCode} (${body.error}): ${body.message}`
);
}
return { name, version };
});
}
/**
* Helper function that returns the latest version for a template
* @method getLatestVersion
* @param {String} name Template name
* @return {Promise} Resolves to latest version
*/
function getLatestVersion(path) {
const url = URL.resolve(SD_API_URL, path);
return request({
method: 'GET',
url,
context: {
token: process.env.SD_TOKEN
}
}).then(response => {
const { body, statusCode } = response;
if (statusCode !== 200) {
throw new Error(`Error getting latest template version. ${statusCode} (${body.error}): ${body.message}`);
}
return body[0].version;
});
}
/**
* Helper function that returns the latest version for a job template
* @param {String} name
* @return {Promise} Resolves to latest version
*/
function getJobTemplateLatestVersion(name) {
const path = `templates/${encodeURIComponent(name)}`;
return getLatestVersion(path);
}
/**
* Helper function that returns the latest version for a pipeline template
* @method getPipelineTemplateLatestVersion
* @param {String} namespace Template namespace
* @param {String} name Template name
* @return {Promise} Resolves to latest version
*/
function getPipelineTemplateLatestVersion(namespace, name) {
const path = `pipeline/templates/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}/versions?count=1`;
return getLatestVersion(path);
}
/**
* Helper function that returns the version from a tag
* @method getVersionFromTag
* @param {String} path The API path
* @param {String} name Template name
* @param {String} tag Tag to fetch version from
* @return {Promise} Resolves the version from given tag
*/
function getVersionFromTag({ path, name, tag }) {
const hostname = SD_API_URL;
const url = URL.resolve(hostname, path);
return request({
method: 'GET',
url,
context: {
token: process.env.SD_TOKEN
}
}).then(response => {
const { body, statusCode } = response;
if (statusCode !== 200) {
throw new Error(
`Error getting version from ${name} tag ${tag}. ${statusCode} (${body.error}): ${body.message}`
);
}
return body.version;
});
}
/**
* Tags a specific template version by posting to the SDAPI endpoint
* @method tagTemplate
* @param {Object} config
* @param {String} config.name Template name
* @param {String} config.tag Template tag
* @param {String} [config.version] Template version
* @return {Promise} Resolves if tagged successfully
*/
function tagTemplate({ path, name, tag, version }) {
const hostname = SD_API_URL;
const url = URL.resolve(hostname, path);
return request({
method: 'PUT',
url,
context: {
token: process.env.SD_TOKEN
},
json: {
version
}
}).then(response => {
const { body, statusCode } = response;
if (statusCode !== 201 && statusCode !== 200) {
throw new Error(`Error tagging template. ${statusCode} (${body.error}): ${body.message}`);
}
return {
name,
tag,
version
};
});
}
/**
* Tags a specific template version by posting to the SDAPI /templates/{templateName}/tags/{tagName} endpoint
* @method tagJobTemplate
* @param {Object} config
* @param {String} config.name Template name
* @param {String} config.tag Template tag
* @param {String} [config.version] Template version
* @return {Promise} Resolves if tagged successfully
*/
async function tagJobTemplate({ name, tag, version }) {
const path = `templates/${encodeURIComponent(name)}/tags/${encodeURIComponent(tag)}`;
let tagVersion = version;
if (!version) {
tagVersion = await getJobTemplateLatestVersion(name);
}
return tagTemplate({ path, name, tag, version: tagVersion });
}
/**
* Tags a specific template version by posting to the SDAPI /pipeline/templates/{name}/tags/{tag} endpoint
* @method tagPipelineTemplate
* @param {Object} config
* @param {String} config.namespace Template namespace
* @param {String} config.name Template name
* @param {String} config.tag Template tag
* @param {String} [config.version] Template version
* @return {Promise} Resolves if tagged successfully
*/
async function tagPipelineTemplate({ namespace, name, tag, version }) {
const path = `pipeline/template/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}/tags/${tag}`;
let tagVersion = version;
if (!version) {
tagVersion = await getPipelineTemplateLatestVersion(name);
}
const res = await tagTemplate({ path, name, tag, version: tagVersion });
return {
namespace,
name,
tag,
version: res.version
};
}
/**
* Removes a tag from a template.
* @param {Object} config - The config for removing the tag.
* @param {string} config.path - The path of the template.
* @param {string} config.name - The name of the template.
* @param {string} config.tag - The tag to be removed.
* @returns {Promise<void>} - A promise that resolves when the tag is successfully removed.
* @throws {Error} - If there is an error removing the tag.
*/
function removeTag({ path, name, tag }) {
const hostname = SD_API_URL;
const url = URL.resolve(hostname, path);
return request({
method: 'DELETE',
url,
context: {
token: process.env.SD_TOKEN
}
}).then(response => {
const { body, statusCode } = response;
if (statusCode !== 204) {
throw new Error(
`Error removing template ${name} tag ${tag}. ${statusCode} (${body.error}): ${body.message}`
);
}
});
}
/**
* Removes a job template.
* @param {Object} config - The config for removing the job template.
* @param {string} config.name - The name of the job template to be removed.
* @returns {Object} - The removed job template's name.
*/
async function removeJobTemplate(name) {
const path = `templates/${encodeURIComponent(name)}`;
await removeTemplate({ path, name });
return {
name
};
}
/**
* Removes a pipeline template.
* @param {Object} config - The config for removing the pipeline template.
* @param {string} config.namespace - The namespace of the pipeline template.
* @param {string} config.name - The name of the pipeline template.
* @returns {Object} - The removed pipeline template's namespace and name.
*/
async function removePipelineTemplate({ namespace, name }) {
const path = `pipeline/templates/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}`;
await removeTemplate({ path, name: `${namespace}/${name}` });
return {
namespace,
name
};
}
/**
* Removes a job template version.
* @param {Object} params - The parameters for removing the job template version.
* @param {string} params.name - The name of the job template.
* @param {string} params.version - The version of the job template.
* @returns {Object} - The removed job template version.
*/
async function removeJobTemplateVersion({ name, version }) {
const path = `templates/${encodeURIComponent(name)}/versions/${version}`;
await removeVersion({ path, name, version });
return {
name,
version
};
}
/**
* Removes a specific version of a pipeline template.
* @param {Object} config - The config for removing the pipeline template version.
* @param {string} config.namespace - The namespace of the pipeline template.
* @param {string} config.name - The name of the pipeline template.
* @param {string} config.version - The version of the pipeline template to remove.
* @returns {Object} - The removed pipeline template version details.
*/
async function removePipelineTemplateVersion({ namespace, name, version }) {
const path = `pipeline/template/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}/versions/${version}`;
await removeVersion({ path, name: `${namespace}/${name}`, version });
return {
namespace,
name,
version
};
}
/**
* Removes a tag from a job template.
* @param {Object} config - The config for removing the tag.
* @param {string} config.name - The name of the job template.
* @param {string} config.tag - The tag to be removed.
* @returns {Object} - The name and tag of the removed tag.
*/
async function removeJobTemplateTag({ name, tag }) {
const path = `templates/${encodeURIComponent(name)}/tags/${tag}`;
await removeTag({ path, name, tag });
return {
name,
tag
};
}
/**
* Removes a pipeline template tag.
* @param {Object} config - The config for removing the tag.
* @param {string} config.namespace - The namespace of the pipeline template.
* @param {string} config.name - The name of the pipeline template.
* @param {string} config.tag - The tag to be removed.
* @returns {Object} - The removed pipeline template tag information.
*/
async function removePipelineTemplateTag({ namespace, name, tag }) {
const path = `pipeline/template/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}/tags/${tag}`;
await removeTag({ path, name: `${namespace}/${name}`, tag });
return {
namespace,
name,
tag
};
}
/**
* Retrieves the version of a job template based on its name and tag.
* @param {Object} config - The config for retrieving the version.
* @param {string} config.name - The name of the job template.
* @param {string} config.tag - The tag of the job template.
* @returns {Promise<Object>} A promise that resolves to an object containing the name, tag, and version of the job template.
*/
async function getVersionFromJobTemplateTag({ name, tag }) {
const path = `templates/${encodeURIComponent(name)}/${tag}`;
const version = await getVersionFromTag({ path, name, tag });
return version;
}
/**
* Retrieves the version of a pipeline template based on the provided namespace, name, and tag.
* @param {Object} config - The config for retrieving the version.
* @param {string} config.namespace - The namespace of the pipeline template.
* @param {string} config.name - The name of the pipeline template.
* @param {string} config.tag - The tag of the pipeline template.
* @returns {Promise<Object>} - A promise that resolves to an object containing the namespace, name, tag, and version of the pipeline template.
*/
async function getVersionFromPipelineTemplateTag({ namespace, name, tag }) {
const path = `pipeline/template/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}/${tag}`;
const version = await getVersionFromTag({ path, name: `${namespace}/${name}`, tag });
return version;
}
module.exports = {
loadYaml,
validateJobTemplate,
publishJobTemplate,
removeJobTemplate,
removeJobTemplateVersion,
tagJobTemplate,
removeJobTemplateTag,
getVersionFromJobTemplateTag,
validatePipelineTemplate,
publishPipelineTemplate,
tagPipelineTemplate,
removePipelineTemplate,
removePipelineTemplateVersion,
removePipelineTemplateTag,
getVersionFromPipelineTemplateTag
};