Skip to content

Commit

Permalink
show number of issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed Mar 18, 2016
1 parent d273503 commit 0f58470
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
}

#gce-update:hover ~ #gce-update-time,
#gce-num-prs:hover ~ #gce-update-time {
#gce-num-prs:hover ~ #gce-update-time,
#gce-num-issues:hover ~ #gce-update-time {
display: block;
}
126 changes: 83 additions & 43 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const isPR = (path) => /^\/[^/]+\/[^/]+\/pull\/\d+/.test(path);
const isIssue = (path) => /^\/[^/]+\/[^/]+\/issues\/\d+/.test(path);
const getCurrentUser = () => $('.js-menu-target img').attr('alt').slice(1) || "";
const isPrivate = () => $('.repo-private-label').length > 0;

function getContributor() {
let $contributor = $(".timeline-comment-wrapper .timeline-comment-header-text strong");
Expand All @@ -18,13 +19,13 @@ function getContributorInfo() {
let pathNameArr = location.pathname.split("/");
let org = pathNameArr[1]; // babel
let repo = pathNameArr[2]; // babel-eslint
let currentPR = pathNameArr[4]; // 3390
let currentNum = pathNameArr[4]; // 3390
let repoPath = org + "/" + repo; // babel/babel-eslint
let contributor = getContributor();

let ret = {
contributor: getContributor(),
currentPR,
currentNum,
repoPath
};

Expand Down Expand Up @@ -68,34 +69,58 @@ function contributorCount({access_token, contributor, repoPath, type}) {
}

let obj = {
prs: json.total_count,
lastUpdate: Date.now()
};

if (type === "pr") {
obj.prs = json.total_count;
} else if (type === "issue") {
obj.issues = json.total_count;
}

if (json.items && json.items.length) {
obj.firstPRNumber = json.items[0].number;
obj[`first${type[0].toUpperCase() + type.slice(1)}Number`] = json.items[0].number;
}

if (obj.prs) {
setStorage(contributor, repoPath, obj);
setStorageProperty(contributor, repoPath, "lastUpdate", obj.lastUpdate);
setStorageProperty(contributor, repoPath, "prs", obj.prs);
setStorageProperty(contributor, repoPath, "firstPrNumber", obj.firstPrNumber);
} else if (obj.issues) {
setStorageProperty(contributor, repoPath, "lastUpdate", obj.lastUpdate);
setStorageProperty(contributor, repoPath, "issues", obj.issues);
setStorageProperty(contributor, repoPath, "firstIssueNumber", obj.firstIssueNumber);
}

return obj;
});
}

function appendPRText(currentPR, repoInfo) {
let {prs, firstPRNumber} = repoInfo;
let text = `${prs} PRs`;
function appendPRText(currentNum, repoInfo) {
let {issues, prs, firstPrNumber, firstIssueNumber} = repoInfo;

if (prs !== undefined) {
let prText = `${prs} PRs`;
if (firstPrNumber === +currentNum) {
prText = "First PR";
if (prs > 1) {
prText += ` out of ${prs} (to the repo)`;
}
}
repoInfo.prText = prText;
}

if (firstPRNumber === +currentPR) {
text = "First PR";
if (prs > 1) {
text += ` out of ${prs} (to the repo)`;
if (issues !== undefined) {
let issueText = `${issues} Issues`;
if (firstIssueNumber === +currentNum) {
issueText = "First Issue";
if (issues > 1) {
issueText += ` out of ${issues} (to the repo)`;
}
}
repoInfo.issueText = issueText;
}

repoInfo.text = text;
return repoInfo;
}

Expand All @@ -108,69 +133,82 @@ function makeUpdateLabel(time) {
}

function injectInitialUI({ contributor, repoPath }) {
if ($("#gce-num-prs").length) return;

let $elem = $(".timeline-comment-header-text").first();
let id = "gce-num-prs";
let prText = makeLabel("Loading # of PRs..");
let updateText = makeLabel("🔄 PRs");

if (!$(id).length) {
$elem.before(`<a href="/${repoPath}/pulls?utf8=%E2%9C%93&q=is:both+is:pr+author:${contributor}" id="${id}">${prText}</a>`);
$elem.before(`<a style="cursor:pointer;" id="gce-update">${updateText}</a>`);
$elem.before(`<a id="gce-update-time" class="timeline-comment-label">N/A</a>`);

let $update = $("#gce-update");
$update.dom[0].addEventListener("click", function() {
setStorage(contributor, repoPath, {});
update(getContributorInfo());
});
}
let prId = "gce-num-prs";
let prText = makeLabel("Loading..");

if ($(`#${prId}`).length) return;

let issueId = "gce-num-issues";
let issueText = makeLabel("Loading..");
let updateText = makeLabel("🔄");

$elem.before(`<a href="/${repoPath}/pulls?utf8=%E2%9C%93&q=is:both+is:pr+author:${contributor}" id="${prId}">${prText}</a>`);
$elem.before(`<a href="/${repoPath}/issues?utf8=%E2%9C%93&q=is:both+is:issue+author:${contributor}" id="${issueId}">${issueText}</a>`);
$elem.before(`<a style="cursor:pointer;" id="gce-update">${updateText}</a>`);
$elem.before(`<a id="gce-update-time" class="timeline-comment-label">N/A</a>`);

let $update = $("#gce-update");
$update.dom[0].addEventListener("click", function() {
setStorage(contributor, repoPath, {});
update(getContributorInfo());
});
}

function updatePRText({ text, lastUpdate }) {
let prText = $("#gce-num-prs .timeline-comment-label");
if (prText.length) {
prText.text(text);
function updateTextNodes({ prText, issueText, lastUpdate }) {
let prNode = $("#gce-num-prs .timeline-comment-label");
if (prNode.length) {
prNode.text(prText);
}

let issueNode = $("#gce-num-issues .timeline-comment-label");
if (issueNode.length) {
issueNode.text(issueText);
}

let updateTime = $("#gce-update-time");
if (updateTime && typeof lastUpdate === "number") {
updateTime.html(`<span>Last Updated </span>${makeUpdateLabel(new Date(lastUpdate))}`);
}
}

function update({ contributor, repoPath, currentPR }) {
function update({ contributor, repoPath, currentNum }) {
getStorage(contributor, repoPath)
.then((storage) => {
let storageRes = storage[contributor][repoPath];
if (storageRes.prs) {
updatePRText(appendPRText(currentPR, storageRes));
if (storageRes.prs || storageRes.issues) {
updateTextNodes(appendPRText(currentNum, storageRes));
} else {
getSyncStorage({ "access_token": null })
.then((res) => {
contributorCount({ access_token: res.access_token, type: "pr", contributor, repoPath})
.then((repoInfo) => {
Promise.all([
contributorCount({ access_token: res.access_token, type: "pr", contributor, repoPath}),
contributorCount({ access_token: res.access_token, type: "issue", contributor, repoPath})
])
.then(([prInfo, issueInfo]) => {
let repoInfo = Object.assign(prInfo, issueInfo);

if (repoInfo.errors) {
updatePRText(repoInfo.errors[0].message);
updateTextNodes(repoInfo.errors[0].message);
return;
}

if (repoInfo.message) {
// API rate limit exceeded for hzoo.
if (repoInfo.message.indexOf(`API rate limit exceeded for ${getCurrentUser()}`) >= 0) {
updatePRText("More than 30 req/min :D");
updateTextNodes("More than 30 req/min :D");
return;
}

// API rate limit exceeded for x.x.x.x.
// (But here's the good news: Authenticated requests get a higher rate limit.
// Check out the documentation for more details.)
if (repoInfo.message.indexOf("the good news") >= 0) {
updatePRText("More than 10 req/min: Maybe add a access_token!");
updateTextNodes("More than 10 req/min: Maybe add a access_token!");
return;
}
}
updatePRText(appendPRText(currentPR, repoInfo));
updateTextNodes(appendPRText(currentNum, repoInfo));
});
});
}
Expand All @@ -179,6 +217,8 @@ function update({ contributor, repoPath, currentPR }) {

document.addEventListener("DOMContentLoaded", () => {
gitHubInjection(window, () => {
// if (isPrivate()) return;

if (isPR(location.pathname) || isIssue(location.pathname)) {
if (getContributor()) {
update(getContributorInfo());
Expand Down
10 changes: 10 additions & 0 deletions src/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ window.setStorage = (CONTRIBUTOR, ORG_REPO_PATH, value) => {
});
};

window.setStorageProperty = (CONTRIBUTOR, ORG_REPO_PATH, prop, value) => {
return window.setSyncStorage({
[CONTRIBUTOR]: {
[ORG_REPO_PATH]: {
[prop]: value
}
}
});
};

window.getStorage = (CONTRIBUTOR, ORG_REPO_PATH) => {
return window.getSyncStorage({
[CONTRIBUTOR]: {
Expand Down

0 comments on commit 0f58470

Please sign in to comment.