-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
122 lines (102 loc) · 3.22 KB
/
background.js
File metadata and controls
122 lines (102 loc) · 3.22 KB
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
const PR_REFRESH_ALARM_NAME = 'pr-refesh-alarm';
async function fetchPrs() {
const { github_token: githubToken } = await chrome.storage.local.get(['github_token']);
if (!githubToken) return;
const response = await fetch('https://api.github.com/user', {
method: 'GET',
headers: {
Authorization: `token ${githubToken}`,
}
})
if (!response.ok) {
if (response.status === 401) {
// Log the user out as their token is probably expired
logout(true);
}
return;
}
const userData = await response.json();
const reviewRequestsResponse = await fetch(`https://api.github.com/search/issues?q=review-requested:${userData.login}+is:pr+state:open`, {
headers: { Authorization: `token ${githubToken}` }
})
if (!reviewRequestsResponse.ok) {
if (reviewRequestsResponse.status === 401) {
logout(true);
}
return;
}
const reviewRequestsData = await reviewRequestsResponse.json();
const openPrsResponse = await fetch(`https://api.github.com/search/issues?q=author:${userData.login}+is:pr+state:open`, {
headers: { Authorization: `token ${githubToken}` }
})
if (!openPrsResponse.ok) {
if (openPrsResponse.status === 401) {
logout(true);
}
return;
}
const openPrsData = await openPrsResponse.json();
const lastPrUpdate = new Date();
const badgeText = `${formatItemsNum(reviewRequestsData.total_count)}|${formatItemsNum(openPrsData.total_count)}`;
chrome.action.setBadgeText({text: badgeText});
// Set badge color to yellow if there are review requests, otherwise black
const badgeColor = reviewRequestsData.total_count > 0 ? '#FFA500' : 'black';
chrome.action.setBadgeBackgroundColor({color: badgeColor});
await chrome.storage.local.set({
reviewRequestsData,
openPrsData,
lastPrUpdate: lastPrUpdate.toString()
})
chrome.runtime.sendMessage({ action: "PrsRefreshed" });
}
async function logout(failure = false) {
await chrome.storage.local.clear()
if (failure) {
chrome.action.setBadgeText({text: '?'});
chrome.action.setBadgeBackgroundColor({color: 'red'});
} else {
chrome.action.setBadgeText({ text: "" });
chrome.action.setBadgeBackgroundColor({ color: [0, 0, 0, 0] });
}
chrome.runtime.sendMessage({ action: "Logout" });
}
function formatItemsNum(amountOfItems) {
if (amountOfItems > 999) {
return '999+';
}
return amountOfItems.toString();
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "refreshPrs") {
fetchPrs().then(sendResponse);
return true;
}
});
async function ensureAlarmExists() {
const alarm = await chrome.alarms.get(PR_REFRESH_ALARM_NAME);
if (!alarm) {
await chrome.alarms.create(PR_REFRESH_ALARM_NAME, { periodInMinutes: 1 });
}
}
chrome.runtime.onStartup.addListener(() => {
ensureAlarmExists();
});
chrome.runtime.onInstalled.addListener(() => {
fetchPrs();
ensureAlarmExists();
chrome.contextMenus.create({
id: "logout",
title: "Logout",
contexts: ["action"],
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "logout") {
logout();
}
});
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === PR_REFRESH_ALARM_NAME) {
fetchPrs();
}
});