Skip to content

Commit

Permalink
Link to current branch in More's Compare and Commits links (#2102)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerone authored and fregante committed Jun 4, 2019
1 parent ed0ace7 commit 0a6b6b4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
14 changes: 11 additions & 3 deletions source/features/more-dropdown.tsx
Expand Up @@ -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';

Expand All @@ -31,10 +31,18 @@ async function init(): Promise<void> {
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(
<a href={`/${repoUrl}/compare`} className="rgh-reponav-more dropdown-item">
<a href={compareUrl} className="rgh-reponav-more dropdown-item">
{icons.darkCompare()} Compare
</a>,

Expand All @@ -43,7 +51,7 @@ async function init(): Promise<void> {
{icons.dependency()} Dependencies
</a>,

<a href={`/${repoUrl}/commits`} className="rgh-reponav-more dropdown-item">
<a href={commitsUrl} className="rgh-reponav-more dropdown-item">
{icons.history()} Commits
</a>,

Expand Down
9 changes: 9 additions & 0 deletions source/libs/utils.ts
Expand Up @@ -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};
Expand Down
41 changes: 41 additions & 0 deletions test/utils.ts
Expand Up @@ -4,6 +4,7 @@ import {
getDiscussionNumber,
getOwnerAndRepo,
getRepoPath,
getRef,
parseTag
} from '../source/libs/utils';

Expand Down Expand Up @@ -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'});
Expand Down

0 comments on commit 0a6b6b4

Please sign in to comment.