screwdriver-api 7.0.130 → 7.0.132

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ 'use strict';
2
+
3
+ const logger = require('screwdriver-logger');
4
+ const { createInternalBuild, updateParentBuilds, getParentBuildStatus, handleNewBuild } = require('./helpers');
5
+
6
+ /**
7
+ * @typedef {import('screwdriver-models').EventFactory} EventFactory
8
+ * @typedef {import('screwdriver-models').BuildFactory} BuildFactory
9
+ * @typedef {import('screwdriver-models').JobFactory} JobFactory
10
+ * @typedef {import('screwdriver-models/lib/event')} Event
11
+ * @typedef {import('screwdriver-models/lib/build')} Build
12
+ * @typedef {import('screwdriver-models/lib/stage')} Stage
13
+ */
14
+
15
+ class JoinBase {
16
+ /**
17
+ * Base class for AND trigger and RemoteJoin
18
+ * @param {Object} app Server app object
19
+ * @param {EventFactory} app.eventFactory Server app object
20
+ * @param {BuildFactory} app.buildFactory Server app object
21
+ * @param {JobFactory} app.jobFactory Server app object
22
+ * @param {Object} config Configuration object
23
+ * @param {Build} config.build
24
+ * @param {Stage} config.stage
25
+ * @param {String} config.username
26
+ * @param {String} config.scmContext
27
+ */
28
+ constructor(app, config) {
29
+ this.eventFactory = app.eventFactory;
30
+ this.buildFactory = app.buildFactory;
31
+ this.jobFactory = app.jobFactory;
32
+
33
+ this.currentBuild = config.build;
34
+ this.stage = config.stage;
35
+ this.username = config.username;
36
+ this.scmContext = config.scmContext;
37
+ }
38
+
39
+ /**
40
+ * Create a build if the next build does not exist.
41
+ * If the next build exists, trigger it if the conditions for triggering are met.
42
+ * @param {Number} pipelineId
43
+ * @param {Event} event
44
+ * @param {Build} nextBuild
45
+ * @param {String} nextJobName
46
+ * @param {String} nextJobId
47
+ * @param {import('./helpers').ParentBuilds} parentBuilds
48
+ * @param {String} parentBuildId
49
+ * @param {String[]} joinListNames
50
+ * @returns {Promise<Build[]|null>}
51
+ */
52
+ async processNextBuild({
53
+ pipelineId,
54
+ event,
55
+ nextBuild,
56
+ nextJobName,
57
+ nextJobId,
58
+ parentBuilds,
59
+ parentBuildId,
60
+ joinListNames
61
+ }) {
62
+ let newBuild;
63
+
64
+ // Create next build
65
+ if (!nextBuild) {
66
+ newBuild = await createInternalBuild({
67
+ jobFactory: this.jobFactory,
68
+ buildFactory: this.buildFactory,
69
+ pipelineId,
70
+ jobName: nextJobName,
71
+ jobId: nextJobId,
72
+ username: this.username,
73
+ scmContext: this.scmContext,
74
+ event, // this is the parentBuild for the next build
75
+ baseBranch: event.baseBranch || null,
76
+ parentBuilds,
77
+ parentBuildId,
78
+ start: false
79
+ });
80
+ } else {
81
+ newBuild = await updateParentBuilds({
82
+ joinParentBuilds: parentBuilds,
83
+ nextBuild,
84
+ build: this.currentBuild
85
+ });
86
+ }
87
+
88
+ if (!newBuild) {
89
+ logger.error(`No build found for ${pipelineId}:${nextJobName}`);
90
+
91
+ return null;
92
+ }
93
+
94
+ /* CHECK IF ALL PARENT BUILDS OF NEW BUILD ARE DONE */
95
+ const { hasFailure, done } = await getParentBuildStatus({
96
+ newBuild,
97
+ joinListNames,
98
+ pipelineId,
99
+ buildFactory: this.buildFactory
100
+ });
101
+
102
+ return handleNewBuild({
103
+ done,
104
+ hasFailure,
105
+ newBuild,
106
+ nextJobName,
107
+ pipelineId,
108
+ stage: this.stage
109
+ });
110
+ }
111
+ }
112
+
113
+ module.exports = {
114
+ JoinBase
115
+ };
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ const { OrBase } = require('./orBase');
4
+
5
+ /**
6
+ * @typedef {import('screwdriver-models/lib/build')} Build
7
+ * @typedef {import('screwdriver-models/lib/event')} Event
8
+ */
9
+
10
+ class OrTrigger extends OrBase {
11
+ /**
12
+ * Trigger the next jobs of the current job
13
+ * @param {Event} event
14
+ * @param {Number} pipelineId
15
+ * @param {String} nextJobName
16
+ * @param {Number} nextJobId
17
+ * @param {import('./helpers').ParentBuilds} parentBuilds
18
+ * @return {Promise<Build|null>}
19
+ */
20
+ async execute(event, pipelineId, nextJobName, nextJobId, parentBuilds) {
21
+ return this.trigger(event, pipelineId, nextJobName, nextJobId, parentBuilds);
22
+ }
23
+ }
24
+
25
+ module.exports = {
26
+ OrTrigger
27
+ };
@@ -0,0 +1,80 @@
1
+ 'use strict';
2
+
3
+ const { createInternalBuild, Status } = require('./helpers');
4
+
5
+ /**
6
+ * @typedef {import('screwdriver-models').BuildFactory} BuildFactory
7
+ * @typedef {import('screwdriver-models').JobFactory} JobFactory
8
+ * @typedef {import('screwdriver-models').PipelineFactory} PipelineFactory
9
+ * @typedef {import('screwdriver-models/lib/build')} Build
10
+ * @typedef {import('screwdriver-models/lib/event')} Event
11
+ */
12
+
13
+ class OrBase {
14
+ /**
15
+ * Trigger the next jobs of the current job
16
+ * @param {Object} app Server app object
17
+ * @param {BuildFactory} app.buildFactory
18
+ * @param {JobFactory} app.jobFactory
19
+ * @param {PipelineFactory} app.pipelineFactory
20
+ * @param {Object} config Configuration object
21
+ * @param {Build} config.currentBuild
22
+ * @param {String} config.username
23
+ * @param {String} config.scmContext
24
+ */
25
+ constructor(app, config) {
26
+ this.buildFactory = app.buildFactory;
27
+ this.jobFactory = app.jobFactory;
28
+ this.pipelineFactory = app.pipelineFactory;
29
+
30
+ this.currentBuild = config.build;
31
+ this.username = config.username;
32
+ this.scmContext = config.scmContext;
33
+ }
34
+
35
+ /**
36
+ * Trigger the next jobs of the current job
37
+ * @param {Event} event
38
+ * @param {Number} pipelineId
39
+ * @param {String} nextJobName
40
+ * @param {Number} nextJobId
41
+ * @param {import('./helpers').ParentBuilds} parentBuilds
42
+ * @return {Promise<Build|null>}
43
+ */
44
+ async trigger(event, pipelineId, nextJobName, nextJobId, parentBuilds) {
45
+ const nextBuild = await this.buildFactory.get({
46
+ eventId: event.id,
47
+ jobId: nextJobId
48
+ });
49
+
50
+ if (nextBuild !== null) {
51
+ if (Status.isStarted(nextBuild.status)) {
52
+ return nextBuild;
53
+ }
54
+
55
+ nextBuild.status = Status.QUEUED;
56
+ await nextBuild.update();
57
+
58
+ return nextBuild.start();
59
+ }
60
+
61
+ return createInternalBuild({
62
+ jobFactory: this.jobFactory,
63
+ buildFactory: this.buildFactory,
64
+ pipelineId,
65
+ jobName: nextJobName,
66
+ jobId: nextJobId,
67
+ username: this.username,
68
+ scmContext: this.scmContext,
69
+ event,
70
+ baseBranch: event.baseBranch || null,
71
+ parentBuilds,
72
+ parentBuildId: this.currentBuild.id,
73
+ start: true
74
+ });
75
+ }
76
+ }
77
+
78
+ module.exports = {
79
+ OrBase
80
+ };
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ const { getParallelBuilds, mergeParentBuilds, getParentBuildIds } = require('./helpers');
4
+ const { JoinBase } = require('./joinBase');
5
+
6
+ /**
7
+ * @typedef {import('screwdriver-models/lib/event')} Event
8
+ * @typedef {import('screwdriver-models/lib/build')} Build
9
+ */
10
+
11
+ class RemoteJoin extends JoinBase {
12
+ /**
13
+ * @param {Object} app Application object
14
+ * @param {Object} config Config object
15
+ * @param {Event} currentEvent Current event
16
+ */
17
+ constructor(app, config, currentEvent) {
18
+ super(app, config);
19
+
20
+ this.currentEvent = currentEvent;
21
+ }
22
+
23
+ /**
24
+ * Trigger the next external jobs of the current job
25
+ * @param {Event} externalEvent Downstream pipeline's event
26
+ * @param {String} nextJobName
27
+ * @param {Number} nextJobId
28
+ * @param {import('./helpers').ParentBuilds} parentBuilds
29
+ * @param {Build[]} groupEventBuilds Builds of the downstream pipeline, where only the latest ones for each job are included that have the same groupEventId as the externalEvent
30
+ * @param {String[]} joinListNames
31
+ * @returns {Promise<Build|null>}
32
+ */
33
+ async execute(externalEvent, nextJobName, nextJobId, parentBuilds, groupEventBuilds, joinListNames) {
34
+ // fetch builds created due to trigger
35
+ const parallelBuilds = await getParallelBuilds({
36
+ eventFactory: this.eventFactory,
37
+ parentEventId: externalEvent.id,
38
+ pipelineId: externalEvent.pipelineId
39
+ });
40
+
41
+ groupEventBuilds.push(...parallelBuilds);
42
+
43
+ // When restart case, should we create a new build ?
44
+ const nextBuild = groupEventBuilds.find(b => b.jobId === nextJobId && b.eventId === externalEvent.id);
45
+
46
+ const newParentBuilds = mergeParentBuilds(parentBuilds, groupEventBuilds, this.currentEvent, externalEvent);
47
+
48
+ const parentBuildId = getParentBuildIds({
49
+ currentBuildId: this.currentBuild.id,
50
+ parentBuilds: newParentBuilds,
51
+ joinListNames,
52
+ pipelineId: externalEvent.pipelineId
53
+ });
54
+
55
+ return this.processNextBuild({
56
+ pipelineId: externalEvent.pipelineId,
57
+ event: externalEvent,
58
+ nextBuild,
59
+ nextJobName,
60
+ nextJobId,
61
+ parentBuilds: newParentBuilds,
62
+ parentBuildId,
63
+ joinListNames
64
+ });
65
+ }
66
+ }
67
+
68
+ module.exports = {
69
+ RemoteJoin
70
+ };
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ const { OrBase } = require('./orBase');
4
+
5
+ /**
6
+ * @typedef {import('screwdriver-models/lib/build')} Build
7
+ * @typedef {import('screwdriver-models/lib/event')} Event
8
+ */
9
+
10
+ class RemoteTrigger extends OrBase {
11
+ /**
12
+ * Trigger the next jobs of the current job
13
+ * @param {Event} event
14
+ * @param {Number} pipelineId
15
+ * @param {String} nextJobName
16
+ * @param {String} nextJobId
17
+ * @param {import('./helpers').ParentBuilds} parentBuilds
18
+ * @returns {Promise<Build|null>}
19
+ */
20
+ async execute(event, pipelineId, nextJobName, nextJobId, parentBuilds) {
21
+ return this.trigger(event, pipelineId, nextJobName, nextJobId, parentBuilds);
22
+ }
23
+ }
24
+
25
+ module.exports = {
26
+ RemoteTrigger
27
+ };