screwdriver-api 8.0.12 → 8.0.14

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screwdriver-api",
3
- "version": "8.0.12",
3
+ "version": "8.0.14",
4
4
  "description": "API server for the Screwdriver.cd service",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -111,7 +111,7 @@
111
111
  "screwdriver-config-parser": "^12.0.0",
112
112
  "screwdriver-coverage-bookend": "^3.0.0",
113
113
  "screwdriver-coverage-sonar": "^5.0.0",
114
- "screwdriver-data-schema": "^25.1.3",
114
+ "screwdriver-data-schema": "^25.2.0",
115
115
  "screwdriver-datastore-sequelize": "^10.0.0",
116
116
  "screwdriver-executor-base": "^11.0.0",
117
117
  "screwdriver-executor-docker": "^8.0.1",
@@ -50,6 +50,42 @@ class AndTrigger extends JoinBase {
50
50
  return relatedBuilds;
51
51
  }
52
52
 
53
+ /**
54
+ * Get next build in the latest child evnet
55
+ * @param {Job} nextJob next job
56
+ * @returns {Promise<Build|null>}
57
+ */
58
+ async fetchNextBuildInChildEvents(nextJob) {
59
+ const childEvents = await this.eventFactory.list({
60
+ params: {
61
+ parentEventId: this.currentEvent.id,
62
+ pipelineId: this.pipelineId
63
+ }
64
+ });
65
+
66
+ if (childEvents.length === 0) {
67
+ return null;
68
+ }
69
+
70
+ const childEventBuilds = await this.buildFactory.list({
71
+ params: {
72
+ eventId: childEvents.map(e => e.id),
73
+ jobId: nextJob.id
74
+ }
75
+ });
76
+
77
+ if (childEventBuilds.length === 0) {
78
+ return null;
79
+ }
80
+
81
+ // Get the build of the latest evnet
82
+ const childEventNextBuild = childEventBuilds.reduce((l, r) => {
83
+ return l.eventId > r.eventId ? l : r;
84
+ });
85
+
86
+ return childEventNextBuild;
87
+ }
88
+
53
89
  /**
54
90
  * Trigger the next jobs of the current job
55
91
  * @param {Job} nextJob
@@ -63,28 +99,31 @@ class AndTrigger extends JoinBase {
63
99
  logger.info(`Fetching finished builds for event ${this.currentEvent.id}`);
64
100
 
65
101
  const relatedBuilds = await this.fetchRelatedBuilds();
66
-
67
- // Find the next build from the related builds for this event
68
- let nextBuild = relatedBuilds.find(b => b.jobId === nextJob.id && b.eventId === this.currentEvent.id);
69
-
70
- if (!nextBuild) {
102
+ const childEventBuild = await this.fetchNextBuildInChildEvents(nextJob);
103
+ const groupEventsNextBuild = relatedBuilds.find(
104
+ b => b.jobId === nextJob.id && b.eventId > this.currentEvent.id
105
+ );
106
+ let currentEventNextBuild = relatedBuilds.find(
107
+ b => b.jobId === nextJob.id && b.eventId === this.currentEvent.id
108
+ );
109
+
110
+ // Find the next build of this event
111
+ if (!currentEventNextBuild) {
71
112
  // If the build to join fails and it succeeds on restart, depending on the timing, the latest build will be that of a child event.
72
113
  // In that case, `nextBuild` will be null and will not be triggered even though there is a build that should be triggered.
73
114
  // Now we need to check for the existence of a build that should be triggered in its own event.
74
- nextBuild = await this.buildFactory.get({
115
+ currentEventNextBuild = await this.buildFactory.get({
75
116
  eventId: this.currentEvent.id,
76
117
  jobId: nextJob.id
77
118
  });
78
119
 
79
- if (nextBuild) {
80
- relatedBuilds.push(nextBuild);
120
+ if (currentEventNextBuild) {
121
+ relatedBuilds.push(currentEventNextBuild);
81
122
  }
82
123
  }
83
124
 
84
- if (!nextBuild) {
85
- // If the build to join is in the child event, its event id is greater than current event.
86
- nextBuild = relatedBuilds.find(b => b.jobId === nextJob.id && b.eventId > this.currentEvent.id);
87
- }
125
+ const nextBuild = childEventBuild || currentEventNextBuild || groupEventsNextBuild;
126
+
88
127
  const newParentBuilds = mergeParentBuilds(parentBuilds, relatedBuilds, this.currentEvent, undefined);
89
128
  let nextEvent = this.currentEvent;
90
129
 
@@ -18,15 +18,18 @@ const ANNOTATION_USE_DEPLOY_KEY = 'screwdriver.cd/useDeployKey';
18
18
  * @return {Promise}
19
19
  */
20
20
  function getPermissionsForOldPipeline({ scmContexts, pipeline, user }) {
21
+ const isPipelineSCMContextObsolete = !scmContexts.includes(pipeline.scmContext);
22
+ const isUserFromAnotherSCMContext = user.scmContext !== pipeline.scmContext;
23
+
21
24
  // this pipeline's scmContext has been removed, allow current admin to change it
22
- if (!scmContexts.includes(pipeline.scmContext)) {
23
- const permission = { admin: false };
25
+ // also allow pipeline admins from other scmContexts to change it
26
+ if (isPipelineSCMContextObsolete || isUserFromAnotherSCMContext) {
27
+ const isUserIdInAdminList = pipeline.adminUserIds.includes(user.id);
28
+ const isSCMUsernameInAdminsObject = !!pipeline.admins[user.username];
24
29
 
25
- if (pipeline.admins[user.username]) {
26
- permission.admin = true;
27
- }
30
+ const isAdmin = isUserIdInAdminList || (isPipelineSCMContextObsolete && isSCMUsernameInAdminsObject);
28
31
 
29
- return Promise.resolve(permission);
32
+ return Promise.resolve({ admin: isAdmin });
30
33
  }
31
34
 
32
35
  return user.getPermissions(pipeline.scmUri);
@@ -136,6 +139,10 @@ module.exports = () => ({
136
139
  [username]: true
137
140
  };
138
141
 
142
+ if (!oldPipeline.adminUserIds.includes(user.id)) {
143
+ oldPipeline.adminUserIds.push(user.id);
144
+ }
145
+
139
146
  if (settings) {
140
147
  oldPipeline.settings = { ...oldPipeline.settings, ...settings };
141
148
  }