screwdriver-api 8.0.180 → 8.0.182

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.180",
3
+ "version": "8.0.182",
4
4
  "description": "API server for the Screwdriver.cd service",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -6,7 +6,7 @@ const merge = require('lodash.mergewith');
6
6
  const isEmpty = require('lodash.isempty');
7
7
  const { PR_JOB_NAME, PR_STAGE_NAME, STAGE_TEARDOWN_PATTERN } = require('screwdriver-data-schema').config.regex;
8
8
  const { getFullStageJobName } = require('../../helper');
9
- const { locker } = require('../../lock');
9
+ const locker = require('../../lock');
10
10
  const { updateVirtualBuildSuccess, emitBuildStatusEvent, isFixedBuild } = require('../triggers/helpers');
11
11
  const TERMINAL_STATUSES = ['FAILURE', 'ABORTED', 'UNSTABLE', 'COLLAPSED'];
12
12
  const FINISHED_STATUSES = ['SUCCESS', ...TERMINAL_STATUSES];
@@ -459,8 +459,16 @@ 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;
462
464
 
463
465
  if (stage) {
466
+ stageResource = `event:${event.id}:stage:${stage.id}`;
467
+ try {
468
+ stageLock = await locker.lock(stageResource);
469
+ } catch (err) {
470
+ stageLock = null;
471
+ }
464
472
  stageBuild = await stageBuildFactory.get({
465
473
  stageId: stage.id,
466
474
  eventId: newEvent.id
@@ -534,6 +542,13 @@ async function updateBuildAndTriggerDownstreamJobs(config, build, server, userna
534
542
  }
535
543
  }
536
544
  }
545
+ if (stageResource) {
546
+ try {
547
+ await locker.unlock(stageLock, stageResource);
548
+ } catch (err) {
549
+ // ignore unlock errors for parity with lock helper behavior
550
+ }
551
+ }
537
552
 
538
553
  // update event status
539
554
  const latestBuilds = await newEvent.getBuilds();
@@ -22,24 +22,24 @@ module.exports = config => ({
22
22
  const { pipelineFactory, eventFactory } = request.server.app;
23
23
  const pipelineId = request.params.id;
24
24
  const { statusColor } = config;
25
- const badgeConfig = {
26
- statusColor
27
- };
28
25
  const contentType = 'image/svg+xml;charset=utf-8';
26
+ let label;
29
27
 
30
28
  try {
31
29
  // Get pipeline
32
30
  const pipeline = await pipelineFactory.get(pipelineId);
33
31
 
34
32
  if (!pipeline) {
35
- return h.response(getPipelineBadge(badgeConfig)).header('Content-Type', contentType);
33
+ return h.response(getPipelineBadge({ statusColor })).header('Content-Type', contentType);
36
34
  }
37
35
 
36
+ label = pipeline.name;
37
+
38
38
  // Get latest pipeline events
39
39
  const latestEvents = await eventFactory.getPipelineTypeBuildEvents(pipelineId);
40
40
 
41
41
  if (!latestEvents || Object.keys(latestEvents).length === 0) {
42
- return h.response(getPipelineBadge(badgeConfig)).header('Content-Type', contentType);
42
+ return h.response(getPipelineBadge({ statusColor, label })).header('Content-Type', contentType);
43
43
  }
44
44
 
45
45
  // Only care about latest
@@ -47,26 +47,19 @@ module.exports = config => ({
47
47
  const builds = await lastEvent.getBuilds({ readOnly: true, sortBy: 'id', sort: 'descending' });
48
48
 
49
49
  if (!builds || builds.length < 1) {
50
- return h.response(getPipelineBadge(badgeConfig)).header('Content-Type', contentType);
50
+ return h.response(getPipelineBadge({ statusColor, label })).header('Content-Type', contentType);
51
51
  }
52
52
 
53
53
  // Convert build statuses
54
54
  const buildsStatus = builds.map(build => build.status.toLowerCase());
55
55
 
56
56
  return h
57
- .response(
58
- getPipelineBadge(
59
- Object.assign(badgeConfig, {
60
- buildsStatus,
61
- label: pipeline.name
62
- })
63
- )
64
- )
57
+ .response(getPipelineBadge({ statusColor, label, buildsStatus }))
65
58
  .header('Content-Type', contentType);
66
59
  } catch (err) {
67
60
  logger.error(`Failed to get badge for pipeline:${pipelineId}: ${err.message}`);
68
61
 
69
- return h.response(getPipelineBadge(badgeConfig));
62
+ return h.response(getPipelineBadge({ statusColor, label }));
70
63
  }
71
64
  },
72
65
  validate: {
@@ -3,6 +3,7 @@
3
3
  const joi = require('joi');
4
4
  const schema = require('screwdriver-data-schema');
5
5
  const idSchema = schema.models.pipeline.base.extract('id');
6
+ const logger = require('screwdriver-logger');
6
7
  const { getJobBadge } = require('./helper');
7
8
 
8
9
  module.exports = config => ({
@@ -22,61 +23,55 @@ module.exports = config => ({
22
23
  const { pipelineFactory } = request.server.app;
23
24
  const { id, jobName } = request.params;
24
25
  const { statusColor } = config;
25
- const badgeConfig = {
26
- statusColor
27
- };
28
26
  const contentType = 'image/svg+xml;charset=utf-8';
27
+ let label;
29
28
 
30
- return Promise.all([
31
- jobFactory.get({
32
- pipelineId: id,
33
- name: jobName
34
- }),
35
- pipelineFactory.get(id)
36
- ])
37
- .then(([job, pipeline]) => {
38
- if (!job) {
39
- return h.response(getJobBadge(badgeConfig)).header('Content-Type', contentType);
40
- }
29
+ try {
30
+ const [job, pipeline] = await Promise.all([
31
+ jobFactory.get({
32
+ pipelineId: id,
33
+ name: jobName
34
+ }),
35
+ pipelineFactory.get(id)
36
+ ]);
37
+
38
+ if (!pipeline) {
39
+ return h.response(getJobBadge({ statusColor })).header('Content-Type', contentType);
40
+ }
41
+
42
+ label = pipeline.name;
43
+
44
+ if (!job) {
45
+ return h.response(getJobBadge({ statusColor, label })).header('Content-Type', contentType);
46
+ }
41
47
 
42
- if (job.state === 'DISABLED') {
43
- return h
44
- .response(
45
- getJobBadge(
46
- Object.assign(badgeConfig, {
47
- builds: [
48
- {
49
- status: 'DISABLED'
50
- }
51
- ],
52
- label: `${pipeline.name}:${jobName}`
53
- })
54
- )
55
- )
56
- .header('Content-Type', contentType);
48
+ label = `${pipeline.name}:${jobName}`;
49
+
50
+ if (job.state === 'DISABLED') {
51
+ return h
52
+ .response(
53
+ getJobBadge({
54
+ statusColor,
55
+ label,
56
+ builds: [{ status: 'DISABLED' }]
57
+ })
58
+ )
59
+ .header('Content-Type', contentType);
60
+ }
61
+
62
+ const builds = await job.getBuilds({
63
+ paginate: {
64
+ page: 1,
65
+ count: 1
57
66
  }
67
+ });
58
68
 
59
- const listConfig = {
60
- paginate: {
61
- page: 1,
62
- count: 1
63
- }
64
- };
69
+ return h.response(getJobBadge({ statusColor, label, builds })).header('Content-Type', contentType);
70
+ } catch (err) {
71
+ logger.error(`Failed to get job badge for pipeline:${id}, job:${jobName}: ${err.message}`);
65
72
 
66
- return job.getBuilds(listConfig).then(builds => {
67
- return h
68
- .response(
69
- getJobBadge(
70
- Object.assign(badgeConfig, {
71
- builds,
72
- label: `${pipeline.name}:${jobName}`
73
- })
74
- )
75
- )
76
- .header('Content-Type', contentType);
77
- });
78
- })
79
- .catch(() => h.response(getJobBadge(badgeConfig)));
73
+ return h.response(getJobBadge({ statusColor, label })).header('Content-Type', contentType);
74
+ }
80
75
  },
81
76
  validate: {
82
77
  params: joi.object({