Skip to content

Commit

Permalink
Merge pull request #110 from screwdriver-cd/temp
Browse files Browse the repository at this point in the history
feat(470): add template schema to models
  • Loading branch information
d2lam authored Mar 3, 2017
2 parents 6ab9d07 + dbf817a commit 9299370
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 2 deletions.
7 changes: 6 additions & 1 deletion config/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ const SCHEMA_TEMPLATE = Joi.object()
* @type {Object}
*/
module.exports = {
template: SCHEMA_TEMPLATE
template: SCHEMA_TEMPLATE,
name: TEMPLATE_NAME,
version: TEMPLATE_VERSION,
description: TEMPLATE_DESCRIPTION,
maintainer: TEMPLATE_MAINTAINER,
config: Job.job
};
3 changes: 2 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ const job = require('./job');
const pipeline = require('./pipeline');
const user = require('./user');
const secret = require('./secret');
const template = require('./template');

module.exports = { build, event, job, pipeline, user, secret };
module.exports = { build, event, job, pipeline, user, secret, template };
106 changes: 106 additions & 0 deletions models/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
'use strict';

const Joi = require('joi');
const mutate = require('../lib/mutate');
const Template = require('../config/template');
const scmUri = Joi.reach(require('./pipeline').base, 'scmUri');

const MODEL = {
id: Joi
.number().integer().positive()
.description('Identifier of this template')
.example(123345),

labels: Joi
.array()
.items(Joi.string())
.description('Labels for template')
.example(['stable', 'latest', 'beta']),

config: Template.config,
name: Template.name,
version: Template.version,
description: Template.description,
maintainer: Template.maintainer,
scmUri
};

module.exports = {
/**
* All the available properties of Template
*
* @property base
* @type {Joi}
*/
base: Joi.object(MODEL).label('Template'),

/**
* Properties for template that will come back during a GET request
*
* @property get
* @type {Joi}
*/
get: Joi.object(mutate(MODEL, [
'id', 'labels', 'config', 'name', 'version', 'description', 'maintainer', 'scmUri'
], [])).label('Get Template'),

/**
* Properties for template that will be passed during a CREATE request
*
* @property create
* @type {Joi}
*/
create: Joi.object(mutate(MODEL, [
'config', 'name', 'version', 'description', 'maintainer'
], ['labels'])).label('Create Template'),

/**
* Properties for template that will be passed during a UPDATE requeste
*
* @property update
* @type {Joi}
*/
update: Joi.object(mutate(MODEL, [], ['labels']))
.label('Update Template'),

/**
* List of fields that determine a unique row
*
* @property keys
* @type {Array}
*/
keys: ['name', 'version'],

/**
* List of all fields in the model
* @property allKeys
* @type {Array}
*/
allKeys: Object.keys(MODEL),

/**
* List of indexes to create in the datastore
*
* @property indexes
* @type {Array}
*/
indexes: ['name'],

/**
* Primary column to sort queries by.
* This defines queries to optionally sort a query result set by version.
* Each range key matches up with an element in the indexes property
*
* @property rangeKeys
* @type {Array}
*/
rangeKeys: ['version'],

/**
* Tablename to be used in the datastore
*
* @property tableName
* @type {String}
*/
tableName: 'templates'
};
19 changes: 19 additions & 0 deletions test/data/template.create.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Template CREATE Example
name: test/template
version: "1.3"
description: Template for testing
maintainer: foo@bar.com
labels:
- stable
- test
- beta
config:
image: node:6
steps:
- install: npm install
- test: npm test
- echo: echo $FOO
environment:
FOO: bar
secrets:
- NPM_TOKEN
21 changes: 21 additions & 0 deletions test/data/template.get.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Template GET Example
id: 123234135
name: test/template
version: "1.3"
description: Template for testing
maintainer: foo@bar.com
scmUri: github.com:123:master
labels:
- stable
- test
- beta
config:
image: node:6
steps:
- install: npm install
- test: npm test
- echo: echo $FOO
environment:
FOO: bar
secrets:
- NPM_TOKEN
4 changes: 4 additions & 0 deletions test/data/template.update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Template UPDATE example
labels:
- stable
- test
21 changes: 21 additions & 0 deletions test/data/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Base Template Example
id: 123234135
name: test/template
version: "1.3"
description: Template for testing
maintainer: foo@bar.com
scmUri: github.com:123:master
labels:
- stable
- test
- beta
config:
image: node:6
steps:
- install: npm install
- test: npm test
- echo: echo $FOO
environment:
FOO: bar
secrets:
- NPM_TOKEN
43 changes: 43 additions & 0 deletions test/models/template.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const assert = require('chai').assert;
const models = require('../../').models;
const validate = require('../helper').validate;

describe('model template', () => {
describe('base', () => {
it('validates the base', () => {
assert.isNull(validate('template.yaml', models.template.base).error);
});
});

describe('create', () => {
it('validates the create', () => {
assert.isNull(validate('template.create.yaml', models.template.create).error);
});

it('fails the create', () => {
assert.isNotNull(validate('empty.yaml', models.template.create).error);
});
});

describe('get', () => {
it('validates the get', () => {
assert.isNull(validate('template.get.yaml', models.template.get).error);
});

it('fails the get', () => {
assert.isNotNull(validate('empty.yaml', models.template.get).error);
});
});

describe('update', () => {
it('validates the update', () => {
assert.isNull(validate('template.update.yaml', models.template.update).error);
});

it('fails the update', () => {
assert.isNotNull(validate('empty.yaml', models.template.update).error);
});
});
});

0 comments on commit 9299370

Please sign in to comment.