Skip to content

Commit

Permalink
fix(1710): Store jobId for external nodes in workflowgraph (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyi committed Jan 14, 2020
1 parent 9a33137 commit 8cd8d7b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
31 changes: 24 additions & 7 deletions lib/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,22 +810,39 @@ class PipelineModel extends BaseModel {
})
// wait until all promises have resolved
.then((updatedJobs) => {
// Add jobId to workflowGraph.nodes
const nodes = this.workflowGraph.nodes;

// Add jobId to workflowGraph.nodes
nodes.forEach((node) => {
// Handle external nodes
if (/sd@/.test(node.name)) {
const pipelineFactory = this._getPipelineFactory();
const [, externalPipelineId, externalJobName] =
/sd@(\d+):([\w-]+)$/.exec(node.name);

return pipelineFactory.get(externalPipelineId).then((externalPipeline) => {
const externalJobId = externalPipeline.workflowGraph.nodes
.find(n => n.name === externalJobName).id;

node.id = externalJobId;
}).then(() => {
delete this.jobs;

return this.update();
});
}
const job = updatedJobs.find(j => j.name === node.name);

// Handle internal nodes
if (job) {
node.id = job.id;
}
});
// jobs updated or new jobs created during sync
// delete it here so next time this.jobs is called a DB query will be forced and new jobs will return
delete this.jobs;

// jobs updated or new jobs created during sync
// delete it here so next time this.jobs is called a DB query will be forced and new jobs will return
delete this.jobs;

return this.update();
return this.update();
});
})
.then(() => this);
}
Expand Down
6 changes: 4 additions & 2 deletions test/data/parser.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@
{ "name": "~pr" },
{ "name": "~commit" },
{ "name": "main" },
{ "name": "publish" }
{ "name": "publish" },
{ "name": "sd@123:main" }
],
"edges": [
{ "src": "~pr", "dest": "main" },
{ "src": "~commit", "dest": "main" },
{ "src": "main", "dest": "publish" }
{ "src": "main", "dest": "publish" },
{ "src": "publish", "dest": "sd@123:main" }
]
},
"annotations": {
Expand Down
40 changes: 37 additions & 3 deletions test/lib/pipeline.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,10 @@ describe('Pipeline Model', () => {
describe('sync', () => {
let publishMock;
let mainMock;
let externalMock;
let mainModelMock;
let publishModelMock;
let externalModelMock;

beforeEach(() => {
datastore.update.resolves(null);
Expand All @@ -414,14 +416,20 @@ describe('Pipeline Model', () => {
name: 'main',
state: 'ENABLED'
};

publishModelMock = {
isPR: sinon.stub().returns(false),
update: sinon.stub(),
id: 2,
name: 'publish',
state: 'ENABLED'
};
externalModelMock = {
isPR: sinon.stub().returns(false),
update: sinon.stub(),
id: 3,
name: 'main',
state: 'ENABLED'
};

publishMock = {
pipelineId: testId,
Expand Down Expand Up @@ -462,6 +470,19 @@ describe('Pipeline Model', () => {
image: 'node:6'
}]
};
externalMock = {
pipelineId: 123,
name: 'main',
permutations: [{
commands: [
{ command: 'npm run bump', name: 'bump' },
{ command: 'npm publish --tag $NODE_TAG', name: 'publish' },
{ command: 'git push origin --tags', name: 'tag' }
],
environment: { NODE_ENV: 'test', NODE_TAG: 'latest' },
image: 'node:4'
}]
};
});

it('create external trigger in datastore for new jobs', () => {
Expand Down Expand Up @@ -529,19 +550,32 @@ describe('Pipeline Model', () => {
jobFactoryMock.list.resolves(jobs);
jobFactoryMock.create.withArgs(mainMock).resolves(mainModelMock);
jobFactoryMock.create.withArgs(publishMock).resolves(publishModelMock);
jobFactoryMock.create.withArgs(externalMock).resolves(externalModelMock);
pipelineFactoryMock.get.resolves({
id: 123,
update: sinon.stub().resolves(null),
remove: sinon.stub().resolves(null),
workflowGraph: { nodes: [
{ name: '~pr' },
{ name: '~commit' },
{ name: 'main', id: 3 }
] }
});

return pipeline.sync().then(() => {
assert.deepEqual(pipeline.workflowGraph, {
nodes: [
{ name: '~pr' },
{ name: '~commit' },
{ name: 'main', id: 1 },
{ name: 'publish', id: 2 }
{ name: 'publish', id: 2 },
{ name: 'sd@123:main', id: 3 }
],
edges: [
{ src: '~pr', dest: 'main' },
{ src: '~commit', dest: 'main' },
{ src: 'main', dest: 'publish' }
{ src: 'main', dest: 'publish' },
{ src: 'publish', dest: 'sd@123:main' }
]
});
});
Expand Down

0 comments on commit 8cd8d7b

Please sign in to comment.