diff --git a/lib/getNextJobs.js b/lib/getNextJobs.js index 99eed16..58c49c7 100644 --- a/lib/getNextJobs.js +++ b/lib/getNextJobs.js @@ -21,17 +21,19 @@ const getNextJobs = (workflowGraph, config) => { } workflowGraph.edges.forEach((edge) => { - // Check if edge src is specific branch commit with regexp - const edgeSrcBranchRegExp = new RegExp('^~commit:/(.+)/$'); + // Check if edge src is specific branch commit or pr with regexp + const edgeSrcBranchRegExp = new RegExp('^~(pr|commit):/(.+)/$'); const edgeSrcBranch = edge.src.match(edgeSrcBranchRegExp); if (edgeSrcBranch) { - // Check if trigger is specific branch commit - const triggerBranchRegExp = new RegExp('^~commit:(.+)$'); + // Check if trigger is specific branch commit or pr + const triggerBranchRegExp = new RegExp('^~(pr|commit):(.+)$'); const triggerBranch = config.trigger.match(triggerBranchRegExp); - if (triggerBranch) { - if (triggerBranch[1].match(edgeSrcBranch[1])) { + // Check whether job types of trigger and edge src match + if (triggerBranch && triggerBranch[1] === edgeSrcBranch[1]) { + // Check if trigger branch and edge src branch regex match + if (triggerBranch[2].match(edgeSrcBranch[2])) { jobs.add(edge.dest); } } diff --git a/test/lib/getNextJobs.test.js b/test/lib/getNextJobs.test.js index a3a40e8..88d9c74 100644 --- a/test/lib/getNextJobs.test.js +++ b/test/lib/getNextJobs.test.js @@ -52,7 +52,10 @@ describe('getNextJobs', () => { { src: '~commit', dest: 'a' }, { src: '~commit:foo', dest: 'b' }, { src: '~commit:/foo-/', dest: 'c' }, - { src: '~commit:/^bar-.*$/', dest: 'd' } + { src: '~commit:/^bar-.*$/', dest: 'd' }, + { src: '~pr:foo', dest: 'e' }, + { src: '~pr:/foo-/', dest: 'f' }, + { src: '~pr:/^bar-.*$/', dest: 'g' } ] }; @@ -66,5 +69,14 @@ describe('getNextJobs', () => { // trigger "bar-foo-prod" branch commit assert.deepEqual(getNextJobs(specificBranchWorkflow, { trigger: '~commit:bar-foo-prod' }), ['c', 'd']); + // trigger by a pull request on "foo" branch + assert.deepEqual(getNextJobs(specificBranchWorkflow, { trigger: '~pr:foo', prNum: '123' }), + ['e']); + // trigger by a pull request on "foo-bar-dev" branch + assert.deepEqual(getNextJobs(specificBranchWorkflow, { trigger: '~pr:foo-bar-dev', + prNum: '123' }), ['f']); + // trigger by a pull request on "bar-foo-prod" branch + assert.deepEqual(getNextJobs(specificBranchWorkflow, { trigger: '~pr:bar-foo-prod', + prNum: '123' }), ['f', 'g']); }); });