Skip to content

Commit

Permalink
match entire body in one operation
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsdennis committed Oct 17, 2023
1 parent d1d376b commit 555050a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 75 deletions.
75 changes: 27 additions & 48 deletions evaluate-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 = [];
Expand Down Expand Up @@ -152,11 +135,7 @@ async function evaluate() {
}
}

function getPullRequestBody(){

}

module.exports = {
evaluate: evaluate,
getDependency: getDependency
getAllDependencies: getAllDependencies
}
45 changes: 18 additions & 27 deletions evaluate-dependencies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}]);
});

0 comments on commit 555050a

Please sign in to comment.