|
3 | 3 | const Joi = require('joi'); |
4 | 4 | const Hoek = require('@hapi/hoek'); |
5 | 5 | const clone = require('clone'); |
| 6 | +const { STAGE_TRIGGER, STAGE_SETUP_TEARDOWN_JOB_NAME } = require('screwdriver-data-schema/config/regex'); |
6 | 7 | const WorkflowParser = require('screwdriver-workflow-parser'); |
7 | 8 | const SlackValidation = require('screwdriver-notifications-slack'); |
8 | 9 | const EmailValidation = require('screwdriver-notifications-email'); |
| 10 | +const { normalizeRequires } = require('./requires'); |
9 | 11 |
|
10 | 12 | // Actual environment size is limited by space, not quantity. |
11 | 13 | // We do this to force sd.yaml to be simpler. |
@@ -242,19 +244,78 @@ async function generateWorkflowGraph(doc, triggerFactory, pipelineId) { |
242 | 244 | return WorkflowParser.getWorkflow(cloneDoc, triggerFactory, pipelineId); |
243 | 245 | } |
244 | 246 |
|
| 247 | +/** |
| 248 | + * Validate that requires fields only reference existing stages |
| 249 | + * @method validateRequiresStagesExist |
| 250 | + * @param {Object} config |
| 251 | + * @param {Object} config.jobs Map of job name to job configuration |
| 252 | + * @param {Object} config.stages Map of stage name to stage configuration |
| 253 | + * @return {Array} List of errors |
| 254 | + */ |
| 255 | +function validateRequiresStagesExist({ jobs, stages }) { |
| 256 | + const errors = []; |
| 257 | + const stageNames = Object.keys(stages || {}); |
| 258 | + const stageNameSet = new Set(stageNames); |
| 259 | + |
| 260 | + // Validate job-level requires that reference stages. |
| 261 | + Object.entries(jobs || {}).forEach(([jobName, job]) => { |
| 262 | + const setupTeardownMatched = STAGE_SETUP_TEARDOWN_JOB_NAME.exec(jobName); |
| 263 | + |
| 264 | + // Stage-level requires are copied to setup jobs during flattening, so skip them here. |
| 265 | + if (setupTeardownMatched && setupTeardownMatched[2] === 'setup') { |
| 266 | + return; |
| 267 | + } |
| 268 | + |
| 269 | + const requiresList = normalizeRequires(job.requires); |
| 270 | + |
| 271 | + requiresList.forEach(requires => { |
| 272 | + const matched = STAGE_TRIGGER.exec(requires); |
| 273 | + |
| 274 | + if (matched && !stageNameSet.has(matched[1])) { |
| 275 | + errors.push( |
| 276 | + `${jobName} job has invalid requires: ${requires}. Cannot find stage ${matched[1]} in stages.` |
| 277 | + ); |
| 278 | + } |
| 279 | + }); |
| 280 | + }); |
| 281 | + |
| 282 | + // Validate stage-level requires that reference stages. |
| 283 | + Object.entries(stages || {}).forEach(([stageName, stage]) => { |
| 284 | + const stageRequiresList = normalizeRequires(stage.requires); |
| 285 | + |
| 286 | + stageRequiresList.forEach(requires => { |
| 287 | + const matched = STAGE_TRIGGER.exec(requires); |
| 288 | + |
| 289 | + if (matched && !stageNameSet.has(matched[1])) { |
| 290 | + errors.push( |
| 291 | + `${stageName} stage has invalid requires: ${requires}. Cannot find stage ${matched[1]} in stages.` |
| 292 | + ); |
| 293 | + } |
| 294 | + }); |
| 295 | + }); |
| 296 | + |
| 297 | + return errors; |
| 298 | +} |
| 299 | + |
245 | 300 | /** |
246 | 301 | * Ensure the workflowGraph is a valid one |
| 302 | + * - Stage references point to existing stages |
247 | 303 | * - No cycles in workflowGraph |
248 | 304 | * @method validateWorkflowGraph |
249 | 305 | * @param {Object} doc Document that went through flattening |
250 | 306 | * @return {Array} List of errors |
251 | 307 | */ |
252 | 308 | function validateWorkflowGraph(doc) { |
| 309 | + const errors = validateRequiresStagesExist({ |
| 310 | + jobs: doc.jobs, |
| 311 | + stages: doc.stages |
| 312 | + }); |
| 313 | + |
253 | 314 | if (WorkflowParser.hasCycle(doc.workflowGraph)) { |
254 | | - return ['Jobs: should not have circular dependency in jobs']; |
| 315 | + errors.push('Jobs: should not have circular dependency in jobs'); |
255 | 316 | } |
256 | 317 |
|
257 | | - return []; |
| 318 | + return errors; |
258 | 319 | } |
259 | 320 |
|
260 | 321 | /** |
|
0 commit comments