Skip to content

Instantly share code, notes, and snippets.

@chrisdothtml
Created August 14, 2019 19:07
Show Gist options
  • Save chrisdothtml/63ff3f0f601a390c2f2c172e45a7af26 to your computer and use it in GitHub Desktop.
Save chrisdothtml/63ff3f0f601a390c2f2c172e45a7af26 to your computer and use it in GitHub Desktop.
Get fixed issue references from GitHub pull request body
/*
* Get fixed issue references from GitHub pull request body
*/
function getIssueRefs(pullRequestBody, defaultRepoName) {
// ref: https://help.github.com/en/articles/closing-issues-using-keywords
const keywords = 'close|closed|closes|fix|fixed|fixes|resolve|resolved|resolves';
// ref: https://help.github.com/en/articles/autolinked-references-and-urls#issues-and-pull-requests
const refTypes = [
// #000
'#',
// org/repo#000
'([a-z-]+/[a-z-.]+)#',
// GH-000
'GH-',
// https://github.com/org/repo/issues/000
'https:\\/\\/github\\.com\\/([a-z-]+\\/[a-z-.]+)\\/issues\\/',
].join('|');
const regex = new RegExp(`(?:${keywords}) (?:${refTypes})(\\d+)`, 'gi');
const result = [];
let match;
while (match = regex.exec(pullRequestBody)) {
const [, repoName1, repoName2, number] = match;
result.push({
repoName: repoName1 || repoName2 || defaultRepoName,
number: parseInt(number),
});
}
return result;
}
getIssueRefs(
// body of a pull request
'Fixes #1, Resolves bar/repo-name#105\n\nLorem ipsum dolor sit amet',
// repoName used for number-only references (e.g. Fixes #000)
'foo/repo-name',
);
/* returns: [
{number: 1, repoName: 'foo/repo-name'},
{number: 105, repoName: 'bar/repo-name'},
] */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment