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

Add clean-issue-filters feature #2114

Merged
merged 8 commits into from Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions source/content.ts
Expand Up @@ -102,6 +102,7 @@ import './features/tag-changelog-link';
import './features/link-to-file-in-file-history';
import './features/clean-sidebar';
import './features/open-issue-to-latest-comment';
import './features/clean-issue-filters';

import './features/scrollable-code-and-blockquote.css';
import './features/center-reactions-popup.css';
Expand Down
43 changes: 43 additions & 0 deletions source/features/clean-issue-filters.tsx
@@ -0,0 +1,43 @@
import select from 'select-dom';
import features from '../libs/features';
import {v4} from '../libs/api';
notlmn marked this conversation as resolved.
Show resolved Hide resolved
import {getOwnerAndRepo, idx} from '../libs/utils';

async function init(): Promise<void> {
const {ownerName, repoName} = getOwnerAndRepo();
const result = await v4(`
query {
repository(owner: "${ownerName}", name: "${repoName}") {
projects { totalCount }
milestones { totalCount }
labels { totalCount }
},
organization(login: "${ownerName}") {
projects { totalCount }
}
notlmn marked this conversation as resolved.
Show resolved Hide resolved
}
`, {
allowErrors: true
});

// Hide projects filter only if there are no repo and organization level projects
if (result.repository.projects.totalCount === 0 && !idx(result, ['organization', 'projects', 'totalCount'])) {
notlmn marked this conversation as resolved.
Show resolved Hide resolved
select('[data-hotkey="p"')!.parentElement!.remove();
}

// Hide milestones filter if there are none
if (result.repository.milestones.totalCount === 0) {
select('[data-hotkey="m"')!.parentElement!.remove();
}
}

features.add({
id: 'clean-issue-filters',
description: 'Hide irrelevant issue/PR filters',
notlmn marked this conversation as resolved.
Show resolved Hide resolved
init,
load: features.onAjaxedPages,
include: [
features.isPRList,
features.isIssueList
fregante marked this conversation as resolved.
Show resolved Hide resolved
]
});
15 changes: 13 additions & 2 deletions source/libs/api.ts
Expand Up @@ -64,12 +64,20 @@ interface GHRestApiOptions {
body?: undefined | JsonObject;
}

interface GHGraphQLApiOptions {
allowErrors?: boolean;
}

const v3defaults: GHRestApiOptions = {
ignoreHTTPStatus: false,
method: 'GET',
body: undefined
};

const v4defaults: GHGraphQLApiOptions = {
allowErrors: false
};

export const v3 = mem(async (
query: string,
options: GHRestApiOptions = v3defaults
Expand Down Expand Up @@ -98,7 +106,10 @@ export const v3 = mem(async (
throw getError(apiResponse);
});

export const v4 = mem(async (query: string): Promise<AnyObject> => {
export const v4 = mem(async (
query: string,
options: GHGraphQLApiOptions = v4defaults
): Promise<AnyObject> => {
const {personalToken} = await settings;

if (!personalToken) {
Expand All @@ -121,7 +132,7 @@ export const v4 = mem(async (query: string): Promise<AnyObject> => {
errors = []
} = apiResponse;

if (errors.length > 0) {
if (errors.length > 0 && !options.allowErrors) {
throw Object.assign(
new RefinedGitHubAPIError('GraphQL:', ...errors.map(error => error.message)),
apiResponse
Expand Down
2 changes: 2 additions & 0 deletions source/libs/page-detect.ts
Expand Up @@ -60,6 +60,8 @@ export const isConflict = (): boolean => /^pull\/\d+\/conflicts/.test(getRepoPat

export const isPRList = (): boolean => getRepoPath() === 'pulls';

export const isIssueList = (): boolean => getRepoPath() === 'issues';

export const isPRCommit = (): boolean => /^pull\/\d+\/commits\/[0-9a-f]{5,40}/.test(getRepoPath()!);

export const isPRConversation = (): boolean => /^pull\/\d+$/.test(getRepoPath()!);
Expand Down
4 changes: 4 additions & 0 deletions source/libs/utils.ts
Expand Up @@ -86,3 +86,7 @@ export function getOP(): string {

return select('.timeline-comment-header-text .author')!.textContent!;
}

export function idx(target: unknown, path: string[]): unknown {
return path.reduce((obj, key) => obj && obj[key], target);
}
9 changes: 9 additions & 0 deletions test/page-detect.ts
Expand Up @@ -234,6 +234,15 @@ test('isPRList', urlMatcherMacro, pageDetect.isPRList, [
'https://github.com/sindresorhus/refined-github/issues'
]);

test('isIssueList', urlMatcherMacro, pageDetect.isIssueList, [
'https://github.com/sindresorhus/refined-github/issues',
'https://github.com/sindresorhus/refined-github/issues?q=is%3Aopen+is%3Aissue',
'https://github.com/sindresorhus/refined-github/issues?q=is%3Aissue+is%3Aclosed'
], [
'https://github.com/sindresorhus/refined-github/issues/2016',
'https://github.com/sindresorhus/refined-github/pulls'
]);

test('isPR', urlMatcherMacro, pageDetect.isPR, [
'https://github.com/sindresorhus/refined-github/pull/148'
], [
Expand Down