diff --git a/source/features/more-dropdown.tsx b/source/features/more-dropdown.tsx index f031e3a2d2b..bd62d5adbbf 100644 --- a/source/features/more-dropdown.tsx +++ b/source/features/more-dropdown.tsx @@ -4,7 +4,7 @@ import select from 'select-dom'; import elementReady from 'element-ready'; import features from '../libs/features'; import * as icons from '../libs/icons'; -import {getRepoURL} from '../libs/utils'; +import {getRepoURL, getRef} from '../libs/utils'; import {isEnterprise} from '../libs/page-detect'; import {appendBefore} from '../libs/dom-utils'; @@ -31,10 +31,18 @@ async function init(): Promise { createDropdown(); } + let compareUrl = `/${repoUrl}/compare`; + let commitsUrl = `/${repoUrl}/commits`; + const ref = getRef(); + if (ref) { + compareUrl += `/${ref}`; + commitsUrl += `/${ref}`; + } + const menu = select('.reponav-dropdown .dropdown-menu')!; menu.append( - + {icons.darkCompare()} Compare , @@ -43,7 +51,7 @@ async function init(): Promise { {icons.dependency()} Dependencies , - + {icons.history()} Commits , diff --git a/source/libs/utils.ts b/source/libs/utils.ts index a7ddf562347..f3a7b2c8057 100644 --- a/source/libs/utils.ts +++ b/source/libs/utils.ts @@ -40,6 +40,15 @@ export const getOwnerAndRepo = (): { return {ownerName, repoName}; }; +export const getRef = (): string | undefined => { + const pathnameParts = location.pathname.split('/'); + if (['commits', 'blob', 'tree', 'blame'].includes(pathnameParts[3])) { + return pathnameParts[4]; + } + + return undefined; +}; + export const parseTag = (tag: string): {version: string; namespace: string} => { const [, namespace = '', version = ''] = tag.match(/(?:(.*)@)?([^@]+)/) || []; return {namespace, version}; diff --git a/test/utils.ts b/test/utils.ts index ec3fe4c5584..ff1a21d54c9 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -4,6 +4,7 @@ import { getDiscussionNumber, getOwnerAndRepo, getRepoPath, + getRef, parseTag } from '../source/libs/utils'; @@ -152,6 +153,46 @@ test('getOwnerAndRepo', t => { }); }); +test('getRef', t => { + const refs: { + [url: string]: string | undefined; + } = { + 'https://github.com/sindresorhus/refined-github': undefined, + 'https://github.com/sindresorhus/refined-github/': undefined, + + 'https://github.com/sindresorhus/refined-github/tree/master': 'master', + 'https://github.com/sindresorhus/refined-github/tree/62007c8b944808d1b46d42d5e22fa65883d1eaec': '62007c8b944808d1b46d42d5e22fa65883d1eaec', + + 'https://github.com/sindresorhus/refined-github/compare': undefined, + 'https://github.com/sindresorhus/refined-github/compare/master': undefined, + 'https://github.com/sindresorhus/refined-github/compare/62007c8b944808d1b46d42d5e22fa65883d1eaec': undefined, + 'https://github.com/sindresorhus/refined-github/compare/master...test': undefined, + + 'https://github.com/sindresorhus/refined-github/commits': undefined, + 'https://github.com/sindresorhus/refined-github/commits/master': 'master', + 'https://github.com/sindresorhus/refined-github/commits/62007c8b944808d1b46d42d5e22fa65883d1eaec': '62007c8b944808d1b46d42d5e22fa65883d1eaec', + + 'https://github.com/sindresorhus/refined-github/releases/tag/v1.2.3': undefined, + + 'https://github.com/sindresorhus/refined-github/blob/master/readme.md': 'master', + 'https://github.com/sindresorhus/refined-github/blob/62007c8b944808d1b46d42d5e22fa65883d1eaec/readme.md': '62007c8b944808d1b46d42d5e22fa65883d1eaec', + + 'https://github.com/sindresorhus/refined-github/wiki/topic': undefined, + + 'https://github.com/sindresorhus/refined-github/blame/master/readme.md': 'master', + 'https://github.com/sindresorhus/refined-github/blame/62007c8b944808d1b46d42d5e22fa65883d1eaec/readme.md': '62007c8b944808d1b46d42d5e22fa65883d1eaec', + + 'https://github.com/sindresorhus/refined-github/pull/123': undefined, + 'https://github.com/sindresorhus/refined-github/pull/2105/commits/': undefined, + 'https://github.com/sindresorhus/refined-github/pull/2105/commits/9df50080dfddee5f7a2a6a1dc4465166339fedfe': undefined + }; + + Object.keys(refs).forEach(url => { + location.href = url; + t.is(refs[url], getRef(), url); + }); +}); + test('parseTag', t => { t.deepEqual(parseTag(''), {namespace: '', version: ''}); t.deepEqual(parseTag('1.2.3'), {namespace: '', version: '1.2.3'});