Skip to content
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

Further improve branch getter function #4187

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
f6630c5
Fix GitHubURL - excluding PR Files
yakov116 Apr 1, 2021
cb1e4f3
Fix tests
yakov116 Apr 1, 2021
ca22fb3
Please check for typo before committing
yakov116 Apr 1, 2021
c80fb13
Fix it for now
yakov116 Apr 1, 2021
eb24a4d
Merge remote-tracking branch 'upstream/githuburl' into githuburl
yakov116 Apr 1, 2021
db42e67
One BIG Mess
yakov116 Apr 1, 2021
83e4980
Lets use the title
yakov116 Apr 1, 2021
2a58301
Merge remote-tracking branch 'upstream/main' into githuburl
yakov116 Apr 5, 2021
4401857
Start fixing
yakov116 Apr 5, 2021
69ef080
Merge branch 'main' into githuburl
yakov116 Apr 6, 2021
659e62e
fix some more
yakov116 Apr 6, 2021
1a04cdd
Exclude pr files
yakov116 Apr 6, 2021
1ca241a
Merge branch 'main' into githuburl
yakov116 Apr 9, 2021
04b7396
Fix some more
yakov116 Apr 9, 2021
9e70957
Fix test for now
yakov116 Apr 9, 2021
44e1a11
Merge remote-tracking branch 'sindresorhus/main' into githuburl
yakov116 Apr 11, 2021
296b022
More cleanup
yakov116 Apr 11, 2021
4254949
Move to its own file
yakov116 Apr 11, 2021
7496845
Fix test
yakov116 Apr 11, 2021
068b21b
Small fix
yakov116 Apr 11, 2021
38bb092
Update comment
yakov116 Apr 11, 2021
a673cb3
Lint
yakov116 Apr 11, 2021
6318630
Lint
yakov116 Apr 12, 2021
f4f8601
Fixup `more-dropdown`
yakov116 Apr 12, 2021
179db54
Lint
yakov116 Apr 12, 2021
e0e4364
Fix `latest-tag-button`
yakov116 Apr 12, 2021
a636300
Add `get branch and filePath from search` test
yakov116 Apr 12, 2021
c6ff873
Update source/github-helpers/index.ts
yakov116 Apr 12, 2021
bc9d068
Update source/github-helpers/index.ts
yakov116 Apr 12, 2021
8b3efcb
Update source/features/more-dropdown.tsx
yakov116 Apr 12, 2021
89b91a6
Not sure what I did here
yakov116 Apr 12, 2021
2f4b63c
Add comment
yakov116 Apr 12, 2021
146190a
Undo TS change
fregante Apr 18, 2021
315f2ab
Update default slashed branch test
fregante Apr 18, 2021
f43511d
Use correct /find/ URL
fregante Apr 18, 2021
dbd0afc
Waiting for element-ready@6
fregante Apr 18, 2021
ccaeeef
Restore index.tsx
fregante Apr 18, 2021
5961155
Merge remote-tracking branch 'upstream/main' into githuburl
yakov116 Apr 19, 2021
62e24d3
Revert "Move to its own file"
yakov116 Apr 19, 2021
586361b
Fix import
yakov116 Apr 19, 2021
17381b8
Move TODO date
yakov116 Apr 19, 2021
c9b0af6
Merge branch 'main' into githuburl
yakov116 Apr 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/features/latest-tag-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async function init(): Promise<false | void> {
}

const defaultBranch = await getDefaultBranch();
if (currentBranch === defaultBranch) {
if (pageDetect.isRepoHome() || currentBranch === defaultBranch) {
link.append(<sup> +{aheadBy}</sup>);
link.setAttribute(
'aria-label',
Expand Down
4 changes: 2 additions & 2 deletions source/features/more-dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {DiffIcon, GitBranchIcon, HistoryIcon, PackageIcon} from '@primer/octicon
import features from '.';
import {appendBefore} from '../helpers/dom-utils';
import getDefaultBranch from '../github-helpers/get-default-branch';
import {buildRepoURL, getCurrentCommittish} from '../github-helpers';
import {buildRepoURL, getCurrentCommittishAnywhere} from '../github-helpers';

function createDropdown(): void {
// Markup copied from native GHE dropdown
Expand Down Expand Up @@ -59,7 +59,7 @@ async function init(): Promise<void> {
'.UnderlineNav-body'
].join());

const reference = getCurrentCommittish() ?? await getDefaultBranch();
const reference = await getCurrentCommittishAnywhere() ?? await getDefaultBranch();
const compareUrl = buildRepoURL('compare', reference);
const commitsUrl = buildRepoURL('commits', reference);
const branchesUrl = buildRepoURL('branches');
Expand Down
9 changes: 5 additions & 4 deletions source/github-helpers/github-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ export default class GitHubURL {
ambiguousReference: string[]
): {branch: string; filePath: string} {
const branch = ambiguousReference[0];
const currentBranch = getCurrentCommittish();
// History pages might use search parameters
const branchFromSearch = this.searchParams.get('branch');
const filePathFromSearch = this.searchParams.getAll('path[]').join('/');
if (filePathFromSearch) {
this.searchParams.delete('branch');
this.searchParams.delete('path[]');
return {branch, filePath: filePathFromSearch};
return {branch: branchFromSearch ?? currentBranch ?? branch, filePath: filePathFromSearch};
Copy link
Member

Choose a reason for hiding this comment

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

When would this link have a file path but not a branch? Steps to reproduce please

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@fregante fregante Apr 12, 2021

Choose a reason for hiding this comment

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

Steps to find find that link on GitHub.com. That's just a URL that anyone can write manually but maybe doesn't exist

Copy link
Member Author

Choose a reason for hiding this comment

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

That is the url we were testing until now. The branch in the path is a new thing.

Copy link
Member

@fregante fregante Apr 12, 2021

Choose a reason for hiding this comment

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

That URL does not have a branch URL parameter (branchFromSearch). Where did you see that?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@fregante fregante Apr 18, 2021

Choose a reason for hiding this comment

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

Anyway, this confirms that there's no way that filePathFromSearch is truey while branchFromSearch isn't, so it should probably be

Suggested change
return {branch: branchFromSearch ?? currentBranch ?? branch, filePath: filePathFromSearch};
return {
branch: this.searchParams.get('branch'),
filePath: filePathFromSearch
};

Copy link
Member Author

Choose a reason for hiding this comment

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

GHE. The branch part is a new thing (at least it was not there when we made GitHubURL

Copy link
Member

@fregante fregante Apr 18, 2021

Choose a reason for hiding this comment

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

Did the pathname look exactly like now too? Because we can easily extract the slashed branch from there instead, and not use the branch parameter at all.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it did.

}

const filePath = ambiguousReference.slice(1).join('/');

const currentBranch = getCurrentCommittish();
const currentBranchSections = currentBranch?.split('/');
const filePath = ambiguousReference.slice(1).join('/');
if (
!currentBranch || // Current branch could not be determined (1/2)
!currentBranchSections || // Current branch could not be determined (2/2)
Expand Down
26 changes: 26 additions & 0 deletions source/github-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,35 @@ export const getCurrentCommittish = (pathname = location.pathname, title = docum
return parsedTitle.groups!.branch;
}

// TODO [2021-08-12]: .last needed for #2799 // GHE
const feedLink = select.last('link[type="application/atom+xml"]');
fregante marked this conversation as resolved.
Show resolved Hide resolved
yakov116 marked this conversation as resolved.
Show resolved Hide resolved
if (feedLink) {
return new URL(feedLink.href)
.pathname
.split('/')
.slice(4) // Drops the initial /user/repo/route/ part
.join('/')
.replace(/\.atom$/, '');
}

return unslashedCommittish;
};

// Used on `isRepoHome` where can also determine the branch from the feedLink
export const getCurrentCommittishAnywhere = async (): Promise<string | undefined> => {
fregante marked this conversation as resolved.
Show resolved Hide resolved
const committish = getCurrentCommittish();
if (committish || !pageDetect.isRepoHome()) {
return committish;
}
fregante marked this conversation as resolved.
Show resolved Hide resolved

const feedLink = await elementReady('a[data-hotkey="t"]')!;
if (!feedLink) {
return;
}

return decodeURIComponent(feedLink.pathname.split('/').pop()!);
};

export const isFirefox = navigator.userAgent.includes('Firefox/');

// The type requires at least one parameter https://stackoverflow.com/a/49910890
Expand Down
16 changes: 16 additions & 0 deletions test/github-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import test from 'ava';
import './fixtures/globals';
import GitHubURL from '../source/github-helpers/github-url';

document.head.insertAdjacentHTML('beforeend', '<a hidden="" data-hotkey="t" data-pjax="true" href="https://github.com/sindresorhus/refined-github/find/Slash%2Fslash"></a>');

test('branch', t => {
const url = new GitHubURL('https://github.com/microsoft/TypeScript/tree/master');
t.is(url.user, 'microsoft');
Expand Down Expand Up @@ -79,3 +81,17 @@ test('get filePath from search', t => {
t.is(url.search, '?after=f23b687b3b89aa95a76193722cdfeff740646670+34');
t.is(String(url), 'https://github.com/yakov116/refined-github/commits/f23b687b3b89aa95a76193722cdfeff740646670/source/features/release-download-count.tsx?after=f23b687b3b89aa95a76193722cdfeff740646670+34');
});

test('get branch and filePath from search', t => {
const url = new GitHubURL('https://github.com/sindresorhus/refined-github/commits/Slash/slash?after=ee8b6422d2ddd1199f557594d03e7ee850e843f0+34&branch=Slash%2Fslash&path%5B%5D=webpack.config.ts');
t.is(url.user, 'sindresorhus');
t.is(url.repository, 'refined-github');
t.is(url.route, 'commits');
t.is(url.branch, 'Slash/slash');
t.is(url.filePath, 'webpack.config.ts');
t.is(url.pathname, '/sindresorhus/refined-github/commits/Slash/slash/webpack.config.ts');
t.is(url.href, 'https://github.com/sindresorhus/refined-github/commits/Slash/slash/webpack.config.ts?after=ee8b6422d2ddd1199f557594d03e7ee850e843f0+34');
t.is(url.search, '?after=ee8b6422d2ddd1199f557594d03e7ee850e843f0+34');
t.is(String(url), 'https://github.com/sindresorhus/refined-github/commits/Slash/slash/webpack.config.ts?after=ee8b6422d2ddd1199f557594d03e7ee850e843f0+34');
});
document.querySelector('[data-hotkey="t"]')!.remove();
6 changes: 4 additions & 2 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,17 @@ test('getCurrentCommittish', t => {
'github.com'
));

document.head.insertAdjacentHTML('beforeend', '<a data-hotkey="t" href="https://github.com/typescript-eslint/typescript-eslint/find/cool%2Fdefault%2Fbranch"></a>');
// Root
t.is(getCurrentCommittish(
'/typescript-eslint/typescript-eslint',
'typescript-eslint/typescript-eslint: Monorepo for all the tooling which enables ESLint to support TypeScript'
), undefined);
), 'cool/default/branch');
t.is(getCurrentCommittish(
'/typescript-eslint/typescript-eslint/tree/chore/lerna-4',
'typescript-eslint/typescript-eslint at chore/lerna-4'
), 'chore/lerna-4');
document.querySelector('[data-hotkey="t"]')!.remove();

// Sub folder
t.is(getCurrentCommittish(
Expand Down Expand Up @@ -289,7 +291,7 @@ test('getCurrentCommittish', t => {
t.is(getCurrentCommittish(
'/typescript-eslint/typescript-eslint/commits/chore/lerna-4/docs/getting-started/README.md',
'History for docs/getting-started/README.md - typescript-eslint/typescript-eslint'
), 'chore'); // Wrong, but
), 'master'); // Uses the feedlink
Copy link
Member

@fregante fregante Apr 24, 2021

Choose a reason for hiding this comment

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

This only shows that the test is wrong, and thus useless.

For this to be tested correctly, it should have the correct feedLink, added and removed just for this test like we do for data-hotkey="t" (including the code to add it before the test)

Suggested change
), 'master'); // Uses the feedlink
), 'chore/lerna-4');
document.querySelector('the feed link')!.remove();


// Single commit
t.is(getCurrentCommittish(
Expand Down