Skip to content

Commit b685520

Browse files
committed
Merge pull request #19 from hzoo/global-scope
Global scope
2 parents 43c3221 + 6eae7e0 commit b685520

3 files changed

Lines changed: 101 additions & 31 deletions

File tree

src/content.js

Lines changed: 98 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const isPR = (path) => /^\/[^/]+\/[^/]+\/pull\/\d+/.test(path);
66
const isIssue = (path) => /^\/[^/]+\/[^/]+\/issues\/\d+/.test(path);
77
const getCurrentUser = () => $(".js-menu-target img").attr("alt").slice(1) || "";
88
const isPrivate = () => $(".repo-private-label").length > 0;
9-
let showOrgOnly = false;
9+
let statsScope = "repo";
1010

1111
function getContributor() {
1212
let $contributor = $(".timeline-comment-wrapper .timeline-comment-header-text strong");
@@ -31,8 +31,13 @@ function getContributorInfo() {
3131
};
3232

3333
// global variable
34-
if (showOrgOnly) {
34+
if (statsScope === "org") {
3535
ret.user = org;
36+
ret.repoPath = org;
37+
}
38+
39+
if (statsScope === "account") {
40+
ret.repoPath = "__self";
3641
}
3742

3843
injectInitialUI(ret);
@@ -51,19 +56,31 @@ function buildUrl({base, q: {type, filterUser, author, repo, user}, sort, order,
5156
query += `${order ? `&order=${order}`: ""}`;
5257
query += `${per_page ? `&per_page=${per_page}`: ""}`;
5358
query += `${sort ? `&sort=${sort}`: ""}`;
59+
5460
return query;
5561
}
5662

5763
function contributorCount({access_token, contributor, user, repoPath, old = {}, type}) {
64+
let repo = repoPath;
65+
66+
// global variable
67+
if (statsScope === "org") {
68+
repo = undefined;
69+
repoPath = repoPath.split("/")[0];
70+
} else if (statsScope === "account") {
71+
repo = undefined;
72+
repoPath = "__self";
73+
}
74+
5875
let searchURL = buildUrl({
5976
access_token,
6077
base: "https://api.github.com/search/issues",
6178
order: "asc",
6279
per_page: "1",
6380
q: {
6481
type,
82+
repo,
6583
author: contributor,
66-
repo: user ? undefined : repoPath,
6784
user: user
6885
},
6986
sort: "created"
@@ -152,6 +169,21 @@ function makeUpdateLabel(time) {
152169
return `<time datetime="${time}" is="relative-time"></time>`;
153170
}
154171

172+
function issueOrPrLink(type, repoPath, contributor) {
173+
let end = `${type === "pr" ? "pulls" : "issues"}?utf8=%E2%9C%93&q=is:both+is:${type}+author:${contributor}`;
174+
175+
// repo
176+
if (repoPath.split("/").length === 2) {
177+
return `/${repoPath}/${end}`;
178+
// account
179+
} else if (repoPath === "__self") {
180+
return `https://github.com/${end}`;
181+
// org
182+
} else {
183+
return `https://github.com/${end}+user:${repoPath}`;
184+
}
185+
}
186+
155187
function injectInitialUI({ contributor, repoPath }) {
156188
let $elem = $(".timeline-comment-header-text").first();
157189
let prId = "gce-num-prs";
@@ -174,20 +206,23 @@ function injectInitialUI({ contributor, repoPath }) {
174206
<div class="dropdown-header">
175207
View options
176208
</div>
177-
<a class="dropdown-item" id="gce-across-this-org">
178-
across this org
179-
</a>
180209
<a class="dropdown-item selected" id="gce-in-this-repo">
181210
${$checkbox}
182211
in this repo
183212
</a>
213+
<a class="dropdown-item" id="gce-in-this-org">
214+
in this org
215+
</a>
216+
<a class="dropdown-item" id="gce-in-this-account">
217+
in this account
218+
</a>
184219
</ul>
185220
</div>
186221
</div>`;
187222

188223
$elem.before(`<span class="timeline-comment-label">
189-
<a href="/${repoPath}/pulls?utf8=%E2%9C%93&q=is:both+is:pr+author:${contributor}" id="${prId}">${prText}</a>
190-
<a href="/${repoPath}/issues?utf8=%E2%9C%93&q=is:both+is:issue+author:${contributor}" id="${issueId}">${issueText}</a>
224+
<a href="${issueOrPrLink("pr", repoPath, contributor)}" id="${prId}">${prText}</a>
225+
<a href="${issueOrPrLink("issue", repoPath, contributor)}" id="${issueId}">${issueText}</a>
191226
${dropdown}
192227
</span>
193228
`);
@@ -200,29 +235,65 @@ ${dropdown}
200235
update(getContributorInfo());
201236
});
202237

203-
let $acrossThisOrg = $("#gce-across-this-org");
238+
let $inThisOrg = $("#gce-in-this-org");
204239
let $inThisRepo = $("#gce-in-this-repo");
240+
let $inThisAccount = $("#gce-in-this-account");
205241
let $dropdownText = $("#gce-dropdown-text");
206242

207-
$acrossThisOrg.dom[0].addEventListener("click", function() {
208-
$acrossThisOrg.addClass("selected");
243+
$inThisOrg.dom[0].addEventListener("click", function() {
244+
$inThisOrg.addClass("selected");
209245
$inThisRepo.removeClass("selected");
210-
$acrossThisOrg.html(`${$checkbox} across this org`);
211-
$inThisRepo.html('in this repo');
212-
$dropdownText.html('across this org');
246+
$inThisAccount.removeClass("selected");
247+
248+
$inThisOrg.html(`${$checkbox} in this org`);
249+
$dropdownText.html("in this org");
250+
251+
$inThisAccount.html("in this account");
252+
$inThisRepo.html("in this repo");
253+
254+
$(`#${prId}`).attr("href", issueOrPrLink("pr", repoPath.split("/")[0], contributor));
255+
$(`#${issueId}`).attr("href", issueOrPrLink("issue", repoPath.split("/")[0], contributor));
256+
213257
// global
214-
showOrgOnly = true;
258+
statsScope = "org";
215259
update(getContributorInfo());
216260
});
217261

218262
$inThisRepo.dom[0].addEventListener("click", function() {
219263
$inThisRepo.addClass("selected");
220-
$acrossThisOrg.removeClass("selected");
264+
$inThisOrg.removeClass("selected");
265+
$inThisAccount.removeClass("selected");
266+
221267
$inThisRepo.html(`${$checkbox} in this repo`);
222-
$acrossThisOrg.html('across this org');
223-
$dropdownText.html('in this repo');
268+
$dropdownText.html("in this repo");
269+
270+
$inThisAccount.html("in this account");
271+
$inThisOrg.html("in this org");
272+
273+
$(`#${prId}`).attr("href", issueOrPrLink("pr", repoPath, contributor));
274+
$(`#${issueId}`).attr("href", issueOrPrLink("issue", repoPath, contributor));
275+
276+
// global
277+
statsScope = "repo";
278+
update(getContributorInfo());
279+
});
280+
281+
$inThisAccount.dom[0].addEventListener("click", function() {
282+
$inThisAccount.addClass("selected");
283+
$inThisOrg.removeClass("selected");
284+
$inThisRepo.removeClass("selected");
285+
286+
$inThisAccount.html(`${$checkbox} in this account`);
287+
$dropdownText.html("in this account");
288+
289+
$inThisRepo.html("in this repo");
290+
$inThisOrg.html("in this org");
291+
292+
$(`#${prId}`).attr("href", issueOrPrLink("pr", "__self", contributor));
293+
$(`#${issueId}`).attr("href", issueOrPrLink("issue", "__self", contributor));
294+
224295
// global
225-
showOrgOnly = false;
296+
statsScope = "account";
226297
update(getContributorInfo());
227298
});
228299
}
@@ -247,7 +318,14 @@ function updateTextNodes({ prText, issueText, lastUpdate }) {
247318
function update({ contributor, repoPath, currentNum, user }) {
248319
getStorage(contributor, repoPath)
249320
.then((storage) => {
250-
let storageRes = storage[contributor][user ? user : repoPath] = {};
321+
let path = repoPath;
322+
if (user) {
323+
path = user;
324+
} else if (statsScope === "account") {
325+
path = "__self";
326+
}
327+
328+
let storageRes = storage[`${contributor}|${path}`] || {};
251329

252330
if (storageRes.prs || storageRes.issues) {
253331
updateTextNodes(appendPRText(currentNum, storageRes));

src/options.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ document.addEventListener("DOMContentLoaded", () => {
2222

2323
clearCacheLink.addEventListener("click", () => {
2424
let temp = accessTokenInput.value;
25-
clearSyncStorage()
26-
.then(() => {
25+
chrome.storage.sync.clear(() => {
2726
setSyncStorage({ "access_token": temp });
2827
document.querySelector("#feedback").textContent = "Storage Cleared";
2928
});

src/storage.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,13 @@ function promisify(func) {
2525

2626
window.getSyncStorage = promisify(chrome.storage.sync.get.bind(chrome.storage.sync));
2727
window.setSyncStorage = promisify(chrome.storage.sync.set.bind(chrome.storage.sync));
28-
window.clearSyncStorage = promisify(chrome.storage.sync.clear.bind(chrome.storage.sync));
2928

3029
window.setStorage = (CONTRIBUTOR, ORG_REPO_PATH, value) => {
3130
return window.setSyncStorage({
32-
[CONTRIBUTOR]: {
33-
[ORG_REPO_PATH]: value
34-
}
31+
[`${CONTRIBUTOR}|${ORG_REPO_PATH}`]: value
3532
});
3633
};
3734

3835
window.getStorage = (CONTRIBUTOR, ORG_REPO_PATH) => {
39-
return window.getSyncStorage({
40-
[CONTRIBUTOR]: {
41-
[ORG_REPO_PATH]: {}
42-
}
43-
});
36+
return window.getSyncStorage(`${CONTRIBUTOR}|${ORG_REPO_PATH}`);
4437
};

0 commit comments

Comments
 (0)