-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphabricator-test-reloader.user.js
243 lines (219 loc) · 5.79 KB
/
phabricator-test-reloader.user.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// ==UserScript==
// @name Phabricator Test Reloader
// @namespace https://github.com/sirbrillig/phabricator-test-reloader
// @version 1.3.1
// @description Reload the Phabricator Differential page periodically while tests run.
// @author Payton Swick <[email protected]>
// @match https://code.a8c.com/D*
// @icon https://www.google.com/s2/favicons?sz=64&domain=a8c.com
// @grant none
// ==/UserScript==
(function () {
'use strict';
const refreshInterval = 1000 * 15;
const successMark = '✅ ';
const failMark = '❌ ';
const closedMark = '🚢 ';
const pendingMark = '⏳ ';
const topOfPageOffsetForRefresh = 300;
const branchId = 'phabricator-test-reloader-branch-name';
const shouldUseDebug =
window.localStorage.getItem('debug') === 'phabricator-test-reloader';
function debug(...args) {
if (!shouldUseDebug) {
return;
}
const output = ['phabricator-test-reloader: ', ...args].join(' ');
console.log(output);
}
function isVisible(elm) {
const rect = elm.getBoundingClientRect();
const viewHeight = Math.max(
document.documentElement.clientHeight,
window.innerHeight,
);
return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
}
function getPageKeyValuePairs() {
const pageKeyValuePairs = new Map();
const keys = Array.from(
document.querySelectorAll('.phui-property-list-key'),
);
const values = Array.from(
document.querySelectorAll('.phui-property-list-value'),
);
keys.forEach((key, index) => {
pageKeyValuePairs.set(key.innerText, values[index]);
});
return pageKeyValuePairs;
}
function getBranchElement() {
const pageKeyValuePairs = getPageKeyValuePairs();
return pageKeyValuePairs.get('Branch');
}
function copyBranchName(event) {
event.preventDefault();
event.stopPropagation();
const copyText = document.getElementById(branchId);
if (!copyText) {
debug('could not copy branch name because it was not found');
return;
}
debug('copying branch name');
navigator.clipboard.writeText(copyText.innerText).then(() => {
alert('Copied branch name to clipboard');
});
}
function makeBranchClickable() {
const branchElement = getBranchElement();
if (!branchElement) {
return;
}
const branchName = branchElement.innerText.split(' ')[0];
const link = `<a href="#" title="Copy branch name to clipboard" id="${branchId}">${branchName}</a>`;
branchElement.innerHTML = branchElement.innerText.replace(
branchName,
link,
);
const branchLink = document.getElementById(branchId);
if (!branchLink) {
debug(
'could not add click handler to branch name because it was not found',
);
return;
}
branchLink.addEventListener('click', copyBranchName);
}
function isBuildAreaVisible() {
const buildArea = Array.from(
document.querySelectorAll('.phui-property-list-key'),
).find((el) => el.textContent.includes('Build Status'));
if (!buildArea) {
return false;
}
return isVisible(buildArea);
}
function isRevisionClosed() {
return Array.from(
document.querySelectorAll('.phui-header-subheader'),
).some((el) => el.textContent.includes('Closed'));
}
function areTestsRunning() {
return (
document.querySelectorAll(
'.phui-status-list-view .phui-icon-view.fa-chevron-circle-right.blue',
).length > 0
);
}
function didTestsFail() {
return (
document.querySelectorAll(
'.phui-status-list-view .phui-icon-view.fa-times-circle.red',
).length > 0
);
}
function didTestsPass() {
if (didTestsFail() || areTestsRunning()) {
return false;
}
return (
document.querySelectorAll(
'.phui-status-list-view .phui-icon-view.fa-check-circle.green',
).length > 0
);
}
function areWeAtPageTop() {
return window.scrollY < topOfPageOffsetForRefresh;
}
function refreshPage() {
// Strip out the hash if one is present so we end up at the top of the page.
let url = window.location.href;
if (window.location.hash) {
url = url.replace(window.location.hash, '');
}
window.location.href = url;
}
function shouldRefresh() {
if (!areTestsRunning()) {
return false;
}
if (didTestsFail()) {
return false;
}
if (areWeAtPageTop() || isBuildAreaVisible() || !isPageActive()) {
return true;
}
return false;
}
function refreshIfNeeded() {
markTitleForTestStatus();
if (shouldRefresh()) {
refreshPage();
return;
}
startRefreshTimer();
}
function isPageActive() {
return !document.hidden;
}
function getMarkForType(type) {
switch (type) {
case 'fail':
return failMark;
case 'pass':
return successMark;
case 'pending':
return pendingMark;
case 'closed':
return closedMark;
}
}
function addTestMarkToTitle(type) {
debug('marking title as', type);
let link = document.querySelector("link[rel~='icon']");
if (!link) {
link = document.createElement('link');
link.rel = 'icon';
document.getElementsByTagName('head')[0].appendChild(link);
}
const mark = getMarkForType(type);
if (!mark) {
debug('no mark found for type', type);
return;
}
link.setAttribute(
'href',
`data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>${mark}</text></svg>`,
);
}
function markTitleForTestStatus() {
if (isRevisionClosed()) {
addTestMarkToTitle('closed');
return;
}
if (didTestsFail()) {
addTestMarkToTitle('fail');
return;
}
if (didTestsPass()) {
addTestMarkToTitle('pass');
return;
}
if (areTestsRunning()) {
addTestMarkToTitle('pending');
return;
}
debug('no test status could be determined');
}
function startRefreshTimer() {
debug('starting refresh timer');
setTimeout(refreshIfNeeded, refreshInterval);
}
function begin() {
debug('starting');
startRefreshTimer();
markTitleForTestStatus();
makeBranchClickable();
}
begin();
})();