-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
const getNextJobs = require('./getNextJobs'); | ||
|
||
/** | ||
* Recursively verify if there is a cycle starting from job | ||
* @method walk | ||
* @param {Object} workflowGraph Directed graph representation of workflow | ||
* @param {String} jobName Job Name | ||
* @param {Set} visitedJobs Unique list of visited jobs | ||
* @return {Boolean} True if a cycle detected | ||
*/ | ||
const walk = (workflowGraph, jobName, visitedJobs) => { | ||
visitedJobs.add(jobName); | ||
|
||
const triggerList = getNextJobs(workflowGraph, { trigger: jobName, prNum: 1 }); | ||
|
||
// Hit a leaf node, must be good | ||
if (triggerList.length === 0) { | ||
return false; | ||
} | ||
|
||
// Check to see if we visited this job before | ||
if (triggerList.some(name => visitedJobs.has(name))) { | ||
return true; | ||
} | ||
|
||
// recursively walk starting from the new jobs | ||
return triggerList.some(name => walk(workflowGraph, name, visitedJobs)); | ||
}; | ||
|
||
/** | ||
* Calculate if the workflow contains a cycle, e.g. A -> B -> A | ||
* @method hasCycle | ||
* @param {Object} workflowGraph Graph representation of workflow | ||
* @return {Boolean} True if a cycle exists anywhere in the workflow | ||
*/ | ||
const hasCycle = workflowGraph => | ||
// Check from all the nodes to capture deteached workflows | ||
workflowGraph.nodes.some(node => walk(workflowGraph, node.name, new Set())); | ||
|
||
module.exports = hasCycle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict'; | ||
|
||
const assert = require('chai').assert; | ||
const hasCycle = require('../../lib/hasCycle'); | ||
|
||
describe('hasCyles', () => { | ||
it('should return true if a workflow has a cycle', () => { | ||
const workflow = { | ||
nodes: [ | ||
{ name: '~pr' }, | ||
{ name: '~commit' }, | ||
{ name: 'A' }, | ||
{ name: 'B' }, | ||
{ name: 'C' }, | ||
{ name: 'D' }, | ||
{ name: 'E' }, | ||
{ name: 'F' } | ||
], | ||
edges: [ | ||
{ src: '~commit', dest: 'A' }, // start | ||
{ src: 'A', dest: 'B' }, // parallel | ||
{ src: 'A', dest: 'C' }, // parallel | ||
{ src: 'B', dest: 'D' }, // serial | ||
{ src: 'C', dest: 'E' }, // serial | ||
{ src: 'D', dest: 'F', join: true }, // join | ||
{ src: 'E', dest: 'F', join: true }, // join | ||
{ src: 'F', dest: 'A' } // cycle | ||
] | ||
}; | ||
|
||
assert.isTrue(hasCycle(workflow)); | ||
}); | ||
|
||
it('should return true if a detached workflow has a cycle', () => { | ||
const workflow = { | ||
nodes: [ | ||
{ name: '~pr' }, | ||
{ name: '~commit' }, | ||
{ name: 'A' }, | ||
{ name: 'B' }, | ||
{ name: 'C' }, | ||
{ name: 'D' } | ||
], | ||
edges: [ | ||
{ src: '~commit', dest: 'A' }, // start | ||
{ src: 'A', dest: 'B' }, // end | ||
{ src: 'C', dest: 'D' }, // detached start | ||
{ src: 'D', dest: 'C' } // detached cycle | ||
] | ||
}; | ||
|
||
assert.isTrue(hasCycle(workflow)); | ||
}); | ||
|
||
it('should return true if a job requires itself', () => { | ||
const workflow = { | ||
nodes: [ | ||
{ name: '~pr' }, | ||
{ name: '~commit' }, | ||
{ name: 'A' } | ||
], | ||
edges: [ | ||
{ src: '~commit', dest: 'A' }, // start | ||
{ src: 'A', dest: 'A' } // cycle | ||
] | ||
}; | ||
|
||
assert.isTrue(hasCycle(workflow)); | ||
}); | ||
|
||
it('should return false if a workflow does not have a cycle', () => { | ||
const workflow = { | ||
nodes: [ | ||
{ name: '~pr' }, | ||
{ name: '~commit' }, | ||
{ name: 'A' }, | ||
{ name: 'B' } | ||
], | ||
edges: [ | ||
{ src: '~commit', dest: 'A' }, // start | ||
{ src: 'A', dest: 'B' } // end | ||
] | ||
}; | ||
|
||
assert.isFalse(hasCycle(workflow)); | ||
}); | ||
}); |