Exclude punctuation surrounding URL references (#183)

Summary:
To avoid confusion, we simultaneously remove unused capturing groups.
This is not strictly necessary, but it makes the code less brittle.

Test Plan:
The newly added test fails before the change to `findReferences.js`.

wchargin-branch: url-punctuation
This commit is contained in:
William Chargin 2018-05-01 14:51:18 -07:00 committed by GitHub
parent acf5000547
commit ee03c58357
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 6 deletions

View File

@ -35,20 +35,24 @@ function findUsernameReferences(body: string): string[] {
}
function findGithubUrlReferences(body: string): string[] {
const githubNamePart = /([a-zA-Z0-9_-]+)/.source;
const githubNamePart = /(?:[a-zA-Z0-9_-]+)/.source;
const urlRegex = new RegExp(
"" +
/(?:\W|^)http(?:s)?:\/\/github.com\//.source +
/(?:\W|^)/.source +
"(" +
/http(?:s)?:\/\/github.com\//.source +
githubNamePart +
"(?:" +
/\//.source +
githubNamePart +
/\/(issues|pull)\//.source +
/(\d+)/.source +
/(#(issue|issuecomment|pullrequestreview|discussion_r)-?(\d+))?/.source +
/\/(?:issues|pull)\//.source +
/(?:\d+)/.source +
/(?:#(?:issue|issuecomment|pullrequestreview|discussion_r)-?(?:\d+))?/
.source +
")?" +
")" +
/(?:[^\w/]|$)/.source,
"gm"
);
return findAllMatches(urlRegex, body).map((match) => match[0].trim());
return findAllMatches(urlRegex, body).map((match) => match[1]);
}

View File

@ -91,6 +91,13 @@ https://github.com/sourcecred/example-repo/pull/3#issuecomment-369162222
).toHaveLength(1);
});
it("allows but excludes leading and trailing punctuation", () => {
const base = "https://github.com/sourcecred/sourcecred/pull/94";
expect(findReferences(`!${base}`)).toEqual([base]);
expect(findReferences(`${base}!`)).toEqual([base]);
expect(findReferences(`!${base}!`)).toEqual([base]);
});
it("finds username references", () => {
expect(findReferences("hello to @wchargin from @decentralion!")).toEqual([
"@wchargin",