diff --git a/evaluate-dependencies.js b/evaluate-dependencies.js index 2a733b3f..14a0e949 100644 --- a/evaluate-dependencies.js +++ b/evaluate-dependencies.js @@ -30,44 +30,34 @@ function extractFromMatch(match) { }; } -function getDependency(line) { - var match = quickLinkRegex.exec(line); - if (match !== null) { - core.info(` Found number-referenced dependency in '${line}'`); - return { - owner: github.context.repo.owner, - repo: github.context.repo.repo, - pull_number: parseInt(match[2], 10) - }; - } - - match = partialLinkRegex.exec(line); - if (match !== null) { - core.info(` Found partial-link dependency in '${line}'`); - return extractFromMatch(match); - } - - match = partialUrlRegex.exec(line); - if (match !== null) { - core.info(` Found partial-url dependency in '${line}'`); - return extractFromMatch(match); - } - - match = fullUrlRegex.exec(line); - if (match !== null) { - core.info(` Found full-url dependency in '${line}'`); - return extractFromMatch(match); +function getAllDependencies(body) { + var allMatches = []; + + var quickLinkMatches = [...body.matchAll(quickLinkRegex)]; + if (quickLinkMatches.length !== 0) { + quickLinkMatches.forEach(match => { + core.info(` Found number-referenced dependency in '${match}'`); + allMatches.push({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + pull_number: parseInt(match[2], 10) + }); + }); } - - match = markdownRegex.exec(line); - if (match !== null) { - core.info(` Found markdown dependency in '${line}'`); - return extractFromMatch(match); + + var extractableMatches = [...body.matchAll(partialLinkRegex)] + .concat([...body.matchAll(partialUrlRegex)]) + .concat([...body.matchAll(fullUrlRegex)]) + .concat([...body.matchAll(markdownRegex)]); + if (extractableMatches.length !== 0) { + extractableMatches.forEach(match => { + core.info(` Found number-referenced dependency in '${match}'`); + allMatches.push(extractFromMatch(match)); + }); } - core.info(` Found no dependency in '${line}'`); - return null; -}; + return allMatches; +} async function evaluate() { try { @@ -87,14 +77,7 @@ async function evaluate() { } core.info('\nReading PR body...'); - const lines = pullRequest.body.split(/\r\n|\r|\n/); - - var dependencies = []; - lines.forEach(l => { - var dependency = getDependency(l); - if (dependency !== null) - dependencies.push(dependency); - }); + var dependencies = getAllDependencies(pullRequest.body); core.info('\nAnalyzing lines...'); var dependencyIssues = []; @@ -152,11 +135,7 @@ async function evaluate() { } } -function getPullRequestBody(){ - -} - module.exports = { evaluate: evaluate, - getDependency: getDependency + getAllDependencies: getAllDependencies } \ No newline at end of file diff --git a/evaluate-dependencies.test.js b/evaluate-dependencies.test.js index 7325b5c2..7db029fa 100644 --- a/evaluate-dependencies.test.js +++ b/evaluate-dependencies.test.js @@ -4,76 +4,67 @@ process.env.GITHUB_REPOSITORY = 'owner/repo'; const shorthand = 'Depends on #14' test('Shorthand', () => { - expect(evaluate.getDependency(shorthand)) - .toStrictEqual({ + expect(evaluate.getAllDependencies(shorthand)) + .toStrictEqual([{ owner: 'owner', repo: 'repo', pull_number: 14 - }); + }]); }); const partialLink = 'Depends on gregsdennis/dependencies-action#5' test('partialLink', () => { - expect(evaluate.getDependency(partialLink)) - .toStrictEqual({ + expect(evaluate.getAllDependencies(partialLink)) + .toStrictEqual([{ owner: 'gregsdennis', repo: 'dependencies-action', pull_number: 5 - }); + }]); }); const shorthandAndPartialLink = `Depends on #14 Depends on gregsdennis/dependencies-action#5` test('shorthandAndPartialLink', () => { - var lines = shorthandAndPartialLink.split(/\r\n|\r|\n/); - expect(evaluate.getDependency(lines[0])) - .toStrictEqual({ + expect(evaluate.getAllDependencies(shorthandAndPartialLink)) + .toStrictEqual([{ owner: 'owner', repo: 'repo', pull_number: 14 - }); - expect(evaluate.getDependency(lines[1])) - .toStrictEqual({ + },{ owner: 'gregsdennis', repo: 'dependencies-action', pull_number: 5 - }); + }]); }); const shorthandAndPartialLinkWithBlankLineAtEnd = `Depends on #14 Depends on gregsdennis/dependencies-action#5 ` test('shorthandAndPartialLinkWithBlankLineAtEnd', () => { - var lines = shorthandAndPartialLinkWithBlankLineAtEnd.split(/\r\n|\r|\n/); - expect(evaluate.getDependency(lines[0])) - .toStrictEqual({ + expect(evaluate.getAllDependencies(shorthandAndPartialLinkWithBlankLineAtEnd)) + .toStrictEqual([{ owner: 'owner', repo: 'repo', pull_number: 14 - }); - expect(evaluate.getDependency(lines[1])) - .toStrictEqual({ + },{ owner: 'gregsdennis', repo: 'dependencies-action', pull_number: 5 - }); + }]); }); const shorthandAndPartialLinkWithBlankLineInMiddle = `Depends on #14 Depends on gregsdennis/dependencies-action#5` test('shorthandAndPartialLinkWithBlankLineInMiddle', () => { - var lines = shorthandAndPartialLinkWithBlankLineInMiddle.split(/\r\n|\r|\n/); - expect(evaluate.getDependency(lines[0])) - .toStrictEqual({ + expect(evaluate.getAllDependencies(shorthandAndPartialLinkWithBlankLineInMiddle)) + .toStrictEqual([{ owner: 'owner', repo: 'repo', pull_number: 14 - }); - expect(evaluate.getDependency(lines[1])) - .toStrictEqual({ + },{ owner: 'gregsdennis', repo: 'dependencies-action', pull_number: 5 - }); + }]); });