-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
208 lines (192 loc) · 9.87 KB
/
script.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
let globalSettings;
let stopped=false;
let inprogress_icon=`
<div style="display: inline-block; vertical-align: sub; height:16px; width:16px; margin-top: 2px">
<svg width="100%" height="100%" fill="none" viewBox="0 0 16 16" class="anim-rotate" xmlns="http://www.w3.org/2000/svg">
<path fill="none" stroke="#DBAB0A" stroke-width="2" d="M3.05 3.05a7 7 0 1 1 9.9 9.9 7 7 0 0 1-9.9-9.9Z" opacity=".5"></path>
<path fill="#DBAB0A" fill-rule="evenodd" d="M8 4a4 4 0 1 0 0 8 4 4 0 0 0 0-8Z" clip-rule="evenodd"></path>
<path fill="#DBAB0A" d="M14 8a6 6 0 0 0-6-6V0a8 8 0 0 1 8 8h-2Z"></path>
</svg>
</div>
`;
let queued_icon=`
<div style="display: inline-block; vertical-align: sub;">
<svg width="16" height="16" style="margin-top: 2px;vertical-align: top; color: #c69026 !important;fill: currentColor;" viewBox="0 0 16 16" version="1.1"><path d="M8 4a4 4 0 1 1 0 8 4 4 0 0 1 0-8Z"></path></svg>
</div>
`;
let success_icon=`
<div style="display: inline-block; vertical-align: sub;">
<svg width="16" height="16" style="margin-top: 2px;vertical-align: top;color: #57AB5A !important;fill: currentColor;" viewBox="0 0 16 16" version="1.1" ><path d="M8 16A8 8 0 1 1 8 0a8 8 0 0 1 0 16Zm3.78-9.72a.751.751 0 0 0-.018-1.042.751.751 0 0 0-1.042-.018L6.75 9.19 5.28 7.72a.751.751 0 0 0-1.042.018.751.751 0 0 0-.018 1.042l2 2a.75.75 0 0 0 1.06 0Z"></path></svg>
</div>
`;
let error_icon=`
<div style="display: inline-block; vertical-align: sub;">
<svg width="16" height="16" style="margin-top: 2px; color:rgb(229, 83, 75);fill:currentColor" viewBox="0 0 16 16" version="1.1"><path d="M2.343 13.657A8 8 0 1 1 13.658 2.343 8 8 0 0 1 2.343 13.657ZM6.03 4.97a.751.751 0 0 0-1.042.018.751.751 0 0 0-.018 1.042L6.94 8 4.97 9.97a.749.749 0 0 0 .326 1.275.749.749 0 0 0 .734-.215L8 9.06l1.97 1.97a.749.749 0 0 0 1.275-.326.749.749 0 0 0-.215-.734L9.06 8l1.97-1.97a.749.749 0 0 0-.326-1.275.749.749 0 0 0-.734.215L8 6.94Z"></path></svg>
</div>
`;
function SaveConfig() {
globalSettings.name=document.getElementById("name").value;
globalSettings.token=document.getElementById("token").value;
if (document.getElementById("orgs").checked) {
globalSettings.area = "orgs";
} else {
globalSettings.area = "users";
}
localStorage.setItem("Settings", JSON.stringify(globalSettings));
stopped = false; //we changed settings, so it may actually work.
if (globalSettings.token === "") {
clearInterval(triggerTimer)
triggerTimer = setInterval(UpdateWorkflows,360000);
} else {
clearInterval(triggerTimer)
triggerTimer = setInterval(UpdateWorkflows,60000);
}
}
function LoadConfig() {
globalSettings = JSON.parse(localStorage.getItem("Settings"));
if (globalSettings === null) {
globalSettings = {token:"",area:"orgs",name:"CatPoweredPlugins"};
}
document.getElementById("name").value=globalSettings.name;
document.getElementById("token").value=globalSettings.token;
document.getElementById(globalSettings.area).checked=true;
}
function compareRuns(a, b) {
const dateA = new Date(a.created_at);
const dateB = new Date(b.created_at);;
if (dateA < dateB) {
return -1;
}
if (dateA > dateB) {
return 1;
}
return 0;
}
function CreateElement(run) {
let rowTemplate = `
<div class="table-row" >
<div style="padding: 5px;">
<div style="width: 40%;display: inline-block;vertical-align: top;">
<a href="${run.html_url}">
${run.status==="queued"?queued_icon:inprogress_icon}
<span style="min-width: 95%"><b>${run.display_title}</b></span>
</a>
<br />
<span class="text-small"><span>${run.name}</span>#${run.run_number}<span> by <a href="${run.actor.html_url}">${run.actor.login}</a></span></span>
</div>
<div style="display: inline;vertical-align: middle; margin-left: 4%;padding-top: 20px;">
<a class="branch-name" target="_parent" style="max-width: 30%;" title="${run.repository.name+"/"+run.head_branch}" href="${run.repository.html_url+"/tree/refs/heads/"+run.head_branch}">${run.repository.name+"/"+run.head_branch}</a>
</div>
<div style="float: right; width:20%;">
<div class="text-small" style="display: inline-block;">
<div>
<div>
<span>
<!-- calendar icon -->
<div style="display: inline-block; vertical-align: sub;">
<svg height="16" viewBox="0 0 16 16" version="1.1" width="16">
<path d="M4.75 0a.75.75 0 0 1 .75.75V2h5V.75a.75.75 0 0 1 1.5 0V2h1.25c.966 0 1.75.784 1.75 1.75v10.5A1.75 1.75 0 0 1 13.25 16H2.75A1.75 1.75 0 0 1 1 14.25V3.75C1 2.784 1.784 2 2.75 2H4V.75A.75.75 0 0 1 4.75 0ZM2.5 7.5v6.75c0 .138.112.25.25.25h10.5a.25.25 0 0 0 .25-.25V7.5Zm10.75-4H2.75a.25.25 0 0 0-.25.25V6h11V3.75a.25.25 0 0 0-.25-.25Z"></path>
</svg>
</div>
<!-- /icon -->
<relative-time tense="past" datetime="${run.created_at}" data-view-component="true" title="${run.created_at}">${run.created_at}</relative-time>
</span>
</div>
</div>
</div>
<div class="text-small" style="display: block;">
<span>
<!-- clock icon -->
<svg height="16" viewBox="0 0 16 16" version="1.1" width="16">
<path d="M5.75.75A.75.75 0 0 1 6.5 0h3a.75.75 0 0 1 0 1.5h-.75v1l-.001.041a6.724 6.724 0 0 1 3.464 1.435l.007-.006.75-.75a.749.749 0 0 1 1.275.326.749.749 0 0 1-.215.734l-.75.75-.006.007a6.75 6.75 0 1 1-10.548 0L2.72 5.03l-.75-.75a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018l.75.75.007.006A6.72 6.72 0 0 1 7.25 2.541V1.5H6.5a.75.75 0 0 1-.75-.75ZM8 14.5a5.25 5.25 0 1 0-.001-10.501A5.25 5.25 0 0 0 8 14.5Zm.389-6.7 1.33-1.33a.75.75 0 1 1 1.061 1.06L9.45 8.861A1.503 1.503 0 0 1 8 10.75a1.499 1.499 0 1 1 .389-2.95Z"></path>
</svg>
<!-- /icon -->
<span><i>${run.status==="queued"?"Queued":"In Progress for <relative-time format=\"elapsed\" datetime=\""+ run.updated_at +"\" data-view-component=\"true\"></relative-time>"}</i></span>
</span>
</div>
</div>
</div>
</div>
`;
return rowTemplate;
}
async function UpdateWorkflows() {
if (stopped) {
return;
}
let statusElement=document.getElementById("status");
statusElement.title="Updating...";
statusElement.innerHTML=inprogress_icon;
let request_repos = new Request(`https://api.github.com/${globalSettings.area}/${globalSettings.name}/repos?per_page=1000`, { method: "GET" });
if (globalSettings.token !== "") {
request_repos.headers.append("Authorization", `Bearer ${globalSettings.token}`);
}
let response_repos = await fetch(request_repos).then(response => {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.json();
}).then(json => {
this.repos = json;
}).catch((err) => this.err = err);
if (typeof err != 'undefined') {
stopped = true;
statusElement.title=err.message;
statusElement.innerHTML=error_icon;
return;
}
let runs=[];
try {
let fromdate = (new Date(new Date().setDate(new Date().getDate()-3))).toISOString().split("T")[0];
let requests = repos.map(function(repo){
let request_workflow = new Request(`https://api.github.com/repos/${globalSettings.name}/${repo.name}/actions/runs?created=>${fromdate}&per_page=100`, { method: "GET" });
if (globalSettings.token !== "") {
request_workflow.headers.append("Authorization", `Bearer ${globalSettings.token}`);
}
return request_workflow;
});
let fetches = requests.map((request) => fetch(request));
let responses = await Promise.all(fetches);
let errors = responses.filter((response) => !response.ok);
if (errors.length > 0) {
throw errors[0];
}
let json = responses.map((response) => response.json());
let data = await Promise.all(json);
data.forEach((result) => runs = runs.concat(result.workflow_runs));
} catch (error) {
this.err2 = error;
}
if (typeof err2 != 'undefined') {
stopped = true;
statusElement.title=err2.message;
statusElement.innerHTML=error_icon;
return;
}
runs = runs.sort(compareRuns).filter(r => ((r.status === "queued") || (r.status === "in_progress")));
let template=`
<div class="vis-only-no-siblings table-row">
<center><p><h3>No pending/running jobs found</h3></p></center>
</div>
`;
for (let run of runs) {
if (run.status === "in_progress") {
template = CreateElement(run) + template;
}
if (run.status === "queued") {
template = template + CreateElement(run);
}
}
document.getElementById("total").innerHTML=runs.length;
document.getElementById("workarea").innerHTML=template;
statusElement.title="Updated successfuly";
statusElement.innerHTML=success_icon+`Last updated: <relative-time format="elapsed" datetime="${(new Date()).toISOString()}" data-view-component="true">${(new Date()).toLocaleString()}</relative-time> ago`
}
LoadConfig();
UpdateWorkflows();
let triggerTimer;
if (globalSettings.token === "") {
triggerTimer = setInterval(UpdateWorkflows,360000);
} else {
triggerTimer = setInterval(UpdateWorkflows,60000);
}