From 3edb0ada51392cdcccba3026d461c05aca3ad4d6 Mon Sep 17 00:00:00 2001 From: Dao Lam Date: Thu, 28 Sep 2017 17:32:12 -0700 Subject: [PATCH] feat(723): allows requires keyword for jobs --- api/validator.js | 3 +- config/job.js | 10 ++- config/regex.js | 2 + package.json | 2 +- test/api/validator.test.js | 10 +++ test/config/job.test.js | 8 +++ test/data/config.job.jobv2.bad.yaml | 13 ++++ test/data/config.job.jobv2.yaml | 13 ++++ test/data/validator-with-requires.input.yaml | 23 +++++++ test/data/validator-with-requires.output.yaml | 62 +++++++++++++++++++ 10 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 test/data/config.job.jobv2.bad.yaml create mode 100644 test/data/config.job.jobv2.yaml create mode 100644 test/data/validator-with-requires.input.yaml create mode 100644 test/data/validator-with-requires.output.yaml diff --git a/api/validator.js b/api/validator.js index 69973af9..7089b8d0 100644 --- a/api/validator.js +++ b/api/validator.js @@ -27,7 +27,8 @@ const SCHEMA_JOB_PERMUTATION = Joi.object() environment: Job.environment, image: Job.image, secrets: Job.secrets, - settings: Job.settings + settings: Job.settings, + requires: Job.requires }).label('Job permutation'); const SCHEMA_JOB_PERMUTATIONS = Joi.array().items(SCHEMA_JOB_PERMUTATION) diff --git a/config/job.js b/config/job.js index 659d7512..b99584b6 100644 --- a/config/job.js +++ b/config/job.js @@ -65,9 +65,16 @@ const SCHEMA_SETTINGS = Joi.object().optional(); const SCHEMA_STEP = Joi.alternatives().try(SCHEMA_STEP_STRING, SCHEMA_STEP_OBJECT); const SCHEMA_STEPS = Joi.array().items(SCHEMA_STEP).min(1); const SCHEMA_TEMPLATE = Joi.string().regex(Regex.FULL_TEMPLATE_NAME); - +const SCHEMA_JOBNAME = Joi.string().regex(Regex.JOB_NAME); +const SCHEMA_TRIGGER = Joi.string().regex(Regex.TRIGGER); // ~commit, ~pr, etc. +const SCHEMA_REQUIRES_VALUE = Joi.alternatives().try(SCHEMA_JOBNAME, SCHEMA_TRIGGER); +const SCHEMA_REQUIRES = Joi.alternatives().try( + Joi.array().items(SCHEMA_REQUIRES_VALUE), + SCHEMA_REQUIRES_VALUE +); const SCHEMA_JOB = Joi.object() .keys({ + requires: SCHEMA_REQUIRES, annotations: Annotations.annotations, description: SCHEMA_DESCRIPTION, environment: SCHEMA_ENVIRONMENT, @@ -91,6 +98,7 @@ module.exports = { image: SCHEMA_IMAGE, job: SCHEMA_JOB, matrix: SCHEMA_MATRIX, + requires: SCHEMA_REQUIRES, secret: SCHEMA_SECRET, secrets: SCHEMA_SECRETS, settings: SCHEMA_SETTINGS, diff --git a/config/regex.js b/config/regex.js index 2574f7af..07580284 100644 --- a/config/regex.js +++ b/config/regex.js @@ -22,6 +22,8 @@ module.exports = { STEP_NAME: /^[\w-]+$/, // Jobs can only be named with A-Z,a-z,0-9,-,_ JOB_NAME: /^[\w-]+$/, + // Can be ~pr, ~commit, or ~commit:branchName + TRIGGER: /^~(pr|commit(:.+)?)$/, // IEEE Std 1003.1-2001 // Environment names contain uppercase letters, digits, and underscore // They cannot start with digits diff --git a/package.json b/package.json index 51d17214..f4a4ce57 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "screwdriver-data-schema", - "version": "17.0.0", + "version": "18.0.0", "description": "Internal Data Schema of Screwdriver", "main": "index.js", "scripts": { diff --git a/test/api/validator.test.js b/test/api/validator.test.js index 1ad4d58a..9fc17842 100644 --- a/test/api/validator.test.js +++ b/test/api/validator.test.js @@ -9,6 +9,11 @@ describe('api validator', () => { it('accepts basic input', () => { assert.isNull(validate('validator.input.yaml', api.validator.input).error); }); + + it('accepts input with requires keyword', () => { + assert.isNull( + validate('validator-with-requires.input.yaml', api.validator.input).error); + }); }); describe('output', () => { @@ -16,6 +21,11 @@ describe('api validator', () => { assert.isNull(validate('validator.output.yaml', api.validator.output).error); }); + it('validates output with requires keyword', () => { + assert.isNull( + validate('validator-with-requires.output.yaml', api.validator.output).error); + }); + it('validates basic output with errors', () => { assert.isNull(validate('validator.erroroutput.yaml', api.validator.output).error); }); diff --git a/test/config/job.test.js b/test/config/job.test.js index 51bd0c90..9a46c031 100644 --- a/test/config/job.test.js +++ b/test/config/job.test.js @@ -10,6 +10,14 @@ describe('config job', () => { assert.isNull(validate('config.job.job.yaml', config.job.job).error); }); + it('validates a job with requires', () => { + assert.isNull(validate('config.job.jobv2.yaml', config.job.job).error); + }); + + it('returns error for bad requires format', () => { + assert.isNotNull(validate('config.job.jobv2.bad.yaml', config.job.job).error); + }); + it('validates a description', () => { assert.isNull(validate('config.job.description.yaml', config.job.job).error); }); diff --git a/test/data/config.job.jobv2.bad.yaml b/test/data/config.job.jobv2.bad.yaml new file mode 100644 index 00000000..574f00e2 --- /dev/null +++ b/test/data/config.job.jobv2.bad.yaml @@ -0,0 +1,13 @@ +environment: + NODE_ENV: production +matrix: + NODE_VERSION: + - 4 + - 5 + - 6 +image: node:{{NODE_VERSION}} +steps: + - init: npm install + - test: npm test + - publish: npm publish +requires: ~notsupportingthis diff --git a/test/data/config.job.jobv2.yaml b/test/data/config.job.jobv2.yaml new file mode 100644 index 00000000..1fc5273b --- /dev/null +++ b/test/data/config.job.jobv2.yaml @@ -0,0 +1,13 @@ +environment: + NODE_ENV: production +matrix: + NODE_VERSION: + - 4 + - 5 + - 6 +image: node:{{NODE_VERSION}} +steps: + - init: npm install + - test: npm test + - publish: npm publish +requires: ~commit diff --git a/test/data/validator-with-requires.input.yaml b/test/data/validator-with-requires.input.yaml new file mode 100644 index 00000000..66ccd16d --- /dev/null +++ b/test/data/validator-with-requires.input.yaml @@ -0,0 +1,23 @@ +yaml: | + jobs: + main: + annotations: + beta.screwdriver.cd/auto_pr_builds: fork-only + description: "This is a description" + image: node:{{NODE_VERSION}} + matrix: + NODE_VERSION: [4,5,6] + steps: + install: npm install + test: npm test + build: npm run build + settings: + email: foo@bar.com + requires: ~commit + + publish: + image: node:4 + steps: + install: npm publish + requires: + - main diff --git a/test/data/validator-with-requires.output.yaml b/test/data/validator-with-requires.output.yaml new file mode 100644 index 00000000..16a2dd7a --- /dev/null +++ b/test/data/validator-with-requires.output.yaml @@ -0,0 +1,62 @@ +jobs: + main: + - image: node:4 + commands: + - name: install + command: npm install + - name: test + command: npm test + - name: build + command: npm run build + annotations: + beta.screwdriver.cd/auto_pr_builds: fork-only + description: "This is a description" + environment: + NODE_VERSION: 4 + settings: + email: foo@bar.com + - image: node:5 + commands: + - name: install + command: npm install + - name: test + command: npm test + - name: build + command: npm run build + annotations: + beta.screwdriver.cd/auto_pr_builds: fork-only + description: "This is a description" + environment: + NODE_VERSION: 5 + settings: + email: foo@bar.com + - image: node:6 + commands: + - name: install + command: npm install + - name: test + command: npm test + - name: build + command: npm run build + annotations: + beta.screwdriver.cd/auto_pr_builds: fork-only + description: "This is a description" + environment: + NODE_VERSION: 6 + settings: + email: foo@bar.com + secrets: + - ANOTHER_SECRET + + publish: + - image: node:4 + requires: main + commands: + - name: install + command: npm publish + environment: {} + secrets: + - NPM_TOKEN + +workflow: + - publish