Skip to content

Commit

Permalink
add doc'
Browse files Browse the repository at this point in the history
  • Loading branch information
d2lam committed Oct 4, 2019
1 parent 06b0e35 commit 0f33d35
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
26 changes: 18 additions & 8 deletions lib/getWorkflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ const filterNodeName = name =>
/**
* Get the list of nodes for the graph
* @method calculateNodes
* @param {Object} jobs Hash of job configs
* @param {Object} jobs Hash of job configs
* @param {TriggerFactory} triggerFactory Trigger Factory to find external triggers
* @param {Number} pipelineId Id of the current pipeline
* @return {Array} List of nodes (jobs)
*/
const calculateNodes = (jobs, triggerFactory) => {
const calculateNodes = async (jobs, triggerFactory, pipelineId) => {
console.log(triggerFactory);
const nodes = new Set(['~pr', '~commit']);

Expand All @@ -30,6 +31,15 @@ const calculateNodes = (jobs, triggerFactory) => {
return [...nodes].map(name => ({ name }));
};

/**
* Build external edges for its downstream jobs, DFS to traverse its children
* @method buildExternalEdge
* @param {String} root Source name. ex `sd@123:main`
* @param {Array} children Array of downstream jobs. ex: [sd@456:test, sd@789:test]
* @param {Object} edges List of graph edges { src, dest }
* @param {TriggerFactory} triggerFactory triggerFactory to figure out its downstream jobs
* @return {Promise} List of graph edges { src, dest }
*/
const buildExternalEdge = async (root, children, edges, triggerFactory) => {
await Promise.all(children.map(async (dest) => {
edges.push({ src: root, dest });
Expand Down Expand Up @@ -101,20 +111,20 @@ const calculateEdges = async (jobs, triggerFactory, pipelineId) => {
/**
* Given a pipeline config, return a directed graph configuration that describes the workflow
* @method getWorkflow
* @param {Object} pipelineConfig A Pipeline Config
* @param {Object} pipelineConfig.jobs Hash of job configs
* @param {TriggerFactory} triggerFactory Trigger Factory to find external triggers
* @param {Number} pipelineId Id of the current pipeline
* @return {Object} List of nodes and edges { nodes, edges }
* @param {Object} pipelineConfig A Pipeline Config
* @param {Object} pipelineConfig.jobs Hash of job configs
* @param {TriggerFactory} triggerFactory Trigger Factory to find external triggers
* @param {Number} pipelineId Id of the current pipeline
* @return {Object} List of nodes and edges {nodes, edges}
*/
const getWorkflow = async (pipelineConfig, triggerFactory, pipelineId) => {
const jobConfig = pipelineConfig.jobs;

if (!jobConfig) {
throw new Error('No Job config provided');
}
const nodes = await calculateNodes(jobConfig, triggerFactory, pipelineId);
const edges = await calculateEdges(jobConfig, triggerFactory, pipelineId);
const nodes = calculateNodes(jobConfig, triggerFactory, pipelineId);

return { nodes, edges };
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "screwdriver-workflow-parser",
"version": "0.0.1",
"version": "1.0.0",
"description": "Parses and converts pipeline configuration into a workflow",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 8 additions & 2 deletions test/lib/getWorkflow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ describe('getWorkflow', () => {
/* A -> B --> C
sd@111:external-level1 -> sd@222:external-level2
sd@333:external-level1 -> sd@444:external-level2
sd@555:external-level2
*/

triggerFactoryMock.getDestFromSrc.withArgs('sd@123:A').resolves([
Expand All @@ -184,7 +185,10 @@ describe('getWorkflow', () => {
'sd@222:external-level2'
]);
triggerFactoryMock.getDestFromSrc.withArgs('sd@333:external-level1').resolves([
'sd@444:external-level2'
'sd@444:external-level2', 'sd@555:external-level2'
]);
triggerFactoryMock.getDestFromSrc.withArgs('sd@555:external-level2').resolves([
'sd@666:external-level3'
]);

const result = await getWorkflow({
Expand Down Expand Up @@ -220,7 +224,9 @@ describe('getWorkflow', () => {
{ src: 'A', dest: 'sd@111:external-level1' },
{ src: 'A', dest: 'sd@333:external-level1' },
{ src: 'sd@111:external-level1', dest: 'sd@222:external-level2' },
{ src: 'sd@333:external-level1', dest: 'sd@444:external-level2' }
{ src: 'sd@333:external-level1', dest: 'sd@444:external-level2' },
{ src: 'sd@333:external-level1', dest: 'sd@555:external-level2' },
{ src: 'sd@555:external-level2', dest: 'sd@666:external-level3' }
]
});
});
Expand Down

0 comments on commit 0f33d35

Please sign in to comment.