@commercetools-frontend/deployment-cli 0.0.5 → 0.0.7

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.
@@ -2,8 +2,6 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var _asyncToGenerator = require('@babel/runtime-corejs3/helpers/asyncToGenerator');
6
- var _regeneratorRuntime = require('@babel/runtime-corejs3/regenerator');
7
5
  var _sliceInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/slice');
8
6
  var mri = require('mri');
9
7
  var _concatInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/concat');
@@ -13,19 +11,18 @@ var _mapInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instan
13
11
  var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter');
14
12
  var _startsWithInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/starts-with');
15
13
  var cosmiconfig = require('cosmiconfig');
16
- var merge = require('lodash.merge');
14
+ var merge = require('lodash/merge');
17
15
  var prompts = require('prompts');
18
16
  var pRetry = require('p-retry');
19
17
  var _slicedToArray = require('@babel/runtime-corejs3/helpers/slicedToArray');
20
18
  var _URLSearchParams = require('@babel/runtime-corejs3/core-js-stable/url-search-params');
21
- var _Object$entries2 = require('@babel/runtime-corejs3/core-js-stable/object/entries');
19
+ var _Object$entries = require('@babel/runtime-corejs3/core-js-stable/object/entries');
22
20
  var _Promise = require('@babel/runtime-corejs3/core-js-stable/promise');
23
21
  var _JSON$stringify = require('@babel/runtime-corejs3/core-js-stable/json/stringify');
24
22
  var fetch = require('node-fetch');
25
23
 
26
24
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
27
25
 
28
- var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);
29
26
  var _sliceInstanceProperty__default = /*#__PURE__*/_interopDefault(_sliceInstanceProperty);
30
27
  var mri__default = /*#__PURE__*/_interopDefault(mri);
31
28
  var _concatInstanceProperty__default = /*#__PURE__*/_interopDefault(_concatInstanceProperty);
@@ -38,51 +35,32 @@ var merge__default = /*#__PURE__*/_interopDefault(merge);
38
35
  var prompts__default = /*#__PURE__*/_interopDefault(prompts);
39
36
  var pRetry__default = /*#__PURE__*/_interopDefault(pRetry);
40
37
  var _URLSearchParams__default = /*#__PURE__*/_interopDefault(_URLSearchParams);
41
- var _Object$entries2__default = /*#__PURE__*/_interopDefault(_Object$entries2);
38
+ var _Object$entries__default = /*#__PURE__*/_interopDefault(_Object$entries);
42
39
  var _Promise__default = /*#__PURE__*/_interopDefault(_Promise);
43
40
  var _JSON$stringify__default = /*#__PURE__*/_interopDefault(_JSON$stringify);
44
41
  var fetch__default = /*#__PURE__*/_interopDefault(fetch);
45
42
 
46
- function loadConfig() {
47
- return _loadConfig.apply(this, arguments);
48
- }
49
- function _loadConfig() {
50
- _loadConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
51
- var deploymentConfigExplorer, defaultConfig, cosmiconfigResult, mergedConfig;
52
- return _regeneratorRuntime__default["default"].wrap(function _callee$(_context7) {
53
- while (1) switch (_context7.prev = _context7.next) {
54
- case 0:
55
- deploymentConfigExplorer = cosmiconfig.cosmiconfig('deployment');
56
- defaultConfig = {
57
- CircleCI: {
58
- apiBaseUrl: 'https://circleci.com/api/v2',
59
- deploymentWorkflowName: 'test_build_and_deploy',
60
- pagination: {
61
- maxPages: 42,
62
- pagesForPipelineSelection: 5
63
- }
64
- },
65
- MerchantCenter: {}
66
- };
67
- _context7.prev = 2;
68
- _context7.next = 5;
69
- return deploymentConfigExplorer.search();
70
- case 5:
71
- cosmiconfigResult = _context7.sent;
72
- mergedConfig = merge__default["default"](defaultConfig, cosmiconfigResult === null || cosmiconfigResult === void 0 ? void 0 : cosmiconfigResult.config);
73
- return _context7.abrupt("return", mergedConfig);
74
- case 10:
75
- _context7.prev = 10;
76
- _context7.t0 = _context7["catch"](2);
77
- console.warn(_context7.t0);
78
- throw new Error('Failed loading a deployment configuration. Create a cosmiconfig for `deployment` for example `deployment.config.cjs`.');
79
- case 14:
80
- case "end":
81
- return _context7.stop();
43
+ async function loadConfig() {
44
+ const deploymentConfigExplorer = cosmiconfig.cosmiconfig('deployment');
45
+ const defaultConfig = {
46
+ CircleCI: {
47
+ apiBaseUrl: 'https://circleci.com/api/v2',
48
+ deploymentWorkflowName: 'test_build_and_deploy',
49
+ pagination: {
50
+ maxPages: 42,
51
+ pagesForPipelineSelection: 5
82
52
  }
83
- }, _callee, null, [[2, 10]]);
84
- }));
85
- return _loadConfig.apply(this, arguments);
53
+ },
54
+ MerchantCenter: {}
55
+ };
56
+ try {
57
+ const cosmiconfigResult = await deploymentConfigExplorer.search();
58
+ const mergedConfig = merge__default["default"](defaultConfig, cosmiconfigResult === null || cosmiconfigResult === void 0 ? void 0 : cosmiconfigResult.config);
59
+ return mergedConfig;
60
+ } catch (e) {
61
+ console.warn(e);
62
+ throw new Error('Failed loading a deployment configuration. Create a cosmiconfig for `deployment` for example `deployment.config.cjs`.');
63
+ }
86
64
  }
87
65
  function throwIfConfigurationLacksRequiredValues(parsedConfiguration) {
88
66
  if (!parsedConfiguration.CircleCI.projectName) {
@@ -90,21 +68,21 @@ function throwIfConfigurationLacksRequiredValues(parsedConfiguration) {
90
68
  }
91
69
  }
92
70
  function throwIfRequiredEnvironmentVariableIsUnset(requiredEnvironmentVariables) {
93
- _forEachInstanceProperty__default["default"](requiredEnvironmentVariables).call(requiredEnvironmentVariables, function (nameOfRequiredEnvironmentVariable) {
94
- var valueOfRequiredEnvironmentVariable = process.env[nameOfRequiredEnvironmentVariable];
71
+ _forEachInstanceProperty__default["default"](requiredEnvironmentVariables).call(requiredEnvironmentVariables, nameOfRequiredEnvironmentVariable => {
72
+ const valueOfRequiredEnvironmentVariable = process.env[nameOfRequiredEnvironmentVariable];
95
73
  if (!valueOfRequiredEnvironmentVariable) throw new Error("Missing '".concat(nameOfRequiredEnvironmentVariable, "' environment variable"));
96
74
  });
97
75
  }
98
- var promptOptions = {
99
- applicationSelect: function applicationSelect(_ref) {
100
- var packages = _ref.packages;
76
+ const promptOptions = {
77
+ applicationSelect: _ref => {
78
+ let packages = _ref.packages;
101
79
  return {
102
80
  type: 'select',
103
81
  name: 'applicationName',
104
82
  message: 'Select an application',
105
- choices: _mapInstanceProperty__default["default"](packages).call(packages, function (_ref2) {
106
- var packageJson = _ref2.packageJson;
107
- var applicationName = packageJson.name.replace('@commercetools-local/application-', '');
83
+ choices: _mapInstanceProperty__default["default"](packages).call(packages, _ref2 => {
84
+ let packageJson = _ref2.packageJson;
85
+ const applicationName = packageJson.name.replace('@commercetools-local/application-', '');
108
86
  return {
109
87
  title: applicationName,
110
88
  value: applicationName
@@ -112,13 +90,13 @@ var promptOptions = {
112
90
  })
113
91
  };
114
92
  },
115
- deploymentPipelineSelect: function deploymentPipelineSelect(_ref3) {
116
- var deploymentPipelines = _ref3.deploymentPipelines;
93
+ deploymentPipelineSelect: _ref3 => {
94
+ let deploymentPipelines = _ref3.deploymentPipelines;
117
95
  return {
118
96
  type: 'select',
119
97
  name: 'deploymentPipeline',
120
98
  message: 'Select the revision you would like to deploy',
121
- choices: _mapInstanceProperty__default["default"](deploymentPipelines).call(deploymentPipelines, function (deploymentPipeline) {
99
+ choices: _mapInstanceProperty__default["default"](deploymentPipelines).call(deploymentPipelines, deploymentPipeline => {
122
100
  var _context, _context2;
123
101
  return {
124
102
  title: _concatInstanceProperty__default["default"](_context = _concatInstanceProperty__default["default"](_context2 = "".concat(deploymentPipeline.vcs.revision.substring(0, 7), " - ")).call(_context2, deploymentPipeline.vcs.commit.subject, " <")).call(_context, deploymentPipeline.trigger.actor.login, ">"),
@@ -127,9 +105,9 @@ var promptOptions = {
127
105
  })
128
106
  };
129
107
  },
130
- deploymentConfirmation: function deploymentConfirmation(_ref4) {
108
+ deploymentConfirmation: _ref4 => {
131
109
  var _context3;
132
- var approvalJob = _ref4.approvalJob,
110
+ let approvalJob = _ref4.approvalJob,
133
111
  revision = _ref4.revision;
134
112
  return {
135
113
  type: 'toggle',
@@ -141,183 +119,108 @@ var promptOptions = {
141
119
  };
142
120
  }
143
121
  };
144
- function paginateToDeploymentPipeline(_x) {
145
- return _paginateToDeploymentPipeline.apply(this, arguments);
146
- }
147
- function _paginateToDeploymentPipeline() {
148
- _paginateToDeploymentPipeline = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2(_ref5) {
149
- var circleCiApis, buildRevision, branch, debug, maxPages, deploymentPipeline, nextPageToken, i, _context8, _context9, pipelineRequest, pipelines, nonScheduledPipelines, _context10, _context11;
150
- return _regeneratorRuntime__default["default"].wrap(function _callee2$(_context13) {
151
- while (1) switch (_context13.prev = _context13.next) {
152
- case 0:
153
- circleCiApis = _ref5.circleCiApis, buildRevision = _ref5.buildRevision, branch = _ref5.branch, debug = _ref5.debug, maxPages = _ref5.maxPages;
154
- i = 0;
155
- case 2:
156
- if (!(i < maxPages)) {
157
- _context13.next = 16;
158
- break;
159
- }
160
- // eslint-disable-next-line no-await-in-loop
161
- pipelineRequest = circleCiApis.pipelines({
162
- pageToken: nextPageToken,
163
- branch: branch
164
- });
165
- _context13.next = 6;
166
- return pipelineRequest.execute({
167
- debug: debug
168
- });
169
- case 6:
170
- pipelines = _context13.sent;
171
- nextPageToken = pipelines.next_page_token;
172
- nonScheduledPipelines = _filterInstanceProperty__default["default"](_context8 = _filterInstanceProperty__default["default"](_context9 = pipelines.items).call(_context9, isNonScheduledPipeline)).call(_context8, isNonErroredPipeline);
173
- if (buildRevision) {
174
- console.log(_concatInstanceProperty__default["default"](_context10 = _concatInstanceProperty__default["default"](_context11 = "\uD83D\uDD04 Trying to find pipeline with revision ".concat(buildRevision, ". Attempt ")).call(_context11, i + 1, " out of ")).call(_context10, maxPages, "."));
175
- deploymentPipeline = _findInstanceProperty__default["default"](nonScheduledPipelines).call(nonScheduledPipelines, function (pipeline) {
176
- var _context12;
177
- return _startsWithInstanceProperty__default["default"](_context12 = pipeline.vcs.revision).call(_context12, buildRevision);
178
- });
179
- } else {
180
- deploymentPipeline = nonScheduledPipelines[0];
181
- }
182
- if (!deploymentPipeline) {
183
- _context13.next = 13;
184
- break;
185
- }
186
- console.log("\u2139\uFE0F Using pipeline for deployment with revision ".concat(deploymentPipeline.vcs.revision, "."));
187
- return _context13.abrupt("break", 16);
188
- case 13:
189
- i++;
190
- _context13.next = 2;
191
- break;
192
- case 16:
193
- return _context13.abrupt("return", deploymentPipeline);
194
- case 17:
195
- case "end":
196
- return _context13.stop();
197
- }
198
- }, _callee2);
199
- }));
200
- return _paginateToDeploymentPipeline.apply(this, arguments);
201
- }
202
- function collectDeploymentPipelines(_x2) {
203
- return _collectDeploymentPipelines.apply(this, arguments);
204
- }
205
- function _collectDeploymentPipelines() {
206
- _collectDeploymentPipelines = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3(_ref6) {
207
- var circleCiApis, branch, pagesForPipelineSelection, debug, deploymentPipelines, nextPageToken, i, _context14, _context15, pipelinesRequest, pipelines, nonScheduledPipelines;
208
- return _regeneratorRuntime__default["default"].wrap(function _callee3$(_context16) {
209
- while (1) switch (_context16.prev = _context16.next) {
210
- case 0:
211
- circleCiApis = _ref6.circleCiApis, branch = _ref6.branch, pagesForPipelineSelection = _ref6.pagesForPipelineSelection, debug = _ref6.debug;
212
- deploymentPipelines = [];
213
- i = 0;
214
- case 3:
215
- if (!(i < pagesForPipelineSelection)) {
216
- _context16.next = 13;
217
- break;
218
- }
219
- // eslint-disable-next-line no-await-in-loop
220
- pipelinesRequest = circleCiApis.pipelines({
221
- pageToken: nextPageToken,
222
- branch: branch
223
- });
224
- _context16.next = 7;
225
- return pipelinesRequest.execute({
226
- debug: debug
227
- });
228
- case 7:
229
- pipelines = _context16.sent;
230
- nonScheduledPipelines = _filterInstanceProperty__default["default"](_context14 = _filterInstanceProperty__default["default"](_context15 = pipelines.items).call(_context15, isNonScheduledPipeline)).call(_context14, isNonErroredPipeline);
231
- deploymentPipelines = _concatInstanceProperty__default["default"](deploymentPipelines).call(deploymentPipelines, nonScheduledPipelines);
232
- case 10:
233
- i++;
234
- _context16.next = 3;
235
- break;
236
- case 13:
237
- return _context16.abrupt("return", deploymentPipelines);
238
- case 14:
239
- case "end":
240
- return _context16.stop();
241
- }
242
- }, _callee3);
243
- }));
244
- return _collectDeploymentPipelines.apply(this, arguments);
122
+ async function paginateToDeploymentPipeline(_ref5) {
123
+ let circleCiApis = _ref5.circleCiApis,
124
+ buildRevision = _ref5.buildRevision,
125
+ branch = _ref5.branch,
126
+ debug = _ref5.debug,
127
+ maxPages = _ref5.maxPages;
128
+ let deploymentPipeline;
129
+ let nextPageToken;
130
+ // eslint-disable-next-line no-plusplus
131
+ for (let i = 0; i < maxPages; i++) {
132
+ var _context4, _context5;
133
+ // eslint-disable-next-line no-await-in-loop
134
+
135
+ const pipelineRequest = circleCiApis.pipelines({
136
+ pageToken: nextPageToken,
137
+ branch
138
+ });
139
+ const pipelines = await pipelineRequest.execute({
140
+ debug
141
+ });
142
+ nextPageToken = pipelines.next_page_token;
143
+ const nonScheduledPipelines = _filterInstanceProperty__default["default"](_context4 = _filterInstanceProperty__default["default"](_context5 = pipelines.items).call(_context5, isNonScheduledPipeline)).call(_context4, isNonErroredPipeline);
144
+ if (buildRevision) {
145
+ var _context6, _context7;
146
+ console.log(_concatInstanceProperty__default["default"](_context6 = _concatInstanceProperty__default["default"](_context7 = "\uD83D\uDD04 Trying to find pipeline with revision ".concat(buildRevision, ". Attempt ")).call(_context7, i + 1, " out of ")).call(_context6, maxPages, "."));
147
+ deploymentPipeline = _findInstanceProperty__default["default"](nonScheduledPipelines).call(nonScheduledPipelines, pipeline => {
148
+ var _context8;
149
+ return _startsWithInstanceProperty__default["default"](_context8 = pipeline.vcs.revision).call(_context8, buildRevision);
150
+ });
151
+ } else {
152
+ deploymentPipeline = nonScheduledPipelines[0];
153
+ }
154
+ if (deploymentPipeline) {
155
+ console.log("\u2139\uFE0F Using pipeline for deployment with revision ".concat(deploymentPipeline.vcs.revision, "."));
156
+ break;
157
+ }
158
+ }
159
+ return deploymentPipeline;
245
160
  }
246
- function waitForDeploymentPipelinePrompt(_x3) {
247
- return _waitForDeploymentPipelinePrompt.apply(this, arguments);
161
+ async function collectDeploymentPipelines(_ref6) {
162
+ let circleCiApis = _ref6.circleCiApis,
163
+ branch = _ref6.branch,
164
+ pagesForPipelineSelection = _ref6.pagesForPipelineSelection,
165
+ debug = _ref6.debug;
166
+ let deploymentPipelines = [];
167
+ let nextPageToken;
168
+ // eslint-disable-next-line no-plusplus
169
+ for (let i = 0; i < pagesForPipelineSelection; i++) {
170
+ var _context9, _context10;
171
+ // eslint-disable-next-line no-await-in-loop
172
+ const pipelinesRequest = circleCiApis.pipelines({
173
+ pageToken: nextPageToken,
174
+ branch
175
+ });
176
+ const pipelines = await pipelinesRequest.execute({
177
+ debug
178
+ });
179
+ const nonScheduledPipelines = _filterInstanceProperty__default["default"](_context9 = _filterInstanceProperty__default["default"](_context10 = pipelines.items).call(_context10, isNonScheduledPipeline)).call(_context9, isNonErroredPipeline);
180
+ deploymentPipelines = _concatInstanceProperty__default["default"](deploymentPipelines).call(deploymentPipelines, nonScheduledPipelines);
181
+ }
182
+ return deploymentPipelines;
248
183
  }
249
- function _waitForDeploymentPipelinePrompt() {
250
- _waitForDeploymentPipelinePrompt = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4(_ref7) {
251
- var circleCiApis, branch, pagesForPipelineSelection, debug, deploymentPipelines, deploymentPipelinePrompt, revision;
252
- return _regeneratorRuntime__default["default"].wrap(function _callee4$(_context17) {
253
- while (1) switch (_context17.prev = _context17.next) {
254
- case 0:
255
- circleCiApis = _ref7.circleCiApis, branch = _ref7.branch, pagesForPipelineSelection = _ref7.pagesForPipelineSelection, debug = _ref7.debug;
256
- _context17.next = 3;
257
- return collectDeploymentPipelines({
258
- branch: branch,
259
- pagesForPipelineSelection: pagesForPipelineSelection,
260
- circleCiApis: circleCiApis,
261
- debug: debug
262
- });
263
- case 3:
264
- deploymentPipelines = _context17.sent;
265
- _context17.next = 6;
266
- return prompts__default["default"](
267
- // @ts-expect-error prompts is not typed
268
- promptOptions.deploymentPipelineSelect({
269
- deploymentPipelines: deploymentPipelines
270
- }));
271
- case 6:
272
- deploymentPipelinePrompt = _context17.sent;
273
- revision = deploymentPipelinePrompt.deploymentPipeline.vcs.revision;
274
- if (revision) {
275
- _context17.next = 11;
276
- break;
277
- }
278
- console.log('☝️ Please select a revision or specify it as a CLI argument via `--build-revision`.');
279
- throw new Error('No revision specified.');
280
- case 11:
281
- return _context17.abrupt("return", deploymentPipelinePrompt.deploymentPipeline);
282
- case 12:
283
- case "end":
284
- return _context17.stop();
285
- }
286
- }, _callee4);
184
+ async function waitForDeploymentPipelinePrompt(_ref7) {
185
+ let circleCiApis = _ref7.circleCiApis,
186
+ branch = _ref7.branch,
187
+ pagesForPipelineSelection = _ref7.pagesForPipelineSelection,
188
+ debug = _ref7.debug;
189
+ const deploymentPipelines = await collectDeploymentPipelines({
190
+ branch,
191
+ pagesForPipelineSelection,
192
+ circleCiApis,
193
+ debug
194
+ });
195
+ const deploymentPipelinePrompt = await prompts__default["default"](
196
+ // @ts-expect-error prompts is not typed
197
+ promptOptions.deploymentPipelineSelect({
198
+ deploymentPipelines
287
199
  }));
288
- return _waitForDeploymentPipelinePrompt.apply(this, arguments);
289
- }
290
- function waitForConfirmationPrompt(_x4) {
291
- return _waitForConfirmationPrompt.apply(this, arguments);
200
+ const revision = deploymentPipelinePrompt.deploymentPipeline.vcs.revision;
201
+ if (!revision) {
202
+ console.log('☝️ Please select a revision or specify it as a CLI argument via `--build-revision`.');
203
+ throw new Error('No revision specified.');
204
+ }
205
+ return deploymentPipelinePrompt.deploymentPipeline;
292
206
  }
293
- function _waitForConfirmationPrompt() {
294
- _waitForConfirmationPrompt = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee5(_ref8) {
295
- var approvalJob, revision;
296
- return _regeneratorRuntime__default["default"].wrap(function _callee5$(_context18) {
297
- while (1) switch (_context18.prev = _context18.next) {
298
- case 0:
299
- approvalJob = _ref8.approvalJob, revision = _ref8.revision;
300
- return _context18.abrupt("return", prompts__default["default"](
301
- // @ts-expect-error prompts is not typed
302
- promptOptions.deploymentConfirmation({
303
- approvalJob: approvalJob,
304
- revision: revision
305
- })));
306
- case 2:
307
- case "end":
308
- return _context18.stop();
309
- }
310
- }, _callee5);
207
+ async function waitForConfirmationPrompt(_ref8) {
208
+ let approvalJob = _ref8.approvalJob,
209
+ revision = _ref8.revision;
210
+ return prompts__default["default"](
211
+ // @ts-expect-error prompts is not typed
212
+ promptOptions.deploymentConfirmation({
213
+ approvalJob,
214
+ revision
311
215
  }));
312
- return _waitForConfirmationPrompt.apply(this, arguments);
313
216
  }
314
217
  function getJobUrl(_ref9) {
315
- var _context4, _context5, _context6;
316
- var pipelineNumber = _ref9.pipelineNumber,
218
+ var _context11, _context12, _context13;
219
+ let pipelineNumber = _ref9.pipelineNumber,
317
220
  workflowId = _ref9.workflowId,
318
221
  jobNumber = _ref9.jobNumber,
319
222
  projectName = _ref9.projectName;
320
- return _concatInstanceProperty__default["default"](_context4 = _concatInstanceProperty__default["default"](_context5 = _concatInstanceProperty__default["default"](_context6 = "https://app.circleci.com/pipelines/github/commercetools/".concat(projectName, "/")).call(_context6, pipelineNumber, "/workflows/")).call(_context5, workflowId, "/jobs/")).call(_context4, jobNumber);
223
+ return _concatInstanceProperty__default["default"](_context11 = _concatInstanceProperty__default["default"](_context12 = _concatInstanceProperty__default["default"](_context13 = "https://app.circleci.com/pipelines/github/commercetools/".concat(projectName, "/")).call(_context13, pipelineNumber, "/workflows/")).call(_context12, workflowId, "/jobs/")).call(_context11, jobNumber);
321
224
  }
322
225
  function isNonScheduledPipeline(pipeline) {
323
226
  return pipeline.trigger.type !== 'schedule' && pipeline.trigger.type !== 'scheduled_pipeline';
@@ -325,458 +228,274 @@ function isNonScheduledPipeline(pipeline) {
325
228
  function isNonErroredPipeline(pipeline) {
326
229
  return pipeline.state !== 'errored';
327
230
  }
328
- function waitForDeploymentJobNumber(_x5, _x6) {
329
- return _waitForDeploymentJobNumber.apply(this, arguments);
330
- }
331
- function _waitForDeploymentJobNumber() {
332
- _waitForDeploymentJobNumber = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee7(_ref10, _ref11) {
333
- var workflowId, deploymentJob, circleCiApis, debug, fetchDeploymentJobNumber, deploymentJobNumber;
334
- return _regeneratorRuntime__default["default"].wrap(function _callee7$(_context22) {
335
- while (1) switch (_context22.prev = _context22.next) {
336
- case 0:
337
- workflowId = _ref10.workflowId, deploymentJob = _ref10.deploymentJob, circleCiApis = _ref10.circleCiApis;
338
- debug = _ref11.debug;
339
- fetchDeploymentJobNumber = /*#__PURE__*/function () {
340
- var _ref12 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee6() {
341
- var _context19;
342
- var jobsRequest, jobs, applicationDeploymentJob;
343
- return _regeneratorRuntime__default["default"].wrap(function _callee6$(_context20) {
344
- while (1) switch (_context20.prev = _context20.next) {
345
- case 0:
346
- jobsRequest = circleCiApis.jobs({
347
- workflowId: workflowId
348
- });
349
- _context20.next = 3;
350
- return jobsRequest.execute({
351
- debug: debug
352
- });
353
- case 3:
354
- jobs = _context20.sent;
355
- applicationDeploymentJob = _findInstanceProperty__default["default"](_context19 = jobs.items).call(_context19, function (job) {
356
- return job.name === deploymentJob;
357
- });
358
- if (!(!applicationDeploymentJob || applicationDeploymentJob.status === 'blocked')) {
359
- _context20.next = 7;
360
- break;
361
- }
362
- throw new Error('Deployment job not yet running. Retrying.');
363
- case 7:
364
- return _context20.abrupt("return", applicationDeploymentJob.job_number);
365
- case 8:
366
- case "end":
367
- return _context20.stop();
368
- }
369
- }, _callee6);
370
- }));
371
- return function fetchDeploymentJobNumber() {
372
- return _ref12.apply(this, arguments);
373
- };
374
- }();
375
- _context22.next = 5;
376
- return pRetry__default["default"](fetchDeploymentJobNumber, {
377
- onFailedAttempt: function onFailedAttempt(error) {
378
- var _context21;
379
- console.log(_concatInstanceProperty__default["default"](_context21 = "\uD83D\uDD04 Trying to find deployment job. Attempt ".concat(error.attemptNumber, " with ")).call(_context21, error.retriesLeft, " retries left."));
380
- },
381
- retries: 10
382
- });
383
- case 5:
384
- deploymentJobNumber = _context22.sent;
385
- return _context22.abrupt("return", deploymentJobNumber);
386
- case 7:
387
- case "end":
388
- return _context22.stop();
389
- }
390
- }, _callee7);
391
- }));
392
- return _waitForDeploymentJobNumber.apply(this, arguments);
231
+ async function waitForDeploymentJobNumber(_ref10, _ref11) {
232
+ let workflowId = _ref10.workflowId,
233
+ deploymentJob = _ref10.deploymentJob,
234
+ circleCiApis = _ref10.circleCiApis;
235
+ let debug = _ref11.debug;
236
+ const fetchDeploymentJobNumber = async () => {
237
+ var _context14;
238
+ const jobsRequest = circleCiApis.jobs({
239
+ workflowId
240
+ });
241
+ const jobs = await jobsRequest.execute({
242
+ debug
243
+ });
244
+ const applicationDeploymentJob = _findInstanceProperty__default["default"](_context14 = jobs.items).call(_context14, job => job.name === deploymentJob);
245
+ if (!applicationDeploymentJob || applicationDeploymentJob.status === 'blocked') {
246
+ throw new Error('Deployment job not yet running. Retrying.');
247
+ }
248
+ return applicationDeploymentJob.job_number;
249
+ };
250
+ const deploymentJobNumber = await pRetry__default["default"](fetchDeploymentJobNumber, {
251
+ onFailedAttempt: error => {
252
+ var _context15;
253
+ console.log(_concatInstanceProperty__default["default"](_context15 = "\uD83D\uDD04 Trying to find deployment job. Attempt ".concat(error.attemptNumber, " with ")).call(_context15, error.retriesLeft, " retries left."));
254
+ },
255
+ retries: 10
256
+ });
257
+ return deploymentJobNumber;
393
258
  }
394
259
 
395
- function approve(_x, _x2, _x3) {
396
- return _approve.apply(this, arguments);
397
- }
398
- function _approve() {
399
- _approve = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(cliFlags, config, circleCiApis) {
400
- var _config$CircleCI$pagi, _config$CircleCI$pagi2, _config$CircleCI$pagi3, _config$CircleCI$pagi4, _context2, _context3, _context5, _confirmationPrompt;
401
- var approvalJob, deploymentJob, _config$deployments, _context, requestedDeployment, deploymentPipeline, workflowsRequest, workflows, buildAndDeployWorkflow, workflowId, jobsRequest, jobs, applicationApprovalJob, applicationApprovalJobId, _context4, confirmationPrompt, approvalRequest, _context6, _context7, deploymentJobNumber;
402
- return _regeneratorRuntime__default["default"].wrap(function _callee$(_context8) {
403
- while (1) switch (_context8.prev = _context8.next) {
404
- case 0:
405
- if (!cliFlags.deployment) {
406
- _context8.next = 9;
407
- break;
408
- }
409
- requestedDeployment = (_config$deployments = config.deployments) === null || _config$deployments === void 0 ? void 0 : _config$deployments[cliFlags.deployment];
410
- if (requestedDeployment) {
411
- _context8.next = 4;
412
- break;
413
- }
414
- throw new Error("\u26A0\uFE0F Deployment ".concat(cliFlags.deployment, " not found in configuration. Make sure it exists."));
415
- case 4:
416
- approvalJob = requestedDeployment.jobs.approval;
417
- deploymentJob = requestedDeployment.jobs.deployment;
418
- console.log(_concatInstanceProperty__default["default"](_context = "\u2139\uFE0F Approving requested deployment ".concat(cliFlags.deployment, " with approval job ")).call(_context, approvalJob, "."));
419
- _context8.next = 12;
420
- break;
421
- case 9:
422
- approvalJob = cliFlags.approvalJob;
423
- deploymentJob = cliFlags.deploymentJob;
424
- console.log("\u2139\uFE0F Approving with approval job ".concat(approvalJob, "."));
425
- case 12:
426
- if (!cliFlags.yes) {
427
- _context8.next = 18;
428
- break;
429
- }
430
- _context8.next = 15;
431
- return paginateToDeploymentPipeline({
432
- circleCiApis: circleCiApis,
433
- buildRevision: cliFlags.buildRevision,
434
- branch: cliFlags.branch,
435
- debug: cliFlags.debug,
436
- maxPages: (_config$CircleCI$pagi = (_config$CircleCI$pagi2 = config.CircleCI.pagination) === null || _config$CircleCI$pagi2 === void 0 ? void 0 : _config$CircleCI$pagi2.maxPages) !== null && _config$CircleCI$pagi !== void 0 ? _config$CircleCI$pagi : 1
437
- });
438
- case 15:
439
- _context8.t0 = _context8.sent;
440
- _context8.next = 21;
441
- break;
442
- case 18:
443
- _context8.next = 20;
444
- return waitForDeploymentPipelinePrompt({
445
- branch: cliFlags.branch,
446
- circleCiApis: circleCiApis,
447
- pagesForPipelineSelection: (_config$CircleCI$pagi3 = (_config$CircleCI$pagi4 = config.CircleCI.pagination) === null || _config$CircleCI$pagi4 === void 0 ? void 0 : _config$CircleCI$pagi4.pagesForPipelineSelection) !== null && _config$CircleCI$pagi3 !== void 0 ? _config$CircleCI$pagi3 : 1,
448
- debug: cliFlags.debug
449
- });
450
- case 20:
451
- _context8.t0 = _context8.sent;
452
- case 21:
453
- deploymentPipeline = _context8.t0;
454
- if (deploymentPipeline) {
455
- _context8.next = 24;
456
- break;
457
- }
458
- throw new Error("\u26A0\uFE0F No workflow called ".concat(config.CircleCI.deploymentWorkflowName, " found in any pipeline for deployment."));
459
- case 24:
460
- console.log("\u2139\uFE0F Found pipeline for deployment with revision ".concat(deploymentPipeline.vcs.revision, "."));
461
- workflowsRequest = circleCiApis.workflows({
462
- pipelineId: deploymentPipeline.id
463
- });
464
- _context8.next = 28;
465
- return workflowsRequest.execute({
466
- debug: cliFlags.debug
467
- });
468
- case 28:
469
- workflows = _context8.sent;
470
- buildAndDeployWorkflow = _findInstanceProperty__default["default"](_context2 = workflows.items).call(_context2, function (workflow) {
471
- return workflow.name === config.CircleCI.deploymentWorkflowName;
472
- });
473
- if (buildAndDeployWorkflow) {
474
- _context8.next = 32;
475
- break;
476
- }
477
- throw new Error("\u26A0\uFE0F No workflow called ".concat(config.CircleCI.deploymentWorkflowName, " found for deployment."));
478
- case 32:
479
- workflowId = buildAndDeployWorkflow.id;
480
- console.log("\u2139\uFE0F Found workflow to build and deploy with id ".concat(workflowId, "."));
481
- jobsRequest = circleCiApis.jobs({
482
- workflowId: workflowId
483
- });
484
- _context8.next = 37;
485
- return jobsRequest.execute({
486
- debug: cliFlags.debug
487
- });
488
- case 37:
489
- jobs = _context8.sent;
490
- console.log("\u2139\uFE0F Found jobs for workflow.");
491
- applicationApprovalJob = _findInstanceProperty__default["default"](_context3 = jobs.items).call(_context3, function (job) {
492
- return job.name === approvalJob;
493
- });
494
- applicationApprovalJobId = applicationApprovalJob === null || applicationApprovalJob === void 0 ? void 0 : applicationApprovalJob.id;
495
- if (applicationApprovalJobId) {
496
- _context8.next = 43;
497
- break;
498
- }
499
- throw new Error(_concatInstanceProperty__default["default"](_context4 = "\u270C\uFE0F Could not find deployment approval job named ".concat(approvalJob, " at revision ")).call(_context4, deploymentPipeline.vcs.revision, ". Maybe try again later."));
500
- case 43:
501
- console.log(_concatInstanceProperty__default["default"](_context5 = "\u2139\uFE0F Found deployment approval job named ".concat(approvalJob, " with id ")).call(_context5, applicationApprovalJobId, "."));
502
- if (cliFlags.yes) {
503
- _context8.next = 48;
504
- break;
505
- }
506
- _context8.next = 47;
507
- return waitForConfirmationPrompt({
508
- approvalJob: approvalJob,
509
- revision: deploymentPipeline.vcs.revision
510
- });
511
- case 47:
512
- confirmationPrompt = _context8.sent;
513
- case 48:
514
- if (!(cliFlags.yes || (_confirmationPrompt = confirmationPrompt) !== null && _confirmationPrompt !== void 0 && _confirmationPrompt.confirmed)) {
515
- _context8.next = 71;
516
- break;
517
- }
518
- if (cliFlags.dryRun) {
519
- console.log("\uD83D\uDE4A Not approving deployment job due to dry run.");
520
- } else {
521
- console.log("\u2139\uFE0F Approving deployment job.");
522
- }
523
- approvalRequest = circleCiApis.approve({
524
- workflowId: workflowId,
525
- approvalRequestId: applicationApprovalJobId
526
- });
527
- _context8.next = 53;
528
- return approvalRequest.execute({
529
- debug: cliFlags.debug,
530
- skip: cliFlags.dryRun
531
- });
532
- case 53:
533
- if (!cliFlags.yes) {
534
- _context8.next = 57;
535
- break;
536
- }
537
- console.log("\u2139\uFE0F Skipping determining deployment job approved by ".concat(approvalJob, " due to '--yes' flag. Please check CircleCI manually."));
538
- _context8.next = 69;
539
- break;
540
- case 57:
541
- if (!cliFlags.dryRun) {
542
- _context8.next = 61;
543
- break;
544
- }
545
- console.log("\u270C\uFE0F Dry running hence could not determine deployment job approved by ".concat(approvalJob, "."));
546
- _context8.next = 69;
547
- break;
548
- case 61:
549
- if (!deploymentJob) {
550
- _context8.next = 68;
551
- break;
552
- }
553
- _context8.next = 64;
554
- return waitForDeploymentJobNumber({
555
- workflowId: workflowId,
556
- deploymentJob: deploymentJob,
557
- circleCiApis: circleCiApis
558
- }, {
559
- debug: cliFlags.debug,
560
- dryRun: cliFlags.dryRun,
561
- yes: cliFlags.yes
562
- });
563
- case 64:
564
- deploymentJobNumber = _context8.sent;
565
- console.log(_concatInstanceProperty__default["default"](_context6 = _concatInstanceProperty__default["default"](_context7 = "\uD83D\uDE4C The deployment via ".concat(approvalJob, " at revision ")).call(_context7, deploymentPipeline.vcs.revision, " is running at: ")).call(_context6, getJobUrl({
566
- projectName: config.CircleCI.projectName,
567
- pipelineNumber: deploymentPipeline.number,
568
- workflowId: workflowId,
569
- jobNumber: deploymentJobNumber
570
- })));
571
- _context8.next = 69;
572
- break;
573
- case 68:
574
- console.log("\u2139\uFE0F Skipping determining deployment job as no '--deployment-job' name to wait for was passed. Please check CircleCI manually.");
575
- case 69:
576
- _context8.next = 72;
577
- break;
578
- case 71:
579
- console.log("\u2139\uFE0F Not approving deployment job. Confirm the prompt or use '--yes' option.");
580
- case 72:
581
- case "end":
582
- return _context8.stop();
583
- }
584
- }, _callee);
585
- }));
586
- return _approve.apply(this, arguments);
260
+ async function approve(cliFlags, config, circleCiApis) {
261
+ var _config$CircleCI$pagi, _config$CircleCI$pagi2, _config$CircleCI$pagi3, _config$CircleCI$pagi4, _context2, _context3, _context5, _confirmationPrompt;
262
+ let approvalJob;
263
+ let deploymentJob;
264
+ if (cliFlags.deployment) {
265
+ var _config$deployments, _context;
266
+ const requestedDeployment = (_config$deployments = config.deployments) === null || _config$deployments === void 0 ? void 0 : _config$deployments[cliFlags.deployment];
267
+ if (!requestedDeployment) {
268
+ throw new Error("\u26A0\uFE0F Deployment ".concat(cliFlags.deployment, " not found in configuration. Make sure it exists."));
269
+ }
270
+ approvalJob = requestedDeployment.jobs.approval;
271
+ deploymentJob = requestedDeployment.jobs.deployment;
272
+ console.log(_concatInstanceProperty__default["default"](_context = "\u2139\uFE0F Approving requested deployment ".concat(cliFlags.deployment, " with approval job ")).call(_context, approvalJob, "."));
273
+ } else {
274
+ approvalJob = cliFlags.approvalJob;
275
+ deploymentJob = cliFlags.deploymentJob;
276
+ console.log("\u2139\uFE0F Approving with approval job ".concat(approvalJob, "."));
277
+ }
278
+ const deploymentPipeline = cliFlags.yes ? await paginateToDeploymentPipeline({
279
+ circleCiApis,
280
+ buildRevision: cliFlags.buildRevision,
281
+ branch: cliFlags.branch,
282
+ debug: cliFlags.debug,
283
+ maxPages: (_config$CircleCI$pagi = (_config$CircleCI$pagi2 = config.CircleCI.pagination) === null || _config$CircleCI$pagi2 === void 0 ? void 0 : _config$CircleCI$pagi2.maxPages) !== null && _config$CircleCI$pagi !== void 0 ? _config$CircleCI$pagi : 1
284
+ }) : await waitForDeploymentPipelinePrompt({
285
+ branch: cliFlags.branch,
286
+ circleCiApis,
287
+ pagesForPipelineSelection: (_config$CircleCI$pagi3 = (_config$CircleCI$pagi4 = config.CircleCI.pagination) === null || _config$CircleCI$pagi4 === void 0 ? void 0 : _config$CircleCI$pagi4.pagesForPipelineSelection) !== null && _config$CircleCI$pagi3 !== void 0 ? _config$CircleCI$pagi3 : 1,
288
+ debug: cliFlags.debug
289
+ });
290
+ if (!deploymentPipeline) {
291
+ throw new Error("\u26A0\uFE0F No workflow called ".concat(config.CircleCI.deploymentWorkflowName, " found in any pipeline for deployment."));
292
+ }
293
+ console.log("\u2139\uFE0F Found pipeline for deployment with revision ".concat(deploymentPipeline.vcs.revision, "."));
294
+ const workflowsRequest = circleCiApis.workflows({
295
+ pipelineId: deploymentPipeline.id
296
+ });
297
+ const workflows = await workflowsRequest.execute({
298
+ debug: cliFlags.debug
299
+ });
300
+ const buildAndDeployWorkflow = _findInstanceProperty__default["default"](_context2 = workflows.items).call(_context2, workflow => workflow.name === config.CircleCI.deploymentWorkflowName);
301
+ if (!buildAndDeployWorkflow) {
302
+ throw new Error("\u26A0\uFE0F No workflow called ".concat(config.CircleCI.deploymentWorkflowName, " found for deployment."));
303
+ }
304
+ const workflowId = buildAndDeployWorkflow.id;
305
+ console.log("\u2139\uFE0F Found workflow to build and deploy with id ".concat(workflowId, "."));
306
+ const jobsRequest = circleCiApis.jobs({
307
+ workflowId
308
+ });
309
+ const jobs = await jobsRequest.execute({
310
+ debug: cliFlags.debug
311
+ });
312
+ console.log("\u2139\uFE0F Found jobs for workflow.");
313
+ const applicationApprovalJob = _findInstanceProperty__default["default"](_context3 = jobs.items).call(_context3, job => job.name === approvalJob);
314
+ const applicationApprovalJobId = applicationApprovalJob === null || applicationApprovalJob === void 0 ? void 0 : applicationApprovalJob.id;
315
+ if (!applicationApprovalJobId) {
316
+ var _context4;
317
+ throw new Error(_concatInstanceProperty__default["default"](_context4 = "\u270C\uFE0F Could not find deployment approval job named ".concat(approvalJob, " at revision ")).call(_context4, deploymentPipeline.vcs.revision, ". Maybe try again later."));
318
+ }
319
+ console.log(_concatInstanceProperty__default["default"](_context5 = "\u2139\uFE0F Found deployment approval job named ".concat(approvalJob, " with id ")).call(_context5, applicationApprovalJobId, "."));
320
+ let confirmationPrompt;
321
+ if (!cliFlags.yes) {
322
+ confirmationPrompt = await waitForConfirmationPrompt({
323
+ approvalJob: approvalJob,
324
+ revision: deploymentPipeline.vcs.revision
325
+ });
326
+ }
327
+ if (cliFlags.yes || (_confirmationPrompt = confirmationPrompt) !== null && _confirmationPrompt !== void 0 && _confirmationPrompt.confirmed) {
328
+ if (cliFlags.dryRun) {
329
+ console.log("\uD83D\uDE4A Not approving deployment job due to dry run.");
330
+ } else {
331
+ console.log("\u2139\uFE0F Approving deployment job.");
332
+ }
333
+ const approvalRequest = circleCiApis.approve({
334
+ workflowId,
335
+ approvalRequestId: applicationApprovalJobId
336
+ });
337
+ await approvalRequest.execute({
338
+ debug: cliFlags.debug,
339
+ skip: cliFlags.dryRun
340
+ });
341
+ if (cliFlags.yes) {
342
+ console.log("\u2139\uFE0F Skipping determining deployment job approved by ".concat(approvalJob, " due to '--yes' flag. Please check CircleCI manually."));
343
+ } else if (cliFlags.dryRun) {
344
+ console.log("\u270C\uFE0F Dry running hence could not determine deployment job approved by ".concat(approvalJob, "."));
345
+ } else if (deploymentJob) {
346
+ var _context6, _context7;
347
+ const deploymentJobNumber = await waitForDeploymentJobNumber({
348
+ workflowId,
349
+ deploymentJob,
350
+ circleCiApis
351
+ }, {
352
+ debug: cliFlags.debug,
353
+ dryRun: cliFlags.dryRun,
354
+ yes: cliFlags.yes
355
+ });
356
+ console.log(_concatInstanceProperty__default["default"](_context6 = _concatInstanceProperty__default["default"](_context7 = "\uD83D\uDE4C The deployment via ".concat(approvalJob, " at revision ")).call(_context7, deploymentPipeline.vcs.revision, " is running at: ")).call(_context6, getJobUrl({
357
+ projectName: config.CircleCI.projectName,
358
+ pipelineNumber: deploymentPipeline.number,
359
+ workflowId,
360
+ jobNumber: deploymentJobNumber
361
+ })));
362
+ } else {
363
+ console.log("\u2139\uFE0F Skipping determining deployment job as no '--deployment-job' name to wait for was passed. Please check CircleCI manually.");
364
+ }
365
+ } else {
366
+ console.log("\u2139\uFE0F Not approving deployment job. Confirm the prompt or use '--yes' option.");
367
+ }
587
368
  }
588
369
 
589
- function processCircleCiResponse(_x) {
590
- return _processCircleCiResponse.apply(this, arguments);
591
- }
592
- function _processCircleCiResponse() {
593
- _processCircleCiResponse = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2(response) {
594
- var _context4, _context5, errorJson, errorText;
595
- return _regeneratorRuntime__default["default"].wrap(function _callee2$(_context6) {
596
- while (1) switch (_context6.prev = _context6.next) {
597
- case 0:
598
- if (response.ok) {
599
- _context6.next = 14;
600
- break;
601
- }
602
- _context6.prev = 1;
603
- _context6.next = 4;
604
- return response.json();
605
- case 4:
606
- errorJson = _context6.sent;
607
- if (errorJson.message.match(/job already approved/i)) {
608
- console.log('ℹ️ Deployment job is already approved.');
609
- process.exit(0);
610
- }
611
- _context6.next = 10;
612
- break;
613
- case 8:
614
- _context6.prev = 8;
615
- _context6.t0 = _context6["catch"](1);
616
- case 10:
617
- _context6.next = 12;
618
- return response.text();
619
- case 12:
620
- errorText = _context6.sent;
621
- throw new Error(_concatInstanceProperty__default["default"](_context4 = _concatInstanceProperty__default["default"](_context5 = "".concat(response.status, ": Network response was not ok.\n\n Status text is ")).call(_context5, response.statusText, " and text is ")).call(_context4, errorText, "."));
622
- case 14:
623
- return _context6.abrupt("return", response.json());
624
- case 15:
625
- case "end":
626
- return _context6.stop();
370
+ async function processCircleCiResponse(response) {
371
+ if (!response.ok) {
372
+ var _context, _context2;
373
+ /**
374
+ * NOTE:
375
+ * Trying to handle known but undocumented responses of the CircleCI API.
376
+ *
377
+ * 1. Message: Already approved job
378
+ * Deployment was already triggered manually or by train the day before.
379
+ */
380
+ try {
381
+ const errorJson = await response.json();
382
+ if (errorJson.message.match(/job already approved/i)) {
383
+ console.log('ℹ️ Deployment job is already approved.');
384
+ process.exit(0);
627
385
  }
628
- }, _callee2, null, [[1, 8]]);
629
- }));
630
- return _processCircleCiResponse.apply(this, arguments);
386
+ } catch (e) {
387
+ // ignore
388
+ }
389
+ const errorText = await response.text();
390
+ throw new Error(_concatInstanceProperty__default["default"](_context = _concatInstanceProperty__default["default"](_context2 = "".concat(response.status, ": Network response was not ok.\n\n Status text is ")).call(_context2, response.statusText, " and text is ")).call(_context, errorText, "."));
391
+ }
392
+ return response.json();
631
393
  }
632
394
  function createCircleCiClient(_ref) {
633
- var projectName = _ref.projectName,
395
+ let projectName = _ref.projectName,
634
396
  apiBaseUrl = _ref.apiBaseUrl;
635
- function _execute2(_x2) {
636
- return _execute.apply(this, arguments);
637
- }
638
- function _execute() {
639
- _execute = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(api) {
640
- var _context2;
641
- var _ref6,
642
- skip,
643
- debug,
644
- url,
645
- urlSearchParams,
646
- _i,
647
- _Object$entries,
648
- _ref7,
649
- _ref8,
650
- key,
651
- value,
652
- response,
653
- processedCircleCiResponse,
654
- _args = arguments;
655
- return _regeneratorRuntime__default["default"].wrap(function _callee$(_context3) {
656
- while (1) switch (_context3.prev = _context3.next) {
657
- case 0:
658
- _ref6 = _args.length > 1 && _args[1] !== undefined ? _args[1] : {}, skip = _ref6.skip, debug = _ref6.debug;
659
- url = _concatInstanceProperty__default["default"](_context2 = "".concat(apiBaseUrl)).call(_context2, api.url);
660
- if (api.params && api.method === 'GET') {
661
- urlSearchParams = new _URLSearchParams__default["default"]();
662
- for (_i = 0, _Object$entries = _Object$entries2__default["default"](api.params); _i < _Object$entries.length; _i++) {
663
- _ref7 = _Object$entries[_i];
664
- _ref8 = _slicedToArray(_ref7, 2);
665
- key = _ref8[0];
666
- value = _ref8[1];
667
- if (value !== null && value !== undefined) {
668
- urlSearchParams.append(key, value);
669
- }
670
- }
671
- url += "?".concat(urlSearchParams.toString());
672
- }
673
- if (!skip) {
674
- _context3.next = 6;
675
- break;
676
- }
677
- if (debug) {
678
- console.log("\uD83C\uDFED Skipping CircleCI call API at: ".concat(url, "."));
679
- }
680
-
681
- // @ts-expect-error
682
- return _context3.abrupt("return", _Promise__default["default"].resolve());
683
- case 6:
684
- if (debug) {
685
- console.log("\uD83C\uDFED Calling CircleCI API at: ".concat(url, "."));
686
- }
687
- _context3.prev = 7;
688
- _context3.next = 10;
689
- return fetch__default["default"](url, {
690
- headers: api.headers,
691
- method: api.method,
692
- body: api.method === 'POST' ? _JSON$stringify__default["default"](api.params) : undefined
693
- });
694
- case 10:
695
- response = _context3.sent;
696
- _context3.next = 13;
697
- return processCircleCiResponse(response);
698
- case 13:
699
- processedCircleCiResponse = _context3.sent;
700
- return _context3.abrupt("return", processedCircleCiResponse);
701
- case 17:
702
- _context3.prev = 17;
703
- _context3.t0 = _context3["catch"](7);
704
- console.log("\u26A0\uFE0F Calling CircleCI API at: ".concat(url, " failed."));
705
- throw _context3.t0;
706
- case 21:
707
- case "end":
708
- return _context3.stop();
397
+ async function execute(api) {
398
+ var _context3;
399
+ let _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
400
+ skip = _ref2.skip,
401
+ debug = _ref2.debug;
402
+ let url = _concatInstanceProperty__default["default"](_context3 = "".concat(apiBaseUrl)).call(_context3, api.url);
403
+ if (api.params && api.method === 'GET') {
404
+ const urlSearchParams = new _URLSearchParams__default["default"]();
405
+ for (const _ref3 of _Object$entries__default["default"](api.params)) {
406
+ var _ref4 = _slicedToArray(_ref3, 2);
407
+ const key = _ref4[0];
408
+ const value = _ref4[1];
409
+ if (value !== null && value !== undefined) {
410
+ urlSearchParams.append(key, value);
709
411
  }
710
- }, _callee, null, [[7, 17]]);
711
- }));
712
- return _execute.apply(this, arguments);
412
+ }
413
+ url += "?".concat(urlSearchParams.toString());
414
+ }
415
+ if (skip) {
416
+ if (debug) {
417
+ console.log("\uD83C\uDFED Skipping CircleCI call API at: ".concat(url, "."));
418
+ }
419
+
420
+ // @ts-expect-error
421
+ return _Promise__default["default"].resolve();
422
+ }
423
+ if (debug) {
424
+ console.log("\uD83C\uDFED Calling CircleCI API at: ".concat(url, "."));
425
+ }
426
+ try {
427
+ const response = await fetch__default["default"](url, {
428
+ headers: api.headers,
429
+ method: api.method,
430
+ body: api.method === 'POST' ? _JSON$stringify__default["default"](api.params) : undefined
431
+ });
432
+ const processedCircleCiResponse = await processCircleCiResponse(response);
433
+ return processedCircleCiResponse;
434
+ } catch (error) {
435
+ console.log("\u26A0\uFE0F Calling CircleCI API at: ".concat(url, " failed."));
436
+ throw error;
437
+ }
713
438
  }
714
439
  return {
715
- pipelines: function pipelines() {
716
- var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
717
- pageToken = _ref2.pageToken,
718
- _ref2$projectSlug = _ref2.projectSlug,
719
- projectSlug = _ref2$projectSlug === void 0 ? "gh/commercetools/".concat(projectName) : _ref2$projectSlug,
720
- _ref2$branch = _ref2.branch,
721
- branch = _ref2$branch === void 0 ? 'main' : _ref2$branch;
440
+ pipelines: function () {
441
+ let _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
442
+ pageToken = _ref5.pageToken,
443
+ _ref5$projectSlug = _ref5.projectSlug,
444
+ projectSlug = _ref5$projectSlug === void 0 ? "gh/commercetools/".concat(projectName) : _ref5$projectSlug,
445
+ _ref5$branch = _ref5.branch,
446
+ branch = _ref5$branch === void 0 ? 'main' : _ref5$branch;
722
447
  return {
723
- execute: function execute(options) {
724
- return _execute2({
725
- url: "/project/".concat(projectSlug, "/pipeline"),
726
- headers: {
727
- 'Content-Type': 'application/json',
728
- // The CLI throws if this is not present on environment
729
- 'Circle-Token': process.env.CIRCLE_TOKEN
730
- },
731
- method: 'GET',
732
- params: {
733
- branch: branch,
734
- 'page-token': pageToken
735
- }
736
- }, options);
737
- }
448
+ execute: options => execute({
449
+ url: "/project/".concat(projectSlug, "/pipeline"),
450
+ headers: {
451
+ 'Content-Type': 'application/json',
452
+ // The CLI throws if this is not present on environment
453
+ 'Circle-Token': process.env.CIRCLE_TOKEN
454
+ },
455
+ method: 'GET',
456
+ params: {
457
+ branch,
458
+ 'page-token': pageToken
459
+ }
460
+ }, options)
738
461
  };
739
462
  },
740
- workflows: function workflows(_ref3) {
741
- var pipelineId = _ref3.pipelineId;
463
+ workflows: _ref6 => {
464
+ let pipelineId = _ref6.pipelineId;
742
465
  return {
743
- execute: function execute(options) {
744
- return _execute2({
745
- url: "/pipeline/".concat(pipelineId, "/workflow"),
746
- headers: {
747
- 'Content-Type': 'application/json',
748
- // The CLI throws if this is not present on environment
749
- 'Circle-Token': process.env.CIRCLE_TOKEN
750
- },
751
- method: 'GET'
752
- }, options);
753
- }
466
+ execute: options => execute({
467
+ url: "/pipeline/".concat(pipelineId, "/workflow"),
468
+ headers: {
469
+ 'Content-Type': 'application/json',
470
+ // The CLI throws if this is not present on environment
471
+ 'Circle-Token': process.env.CIRCLE_TOKEN
472
+ },
473
+ method: 'GET'
474
+ }, options)
754
475
  };
755
476
  },
756
- jobs: function jobs(_ref4) {
757
- var workflowId = _ref4.workflowId;
477
+ jobs: _ref7 => {
478
+ let workflowId = _ref7.workflowId;
758
479
  return {
759
- execute: function execute(options) {
760
- return _execute2({
761
- url: "/workflow/".concat(workflowId, "/job"),
762
- headers: {
763
- 'Content-Type': 'application/json',
764
- // The CLI throws if this is not present on environment
765
- 'Circle-Token': process.env.CIRCLE_TOKEN
766
- },
767
- method: 'GET'
768
- }, options);
769
- }
480
+ execute: options => execute({
481
+ url: "/workflow/".concat(workflowId, "/job"),
482
+ headers: {
483
+ 'Content-Type': 'application/json',
484
+ // The CLI throws if this is not present on environment
485
+ 'Circle-Token': process.env.CIRCLE_TOKEN
486
+ },
487
+ method: 'GET'
488
+ }, options)
770
489
  };
771
490
  },
772
- approve: function approve(_ref5) {
773
- var workflowId = _ref5.workflowId,
774
- approvalRequestId = _ref5.approvalRequestId;
491
+ approve: _ref8 => {
492
+ let workflowId = _ref8.workflowId,
493
+ approvalRequestId = _ref8.approvalRequestId;
775
494
  return {
776
- execute: function execute(options) {
777
- var _context;
778
- return _execute2({
779
- url: _concatInstanceProperty__default["default"](_context = "/workflow/".concat(workflowId, "/approve/")).call(_context, approvalRequestId, " "),
495
+ execute: options => {
496
+ var _context4;
497
+ return execute({
498
+ url: _concatInstanceProperty__default["default"](_context4 = "/workflow/".concat(workflowId, "/approve/")).call(_context4, approvalRequestId, " "),
780
499
  headers: {
781
500
  'Content-Type': 'application/json',
782
501
  // The CLI throws if this is not present on environment
@@ -790,53 +509,41 @@ function createCircleCiClient(_ref) {
790
509
  };
791
510
  }
792
511
 
793
- function run() {
794
- return _run.apply(this, arguments);
795
- }
796
- function _run() {
797
- _run = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
798
- var config, circleCiApis, _context, cliFlags, cliCommands, cliCommand;
799
- return _regeneratorRuntime__default["default"].wrap(function _callee$(_context2) {
800
- while (1) switch (_context2.prev = _context2.next) {
801
- case 0:
802
- _context2.next = 2;
803
- return loadConfig();
804
- case 2:
805
- config = _context2.sent;
806
- throwIfConfigurationLacksRequiredValues(config);
807
- circleCiApis = createCircleCiClient({
808
- projectName: config.CircleCI.projectName,
809
- apiBaseUrl: config.CircleCI.apiBaseUrl
810
- });
811
- _context2.prev = 5;
812
- cliFlags = mri__default["default"](_sliceInstanceProperty__default["default"](_context = process.argv).call(_context, 2), {
813
- alias: {
814
- help: ['h']
815
- },
816
- boolean: ['dry-run', 'debug', 'yes'],
817
- default: {
818
- branch: 'main'
819
- }
820
- });
821
- cliCommands = cliFlags._;
822
- if (cliCommands.length === 0 ||
823
- // @ts-expect-error mri is not typed for the help command.
824
- cliCommands.help && cliCommands.length === 0) {
825
- console.log("\nUsage: deployment-cli [command] [flags]\n\nDisplays help information.\n\nCommand:\n approve Approves a job and by this triggers a deployment of a component. It requires a \"CIRCLE_TOKEN\" environment variable (https://circleci.com/docs/2.0/managing-api-tokens/).\n\nOptions:\n --approval-job The name of the approval job to approve a deployment with.\n --deployment-job (optional) The name of the deployment job triggered by the approval job. If passed the CLI will print a URL to the deployment triggered on CircleCI.\n --deployment (optional) The name of a deployment configured in the configuration file.\n --build-revision <git-sha> (optional) The git commit SHA that needs to be deployed. If not specified, the last successful pipeline is used.\n --branch (optional) The git branch to deploy from. If not specified, defaults to 'main'.\n --yes (optional) Skip all confirmation prompts. Useful in Continuous integration (CI) to automatically answer confirmation questions.\n\nGeneral options:\n --dry-run (optional) Simulate a deployment.\n --debug (optional) Print additional debug information.\n");
826
- process.exit(0);
827
- }
828
- cliCommand = cliCommands[0];
829
- if (cliFlags['dry-run']) {
830
- console.log("\n \uD83D\uDE4A Do not worry. This is a dry run!\n ");
831
- }
832
- console.log("\uD83C\uDFC3 Running ".concat(cliCommand, " command"));
833
- _context2.t0 = cliCommand;
834
- _context2.next = _context2.t0 === 'approve' ? 15 : 19;
835
- break;
836
- case 15:
512
+ async function run() {
513
+ const config = await loadConfig();
514
+ throwIfConfigurationLacksRequiredValues(config);
515
+ const circleCiApis = createCircleCiClient({
516
+ projectName: config.CircleCI.projectName,
517
+ apiBaseUrl: config.CircleCI.apiBaseUrl
518
+ });
519
+ try {
520
+ var _context;
521
+ const cliFlags = mri__default["default"](_sliceInstanceProperty__default["default"](_context = process.argv).call(_context, 2), {
522
+ alias: {
523
+ help: ['h']
524
+ },
525
+ boolean: ['dry-run', 'debug', 'yes'],
526
+ default: {
527
+ branch: 'main'
528
+ }
529
+ });
530
+ const cliCommands = cliFlags._;
531
+ if (cliCommands.length === 0 ||
532
+ // @ts-expect-error mri is not typed for the help command.
533
+ cliCommands.help && cliCommands.length === 0) {
534
+ console.log("\nUsage: deployment-cli [command] [flags]\n\nDisplays help information.\n\nCommand:\n approve Approves a job and by this triggers a deployment of a component. It requires a \"CIRCLE_TOKEN\" environment variable (https://circleci.com/docs/2.0/managing-api-tokens/).\n\nOptions:\n --approval-job The name of the approval job to approve a deployment with.\n --deployment-job (optional) The name of the deployment job triggered by the approval job. If passed the CLI will print a URL to the deployment triggered on CircleCI.\n --deployment (optional) The name of a deployment configured in the configuration file.\n --build-revision <git-sha> (optional) The git commit SHA that needs to be deployed. If not specified, the last successful pipeline is used.\n --branch (optional) The git branch to deploy from. If not specified, defaults to 'main'.\n --yes (optional) Skip all confirmation prompts. Useful in Continuous integration (CI) to automatically answer confirmation questions.\n\nGeneral options:\n --dry-run (optional) Simulate a deployment.\n --debug (optional) Print additional debug information.\n");
535
+ process.exit(0);
536
+ }
537
+ const cliCommand = cliCommands[0];
538
+ if (cliFlags['dry-run']) {
539
+ console.log("\n \uD83D\uDE4A Do not worry. This is a dry run!\n ");
540
+ }
541
+ console.log("\uD83C\uDFC3 Running ".concat(cliCommand, " command"));
542
+ switch (cliCommand) {
543
+ case 'approve':
544
+ {
837
545
  throwIfRequiredEnvironmentVariableIsUnset(['CIRCLE_TOKEN']);
838
- _context2.next = 18;
839
- return approve({
546
+ await approve({
840
547
  deployment: cliFlags.deployment,
841
548
  branch: cliFlags.branch,
842
549
  approvalJob: cliFlags['approval-job'],
@@ -846,25 +553,16 @@ function _run() {
846
553
  debug: cliFlags.debug,
847
554
  dryRun: cliFlags['dry-run']
848
555
  }, config, circleCiApis);
849
- case 18:
850
556
  process.exit(0);
851
- case 19:
852
- throw new Error("\uD83D\uDC80 Unknown command \"".concat(cliCommand, "\"."));
853
- case 20:
854
- _context2.next = 26;
855
- break;
856
- case 22:
857
- _context2.prev = 22;
858
- _context2.t1 = _context2["catch"](5);
859
- console.error(_context2.t1);
860
- process.exit(1);
861
- case 26:
862
- case "end":
863
- return _context2.stop();
864
- }
865
- }, _callee, null, [[5, 22]]);
866
- }));
867
- return _run.apply(this, arguments);
557
+ }
558
+ // eslint-disable-next-line no-fallthrough
559
+ default:
560
+ throw new Error("\uD83D\uDC80 Unknown command \"".concat(cliCommand, "\"."));
561
+ }
562
+ } catch (error) {
563
+ console.error(error);
564
+ process.exit(1);
565
+ }
868
566
  }
869
567
 
870
568
  exports.run = run;