Skip to content

Commit b538ffa

Browse files
authored
Merge pull request screwdriver-cd#8 from screwdriver-cd/RestrictedNames
Restricting sd- from the step names
2 parents 0d5a9b4 + cda2ba7 commit b538ffa

3 files changed

Lines changed: 48 additions & 7 deletions

File tree

lib/phase/functional.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ function getDefaultWorkflow(jobs) {
3030
* Make sure the Matrix they specified is valid
3131
* @method validateJobMatrix
3232
* @param {Object} job Job to inspect
33-
* @param {String} jobName Name of job (for error reporting)
33+
* @param {String} prefix Prefix before reporting errors
3434
* @return {Array} List of errors
3535
*/
36-
function validateJobMatrix(job, jobName) {
36+
function validateJobMatrix(job, prefix) {
3737
let matrixSize = 1;
3838
const errors = [];
3939
const matrix = Hoek.reach(job, 'matrix', {
@@ -45,7 +45,7 @@ function validateJobMatrix(job, jobName) {
4545
const environmentSize = Object.keys(matrix).length + Object.keys(environment).length;
4646

4747
if (environmentSize > MAX_ENVIRONMENT_VARS) {
48-
errors.push(`Job ${jobName}: "environment" and "matrix" can only have a combined ` +
48+
errors.push(`${prefix}: "environment" and "matrix" can only have a combined ` +
4949
` maxiumum of ${MAX_ENVIRONMENT_VARS} environment variables defined ` +
5050
`(currently ${environmentSize})`);
5151
}
@@ -55,13 +55,37 @@ function validateJobMatrix(job, jobName) {
5555
});
5656

5757
if (matrixSize > MAX_PERMUTATIONS) {
58-
errors.push(`Job ${jobName}: Matrix cannot contain >${MAX_PERMUTATIONS} permutations ` +
58+
errors.push(`${prefix}: "matrix" cannot contain >${MAX_PERMUTATIONS} permutations ` +
5959
`(currently ${matrixSize})`);
6060
}
6161

6262
return errors;
6363
}
6464

65+
/**
66+
* Make sure the Steps are possible
67+
* - Check it doesn't start with sd-
68+
* @method validateJobSteps
69+
* @param {Object} job Job to inspect
70+
* @param {String} prefix Prefix before reporting errors
71+
* @return {Array} List of errors
72+
*/
73+
function validateJobSteps(job, prefix) {
74+
const errors = [];
75+
const steps = Hoek.reach(job, 'commands', {
76+
default: []
77+
});
78+
79+
steps.forEach((step) => {
80+
if (step.name.indexOf('sd-') === 0) {
81+
errors.push(`${prefix}: Step "${step.name}": `
82+
+ 'cannot use a restricted prefix "sd-"');
83+
}
84+
});
85+
86+
return errors;
87+
}
88+
6589
/**
6690
* Ensure the job is something that can be run
6791
* - has at least one step
@@ -94,13 +118,16 @@ function validateJobSchema(doc) {
94118

95119
// Validate jobs contain required minimum fields
96120
Object.keys(doc.jobs).forEach((jobName) => {
121+
const prefix = `Job "${jobName}"`;
122+
97123
try {
98-
Joi.attempt(doc.jobs[jobName], SCHEMA_JOB, `Job ${jobName}`);
124+
Joi.attempt(doc.jobs[jobName], SCHEMA_JOB, prefix);
99125
} catch (err) {
100126
errors.push(err.message);
101127
}
102128

103-
errors = errors.concat(validateJobMatrix(doc.jobs[jobName], jobName));
129+
errors = errors.concat(validateJobMatrix(doc.jobs[jobName], prefix));
130+
errors = errors.concat(validateJobSteps(doc.jobs[jobName], prefix));
104131
});
105132

106133
return errors;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
jobs:
2+
main:
3+
image: node:4
4+
steps:
5+
- sd-setup: woot

test/index.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,16 @@ describe('config parser', () => {
140140
it('returns an error if matrix is too big', (done) => {
141141
parser(loadData('too-big-matrix.yaml'), (err) => {
142142
assert.isNotNull(err);
143-
assert.match(err.toString(), /Job main: Matrix cannot contain >25 permutations/);
143+
assert.match(err.toString(), /Job "main": "matrix" cannot contain >25 perm/);
144+
done();
145+
});
146+
});
147+
148+
it('returns an error if using restricted step names', (done) => {
149+
parser(loadData('restricted-step-name.yaml'), (err) => {
150+
assert.isNotNull(err);
151+
assert.match(err.toString(),
152+
/Job "main": Step "sd-setup": cannot use a restricted prefix "sd-"/);
144153
done();
145154
});
146155
});

0 commit comments

Comments
 (0)