-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension.js
138 lines (121 loc) · 4.34 KB
/
extension.js
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
var successColor = '#5cb85c';
var warningColor = '#f0ad4e';
var errorColor = '#d9534f';
var okLimit = 1;
var warningLimit = 3;
var pollInterval = 1; //in minutes
var username = '';
var usernameURL = 'https://api.github.com/user';
var searchReviewsURL = 'https://api.github.com/search/issues?q=type:pr is:open review-requested:';
var searchAssigneesURL = 'https://api.github.com/search/issues?q=type:pr is:open assignee:';
var assigneesURL = 'https://github.com/pulls/assigned';
var reviewsURL = 'https://github.com/pulls/review-requested';
var optionsURL = 'chrome://extensions/?options=' + chrome.runtime.id;
var tokenOk = true;
var reviewsCounter = 0;
var pullRequestUrls = [];
// open options page on a fresh installation
chrome.runtime.onInstalled.addListener(function (object) {
if (chrome.runtime.OnInstalledReason.INSTALL === object.reason) {
chrome.tabs.create({url: optionsURL});
}
});
setInterval(updateCounter, pollInterval * (60 * 1000));
init();
function openCurrentURL() {
function hasAssignedPullRequests() {
return pullRequestUrls.length !== reviewsCounter;
}
if (tokenOk) {
if (pullRequestUrls.length === 1) {
chrome.tabs.create({'url': pullRequestUrls[0]});
}
else {
if (pullRequestUrls.length === 0 || hasAssignedPullRequests()) {
chrome.tabs.create({'url': assigneesURL});
}
if (reviewsCounter > 0) {
chrome.tabs.create({'url': reviewsURL});
}
}
}
else {
chrome.tabs.create({'url': optionsURL});
}
}
function init() {
tokenOk = true;
chrome.browserAction.onClicked.removeListener(openCurrentURL);
chrome.browserAction.onClicked.addListener(openCurrentURL);
updateCounter();
}
function getHtmlUrls(issues) {
return issues.items.map(function(review) { return review.pull_request.html_url; });
}
function getUniquePullRequestUrls(reviewsResponse, assigneesResponse) {
var reviewPullRequestUrls = getHtmlUrls(reviewsResponse);
var assignedPullRequestUrls = getHtmlUrls(assigneesResponse);
return reviewPullRequestUrls.concat(assignedPullRequestUrls).filter(function(v, i, a){ return a.indexOf(v) === i});
}
function chooseColor(counter) {
var color = errorColor;
if (counter < okLimit) {
color = successColor;
}
else if (counter < warningLimit) {
color = warningColor;
}
else {
color = errorColor;
}
return color;
}
function elaborateResponse(reviewsResponse, assigneesResponse) {
pullRequestUrls = getUniquePullRequestUrls(reviewsResponse, assigneesResponse);
reviewsCounter = reviewsResponse.total_count;
chrome.browserAction.setBadgeText({text: '' + pullRequestUrls.length});
var color = chooseColor(pullRequestUrls.length);
chrome.browserAction.setBadgeBackgroundColor({color: color});
}
function executeRequest(url, token, successCallback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
successCallback(JSON.parse(xhr.responseText));
}
else if (xhr.status === 401) {
chrome.browserAction.setBadgeText({text: 'X'});
chrome.browserAction.setBadgeBackgroundColor({color: errorColor});
tokenOk = false;
}
}
};
xhr.open('GET', url, true);
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(':' + token));
xhr.send();
}
function updateCounter() {
var reviewsResponse;
var assigneesResponse;
var token;
chrome.storage.sync.get({
token: null, username: null
}, function (items) {
token = items.token;
username = items.username;
if (username === null) {
executeRequest(usernameURL, token, function (responseJSON) {
username = responseJSON.login;
chrome.storage.sync.set({username: username});
});
}
executeRequest(searchReviewsURL + username, token, function (responseJSON) {
reviewsResponse = responseJSON;
executeRequest(searchAssigneesURL + username, token, function (responseJSON) {
assigneesResponse = responseJSON;
elaborateResponse(reviewsResponse, assigneesResponse);
});
});
});
}