screwdriver-api 8.0.190 → 8.0.192

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.190",
3
+ "version": "8.0.192",
4
4
  "description": "API server for the Screwdriver.cd service",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -459,11 +459,11 @@ async function updateBuildAndTriggerDownstreamJobs(config, build, server, userna
459
459
  let stageBuild;
460
460
  const isStageTeardown = STAGE_TEARDOWN_PATTERN.test(job.name);
461
461
  let stageBuildHasFailure = false;
462
- let stageLock;
463
- let stageResource;
464
462
 
465
463
  if (stage) {
466
- stageResource = `event:${event.id}:stage:${stage.id}`;
464
+ const stageResource = `event:${event.id}:stage:${stage.id}`;
465
+ let stageLock;
466
+
467
467
  try {
468
468
  stageLock = await locker.lock(stageResource);
469
469
  } catch (err) {
@@ -476,6 +476,11 @@ async function updateBuildAndTriggerDownstreamJobs(config, build, server, userna
476
476
 
477
477
  stageBuild = await updateStageBuildStatus({ stageBuild, newStatus: newBuild.status, job });
478
478
 
479
+ try {
480
+ await locker.unlock(stageLock, stageResource);
481
+ } catch (err) {
482
+ // ignore unlock errors for parity with lock helper behavior
483
+ }
479
484
  stageBuildHasFailure = TERMINAL_STATUSES.includes(stageBuild.status);
480
485
  }
481
486
 
@@ -497,6 +502,15 @@ async function updateBuildAndTriggerDownstreamJobs(config, build, server, userna
497
502
  // (if stage teardown build exists, and stageBuild.status is negative,
498
503
  // and there are no active stage builds, and teardown build is not started)
499
504
  if (stage && FINISHED_STATUSES.includes(newBuild.status) && !isStageTeardown) {
505
+ const stageResource = `event:${event.id}:stage:${stage.id}`;
506
+ let stageLock;
507
+
508
+ try {
509
+ stageLock = await locker.lock(stageResource);
510
+ } catch (err) {
511
+ stageLock = null;
512
+ }
513
+
500
514
  const stageTeardownName = getFullStageJobName({ stageName: stage.name, jobName: 'teardown' });
501
515
  const stageTeardownJob = await jobFactory.get({ pipelineId: pipeline.id, name: stageTeardownName });
502
516
  let stageTeardownBuild = await buildFactory.get({ eventId: newEvent.id, jobId: stageTeardownJob.id });
@@ -541,8 +555,7 @@ async function updateBuildAndTriggerDownstreamJobs(config, build, server, userna
541
555
  }
542
556
  }
543
557
  }
544
- }
545
- if (stageResource) {
558
+
546
559
  try {
547
560
  await locker.unlock(stageLock, stageResource);
548
561
  } catch (err) {
@@ -65,21 +65,16 @@ module.exports = () => ({
65
65
  throw boom.conflict(`Token ${match.name} already exists`);
66
66
  }
67
67
 
68
- let payloadOptions;
69
-
70
- if (request.payload && request.payload.options) {
71
- payloadOptions = request.payload.options;
72
- }
73
-
74
68
  Object.keys(request.payload).forEach(key => {
75
69
  if (key === 'options') {
76
- const hasResourceUpdates =
77
- payloadOptions.resources && Object.keys(payloadOptions.resources).length > 0;
70
+ const payloadOptions = request.payload.options || {};
71
+ const tokenOptions = token.options || {};
72
+ const hasResourceUpdates = Object.keys(payloadOptions.resources || {}).length > 0;
78
73
 
79
74
  token.options = {
80
- ...token.options,
75
+ ...tokenOptions,
81
76
  ...payloadOptions,
82
- resources: hasResourceUpdates ? payloadOptions.resources : token.options.resources
77
+ resources: hasResourceUpdates ? payloadOptions.resources : tokenOptions.resources || {}
83
78
  };
84
79
  } else {
85
80
  token[key] = request.payload[key];
@@ -50,21 +50,18 @@ module.exports = () => ({
50
50
  throw boom.conflict(`Token with name ${match.name} already exists`);
51
51
  }
52
52
 
53
- let payloadOptions;
54
-
55
- if (request.payload && request.payload.options) {
56
- payloadOptions = request.payload.options;
57
- }
58
-
59
53
  Object.keys(request.payload).forEach(key => {
60
54
  if (key === 'options') {
61
- const hasResourceUpdates =
62
- payloadOptions.resources && Object.keys(payloadOptions.resources).length > 0;
55
+ const payloadOptions = request.payload.options || {};
56
+ const tokenOptions = token.options || {};
57
+ const hasResourceUpdates = Object.keys(payloadOptions.resources || {}).length > 0;
63
58
 
64
59
  token.options = {
65
- ...token.options,
60
+ ...tokenOptions,
66
61
  ...payloadOptions,
67
- resources: hasResourceUpdates ? payloadOptions.resources : token.options.resources
62
+ resources: hasResourceUpdates
63
+ ? payloadOptions.resources
64
+ : tokenOptions.resources || {}
68
65
  };
69
66
  } else {
70
67
  token[key] = request.payload[key];