-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from zeebe-io/referred-issues
Referred issues placeholder
- Loading branch information
Showing
7 changed files
with
260 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import dedent from "dedent"; | ||
import { getMentionedIssueRefs } from "../utils"; | ||
|
||
describe("get mentioned issues", () => { | ||
describe("returns an empty list", () => { | ||
it("for an empty text", () => { | ||
expect(getMentionedIssueRefs("")).toHaveLength(0); | ||
}); | ||
|
||
it("for a text without mentioned issues", () => { | ||
expect(getMentionedIssueRefs(text({}))).toHaveLength(0); | ||
}); | ||
|
||
it("for a text with an issue reference as part of a word", () => { | ||
expect(getMentionedIssueRefs(text({ part: "#123" }))).toHaveLength(0); | ||
}); | ||
|
||
it("for a text with an external issue reference as part of a word", () => { | ||
expect( | ||
getMentionedIssueRefs(text({ part: "zeebe-io/zeebe#123" })) | ||
).toHaveLength(0); | ||
}); | ||
|
||
it("for a text with an issue url as part of a word", () => { | ||
expect( | ||
getMentionedIssueRefs( | ||
text({ part: "github.com/zeebe-io/backport-action/issues/123" }) | ||
) | ||
).toHaveLength(0); | ||
}); | ||
}); | ||
|
||
describe("returns a single reference", () => { | ||
it("for a text with an issue reference at the start", () => { | ||
expect(getMentionedIssueRefs(text({ start: "#123" }))).toEqual(["#123"]); | ||
}); | ||
it("for a text with an issue reference in the middle", () => { | ||
expect(getMentionedIssueRefs(text({ middle: "#123" }))).toEqual(["#123"]); | ||
}); | ||
it("for a text with an issue reference at the end", () => { | ||
expect(getMentionedIssueRefs(text({ end: "#123" }))).toEqual(["#123"]); | ||
}); | ||
|
||
it("for a text with an external issue reference at the start", () => { | ||
expect( | ||
getMentionedIssueRefs(text({ start: "zeebe-io/zeebe#123" })) | ||
).toEqual(["zeebe-io/zeebe#123"]); | ||
}); | ||
it("for a text with an external issue reference in the middle", () => { | ||
expect( | ||
getMentionedIssueRefs(text({ middle: "zeebe-io/zeebe#123" })) | ||
).toEqual(["zeebe-io/zeebe#123"]); | ||
}); | ||
it("for a text with an external issue reference at the end", () => { | ||
expect( | ||
getMentionedIssueRefs(text({ end: "zeebe-io/zeebe#123" })) | ||
).toEqual(["zeebe-io/zeebe#123"]); | ||
}); | ||
|
||
it("for a text with an issue url at the start", () => { | ||
expect( | ||
getMentionedIssueRefs( | ||
text({ start: "github.com/zeebe-io/backport-action/issues/123/" }) | ||
) | ||
).toEqual(["zeebe-io/backport-action#123"]); | ||
}); | ||
it("for a text with an issue url in the middle", () => { | ||
expect( | ||
getMentionedIssueRefs( | ||
text({ middle: "github.com/zeebe-io/backport-action/issues/123" }) | ||
) | ||
).toEqual(["zeebe-io/backport-action#123"]); | ||
}); | ||
it("for a text with an issue url at the end", () => { | ||
expect( | ||
getMentionedIssueRefs( | ||
text({ end: "github.com/zeebe-io/backport-action/issues/123" }) | ||
) | ||
).toEqual(["zeebe-io/backport-action#123"]); | ||
}); | ||
}); | ||
|
||
describe("returns all references", () => { | ||
it("for a text with an issue reference at the start, middle and end", () => { | ||
expect( | ||
getMentionedIssueRefs( | ||
text({ start: "#123", middle: "#234", end: "#345" }) | ||
) | ||
).toEqual(["#123", "#234", "#345"]); | ||
}); | ||
it("for a text with an external issue reference at the start, middle and end", () => { | ||
expect( | ||
getMentionedIssueRefs( | ||
text({ | ||
start: "zeebe-io/zeebe#123", | ||
middle: "zeebe-io/zeebe#234", | ||
end: "zeebe-io/zeebe#345", | ||
}) | ||
) | ||
).toEqual([ | ||
"zeebe-io/zeebe#123", | ||
"zeebe-io/zeebe#234", | ||
"zeebe-io/zeebe#345", | ||
]); | ||
}); | ||
it("for a text with an issue url at the start, middle and end", () => { | ||
const base = "github.com/zeebe-io/backport-action/issues/"; | ||
expect( | ||
getMentionedIssueRefs( | ||
text({ start: `${base}123`, middle: `${base}234`, end: `${base}345` }) | ||
) | ||
).toEqual([ | ||
"zeebe-io/backport-action#123", | ||
"zeebe-io/backport-action#234", | ||
"zeebe-io/backport-action#345", | ||
]); | ||
}); | ||
it("for a text with an external issue url at the start, middle and end", () => { | ||
const base = "github.com/zeebe-io/zeebe/issues/"; | ||
expect( | ||
getMentionedIssueRefs( | ||
text({ start: `${base}123`, middle: `${base}234`, end: `${base}345` }) | ||
) | ||
).toEqual([ | ||
"zeebe-io/zeebe#123", | ||
"zeebe-io/zeebe#234", | ||
"zeebe-io/zeebe#345", | ||
]); | ||
}); | ||
}); | ||
|
||
// todo deal with urls to unrelated repos | ||
}); | ||
|
||
function text({ | ||
start = "", | ||
middle = "", | ||
end = "", | ||
part = "", | ||
}: { | ||
start?: string; | ||
middle?: string; | ||
end?: string; | ||
part?: string; | ||
}) { | ||
return dedent`${start ?? ""} foo bar | ||
bar bar ${middle ?? ""} bar | ||
foo/${part ?? ""} foo${part ?? ""}foo ${part ?? ""}foo | ||
foo bar bar foo ${end ?? ""}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const patterns = { | ||
// matches urls to github issues at start, middle, end of line as individual word | ||
// may be lead and trailed by whitespace which should be trimmed | ||
// captures the `org`, `repo` and `number` of the issue | ||
// https://regex101.com/r/XKRl8q/5 | ||
url: { | ||
global: | ||
/(?:^| )(?:(?:https:\/\/)?(?:www\.)?github\.com\/(?<org>[^ \/\n]+)\/(?<repo>[^ \/\n]+)\/issues\/(?<number>[0-9]+)(?:\/)?)(?: |$)/gm, | ||
first: | ||
/(?:^| )(?:(?:https:\/\/)?(?:www\.)?github\.com\/(?<org>[^ \/\n]+)\/(?<repo>[^ \/\n]+)\/issues\/(?<number>[0-9]+)(?:\/)?)(?: |$)/m, | ||
}, | ||
|
||
// matches `#123` at start, middle, end of line as individual word | ||
// may be lead and trailed by whitespace which should be trimmed | ||
// captures `number` of the issue (and optionally the `org` and `repo`) | ||
// https://regex101.com/r/2gAB8O/2 | ||
ref: /(?:^| )((?<org>[^\n #\/]+)\/(?<repo>[^\n #\/]+))?#(?<number>[0-9]+)(?: |$)/gm, | ||
}; | ||
|
||
/** | ||
* @param body Text in which to search for mentioned issues | ||
* @returns All found mentioned issues as GitHub issue references | ||
*/ | ||
export function getMentionedIssueRefs(body: string): string[] { | ||
const issueUrls = | ||
body.match(patterns.url.global)?.map((url) => toRef(url)) ?? []; | ||
const issueRefs = body.match(patterns.ref) ?? []; | ||
return issueUrls.concat(issueRefs).map((ref) => ref.trim()); | ||
} | ||
|
||
const toRef = (url: string) => { | ||
// matchAll is not yet available to directly access the captured groups of all matches | ||
// so this maps the urls to GitHub refs by matching again without the global flag | ||
const result = patterns.url.first.exec(url); | ||
if (!result) { | ||
console.error( | ||
`Expected to transform url (${url}) to GitHub reference, but it did not match pattern'` | ||
); | ||
return ""; | ||
} | ||
const [, org, repo, number] = result; | ||
return `${org}/${repo}#${number}`; | ||
}; |