1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
import React from 'dom-chef';
import cache from 'webext-storage-cache';
import select from 'select-dom';
import {BugIcon} from '@primer/octicons-react';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';
import features from '../feature-manager';
import * as api from '../github-helpers/api';
import {cacheByRepo, getRepo} from '../github-helpers';
import SearchQuery from '../github-helpers/search-query';
import abbreviateNumber from '../helpers/abbreviate-number';
import {highlightTab, unhighlightTab} from '../helpers/dom-utils';
const supportedLabels = /^(bug|bug-?fix|confirmed-bug|type[:/]bug|kind[:/]bug|(:[\w-]+:|\p{Emoji})bug)$/iu;
const getBugLabelCacheKey = (): string => 'bugs-label:' + getRepo()!.nameWithOwner;
const getBugLabel = async (): Promise<string | undefined> => cache.get<string>(getBugLabelCacheKey());
const isBugLabel = (label: string): boolean => supportedLabels.test(label.replace(/\s/g, ''));
async function countBugsWithUnknownLabel(): Promise<number> {
const {repository} = await api.v4(`
repository() {
labels(query: "bug", first: 10) {
nodes {
name
issues(states: OPEN) {
totalCount
}
}
}
}
`);
const label: AnyObject | undefined = repository.labels.nodes
.find((label: AnyObject) => isBugLabel(label.name));
if (!label) {
return 0;
}
void cache.set(getBugLabelCacheKey(), label.name ?? false);
return label.issues.totalCount ?? 0;
}
async function countIssuesWithLabel(label: string): Promise<number> {
const {repository} = await api.v4(`
repository() {
label(name: "${label}") {
issues(states: OPEN) {
totalCount
}
}
}
`);
return repository.label?.issues.totalCount ?? 0;
}
const countBugs = cache.function('bugs', async (): Promise<number> => {
const bugLabel = await getBugLabel();
return bugLabel
? countIssuesWithLabel(bugLabel)
: countBugsWithUnknownLabel();
}, {
maxAge: {minutes: 30},
staleWhileRevalidate: {days: 4},
cacheKey: cacheByRepo,
});
async function getSearchQueryBugLabel(): Promise<string> {
return 'label:' + SearchQuery.escapeValue(await getBugLabel() ?? 'bug');
}
async function isBugsListing(): Promise<boolean> {
return SearchQuery.from(location).includes(await getSearchQueryBugLabel());
}
async function addBugsTab(): Promise<void | false> {
// Query API as early as possible, even if it's not necessary on archived repos
const countPromise = countBugs();
// On a label:bug listing:
// - always show the tab, as soon as possible
// - update the count later
// On other pages:
// - only show the tab if needed
if (!await isBugsListing() && await countPromise === 0) {
return false;
}
const issuesTab = await elementReady('a.UnderlineNav-item[data-hotkey="g i"]', {waitForChildren: false});
if (!issuesTab) {
// Issues are disabled
return false;
}
// Copy Issues tab
const bugsTab = issuesTab.cloneNode(true);
bugsTab.classList.add('rgh-bugs-tab');
unhighlightTab(bugsTab);
// Disable unwanted behavior #3001
delete bugsTab.dataset.hotkey;
delete bugsTab.dataset.selectedLinks;
bugsTab.removeAttribute('id');
// Update its appearance
const bugsTabTitle = select('[data-content]', bugsTab)!;
bugsTabTitle.dataset.content = 'Bugs';
bugsTabTitle.textContent = 'Bugs';
select('.octicon', bugsTab)!.replaceWith(<BugIcon className="UnderlineNav-octicon d-none d-sm-inline"/>);
// Set temporary counter
const bugsCounter = select('.Counter', bugsTab)!;
bugsCounter.textContent = '0';
bugsCounter.title = '';
// Update Bugs’ link
bugsTab.href = SearchQuery.from(bugsTab).add(await getSearchQueryBugLabel()).href;
// In case GitHub changes its layout again #4166
if (issuesTab.parentElement instanceof HTMLLIElement) {
issuesTab.parentElement.after(<li className="d-flex">{bugsTab}</li>);
} else {
issuesTab.after(bugsTab);
}
// Trigger a reflow to push the right-most tab into the overflow dropdown
window.dispatchEvent(new Event('resize'));
// Update bugs count
try {
const bugCount = await countPromise;
bugsCounter.textContent = abbreviateNumber(bugCount);
bugsCounter.title = bugCount > 999 ? String(bugCount) : '';
} catch (error) {
bugsCounter.remove();
throw error; // Likely an API call error that will be handled by the init
}
}
function highlightBugsTab(): void {
// Remove highlighting from "Issues" tab
unhighlightTab(select('.UnderlineNav-item[data-hotkey="g i"]')!);
highlightTab(select('.rgh-bugs-tab')!);
}
async function removePinnedIssues(): Promise<void> {
const pinnedIssues = await elementReady('.js-pinned-issues-reorder-container', {waitForChildren: false});
pinnedIssues?.remove();
}
async function updateBugsTagHighlighting(): Promise<void | false> {
if (await countBugs() === 0) {
return false;
}
const bugLabel = await getBugLabel() ?? 'bug';
if (
(pageDetect.isRepoTaxonomyIssueOrPRList() && location.href.endsWith('/labels/' + encodeURIComponent(bugLabel)))
|| (pageDetect.isRepoIssueList() && await isBugsListing())
) {
void removePinnedIssues();
highlightBugsTab();
return;
}
if (pageDetect.isIssue() && await elementReady(`#partial-discussion-sidebar .IssueLabel[data-name="${bugLabel}"]`)) {
highlightBugsTab();
return;
}
return false;
}
async function init(): Promise<void | false> {
if (!select.exists('.rgh-bugs-tab')) {
await addBugsTab();
}
await updateBugsTagHighlighting();
}
void features.add(import.meta.url, {
include: [
pageDetect.hasRepoHeader,
],
init,
});
|