-
Notifications
You must be signed in to change notification settings - Fork 89
/
wpt-amend-metadata.js
473 lines (421 loc) · 13.8 KB
/
wpt-amend-metadata.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/**
* Copyright 2020 The WPT Dashboard Project. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
import '../node_modules/@polymer/paper-dialog/paper-dialog.js';
import '../node_modules/@polymer/paper-dialog-scrollable/paper-dialog-scrollable.js';
import '../node_modules/@polymer/paper-input/paper-input.js';
import '../node_modules/@polymer/paper-toast/paper-toast.js';
import { html, PolymerElement } from '../node_modules/@polymer/polymer/polymer-element.js';
import { LoadingState } from './loading-state.js';
import { ProductInfo } from './product-info.js';
import { PathInfo } from './path.js';
const AmendMetadataMixin = (superClass) => class extends superClass {
static get properties() {
return {
selectedMetadata: {
type: Array,
value: [],
},
hasSelections: {
type: Boolean,
computed: 'computeHasSelections(selectedMetadata)',
},
selectedCells: {
type: Array,
value: [],
},
isTriageMode: {
type: Boolean
},
};
}
static get observers() {
return [
'pathChanged(path)',
];
}
pathChanged() {
this.selectedMetadata = [];
}
computeHasSelections(selectedMetadata) {
return selectedMetadata.length > 0;
}
handleClear(selectedMetadata) {
if (selectedMetadata.length === 0 && this.selectedCells.length) {
for (const cell of this.selectedCells) {
cell.removeAttribute('selected');
}
this.selectedCells = [];
}
}
handleHover(td, canAmend) {
if (!canAmend) {
if (td.hasAttribute('triage')) {
td.removeAttribute('triage');
}
return;
}
td.setAttribute('triage', 'triage');
}
handleSelect(td, browser, test, toast) {
if (this.selectedMetadata.find(s => s.test === test && s.product === browser)) {
this.selectedMetadata = this.selectedMetadata.filter(s => !(s.test === test && s.product === browser));
this.selectedCells = this.selectedCells.filter(c => c !== td);
td.removeAttribute('selected');
} else {
const selected = { test: test, product: browser };
this.selectedMetadata = [...this.selectedMetadata, selected];
td.setAttribute('selected', 'selected');
this.selectedCells.push(td);
}
if (this.selectedMetadata.length) {
toast.show();
}
}
handleTriageModeChange(mode, toast) {
if (mode) {
toast.show();
return;
}
if (this.selectedMetadata.length > 0) {
this.selectedMetadata = [];
}
toast.hide();
}
triageToastMsg(arrayLen) {
if (arrayLen > 0) {
return arrayLen + ' ' + this.pluralize('test', arrayLen) + ' selected';
} else {
return 'Select some cells to triage';
}
}
};
// AmendMetadata is a UI component that allows the user to associate a set of
// tests or test results with a URL (usually a link to a bug-tracker). It is
// commonly referred to as the 'triage UI'.
class AmendMetadata extends LoadingState(PathInfo(ProductInfo(PolymerElement))) {
static get is() {
return 'wpt-amend-metadata';
}
static get template() {
return html`
<style>
img.browser {
height: 26px;
width: 26px;
position: relative;
margin-right: 10px;
}
paper-button {
text-transform: none;
margin-top: 5px;
}
paper-input {
text-transform: none;
align-items: center;
margin-bottom: 20px;
margin-left: 10px;
}
.metadata-entry {
display: flex;
align-items: center;
margin-top: 20px;
margin-bottom: 0px;
}
.link {
align-items: center;
color: white;
}
li {
margin-top: 5px;
margin-left: 30px;
}
.list {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
max-width: 100ch;
display: inline-block;
vertical-align: bottom;
}
</style>
<paper-dialog id="dialog">
<h3>Triage Failing Tests (<a href="https://github.com/web-platform-tests/wpt-metadata/blob/master/README.md" target="_blank">See metadata documentation</a>)</h3>
<paper-dialog-scrollable>
<template is="dom-repeat" items="[[displayedMetadata]]" as="node">
<div class="metadata-entry">
<img class="browser" src="[[displayMetadataLogo(node.product)]]">
:
<paper-input label="Bug URL" on-input="handleFieldInput" value="{{node.url}}" autofocus></paper-input>
<template is="dom-if" if="[[!node.product]]">
<paper-input label="Label" on-input="handleFieldInput" value="{{node.label}}"></paper-input>
</template>
</div>
<template is="dom-repeat" items="[[node.tests]]" as="test">
<li>
<div class="list"> [[test]] </div>
<template is="dom-if" if="[[hasSearchURL(node.product)]]">
<a href="[[getSearchURL(test, node.product)]]" target="_blank"> [Search for bug] </a>
</template>
<template is="dom-if" if="[[hasFileIssueURL(node.product)]]">
<a href="[[getFileIssueURL(test)]]" target="_blank"> [File test-level issue] </a>
</template>
</li>
</template>
</template>
</paper-dialog-scrollable>
<div class="buttons">
<paper-button onclick="[[close]]">Dismiss</paper-button>
<paper-button disabled="[[triageSubmitDisabled]]" onclick="[[triage]]" dialog-confirm>Triage</paper-button>
</div>
</paper-dialog>
<paper-toast id="show-pr" duration="10000"><span>[[errorMessage]]</span><a class="link" target="_blank" href="[[prLink]]">[[prText]]</a></paper-toast>
`;
}
static get properties() {
return {
prLink: String,
prText: String,
errorMessage: String,
fieldsFilled: Object,
selectedMetadata: {
type: Array,
notify: true,
},
displayedMetadata: {
type: Array,
value: []
},
triageSubmitDisabled: {
type: Boolean,
value: true
}
};
}
constructor() {
super();
this.triage = this.triageSubmit.bind(this);
this.close = this.close.bind(this);
this.enter = this.triageOnEnter.bind(this);
}
get dialog() {
return this.$.dialog;
}
open() {
this.dialog.open();
this.populateDisplayData();
this.dialog.addEventListener('keydown', this.enter);
}
close() {
this.dialog.removeEventListener('keydown', this.enter);
this.triageSubmitDisabled = true;
this.selectedMetadata = [];
this.fieldsFilled = {filled: [], numEmpty: 0};
this.dialog.close();
}
triageSubmit() {
this.handleTriage();
this.close();
}
triageOnEnter(e) {
if (e.which === 13 && !this.triageSubmitDisabled) {
this.triageSubmit();
}
}
getTriagedMetadataMap(displayedMetadata) {
var link = {};
if (this.computePathIsATestFile(this.path)) {
link[this.path] = [];
for (const entry of displayedMetadata) {
if (entry.url === '') {
continue;
}
const results = [];
for (const test of entry.tests) {
results.push({ 'subtest': test });
}
link[this.path].push({ 'url': entry.url, 'product': entry.product, 'results': results });
}
} else {
for (const entry of displayedMetadata) {
// entry.url always exists while entry.label only exists when product is empty;
// in other words, a test-level triage.
if (entry.url === '' && !entry.label) {
continue;
}
for (const test of entry.tests) {
if (!(test in link)) {
link[test] = [];
}
const metadata = {};
if (entry.url !== '') {
metadata['url'] = entry.url;
}
if (entry.product !== '') {
metadata['product'] = entry.product;
}
if (entry.label && entry.label !== '') {
metadata['label'] = entry.label;
}
link[test].push(metadata);
}
}
}
return link;
}
hasSearchURL(product) {
return [
'chrome',
'chromium',
'deno',
'edge',
'firefox',
'ladybird',
'node.js',
'safari',
'servo',
'wktr',
'webkitgtk',
].includes(product);
}
getSearchURL(testName, product) {
if (this.computePathIsATestFile(testName)) {
// Remove name flags and extensions: https://web-platform-tests.org/writing-tests/file-names.html
testName = testName.split('.')[0];
} else {
testName = testName.replace(/((\/\*)?$)/, '');
}
if (product === 'chrome' || product === 'chromium' || product === 'edge') {
return `https://bugs.chromium.org/p/chromium/issues/list?q="${testName}"`;
}
if (product === 'deno') {
return `https://github.com/denoland/deno/issues?q="${testName}"`;
}
if (product === 'firefox') {
return `https://bugzilla.mozilla.org/buglist.cgi?quicksearch="${testName}"`;
}
if (product === 'ladybird') {
return `https://github.com/LadybirdBrowser/ladybird/issues?q="${testName}"`;
}
if (product === 'node.js') {
return `https://github.com/nodejs/node/issues?q="${testName}"`;
}
if (product === 'safari' || product === 'wktr' || product === 'webkitgtk') {
return `https://bugs.webkit.org/buglist.cgi?quicksearch="${testName}"`;
}
if (product === 'servo') {
return `https://github.com/servo/servo/issues?q="${testName}"`;
}
}
hasFileIssueURL(product) {
// We only support filing issues for test-level problems
// (https://github.com/web-platform-tests/wpt.fyi/issues/2420). In this
// class the test-level product is represented by an empty string.
return product === '';
}
getFileIssueURL(testName) {
const params = new URLSearchParams();
params.append('title', `[compat2021] ${testName} fails due to test issue`);
params.append('labels', 'compat2021-test-issue');
return `https://github.com/web-platform-tests/wpt-metadata/issues/new?${params}`;
}
populateDisplayData() {
this.displayedMetadata = [];
// Info to keep track of which fields have been filled.
this.fieldsFilled = {filled: [], numEmpty: 0};
const browserMap = {};
for (const entry of this.selectedMetadata) {
if (!(entry.product in browserMap)) {
browserMap[entry.product] = [];
}
let test = entry.test;
if (!this.computePathIsATestFile(this.path) && this.computePathIsASubfolder(test)) {
test = test + '/*';
}
browserMap[entry.product].push(test);
}
for (const key in browserMap) {
let node = { product: key, url: '', tests: browserMap[key] };
// when key (product) is empty, we will set a label field because
// this is a test-level triage.
if (key === '') {
node['label'] = '';
}
this.displayedMetadata.push(node);
this.fieldsFilled.filled.push(false);
}
// A URL or label must be supplied for every triage item,
// which are all currently empty.
this.fieldsFilled.numEmpty = this.displayedMetadata.length;
}
handleFieldInput(event) {
// Detect which input was filled.
const index = event.model.__data.index;
const url = this.displayedMetadata[index].url;
const label = this.displayedMetadata[index].label;
// Check if the input is empty.
if (url === '' && (label === '' || label === undefined)) {
// If the field was previously considered filled, it's now empty.
if (this.fieldsFilled.filled[index]) {
this.fieldsFilled.numEmpty++;
}
this.fieldsFilled.filled[index] = false;
} else if (!this.fieldsFilled.filled[index]) {
// If the field was previously empty, it is now considered filled.
this.fieldsFilled.numEmpty--;
this.fieldsFilled.filled[index] = true;
}
// If all triage items have input, triage can be submitted.
this.triageSubmitDisabled = this.fieldsFilled.numEmpty > 0;
}
handleTriage() {
const url = new URL('/api/metadata/triage', window.location);
const toast = this.shadowRoot.querySelector('#show-pr');
const triagedMetadataMap = this.getTriagedMetadataMap(this.displayedMetadata);
if (Object.keys(triagedMetadataMap).length === 0) {
this.selectedMetadata = [];
let errMsg = '';
if (this.displayedMetadata.length > 0 && this.displayedMetadata[0].product === '') {
errMsg = 'Failed to triage: Bug URL and Label fields cannot both be empty.';
} else {
errMsg = 'Failed to triage: Bug URLs cannot be empty.';
}
this.errorMessage = errMsg;
toast.open();
return;
}
const fetchOpts = {
method: 'PATCH',
body: JSON.stringify(triagedMetadataMap),
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
};
window.fetch(url, fetchOpts).then(
async r => {
this.prText = '';
this.prLink = '';
this.errorMessage = '';
let text = await r.text();
if (!r.ok || r.status !== 200) {
throw new Error(`${r.status}: ${text}`);
}
return text;
})
.then(text => {
this.prLink = text;
this.prText = 'Created PR: ' + text;
this.dispatchEvent(new CustomEvent('triagemetadata', { bubbles: true, composed: true }));
toast.open();
}).catch(error => {
this.errorMessage = error.message;
toast.open();
});
this.selectedMetadata = [];
}
}
window.customElements.define(AmendMetadata.is, AmendMetadata);
export { AmendMetadataMixin, AmendMetadata };