@secureci/cli 0.5.1 → 0.6.0

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.
@@ -4,11 +4,15 @@ exports.dryRun = dryRun;
4
4
  const workflow_parser_1 = require("../parser/workflow-parser");
5
5
  const logger_1 = require("../utils/logger");
6
6
  const validator_1 = require("../parser/validator");
7
+ const expression_1 = require("./expression");
8
+ const job_plan_1 = require("./job-plan");
9
+ // A matrix expression reading a context a dry run doesn't have.
10
+ const RUN_TIME_MATRIX = /\$\{\{[^}]*\b(?:needs|inputs|vars|github|secrets)\b/;
7
11
  /**
8
12
  * Perform a dry run of a workflow or action file
9
13
  * Shows what would be executed without actually running anything
10
14
  */
11
- function dryRun(filePath) {
15
+ function dryRun(filePath, options = {}) {
12
16
  try {
13
17
  const parsed = (0, workflow_parser_1.parseFile)(filePath);
14
18
  // A file a real run would reject fails the dry run too.
@@ -23,7 +27,7 @@ function dryRun(filePath) {
23
27
  return { success: false, type: parsed.type };
24
28
  }
25
29
  if (parsed.type === 'workflow') {
26
- return dryRunWorkflow(parsed);
30
+ return dryRunWorkflow(parsed, options);
27
31
  }
28
32
  else {
29
33
  return dryRunAction(parsed);
@@ -37,22 +41,56 @@ function dryRun(filePath) {
37
41
  };
38
42
  }
39
43
  }
40
- function dryRunWorkflow(parsed) {
44
+ function dryRunWorkflow(parsed, options) {
41
45
  const workflow = parsed.file;
46
+ const filter = options.matrix ?? {};
47
+ const definedKeys = new Set();
48
+ let runTimeMatrix = false;
49
+ let unmatched = false;
42
50
  logger_1.logger.info(`Workflow: ${workflow.name}`);
43
51
  logger_1.logger.info(`Trigger: ${JSON.stringify(workflow.on)}`);
44
- const jobs = Object.entries(workflow.jobs).map(([jobId, job]) => {
52
+ const selectedJobs = Object.entries(workflow.jobs).filter(([jobId]) => !options.job || jobId === options.job);
53
+ const jobs = selectedJobs.flatMap(([jobId, job]) => {
54
+ // Matrix combinations, as far as they can be known without running.
55
+ let matrix;
56
+ const rawMatrix = job.strategy?.matrix;
57
+ const atRunTime = rawMatrix !== undefined &&
58
+ RUN_TIME_MATRIX.test(JSON.stringify(rawMatrix));
59
+ if (atRunTime) {
60
+ runTimeMatrix = true;
61
+ }
62
+ else if (rawMatrix !== undefined) {
63
+ const combinations = (0, job_plan_1.expandMatrix)(rawMatrix, (0, expression_1.createContext)());
64
+ combinations.forEach((c) => Object.keys(c).forEach((k) => definedKeys.add(k)));
65
+ const { keys, matches } = (0, job_plan_1.filterMatrix)(combinations, filter);
66
+ if (keys.length > 0 && matches.length === 0) {
67
+ logger_1.logger.warn((0, job_plan_1.noMatrixMatchMessage)(jobId, filter, combinations));
68
+ unmatched = true;
69
+ return [];
70
+ }
71
+ matrix = matches.map((m) => (0, job_plan_1.matrixLabel)(m.values));
72
+ }
45
73
  logger_1.logger.step(`Job: ${jobId} (${job.name || jobId})`);
74
+ if (atRunTime) {
75
+ logger_1.logger.info(' Matrix: resolved at run time');
76
+ }
77
+ else if (matrix) {
78
+ logger_1.logger.info(` Matrix (${matrix.length} combination(s)):`);
79
+ matrix.forEach((label) => logger_1.logger.info(` ${label}`));
80
+ }
46
81
  // Handle reusable workflow calls
47
82
  if (job.uses) {
48
83
  logger_1.logger.info(` Uses workflow: ${job.uses}`);
49
84
  if (job.with) {
50
85
  logger_1.logger.info(` With inputs: ${JSON.stringify(job.with)}`);
51
86
  }
52
- return {
53
- name: jobId,
54
- steps: [`Calls reusable workflow: ${job.uses}`]
55
- };
87
+ return [
88
+ {
89
+ name: jobId,
90
+ steps: [`Calls reusable workflow: ${job.uses}`],
91
+ ...(matrix ? { matrix } : {})
92
+ }
93
+ ];
56
94
  }
57
95
  // Handle regular jobs
58
96
  if (job['runs-on']) {
@@ -74,11 +112,18 @@ function dryRunWorkflow(parsed) {
74
112
  logger_1.logger.info(stepDesc);
75
113
  return stepDesc;
76
114
  });
77
- return {
78
- name: jobId,
79
- steps
80
- };
115
+ return [{ name: jobId, steps, ...(matrix ? { matrix } : {}) }];
81
116
  });
117
+ // A key might belong to a matrix only known at run time.
118
+ if (!runTimeMatrix) {
119
+ for (const key of (0, job_plan_1.unknownMatrixKeys)(filter, definedKeys)) {
120
+ logger_1.logger.warn(`--matrix ${key}: no matrix job defines this key; it was ignored`);
121
+ }
122
+ }
123
+ if (unmatched) {
124
+ logger_1.logger.error('Dry run failed: a --matrix filter matched no combination');
125
+ return { success: false, type: 'workflow', jobs };
126
+ }
82
127
  logger_1.logger.success('Dry run completed successfully');
83
128
  return {
84
129
  success: true,