-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgerrit-checklist.js
266 lines (238 loc) · 7.96 KB
/
gerrit-checklist.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
// ==UserScript==
// @name Gerrit Review Checklist 2.0
// @namespace https://github.com/mziwisky/gerrit-checklist
// @version 2.0.0
// @description Add a checklist to your Gerrit review panel, and record responses with your submitted comments.
// @match https://gerrit.instructure.com/*
// @copyright 2014+ Michael Ziwisky, 2019+ Kyle Grinstead
// ==/UserScript==
// example .gerrit-checklist.json file
// this file should be in the root directory of the repo
//
// {
// "checklist": [
// "FE: works in a mobile browser",
// "BE: queries are optimized"
// ],
// "defaultStatus": false // a default status of false == Not Checked, true == Checked, null == N/A
// }
function ReviewChecklistManager(optsList, defaultStatus) {
console.log('Gerrit review checklist added!');
const mgr = this;
this.options = optsList.map(function(opt) {
const option = {
text: opt,
status: defaultStatus,
setStatus: function(status) {
this.status = status;
mgr.updateManagedTextArea();
},
};
return option;
});
this.isPolyGerrit = !!document.querySelector('gr-app');
this.textAreaDomSelector = this.isPolyGerrit
? 'gr-reply-dialog gr-textarea textarea'
: '.popupContent .gwt-TextArea';
}
function buildElement(tag, options) {
const element = document.createElement(tag);
Object.keys(options || {}).forEach(function(optionKey) {
if (options[optionKey] !== null) {
element[optionKey] = options[optionKey];
}
});
return element;
}
ReviewChecklistManager.create = function(defaultOptsList) {
if (ReviewChecklistManager.instanceCreated) {
console.log('Only a single Gerrit checklist is allowed.');
return;
}
ReviewChecklistManager.instanceCreated = true;
const repoName = document.location.toString().match(/\/c\/(.*)\/\+/)[1];
fetch(
`/plugins/gitiles/${repoName}/+show/master/.review-checklist.json?format=text`,
)
.then(function(res) {
return res.text().then(function(base64) {
if (base64 && base64 !== '') {
const customSettings = JSON.parse(atob(base64));
const {checklist, defaultStatus} = customSettings;
new ReviewChecklistManager(checklist, defaultStatus).activate();
} else {
new ReviewChecklistManager(defaultOptsList, null).activate();
}
});
})
.catch(function() {
new ReviewChecklistManager(defaultOptsList, null).activate();
});
};
ReviewChecklistManager.prototype.activate = function() {
new MutationObserver(this.domChangeListener.bind(this)).observe(
document.body,
{childList: true, subtree: true},
);
};
ReviewChecklistManager.prototype.domChangeListener = function() {
const gerTextArea = document.querySelector(this.textAreaDomSelector);
if (!gerTextArea) return; // review popover is not open
this.manageTextArea(gerTextArea);
if (!this.optionsPresent()) {
this.insertOptions();
}
};
ReviewChecklistManager.prototype.manageTextArea = function(gerTextArea) {
if (this.gerTextArea && this.gerTextArea === gerTextArea) return;
if (!this.textArea) {
this.createStandinTextArea();
}
this.gerTextArea = gerTextArea;
this.textArea.value = '';
if (this.isPolyGerrit) {
gerTextArea.parentElement.parentElement.style.minHeight = '11em';
gerTextArea.parentElement.parentElement.style.overflow = 'scroll';
} else {
gerTextArea.parentElement.parentElement.style.maxHeight = 'none'; // let gray bg grow to fit new checkboxes
}
gerTextArea.parentElement.insertBefore(
this.textArea,
gerTextArea.nextSibling,
);
gerTextArea.style.position = 'fixed';
gerTextArea.style.left = '-10000px'; // like hide(), but allows it to get focus
const ta = this.textArea;
gerTextArea.addEventListener('focus', function() {
ta.focus();
});
this.updateManagedTextArea();
};
ReviewChecklistManager.prototype.createStandinTextArea = function() {
let textAreaOptions;
if (this.isPolyGerrit) {
textAreaOptions = {
autocomplete: true,
placeholder: 'Say something nice...',
style: 'height: inherit;',
rows: 4,
};
} else {
textAreaOptions = {
rows: 5,
cols: 70,
};
}
this.textArea = buildElement('textarea', textAreaOptions);
this.textArea.classList.add('style-scope', 'iron-autogrow-textarea');
this.textArea.addEventListener(
'change',
this.updateManagedTextArea.bind(this),
);
this.textArea.addEventListener(
'input',
this.updateManagedTextArea.bind(this),
);
// forward special key sequences to the original textArea to get special behaviors
const mgr = this;
this.textArea.addEventListener('keydown', function(evt) {
if (
(evt.which == 13 && evt.ctrlKey) || // ctrl-Enter
evt.which == 27 // esc
) {
mgr.gerTextArea.focus();
mgr.gerTextArea.trigger(evt);
}
});
};
ReviewChecklistManager.prototype.updateManagedTextArea = function() {
if (!this.gerTextArea) return;
this.gerTextArea.value = '' + this.textArea.value + this.checklistText();
this.gerTextArea.dispatchEvent(new Event('input', {bubbles: true}));
};
ReviewChecklistManager.prototype.insertOptions = function() {
this.textArea.parentElement.appendChild(buildElement('hr'));
this.textArea.parentElement.appendChild(this.optionsTable());
};
ReviewChecklistManager.prototype.optionsTable = function() {
if (!this._optionsTable) {
this._optionsTable = this.buildOptionsTable();
}
return this._optionsTable;
};
ReviewChecklistManager.prototype.checklistText = function() {
const positive = [];
const negative = [];
this.options.forEach(function(option) {
if (option.status === true) positive.push(option.text);
else if (option.status === false) negative.push(option.text);
});
let message = '';
if (positive.length > 0) {
message += '\n\n Reviewer checked:';
positive.forEach(function(pos) {
message += '\n * ' + pos;
});
}
if (negative.length > 0) {
message += '\n\n Reviewer DID NOT check:';
negative.forEach(function(neg) {
message += '\n * ' + neg;
});
}
return message;
};
ReviewChecklistManager.prototype.buildOptionsTable = function() {
const headers = ['N/A', 'no', 'yes'];
const statuses = [null, false, true];
const rows = this.options.map(function(option, index) {
const tr = buildElement('tr');
statuses.forEach(function(val) {
const box = buildElement('input', {
type: 'radio',
name: 'checkbox_' + index,
checked: val === option.status ? 'checked' : null,
});
box.addEventListener('change', function() {
option.setStatus(val);
});
const td = buildElement('td');
td.appendChild(box);
tr.appendChild(td);
});
const td = buildElement('td', {
align: 'left',
style: 'vertical-align: middle; padding-left: 5px; font-size: 0.9em',
textContent: option.text,
});
tr.appendChild(td);
return tr;
});
const headerRow = buildElement('tr');
headers.forEach(function(text) {
const th = buildElement('th', {align: 'center', textContent: text});
headerRow.appendChild(th);
});
const firstTh = buildElement('th', {textContent: 'Did you verify:'});
headerRow.appendChild(firstTh);
const thead = buildElement('thead');
thead.appendChild(headerRow);
const tbody = buildElement('tbody', {id: 'review-checklist'});
rows.forEach(function(row) {
tbody.appendChild(row);
});
const table = buildElement('table', {cellSpacing: 8, cellPadding: 0});
table.appendChild(thead);
table.appendChild(tbody);
return table;
};
ReviewChecklistManager.prototype.optionsPresent = function() {
return !!document.querySelector('#review-checklist');
};
ReviewChecklistManager.create([
'Changeset checked out and tried',
'Commit message test plan is sufficient for manual sanity checking',
'Automated tests cover all necessary cases',
'User-facing strings/dates/times/numbers are internationalized',
'UI interactions are accessible to screen reader, keyboard only, and visually impaired users',
]);