-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoller.js
More file actions
97 lines (87 loc) · 3.03 KB
/
poller.js
File metadata and controls
97 lines (87 loc) · 3.03 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
const baseUrl = 'https://api.buildkite.com/v2/';
const authenticateUrl = 'user';
const allBuildsUrl = 'builds';
const authenticate = async (token) => {
const url = `${baseUrl}${authenticateUrl}`;
return await fetch(url, {
method: 'GET',
headers: new Headers({
'Authorization': 'Bearer ' + token,
}),
})
.then(response => response.json())
.then(response => {
console.log(`Authentication success`);
return response.id;
})
.catch(response => {
console.log(`Authentication failed: ${response.error}`)
return '';
});
}
const getBuildsForUser = async (maxCount = 1, userId, token) => {
let url = `${baseUrl}${allBuildsUrl}?creator=${userId}&per_page=${maxCount}`;
return await fetch(url, {
method: 'GET',
headers: new Headers({
'Authorization': 'Bearer '+ token,
}),
})
.then(response => response.json())
.then(response => {
console.log(`All builds for user: `, response);
return response;
})
.catch(failure => console.log(`Failed to get all builds for user: `, failure.error));
}
const poll = (token, userId) => { // todo: save data for display in tracking
getBuildsForUser(1, userId, token)
.then( builds => {
const build = builds[0];
const state = build.blocked ? 'blocked' : build.state;
let icon = '';
const imgFolder = '../../../images/';
switch(state) {
case 'failed':
case 'canceled':
default:
icon = `${imgFolder}red-icon-128.png`;
break;
case 'running':
case 'blocked':
icon = `${imgFolder}orange-icon-128.png`;
break;
case 'passed':
icon = `${imgFolder}green-icon-128.png`;
break;
}
if(icon !== '') {
chrome.action.setIcon({ path: icon});
setTracking(build)
}
});
}
function setTracking(build) {
const pipeline = build.pipeline.description === '' ? build.pipeline.slug : build.pipeline.description;
const state = build.blocked ? 'step-blocked' : build.state;
console.log("saving tracking",build)
chrome.storage.local.set({['tracking']: `
<a href="${build.web_url}" class="repo">${pipeline}</a>
<a class='pr ${state}' href=${build.web_url} target="_blank">
<p class='pr-title'>${build.number}</p>
<p class='pr-title'>${build.message}</p>
</a>
`})
}
chrome.alarms.create({ periodInMinutes: 1 });
chrome.alarms.onAlarm.addListener(() => {
chrome.storage.local.get('buildkiteToken', async (items) => {
const storedData = [];
Object.keys(items).forEach(key => {
storedData[key] = items[key];
});
const token = storedData['buildkiteToken'];
const userId = await authenticate(token);
poll(token, userId);
});
});