Add support for detecting cross-repo references (#459)

In GitHub, you can make cross repo references. For example,
sourcecred/sourcecred#459 is one such reference. This commit adds
support for detecting those references and adding them to the GitHub
graph.

Test plan:
See attached unit tests.
This commit is contained in:
Dandelion Mané 2018-06-29 17:53:15 -07:00 committed by GitHub
parent ba4fa8e820
commit 0b3c91a7bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 4 deletions

View File

@ -12,11 +12,22 @@ export function parseReferences(body: string): ParsedReference[] {
// https://github.com/sourcecred/sourcecred/pull/130#pullrequestreview-113849998
return [
...findNumericReferences(body),
...findRepoNumericReferences(body),
...findGithubUrlReferences(body),
...findUsernameReferences(body),
];
}
function findRepoNumericReferences(body: string): ParsedReference[] {
return findAllMatches(
/(?:\W|^)([a-zA-Z0-9-]+\/[a-zA-Z0-9-]+#\d+)(?:\W|$)/g,
body
).map((x) => ({
refType: "BASIC",
ref: x[1],
}));
}
function findNumericReferences(body: string): ParsedReference[] {
return findAllMatches(/(?:\W|^)(#\d+)(?:\W|$)/g, body).map((x) => ({
refType: "BASIC",

View File

@ -32,10 +32,18 @@ describe("parseReferences", () => {
expect(parseReferences("foo#123 #124bar")).toHaveLength(0);
});
it("does not yet find concise cross-repo links", () => {
// The link below is valid, when we add cross-repo support we
// should fix this test case
expect(parseReferences("sourcecred/sourcecred#12")).toHaveLength(0);
describe("cross-repo links", () => {
const repoRef = "sourcecred/sourcecred#12";
it("a bare link", () => {
expect(parseReferences(repoRef)).toEqual([
{refType: "BASIC", ref: repoRef},
]);
});
it("a link with surrounding context", () => {
expect(parseReferences("please see sourcecred/sourcecred#12")).toEqual([
{refType: "BASIC", ref: repoRef},
]);
});
});
it("finds a trivial url reference", () => {

View File

@ -361,6 +361,10 @@ export class RelationalView {
}
if (e instanceof Issue || e instanceof Pull) {
refToAddress.set(`#${e.number()}`, a);
refToAddress.set(
`${e.parent().owner()}/${e.parent().name()}#${e.number()}`,
a
);
}
}
for (const e of this.textContentEntities()) {