Skip to content

Commit

Permalink
#17 Support the build step's metadata annotations:
Browse files Browse the repository at this point in the history
* Add support for post step annotation ops
  • Loading branch information
noamt committed Dec 15, 2016
1 parent d5b70ac commit ca217d3
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 15 deletions.
30 changes: 30 additions & 0 deletions schema/1.0/base-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@ class BaseSchema {
});
}

static _getMetadataAnnotationSetSchema() {
return Joi.array().items(
Joi.alternatives().try(
Joi.object().pattern(/^[A-Za-z0-9_]+$/, Joi.alternatives().try(
[
Joi.string(),
Joi.boolean(),
Joi.number(),
Joi.object({ evaluate: Joi.string().required() })
]
)), Joi.string().regex(/^[A-Za-z0-9_]+$/)
)
);
}

_applyMetadataAnnotationSchemaProperties(schemaProperties) {
const metadataAnnotationSchema = Joi.object({
metadata: Joi.object({
set: Joi.array().items(
Joi.object().pattern(/^.+$/, BaseSchema._getMetadataAnnotationSetSchema())
)
})
});
return Object.assign(schemaProperties, {
'on_success': metadataAnnotationSchema,
'on_fail': metadataAnnotationSchema,
'on_finish': metadataAnnotationSchema,
});
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
Expand Down
11 changes: 1 addition & 10 deletions schema/1.0/steps/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,7 @@ class Build extends BaseSchema {
build_arguments: Joi.array().items(Joi.string()),
tag: Joi.string(),
metadata: Joi.object({
set: Joi.array().items(
Joi.alternatives().try(
Joi.object().pattern(/^[A-Za-z09_]+$/, Joi.alternatives().try(
[
Joi.string(),
Joi.boolean(),
Joi.number(),
Joi.object({ evaluate: Joi.string().required() })
]
)), Joi.string()))
set: Build._getMetadataAnnotationSetSchema()
})

};
Expand Down
1 change: 1 addition & 0 deletions schema/1.0/steps/composition.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Composition extends BaseSchema {
'composition_candidates': Joi.object().required(),
'composition_variables': Joi.array().items(Joi.string()),
};
this._applyMetadataAnnotationSchemaProperties(compositionProperties);
return this._createSchema(compositionProperties).unknown();
}

Expand Down
1 change: 1 addition & 0 deletions schema/1.0/steps/freestyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Freestyle extends BaseSchema {
environment: Joi.array().items(Joi.string()),
entry_point: Joi.alternatives().try(Joi.string(), Joi.array().items(Joi.string()))
};
this._applyMetadataAnnotationSchemaProperties(freestyleProperties);
return this._createSchema(freestyleProperties).unknown();
}

Expand Down
1 change: 1 addition & 0 deletions schema/1.0/steps/git-clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class GitClone extends BaseSchema {
revision: Joi.string(),
credentials: BaseSchema._getCredentialsSchema()
};
this._applyMetadataAnnotationSchemaProperties(gitCloneProperties);
return this._createSchema(gitCloneProperties);
}

Expand Down
1 change: 1 addition & 0 deletions schema/1.0/steps/launch-composition.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CompositionLaunch extends BaseSchema {
composition: Joi.alternatives(Joi.object(), Joi.string()).required(),
'composition_variables': Joi.array().items(Joi.string()),
};
this._applyMetadataAnnotationSchemaProperties(compositionProperties);
return this._createSchema(compositionProperties).unknown();
}

Expand Down
1 change: 1 addition & 0 deletions schema/1.0/steps/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Push extends BaseSchema {
region: Joi.string()

};
this._applyMetadataAnnotationSchemaProperties(pushProperties);
return this._createSchema(pushProperties);
}
}
Expand Down
207 changes: 202 additions & 5 deletions tests/unit/validator.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,142 @@ describe('Validate Codefresh YAML', () => {
}
}, '"tag" must be a string', done);
});

it('Unknown post-step metadata operation', (done) => {
validateForError({
version: '1.0',
steps: {
push: {
'type': 'push',
'candidate': 'teh-image',
'on_finish': {
metadata: {
put: [
{
'${{build_prj.image}}': [
{ 'qa': 'pending' }
]
}
]
}
}
}
}
}, '"put" is not allowed', done);
});

it('Unknown post-step metadata entry', (done) => {
validateForError({
version: '1.0',
steps: {
push: {
'type': 'push',
'candidate': 'teh-image',
'on_finish': {
metadata: {
set: {
'${{build_prj.image}}': [
{ 'qa': 'pending' }
]
}
}
}
}
}
}, '"set" must be an array', done);
});

it('Unspecified image to annotate', (done) => {
validateForError({
version: '1.0',
steps: {
push: {
'type': 'push',
'candidate': 'teh-image',
'on_finish': {
metadata: {
set: [
{ 'qa': 'pending' }
]
}
}
}
}
}, '"qa" must be an array', done);
});

it('Invalid post-step metadata annotation key', (done) => {
validateForError({
version: '1.0',
steps: {
push: {
'type': 'push',
'candidate': 'teh-image',
'on_finish': {
metadata: {
set: [
{
'${{build_prj.image}}': [
'an invalid key'
]
}
]
}
}
}
}
}, '"an invalid key" fails to match', done);
});

it('Invalid post-step metadata annotation value', (done) => {
validateForError({
version: '1.0',
steps: {
push: {
'type': 'push',
'candidate': 'teh-image',
'on_finish': {
metadata: {
set: [
{
'${{build_prj.image}}': [
{ 'key1': [] }
]
}
]
}
}
}
}
}, '"key1" must be a', done);
});

it('Invalid post-step metadata annotation evaluation expression', (done) => {
validateForError({
version: '1.0',
steps: {
push: {
'type': 'push',
'candidate': 'teh-image',
'on_finish': {
metadata: {
set: [
{
'${{build_prj.image}}': [
{
'jimbob': {
eval: 'jimbob == jimbob'
}
}
]
}
]
}
}
}
}
}, '"evaluate" is required', done);
});
});

describe('Freestyle step attributes', () => {
Expand Down Expand Up @@ -798,7 +934,22 @@ describe('Validate Codefresh YAML', () => {
'commands': ['jim', 'bob'],
'environment': ['key=value', 'key1=value¡'],
'fail_fast': true,
'when': { branch: { only: ['master'] } }
'when': { branch: { only: ['master'] } },
'on_success': {
metadata: {
set: [
{
'${{build_prj.image}}': [
{ 'qa': 'pending' },
{ 'healthy': true },
{ 'quality': 67 },
{ 'is_tested': { evaluate: '${{unit_test_step.status}} === success' } },
'dangling'
]
}
]
}
}
},
clone: {
'type': 'git-clone',
Expand All @@ -809,7 +960,22 @@ describe('Validate Codefresh YAML', () => {
'revision': 'abcdef12345',
'credentials': { username: 'subject', password: 'credentials' },
'fail_fast': true,
'when': { branch: { ignore: ['develop'] } }
'when': { branch: { ignore: ['develop'] } },
'on_fail': {
metadata: {
set: [
{
'${{build_prj.image}}': [
{ 'qa': 'pending' },
{ 'healthy': true },
{ 'quality': 67 },
{ 'is_tested': { evaluate: '${{unit_test_step.status}} === success' } },
'dangling'
]
}
]
}
}
},
build_string_dockerfile: {
'type': 'build',
Expand Down Expand Up @@ -853,9 +1019,25 @@ describe('Validate Codefresh YAML', () => {
'registry': 'dtr.host.com',
'credentials': { username: 'subject', password: 'credentials' },
'fail_fast': true,
'when': { branch: { only: ['/FB-/i'] } }
'when': { branch: { only: ['/FB-/i'] } },
'on_finish': {
metadata: {
set: [
{
'${{build_prj.image}}': [
{ 'qa': 'pending' },
{ 'healthy': true },
{ 'quality': 67 },
{ 'is_tested': { evaluate: '${{unit_test_step.status}} === success' } },
'dangling'
]
}
]
}
}
},
composition: {

composition: {
'type': 'composition',
'description': 'desc',
'title': 'Composition step',
Expand All @@ -872,7 +1054,22 @@ describe('Validate Codefresh YAML', () => {
},
'composition_variables': ['jim=bob'],
'fail_fast': true,
'when': { condition: { any: { noDetectedSkipCI: 'includes(\'${{CF_COMMIT_MESSAGE}}\', \'[skip ci]\') == false' } } }
'when': { condition: { any: { noDetectedSkipCI: 'includes(\'${{CF_COMMIT_MESSAGE}}\', \'[skip ci]\') == false' } } },
'on_success': {
metadata: {
set: [
{
'${{build_prj.image}}': [
{ 'qa': 'pending' },
{ 'healthy': true },
{ 'quality': 67 },
{ 'is_tested': { evaluate: '${{unit_test_step.status}} === success' } },
'dangling'
]
}
]
}
}
}
}
});
Expand Down

0 comments on commit ca217d3

Please sign in to comment.