Skip to content

Commit

Permalink
feat(3112): Add support to start an event from a stage (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagar1312 authored Jul 5, 2024
1 parent 4d6d9e7 commit 8291226
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/eventFactory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { TRIGGER, EXTERNAL_TRIGGER, COMMIT_TRIGGER, PR_TRIGGER, RELEASE_TRIGGER, TAG_TRIGGER } =
const { TRIGGER, EXTERNAL_TRIGGER, COMMIT_TRIGGER, PR_TRIGGER, RELEASE_TRIGGER, TAG_TRIGGER, QUALIFIED_STAGE_NAME } =
require('screwdriver-data-schema').config.regex;
const workflowParser = require('screwdriver-workflow-parser');
const logger = require('screwdriver-logger');
Expand Down Expand Up @@ -327,13 +327,18 @@ function createBuilds(config) {
releaseName,
ref
} = config;
const { startFrom } = eventConfig;
let { startFrom } = eventConfig;
let rootDir = '';

if (!startFrom) {
return null;
}

// If startFrom is a stage, point it to stage setup job
if (QUALIFIED_STAGE_NAME.test(startFrom)) {
startFrom = `${startFrom}:setup`;
}

return Promise.all([pipeline.branch, pipeline.getJobs(), pipeline.rootDir])
.then(([branch, jobs, root]) => {
rootDir = root;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"screwdriver-config-parser": "^10.0.0",
"screwdriver-data-schema": "^23.0.4",
"screwdriver-data-schema": "^23.4.0",
"screwdriver-logger": "^2.0.0",
"screwdriver-workflow-parser": "^4.1.0"
}
Expand Down
120 changes: 120 additions & 0 deletions test/lib/eventFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ describe('Event Factory', () => {
{ name: '~commit' },
{ name: 'main' },
{ name: 'disabledJob' },
{ name: 'stage@integration:setup' },
{ name: 'int-deploy' },
{ name: 'int-test' },
{ name: 'int-certify' },
{ name: 'stage@integration:teardown' },
{ name: 'publish' },
{ name: '~sd@123:main' },
{ name: '~commit:branch' },
Expand All @@ -189,6 +194,11 @@ describe('Event Factory', () => {
{ src: '~pr', dest: 'main' },
{ src: '~commit', dest: 'main' },
{ src: 'main', dest: 'disabledJob' },
{ src: 'main', dest: 'stage@integration:setup' },
{ src: 'stage@integration:setup', dest: 'int-deploy' },
{ src: 'int-deploy', dest: 'int-test' },
{ src: 'int-test', dest: 'int-certify' },
{ src: 'int-certify', dest: 'stage@integration:teardown' },
{ src: '~pr', dest: 'publish' },
{ src: '~pr', dest: 'pr-only' },
{ src: '~commit', dest: 'only-commit' },
Expand Down Expand Up @@ -221,6 +231,11 @@ describe('Event Factory', () => {
{ name: '~commit' },
{ name: 'main' },
{ name: 'disabledJob' },
{ name: 'stage@integration:setup' },
{ name: 'int-deploy' },
{ name: 'int-test' },
{ name: 'int-certify' },
{ name: 'stage@integration:teardown' },
{ name: 'publish' },
{ name: '~sd@123:main' },
{ name: '~commit:branch' },
Expand All @@ -235,6 +250,11 @@ describe('Event Factory', () => {
{ src: '~pr', dest: 'main' },
{ src: '~commit', dest: 'main' },
{ src: 'main', dest: 'disabledJob' },
{ src: 'main', dest: 'stage@integration:setup' },
{ src: 'stage@integration:setup', dest: 'int-deploy' },
{ src: 'int-deploy', dest: 'int-test' },
{ src: 'int-test', dest: 'int-certify' },
{ src: 'int-certify', dest: 'stage@integration:teardown' },
{ src: '~pr', dest: 'publish' },
{ src: '~pr', dest: 'pr-only' },
{ src: '~commit', dest: 'only-commit' },
Expand Down Expand Up @@ -385,6 +405,66 @@ describe('Event Factory', () => {
state: 'ENABLED',
parsePRJobName: sinon.stub().returns('main'),
isPR: sinon.stub().returns(true)
},
{
id: 11,
pipelineId: 8765,
name: 'stage@integration:setup',
permutations: [
{
requires: ['main']
}
],
state: 'ENABLED',
isPR: sinon.stub().returns(false)
},
{
id: 12,
pipelineId: 8765,
name: 'int-deploy',
permutations: [
{
requires: ['stage@integration:setup']
}
],
state: 'ENABLED',
isPR: sinon.stub().returns(false)
},
{
id: 13,
pipelineId: 8765,
name: 'int-test',
permutations: [
{
requires: ['int-deploy']
}
],
state: 'ENABLED',
isPR: sinon.stub().returns(false)
},
{
id: 14,
pipelineId: 8765,
name: 'int-certify',
permutations: [
{
requires: ['int-test']
}
],
state: 'ENABLED',
isPR: sinon.stub().returns(false)
},
{
id: 15,
pipelineId: 8765,
name: 'stage@integration:teardown',
permutations: [
{
requires: ['int-certify']
}
],
state: 'ENABLED',
isPR: sinon.stub().returns(false)
}
];

Expand Down Expand Up @@ -1143,6 +1223,46 @@ describe('Event Factory', () => {
});
});

it('should create build, if startFrom is a stage setup job name', () => {
config.startFrom = 'stage@integration:setup';

return eventFactory.create(config).then(model => {
assert.instanceOf(model, Event);
assert.notCalled(jobFactoryMock.create);
assert.notCalled(syncedPipelineMock.syncPR);
assert.calledOnce(pipelineMock.sync);
assert.calledOnce(buildFactoryMock.create);
assert.calledWith(
buildFactoryMock.create,
sinon.match({
parentBuildId: 12345,
eventId: model.id,
jobId: 11
})
);
});
});

it('should create build for stage setup job, if startFrom is a stage name', () => {
config.startFrom = 'stage@integration';

return eventFactory.create(config).then(model => {
assert.instanceOf(model, Event);
assert.notCalled(jobFactoryMock.create);
assert.notCalled(syncedPipelineMock.syncPR);
assert.calledOnce(pipelineMock.sync);
assert.calledOnce(buildFactoryMock.create);
assert.calledWith(
buildFactoryMock.create,
sinon.match({
parentBuildId: 12345,
eventId: model.id,
jobId: 11
})
);
});
});

it('should create build if startFrom is ~release', () => {
const releaseWorkflow = {
nodes: [
Expand Down

0 comments on commit 8291226

Please sign in to comment.