screwdriver-api 8.0.13 → 8.0.15

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/lib/server.js CHANGED
@@ -9,12 +9,11 @@ process.on('unhandledRejection', (reason, p) => {
9
9
  });
10
10
 
11
11
  /**
12
- * If we're throwing errors, let's have them say a little more than just 500
13
- * @method prettyPrintErrors
12
+ * @method handlePreResponseLogs
14
13
  * @param {Hapi.Request} request Hapi Request object
15
14
  * @param {Hapi.h} h Hapi Response Toolkit
16
15
  */
17
- function prettyPrintErrors(request, h) {
16
+ function handlePreResponseLogs(request, h) {
18
17
  const { response } = request;
19
18
  const { release } = request.server.app;
20
19
 
@@ -22,31 +21,45 @@ function prettyPrintErrors(request, h) {
22
21
  h.state(release.cookieName, release.cookieValue);
23
22
  }
24
23
 
25
- if (!response.isBoom) {
26
- return h.continue;
27
- }
24
+ // Pretty print errors
25
+ if (response.isBoom) {
26
+ const err = response;
27
+ const errName = err.output.payload.error;
28
+ const errMessage = err.message;
29
+ const { statusCode } = err.output.payload;
30
+ const stack = err.stack || errMessage;
31
+
32
+ // If we're throwing errors, let's have them say a little more than just 500
33
+ if (statusCode === 500) {
34
+ request.log(['server', 'error'], stack);
35
+ }
28
36
 
29
- const err = response;
30
- const errName = err.output.payload.error;
31
- const errMessage = err.message;
32
- const { statusCode } = err.output.payload;
33
- const stack = err.stack || errMessage;
37
+ const res = {
38
+ statusCode,
39
+ error: errName,
40
+ message: errMessage
41
+ };
34
42
 
35
- if (statusCode === 500) {
36
- request.log(['server', 'error'], stack);
37
- }
43
+ if (err.data) {
44
+ res.data = err.data;
45
+ }
38
46
 
39
- const res = {
40
- statusCode,
41
- error: errName,
42
- message: errMessage
43
- };
47
+ return h.response(res).code(statusCode);
48
+ }
44
49
 
45
- if (err.data) {
46
- res.data = err.data;
50
+ // Log request payload when it takes longer than 5 seconds to respond
51
+ // This is to prevent logging payloads for every request
52
+ if (request.info && request.info.received && Date.now() - request.info.received > 5000 && request.payload) {
53
+ request.log(['payload'], {
54
+ method: request.method,
55
+ path: request.path,
56
+ payload: request.payload,
57
+ statusCode: request.response && request.response.statusCode,
58
+ responseTime: Date.now() - request.info.received
59
+ });
47
60
  }
48
61
 
49
- return h.response(res).code(statusCode);
62
+ return h.continue;
50
63
  }
51
64
 
52
65
  /**
@@ -164,8 +177,7 @@ module.exports = async config => {
164
177
  });
165
178
  }
166
179
 
167
- // Write prettier errors
168
- server.ext('onPreResponse', prettyPrintErrors);
180
+ server.ext('onPreResponse', handlePreResponseLogs);
169
181
 
170
182
  // Audit log
171
183
  if (config.log && config.log.audit.enabled) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screwdriver-api",
3
- "version": "8.0.13",
3
+ "version": "8.0.15",
4
4
  "description": "API server for the Screwdriver.cd service",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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