-
Notifications
You must be signed in to change notification settings - Fork 21
/
build.js
763 lines (653 loc) · 25.8 KB
/
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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
'use strict';
const boom = require('@hapi/boom');
const dayjs = require('dayjs');
const hoek = require('@hapi/hoek');
const deepcopy = require('deepcopy');
const logger = require('screwdriver-logger');
const { PR_JOB_NAME, EXTERNAL_TRIGGER } = require('screwdriver-data-schema').config.regex;
const { SCM_STATE_MAP, SCM_STATUSES } = require('screwdriver-data-schema').plugins.scm;
const BaseModel = require('./base');
// Symbols for private members
const executor = Symbol('executor');
const apiUri = Symbol('apiUri');
const tokenGen = Symbol('tokenGen');
const uiUri = Symbol('uiUri');
const ABORT_CODE = 130; // 128 + SIGINT 2 (^C)
const TEMPORAL_JWT_TIMEOUT = 12 * 60; // 12 hours in minutes
/**
* Get the array of ids for jobs that match the names passed in
* @method findIdsOfMatchedJobs
* @param {Array} jobs Array of jobs
* @param {Array} jobNames Array of job names to find the ids
* @return {Array} Array of job ids that match the job names
*/
function findIdsOfMatchedJobs(jobs, jobNames) {
return jobs.reduce((match, j) => {
let { name } = j;
// Get the actual job name if the matching job is a PR job
if (j.isPR()) {
const prJobName = j.name.match(PR_JOB_NAME);
if (prJobName) {
name = prJobName[2];
}
}
if (jobNames.includes(name)) {
match.push(j.id);
}
return match;
}, []);
}
/**
* Get a list of blockedBy jobIds
* If the blocking job is from an external pipeline, look up the external pipeline to find the jobId
* @method getBlockedByIds
* @param {Pipeline} pipeline Current Pipeline
* @param {Job} job Current Job that contains the blockedBy configuration
* @return {Promise} Array of blockedby JobIds
*/
function getBlockedByIds(pipeline, job) {
let blockedByNames = job.permutations[0].blockedBy;
let blockedByIds = [job.id]; // Always blocked by itself
if (!blockedByNames || blockedByNames.length === 0) {
return Promise.resolve(blockedByIds);
}
const externalBlockedByNames = blockedByNames.filter(name => name.startsWith('~sd@'));
// Remove ~ prefix for job names
blockedByNames = blockedByNames.map(name => name.replace('~', ''));
return pipeline.getJobs().then(pipelineJobs => {
// Get internal blocked by first
blockedByIds = blockedByIds.concat(findIdsOfMatchedJobs(pipelineJobs, blockedByNames));
// If there is no external blocked by, just return
if (externalBlockedByNames.length === 0) {
return Promise.resolve(blockedByIds);
}
// eslint-disable-next-line global-require
const PipelineFactory = require('./pipelineFactory');
const pipelineFactory = PipelineFactory.getInstance();
// Go through the external pipeline Ids and find the matching job id
return (
Promise.all(
externalBlockedByNames.map(fullname => {
const [, pid, jobname] = fullname.match(EXTERNAL_TRIGGER);
return pipelineFactory
.get(parseInt(pid, 10)) // convert pid from string to number
.then(p => {
// If pipeline doesn't exist, don't try to get pipeline jobs
if (!p) {
return [];
}
// Without PR jobs
return p.getJobs({ type: 'pipeline' });
})
.then(jobs => findIdsOfMatchedJobs(jobs, [jobname]));
})
)
// Merge the results
.then(jobIds => blockedByIds.concat(...jobIds))
);
});
}
/**
* Extracts the key value pairs from meta summary and converts
* them into markdown format
* @method formatSummary
* @param {Object} summary Meta summary object
* @return {String} Formatted summary with good things from markdown
*/
function formatSummary(summary) {
let formattedSummary = '';
Object.keys(summary).forEach(key => {
if (typeof summary[key] === 'string') {
formattedSummary += `__${key}__ - ${summary[key]}\n`;
}
});
return formattedSummary;
}
/**
* Extracts the summary from metadata and builds a comment
* @method getPrComment
* @param {Object} config
* @param {Object} config.metadata Build metadata
* @param {String} config.buildId ID of the build
* @param {String} config.buildUrl Build url
* @param {String} config.container Container the build is running in (e.g.: node:8)
* @return {String} comment PR comment
*/
function getPrComment({ metadata, buildId, buildUrl, container }) {
// Format of the comment
/**
### SD Build [#133652](https://cd.screwdriver.cd/pipelines/1/builds/133652)
_node:8_
- - - -
__coverage__ - Coverage increased by 15%
__markdown__ - **this** should have been **bold** or *italic*
###### ~ Screwdriver automated build summary
*/
const commentPrefix = `### SD Build [#${buildId}](${buildUrl})\n_${container}_\n- - - -`;
const commentSuffix = '###### ~ Screwdriver automated build summary';
const summaryText = formatSummary(metadata);
return summaryText ? [commentPrefix, summaryText, commentSuffix].join('\n') : null;
}
/**
* Gets status configs to call updateCommitStatus with
* @param {Object} metadata Metadata object
* @return {Array} Status configs
*/
function getStatusConfig({ metadata }) {
const statusConfigs = [];
/* eslint-disable consistent-return */
Object.keys(metadata).forEach(fieldName => {
let checkObject = metadata[fieldName];
// Break early if status is not formatted correctly
if (typeof checkObject !== 'object' && typeof checkObject !== 'string') {
logger.info(
`Skipping adding meta status for meta.status.${fieldName}: ` +
'is not an object or JSON-parseable string.'
);
return;
}
// Parse JSON string
if (typeof checkObject === 'string') {
try {
checkObject = JSON.parse(checkObject);
} catch (e) {
const message =
`Skipping adding meta status for meta.status.${fieldName}: ` +
'string is not a JSON-parseable string.';
logger.info(message);
return;
}
}
const status = hoek.reach(checkObject, 'status', { default: 'PENDING' });
if (!SCM_STATUSES.includes(status)) {
const message =
`Skipping adding meta status for meta.status.${fieldName}: ` +
`status is not valid. It must be one of ${SCM_STATUSES.join(', ')}`;
logger.info(message);
return;
}
const defaultMessages = {
SUCCESS: `${fieldName} check succeeded`,
FAILURE: `${fieldName} check failed`,
PENDING: `${fieldName} check pending`
};
const message = hoek.reach(checkObject, 'message', {
default: defaultMessages[status] || defaultMessages.pending
});
const url = hoek.reach(checkObject, 'url') ? encodeURI(hoek.reach(checkObject, 'url')) : null;
const config = {
context: fieldName,
buildStatus: status,
description: message
};
if (url) {
config.url = url;
}
statusConfigs.push(config);
});
/* eslint-enable consistent-return */
return statusConfigs;
}
/**
* @description Gets the token from token gen fn
* @param {Object} self -> this object
* @param {Object} job
* @param {Object} pipeline
* @return {String} A jwt token
*/
function getToken(self, job, pipeline) {
const tokenGenConfig = {
isPR: job.isPR(),
jobId: job.id,
eventId: self.eventId,
pipelineId: pipeline.id,
configPipelineId: pipeline.configPipelineId
};
if (job.prParentJobId) {
tokenGenConfig.prParentJobId = job.prParentJobId;
}
// use temporal scope unless executor is queue
const tokenScope = self[executor].name === 'queue' ? 'sdapi' : 'temporal';
const token = self[tokenGen](self.id, tokenGenConfig, pipeline.scmContext, TEMPORAL_JWT_TIMEOUT, [tokenScope]);
return token;
}
class BuildModel extends BaseModel {
/**
* Construct a BuildModel object
* @method constructor
* @param {Object} config
* @param {Object} config.datastore Object that will perform operations on the datastore
* @param {Object} config.executor Object that will perform executor operations
* @param {String} config.jobId The ID of the associated job to start
* @param {String} config.apiUri URI back to the API
* @param {String} config.uiUri URI back to the UI
* @param {String} config.tokenGen Generator for building tokens
* @param {String} [config.sha] The sha of the build
* @param {String} [config.container] The kind of container to use
*/
constructor(config) {
super('build', config);
this[executor] = config.executor;
this[apiUri] = config.apiUri;
this[tokenGen] = config.tokenGen;
this[uiUri] = config.uiUri;
}
/**
* Update status to SCM
* @method updateSCM
* @param {Pipeline} pipeline The build's pipeline
* @return {Promise}
*/
updateCommitStatus(pipeline) {
return Promise.all([this.job, pipeline.token])
.then(([job, token]) => {
const buildUrl = `${this[uiUri]}/pipelines/${pipeline.id}/builds/${this.id}`;
const config = {
token,
scmUri: pipeline.scmUri,
scmContext: pipeline.scmContext,
sha: this.sha,
buildStatus: SCM_STATE_MAP[this.status],
jobName: job.name,
url: buildUrl,
pipelineId: pipeline.id
};
// Skipping handling the empty build status
if (config.buildStatus === '') {
return Promise.resolve();
}
const updateTasks = [this.scm.updateCommitStatus(config)];
// Write meta summary to PR comment if meta.meta.summary object exists
if (hoek.reach(this.meta, 'meta.summary') && this.isDone() && job.isPR()) {
const comment = getPrComment({
metadata: hoek.reach(this.meta, 'meta.summary'),
buildId: this.id,
buildUrl: config.url,
container: this.container
});
const prNum = parseInt(config.jobName.match(/^PR-([0-9]+):[\w-]+$/)[1], 10);
const prConfig = {
comment,
prNum,
scmContext: config.scmContext,
scmUri: config.scmUri,
token
};
if (comment) {
updateTasks.push(this.scm.addPrComment(prConfig));
}
}
// Update git commit status if meta.meta.status object exists
if (hoek.reach(this.meta, 'meta.status') && this.isDone() && job.isPR()) {
const statusConfigs = getStatusConfig({
metadata: hoek.reach(this.meta, 'meta.status')
});
if (statusConfigs && statusConfigs.length > 0) {
statusConfigs.forEach(c => {
const customStatusConfig = hoek.applyToDefaults(config, c);
updateTasks.push(this.scm.updateCommitStatus(customStatusConfig));
});
}
}
return Promise.all(updateTasks);
})
.catch(err => {
logger.error(`Failed to update commit status: ${err}`);
return Promise.resolve();
});
}
/**
* Get models for all steps
* @method getSteps
* @return {Promise}
*/
getSteps() {
const listConfig = {
params: {
buildId: this.id
}
};
// Lazy load factory dependency to prevent circular dependency issues
// https://nodejs.org/api/modules.html#modules_cycles
/* eslint-disable global-require */
const StepFactory = require('./stepFactory');
/* eslint-enable global-require */
const factory = StepFactory.getInstance();
return factory.list(listConfig);
}
/**
* Lazy load a job model
* @property pipeline
* @return {Promise} Resolves to the job associated with this build
*/
get job() {
// Lazy load factory dependency to prevent circular dependency issues
// https://nodejs.org/api/modules.html#modules_cycles
/* eslint-disable global-require */
const JobFactory = require('./jobFactory');
/* eslint-enable global-require */
delete this.job;
const factory = JobFactory.getInstance();
const job = factory.get(this.jobId);
// ES6 has weird getters and setters in classes,
// so we redefine the pipeline property here to resolve to the
// resulting promise and not try to recreate the factory, etc.
Object.defineProperty(this, 'job', {
enumerable: true,
value: job
});
return job;
}
/**
* Lazy load a pipeline model for the build
* @property pipeline
* @return {Promise}
*/
get pipeline() {
delete this.pipeline;
const pipeline = this.job.then(job => {
if (!job) {
throw new Error('Job does not exist');
}
return job.pipeline.then(p => {
if (!p) {
throw new Error('Pipeline does not exist');
}
return p;
});
});
// ES6 has weird getters and setters in classes,
// so we redefine the pipeline property here to resolve to the
// resulting promise and not try to recreate the factory, etc.
Object.defineProperty(this, 'pipeline', {
enumerable: true,
value: pipeline
});
return pipeline;
}
/**
* Lazy load the secrets model for the build
* @property secrets
* @return {Promise}
*/
get secrets() {
delete this.secrets;
const secrets = this.job.then(job => {
if (!job) {
throw new Error('Job does not exist');
}
return job.secrets;
});
// ES6 has weird getters and setters in classes,
// so we redefine the secrets property here to resolve to the
// resulting promise and not try to recreate the factory, etc.
Object.defineProperty(this, 'secrets', {
enumerable: true,
value: secrets
});
return secrets;
}
/**
* Start this build and update commit status as pending
* @method start
* @param startConfig
* @param startConfig.causeMessage Message that describes why the event was created
* @return {Promise}
*/
start(startConfig = {}) {
const { causeMessage } = startConfig;
// Make sure that a pipeline and job is associated with the build
return this.job.then(job =>
job.pipeline
.then(pipeline =>
getBlockedByIds(pipeline, job)
.then(blockedBy => {
const config = {
build: this,
jobId: job.id,
jobState: job.state,
jobArchived: job.archived,
jobName: job.name,
annotations: hoek.reach(job.permutations[0], 'annotations', { default: {} }),
blockedBy,
pipeline: {
id: pipeline.id,
scmContext: pipeline.scmContext,
configPipelineId: pipeline.configPipelineId
},
causeMessage: causeMessage || '',
freezeWindows: hoek.reach(job.permutations[0], 'freezeWindows', { default: [] }),
apiUri: this[apiUri],
buildId: this.id,
eventId: this.eventId,
container: this.container,
tokenGen: this[tokenGen],
token: getToken(this, job, pipeline),
pipelineId: pipeline.id,
isPR: job.isPR(),
prParentJobId: job.prParentJobId
};
if (this.buildClusterName) {
config.buildClusterName = this.buildClusterName;
}
if (hoek.reach(job.permutations[0], 'provider')) {
config.provider = job.permutations[0].provider;
}
return this[executor].start(config);
})
.then(() => this.updateCommitStatus(pipeline))
) // update github
.then(() => this)
);
}
/**
* Update a build and update github status
* @method update
* @return {Promise}
*/
update() {
let prom = Promise.resolve();
// Abort running steps. If no steps ever ran, abort the first step
const abortSteps = () => {
const now = new Date().toISOString();
return this.getSteps().then(steps => {
if (steps.length === 0) {
return Promise.resolve();
}
return Promise.all(
steps.map(step => {
if (step.startTime && !step.endTime) {
step.endTime = now;
step.code = ABORT_CODE;
}
return step.update();
})
);
});
};
// stop the build if we're done
if (this.isDone()) {
prom = abortSteps().then(() => this.stop());
}
// check if the status is changing
if (this.isDirty('status')) {
// update scm with status
prom = prom.then(() => this.pipeline).then(pipeline => this.updateCommitStatus(pipeline));
}
return prom
.then(() => Promise.all([this.job, this.pipeline])) // get latest status
.then(([job, pipeline]) => {
// status is changing to RUNNING so start timer
if (this.status === 'RUNNING' && this.isDirty('status')) {
const config = {
buildId: this.id,
jobId: job.id,
startTime: this.startTime,
annotations: job.permutations[0].annotations,
buildStatus: this.status,
pipelineId: pipeline.id,
token: getToken(this, job, pipeline)
};
if (hoek.reach(job.permutations[0], 'provider')) {
config.provider = job.permutations[0].provider;
}
return this[executor].startTimer(config);
}
return Promise.resolve();
})
.then(() => super.update())
.then(() => this);
}
/**
* Remove all steps associated with this build and the build itself
* @return {Promise} Resolves to null if remove successfully
*/
remove() {
// eslint-disable-next-line global-require
const stepFactory = require('./stepFactory');
const factory = stepFactory.getInstance();
return factory.removeSteps({ buildId: this.id }).then(() => super.remove());
}
/**
* Stop a build
* @method stop
* @return {Promise}
*/
stop() {
return this.job
.then(job =>
job.pipeline
.then(pipeline => Promise.all([getBlockedByIds(pipeline, job), this.pipeline]))
.then(([blockedBy, pipeline]) => {
const config = {
annotations: job.permutations[0].annotations,
freezeWindows: job.permutations[0].freezeWindows,
blockedBy,
buildId: this.id,
jobId: job.id,
pipelineId: pipeline.id,
token: getToken(this, job, pipeline),
jobName: job.name,
apiUri: this[apiUri]
};
if (this.buildClusterName) {
config.buildClusterName = this.buildClusterName;
}
if (hoek.reach(job.permutations[0], 'provider')) {
config.provider = job.permutations[0].provider;
}
return this[executor].stop(config).then(() => this[executor].stopTimer(config));
})
)
.then(() => this)
.catch(err => {
logger.error(`Failed to stop a build: ${err}`);
return Promise.reject(err);
});
}
/**
* Stops a frozen build
* @method stopFrozen
* @param {String} previousStatus The previous build status
* @return {Promise}
*/
stopFrozen(previousStatus) {
return this.job
.then(job =>
job.pipeline.then(pipeline => {
const config = {
buildId: this.id,
jobId: job.id,
pipelineId: pipeline.id,
token: getToken(this, job, pipeline),
status: previousStatus
};
if (hoek.reach(job.permutations[0], 'provider')) {
config.provider = job.permutations[0].provider;
}
return this[executor].stopFrozen(config);
})
)
.then(() => this);
}
/**
* Unzip a ZIP for build artifacts
* @method unzipArtifacts
* @return {Promise}
*/
unzipArtifacts() {
return Promise.all([this.job, this.pipeline]).then(([job, pipeline]) =>
this[executor].unzipArtifacts({
buildId: this.id,
token: getToken(this, job, pipeline)
})
);
}
/**
* Check if a build is done
* @method isDone
* @return boolean
*/
isDone() {
return (
['ABORTED', 'FAILURE', 'SUCCESS', 'COLLAPSED'].includes(this.status) ||
(this.status === 'UNSTABLE' && !!this.endTime)
);
}
/**
* getMetrics for this build
* @method getMetrics
* @param {Object} [config] Configuration object
* @param {String} [config.startTime] Look at steps created after this startTime
* @param {String} [config.endTime] Look at steps created before this endTime
* @param {String} [config.stepName] Name of step
* @return {Promise} Resolves to array of metrics for steps belong to this build
*/
getMetrics(config = { stepName: null }) {
// add build createTime to step metrics
const { createTime } = this;
// filter out old steps without id and buildId info
return this.getSteps().then(steps => {
let targetSteps = steps;
// If stepName, get only that step
if (config.stepName) {
targetSteps = steps.filter(s => s.name === config.stepName);
}
// Generate metrics
return Promise.all(
targetSteps.map(s => {
// For older steps, add buildId info to it
if (!s.buildId) {
s.buildId = this.id;
}
const { id, name, code, startTime, endTime } = s;
const duration = startTime && endTime ? dayjs(endTime).diff(dayjs(startTime), 'second') : null;
return { id, name, code, duration, createTime };
})
);
});
}
/**
* Get a JSON representation of the build data with steps
* @method toJsonWithSteps
* @return {Promise} Resolves JSON of build with steps
*/
toJsonWithSteps() {
// eslint-disable-next-line global-require
const StepFactory = require('./stepFactory');
const stepFactory = StepFactory.getInstance();
return stepFactory
.list({
params: { buildId: this.id },
sortBy: 'id',
sort: 'ascending'
})
.then(steps => {
if (!steps.length) {
throw boom.notFound('Steps do not exist');
}
return Object.assign(deepcopy(this.toJson()), { steps });
});
}
}
module.exports = BuildModel;