Skip to content

Shorten no-referrer GitHub URLs too #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 16, 2021
42 changes: 39 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const patchDiffRegex = /[.](patch|diff)$/;
const releaseRegex = /^releases[/]tag[/]([^/]+)/;
const labelRegex = /^labels[/]([^/]+)/;
const compareRegex = /^compare[/]([^/]+)/;
const pullRegex = /^pull[/](\d+)[/]([^/]+)(?:[/]([\da-f]{40})[.][.]([\da-f]{40}))?$/;
const pullRegex = /^pull[/](\d+)(?:[/]([^/]+))?(?:[/]([\da-f]{40})[.][.]([\da-f]{40}))?$/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to use named groups here to describe what each group is capturing, even if unused. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes 👍

const issueRegex = /^issues[/](\d+)$/;
const commitRegex = /^commit[/]([\da-f]{40})$/;
const releaseArchiveRegex = /^archive[/](.+)([.]zip|[.]tar[.]gz)/;
const releaseDownloadRegex = /^releases[/]download[/]([^/]+)[/](.+)/;
const dependentsRegex = /^network[/]dependents[/]?$/;
Expand All @@ -23,6 +25,18 @@ function styleRevision(revision) {
return `<code>${revision}</code>`;
}

function commentIndicator(hash) {
if (hash.startsWith('#issue-') || hash.startsWith('#commitcomment-')) {
return ' (comment)';
}

if (hash.startsWith('#pullrequestreview-')) {
return ' (review)';
}

return '';
}

// Filter out null values
function joinValues(array, delimiter = '/') {
return array.filter(s => s).join(delimiter);
Expand Down Expand Up @@ -57,6 +71,11 @@ function shortenURL(href, currentUrl = 'https://github.com') {
'https://rawgit.com',
].includes(origin);

const isRedirection = [
'https://togithub.com', // Renovate
'https://github-redirect.dependabot.com', // Dependabot
].includes(origin);

let [
user,
repo,
Expand All @@ -80,7 +99,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
filePath = filePath.join('/');

const isLocal = origin === currentUrl.origin;
const isThisRepo = (isLocal || isRaw) && currentRepo === `${user}/${repo}`;
const isThisRepo = (isLocal || isRaw || isRedirection) && currentRepo === `${user}/${repo}`;
const isReserved = reservedPaths.includes(user);
const isDependents = dependentsRegex.test(repoPath);
const isDependencies = dependenciesRegex.test(repoPath);
Expand All @@ -91,6 +110,8 @@ function shortenURL(href, currentUrl = 'https://github.com') {
const [, label] = repoPath.match(labelRegex) || [];
const [, compare] = repoPath.match(compareRegex) || [];
const [, pull, pullPage, pullPartialStart, pullPartialEnd] = repoPath.match(pullRegex) || [];
const [, issue] = isRedirection ? repoPath.match(issueRegex) || [] : [];
const [, commit] = isRedirection ? repoPath.match(commitRegex) || [] : [];
const isFileOrDir = revision && [
'raw',
'tree',
Expand All @@ -105,7 +126,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
* Shorten URL
*/

if (isReserved || pathname === '/' || (!isLocal && !isRaw)) {
if (isReserved || pathname === '/' || (!isLocal && !isRaw && !isRedirection)) {
return href
.replace(/^https:[/][/]/, '')
.replace(/^www[.]/, '')
Expand Down Expand Up @@ -179,6 +200,21 @@ function shortenURL(href, currentUrl = 'https://github.com') {
return `${partial}${search}${hash} (compare)`;
}

// Shorten URLs that would otherwise be natively shortened
if (isRedirection) {
if (issue) {
return `${repoUrl}#${issue}${commentIndicator(hash)}`;
}

if (pull) {
return `${repoUrl}#${pull}${commentIndicator(hash)}`;
}

if (commit) {
return joinValues([repoUrl, `<code>${commit.slice(0, 7)}</code>`], '@') + commentIndicator(hash);
}
}

let query = searchParams.get('q') ?? '';
if (query) {
searchParams.delete('q');
Expand Down
40 changes: 40 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,44 @@ test('GitHub.com URLs', urlMatcherMacro, new Map([
'https://github.com/sindresorhus/notifier-for-github/pull/253/files/6b4489d417c9425dc27c5fb8d6b4a8518debd035..60cdcf3c3646164441bf8f037cef620479cdec59',
'<code>6b4489d4..60cdcf3c</code> (#253)',
],
[
'https://togithub.com/fregante/shorten-repo-url/issues/25',
'#25',
],
[
'https://togithub.com/fregante/shorten-repo-url/issues/28#issue-850900171',
'#28 (comment)',
],
[
'https://togithub.com/fregante/shorten-repo-url/pull/32',
'#32',
],
[
'https://togithub.com/fregante/shorten-repo-url/pull/32/files',
'#32 (files)',
],
[
'https://togithub.com/fregante/shorten-repo-url/pull/33#pullrequestreview-801229042',
'#33 (review)',
],
[
'https://togithub.com/nodejs/node/pull/123',
'nodejs/node#123',
],
[
'https://togithub.com/nodejs/node/pull/123/files',
'nodejs/node#123 (files)',
],
[
'https://togithub.com/fregante/shorten-repo-url/commit/98c6175b0cbd4caca71d24e68e57b942b0dfb549',
'<code>98c6175</code>',
],
[
'https://togithub.com/refined-github/refined-github/commit/4f270c4f50e0a2a20085a6e92095117f10340322',
'refined-github/refined-github@<code>4f270c4</code>',
],
[
'https://togithub.com/refined-github/refined-github/commit/e81a9646b448d90c7e02ab41332cab0507dccbbd#commitcomment-60089354',
'refined-github/refined-github@<code>e81a964</code> (comment)',
],
]));