Skip to content

Commit

Permalink
notary-rest validation up & running through notary-hive
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahm committed Oct 3, 2017
1 parent af1c0e5 commit 5c853e7
Show file tree
Hide file tree
Showing 14 changed files with 1,529 additions and 182 deletions.
10 changes: 9 additions & 1 deletion packages/notary-core/src/contracts/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ export default class Contract {
this.validate();

if (!this.name) {
this.name = this.meta.name ? this.meta.name : `default__${this.type}__${this.integrationType}`;
if (this.meta.name) {
this.name = this.meta.name
} else {
if (this.type === 'expectation') {
this.name = `expectation_upstream_${Buffer.from(`${this.upstream.repo}|${this.upstream.dir}`).toString('base64')}__${this.integrationType}`;
} else {
this.name = `promise__default__${this.integrationType}`;
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/notary-core/src/contracts/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default {
err,
`Failed to fetch contracts for workspace: ${workspace.project.repo}/${workspace.project
.dir}` +
`@ ${workspace.rev}` +
` @ ${workspace.rev}` +
err.message
);
}
Expand Down
107 changes: 79 additions & 28 deletions packages/notary-core/src/contracts/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ProjectRevisionRepository from '../projects/project_revision_repository';
import Contract from './contract';
import { Project } from '../projects/models';
import Definitions from './definition-validator';
import Integrations from './integrations';
import config from '../config';

/**
* Two cases, a project might:
Expand Down Expand Up @@ -75,15 +75,28 @@ async function contractsPathsValidation(def, dirBasePath) {
async function contractsSchemaValidation(projectRevision) {
return Promise.all(
projectRevision.contracts.map(async c => {
const facade = Integrations.get(c.integrationType);
return await facade.validateContractSchema(projectRevision, c).catch(err => {
const response = await config.hive.publish(`CONTRACT_VALIDATE_SCHEMA_${c.integrationType.toUpperCase()}`, {
projectDisplayName: `${projectRevision.project().repo}:${projectRevision.project().dir}`,
projectId: Buffer.from(`${projectRevision.project().repo}|${projectRevision.project().dir}`).toString('base64'),
revision: projectRevision.rev(),
contractDefinition: c
}, false, 'notary-core');

let err;
if (!response || response.length === 0) {
err = `No plugin available to validate ${c.integrationType}`;
} else if(response[0] && response[0].errors !== null) {
err = response[0].errors;
}

if (err) {
throw new VError(
`Error! contract at ${projectRevision.project().repo}:` +
`${projectRevision.project()
.dir}/${c.dir} of type ${c.integrationType} is not valid: ` +
err.message
`${projectRevision.project()
.dir}/${c.dir} of type ${c.integrationType} is not valid: ` +
err
);
});
}
})
);
}
Expand Down Expand Up @@ -136,21 +149,39 @@ async function producerPromisesValidation(projectRevision, def) {
})
.filter(pair => pair !== null)
.map(async pair => {
const facade = Integrations.get(promise.integrationType);

return await facade
.validate(projectRevision, pair.consumerProjectRev, promise, pair.consumerExpectation)
.catch(err => {
throw new VError(
`Consumer [ ${pair.consumerProjectRev.project()
.repo}:${pair.consumerProjectRev.project()
.dir}/ @ ${pair.consumerProjectRev.rev()} ] ` +
`expectations of type (${pair.consumerExpectation
.integrationType}) is broken: \n` +
`============================================================================ \n` +
err.message
);
});
const response = await config.hive.publish(`CONTRACT_VALIDATE_PROMISE_EXPECTATION_${promise.integrationType.toUpperCase()}`, {
promise: {
projectDisplayName: `${projectRevision.project().repo}:${projectRevision.project().dir}`,
projectId: Buffer.from(`${projectRevision.project().repo}|${projectRevision.project().dir}`).toString('base64'),
revision: projectRevision.rev(),
contractDefinition: promise
},
expectation: {
projectDisplayName: `${pair.consumerProjectRev.project().repo}:${pair.consumerProjectRev.project().dir}`,
projectId: Buffer.from(`${pair.consumerProjectRev.project().repo}|${pair.consumerProjectRev.project().dir}`).toString('base64'),
revision: pair.consumerProjectRev.rev(),
contractDefinition: pair.consumerExpectation
}
}, false, 'notary-core');

let err;
if (!response || response.length === 0) {
err = `No plugin available to validate ${promise.integrationType}`;
} else if(response[0] && response[0].errors !== null) {
err = response[0].errors;
}

if (err) {
throw new VError(
`Consumer [ ${pair.consumerProjectRev.project()
.repo}:${pair.consumerProjectRev.project()
.dir}/ @ ${pair.consumerProjectRev.rev()} ] ` +
`expectations of type (${pair.consumerExpectation
.integrationType}) is broken: \n` +
`============================================================================ \n` +
err
);
}
})
);
})
Expand Down Expand Up @@ -196,17 +227,37 @@ async function consumerExpectationsValidation(projectRevision, def) {
});
}

const facade = Integrations.get(e.integrationType);
const response = await config.hive.publish(`CONTRACT_VALIDATE_PROMISE_EXPECTATION_${e.integrationType.toUpperCase()}`, {
promise: {
projectDisplayName: `${upstream.project().repo}:${upstream.project().dir}`,
projectId: Buffer.from(`${upstream.project().repo}|${upstream.project().dir}`).toString('base64'),
revision: upstream.rev(),
contractDefinition: upstreamPromise
},
expectation: {
projectDisplayName: `${projectRevision.project().repo}:${projectRevision.project().dir}`,
projectId: Buffer.from(`${projectRevision.project().repo}|${projectRevision.project().dir}`).toString('base64'),
revision: projectRevision.rev(),
contractDefinition: e
}
}, false, 'notary-core');

return await facade.validate(upstream, projectRevision, upstreamPromise, e).catch(err => {
let err;
if (!response || response.length === 0) {
err = `No plugin available to validate ${e.integrationType}`;
} else if(response[0] && response[0].errors !== null) {
err = response[0].errors;
}

if (err) {
throw new VError(
`Producer [ ${upstream.project().repo}:${upstream.project()
.dir}/ @ ${upstream.rev()} ] ` +
`expectations of type (${e.integrationType}) is broken: \n` +
`============================================================================ \n` +
err.message
`expectations of type (${e.integrationType}) is broken: \n` +
`============================================================================ \n` +
err
);
});
}
})
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/notary-core/src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ server.post('/projects/:id/revisions/:revision', async (req, res) => {
const [repo, dir] = Buffer.from(req.params['id'], 'base64').toString().split('|');
await syncHelper.syncProjectWorkspace(
new ProjectWorkspace({project: new Project({repo, dir}), rev: req.params['revision']}),
true
false
);

res.redirect(`/projects/${req.params['id']}/revisions/${req.params['revision']}`);
Expand Down
Loading

0 comments on commit 5c853e7

Please sign in to comment.