forked from cmcarthur/github-projects-story-points
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-storypoints.js
338 lines (288 loc) · 10.7 KB
/
github-storypoints.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
(function() {
'use strict';
var estimateRegEx = /^([\d\.]+) pt$/im;
const activeColumns = ['📅 Planned', '🚧 In progress', '👀 In review', '🔬 In QA'];
const closedColumns = ['📦 Done', '✅ Accepted'];
const githubCredentials = new Promise(resolve => {
chrome.storage.sync.get({ githubUser: '', githubToken: '' }, items => resolve(items));
});
const debounce = function (func, wait) {
let timeout;
return function() {
const context = this, args = arguments;
const later = function() {
timeout = null;
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};
const getColumnCards = (column) => {
return Array
.from(column.getElementsByClassName('issue-card'))
.filter(card => !card.classList.contains('sortable-ghost'))
.filter(card => getComputedStyle(card).getPropertyValue('display') != 'none');
}
const moveTo = (card_id, position) => async (e) => {
e.preventDefault();
e.stopPropagation();
e.target.innerHTML = '⏳';
const { githubUser, githubToken } = await githubCredentials;
if (!githubUser || !githubToken) {
alert('please set github credentials in Github Projects Story Points settings');
return;
}
const res = await fetch(
`https://api.github.com/projects/columns/cards/${card_id}/moves`, {
headers: {
'Authorization': 'Basic ' + btoa(githubUser + ":" + githubToken),
'Accept': 'application/vnd.github.inertia-preview+json',
},
method: 'POST',
body: JSON.stringify({ position }),
}
);
if (!res.ok) {
console.error(`Github API error: ${res.statusTest}\n${await res.text()}`);
alert(`Github API error: ${res.statusTest}`);
} else {
/*
const parentNode = card.parentNode;
if (parentNode && position === 'top') {
parentNode.prepend(card);
} else if (parentNode && position === 'bottom') {
parentNode.insertBefore(card, null);
}
*/
}
return false;
}
const addCardButtonsForColumn = (column) => {
getColumnCards(column)
.filter(card => card.querySelectorAll('.gpsp-card-buttons').length == 0)
.forEach(card => {
const buttons = document.createElement('div');
buttons.classList.add('gpsp-card-buttons');
const addButton = (text, dir) => {
const button = document.createElement('div');
button.innerText = text;
button.addEventListener('click', moveTo(card.dataset.cardId, dir), {capture: true});
button.addEventListener('mousedown', e => { e.preventDefault(); }, {capture: true});
buttons.append(button);
};
addButton('↑', 'top');
addButton('↓', 'bottom');
card.append(buttons);
});
};
// extract story points for all visible issues
const extractStoryPoints = () => {
console.log('extracting points');
const result = [];
const project = document.querySelector('.project-columns-container');
const columns = project.querySelectorAll('.js-project-column');
for (const columnElement of columns) {
const column = {
name: columnElement.getElementsByClassName('js-project-column-name')[0].innerText,
element: columnElement,
};
const cards = getColumnCards(columnElement);
for (const card of cards) {
const issueContainer = card.querySelector(
'article > div > div > .js-project-issue-details-container, ' +
'.js-issue-note > .js-project-issue-details-container');
if (!issueContainer) {
continue; // this is a note
}
const number = issueContainer.querySelector('.js-issue-number').innerText;
const assigneeButtons =
[...issueContainer.querySelectorAll('button[data-card-filter^="assignee:"]')];
const assignees = assigneeButtons.map(el => ({
name: el.dataset.cardFilter.substring("assignee:".length), button: el,
}));
// compute sum of all labels matching estimate regexp
const estimates = Array
.from(issueContainer.getElementsByClassName('IssueLabel'))
.map(label => parseFloat((label.innerText.trim().match(estimateRegEx) || [null, ''])[1]))
.filter(x => !isNaN(x));
const points = estimates.length > 0 ? estimates.reduce((x, y) => x + y, 0) : NaN;
result.push({number, points, column, assignees});
}
}
return result;
};
const addStoryPoints = (acc, issue) => {
if (!isNaN(issue.points)) { acc.points += issue.points; }
else { acc.unestimated += 1; }
return acc;
}
const aggStoryPoints = (issues, filter) =>
issues.filter(filter).reduce(addStoryPoints, { points: 0, unestimated: 0 });
const getOrAppendChild = (parent, cls, newEl, insertBefore = null) => {
const matches = parent.getElementsByClassName(cls);
if (matches.length > 0) {
return matches[0];
} else {
const el = typeof newEl === 'string' ? document.createElement(newEl) : newEl();
el.classList.add(cls);
parent.insertBefore(el, insertBefore);
return el;
}
}
const updateColumnsStoryPoints = (issues) => {
const project = document.querySelector('.project-columns-container');
const columns = project.querySelectorAll('.js-project-column');
for (const columnElement of columns) {
const { points, unestimated } = aggStoryPoints(issues, i => i.column.element === columnElement);
// Apply DOM changes:
const countEl = columnElement.querySelector('.js-column-card-count');
const pointsEl = getOrAppendChild(countEl, 'gpsp-column-points', 'div');
pointsEl.innerText = `(${points} pt${unestimated ? ` + ${unestimated} unestimated` : ''})`;
}
}
const updateTotalStoryPoints = (issues) => {
const active = aggStoryPoints(issues, i => activeColumns.includes(i.column.name));
const closed = aggStoryPoints(issues, i => closedColumns.includes(i.column.name));
const fmt = ({points, unestimated}) =>
`${points} pt${unestimated ? ` + ${unestimated} unestimated` : ''}`;
// Apply DOM changes:
const projectTitle = document.querySelector('.project-header .js-project-hovercard .js-project-name-label');
const pointsElement = getOrAppendChild(projectTitle, 'gpsp-total-points', 'span');
pointsElement.innerText = `(active: ${fmt(active)} / closed: ${fmt(closed)})`;
}
let assigneeStoryPointsState = 'active';
const updateAssigneesStoryPoints = (issues) => {
const assigneeMap = {};
for (const issue of issues) {
const status =
activeColumns.includes(issue.column.name) ? 'active' :
closedColumns.includes(issue.column.name) ? 'closed' : undefined;
if (status === undefined) {
continue;
}
const points = {
points: issue.points / issue.assignees.length,
unestimated: issue.unestimated / issue.assignees.length,
};
for (const assignee of issue.assignees) {
if (!(assignee.name in assigneeMap)) {
assigneeMap[assignee.name] = {
button: assignee.button,
active: { points: 0, unestimated: 0 },
closed: { points: 0, unestimated: 0 },
};
}
addStoryPoints(assigneeMap[assignee.name][status], points);
}
}
const assigneeNames = Object.keys(assigneeMap);
assigneeNames.sort();
const projectHeader = document.querySelector('.project-header');
const assigneesBar = getOrAppendChild(
projectHeader.parentNode, 'gpsp-assignees-bar', 'div', projectHeader.nextSibling);
assigneesBar.innerHTML = '';
const span = document.createElement('span');
span.innerText = ' issues:';
assigneesBar.appendChild(span);
const statusSwitch = document.createElement('a');
statusSwitch.classList.add('tooltipped', 'tooltipped-se', 'tooltipped-multiline');
statusSwitch.ariaLabel =
`Active columns:\n${activeColumns.join('\n')}\n\nClosed columns:\n${closedColumns.join('\n')}`;
statusSwitch.href = '#';
statusSwitch.innerText =
assigneeStoryPointsState.charAt(0).toUpperCase() + assigneeStoryPointsState.slice(1);
statusSwitch.addEventListener('click', (e) => {
const statuses = ['active', 'closed', 'active / closed'];
e.preventDefault();
assigneeStoryPointsState = statuses[(statuses.indexOf(assigneeStoryPointsState) + 1) % 3];
updateAssigneesStoryPoints(extractStoryPoints());
});
span.prepend(statusSwitch);
const fmt = ({points, unestimated}) => `${points} pt${unestimated ? ` + ${unestimated}` : ''}`;
assigneeNames.forEach(name => {
const el = document.createElement('div');
const avatar = document.createElement('span');
avatar.classList.add('tooltipped', 'tooltipped-ne', 'tooltipped-multiline');
avatar.ariaLabel = name;
avatar.insertBefore(assigneeMap[name].button.cloneNode(true), null);
el.insertBefore(avatar, null);
const sp = document.createElement('span');
const { active, closed } = assigneeMap[name];
sp.innerText =
assigneeStoryPointsState === 'active' ? `${fmt(active)}` :
assigneeStoryPointsState === 'closed' ? `${fmt(closed)}` :
`${fmt(active)} / ${fmt(closed)}`;
el.insertBefore(sp, null);
assigneesBar.insertBefore(el, null);
});
}
// update story points widgets
const updateStoryPoints = () => {
const issues = extractStoryPoints();
updateColumnsStoryPoints(issues);
updateTotalStoryPoints(issues);
updateAssigneesStoryPoints(issues);
};
const updateStoryPointsHandler = debounce(updateStoryPoints, 100);
const start = () => {
const project = document.querySelector('.project-columns-container');
const columns = project.querySelectorAll('.js-project-column');
for (const column of columns) {
const addCardButtonsForColumnHandler = debounce(() => addCardButtonsForColumn(column), 100);
const columnArea = column.querySelector('.js-project-column-cards');
columnArea.addEventListener('DOMSubtreeModified', () => {
updateStoryPointsHandler();
addCardButtonsForColumnHandler();
});
}
};
const addStyle = () => {
const sheet = document.createElement('style');
sheet.innerHTML = `
.gpsp-card-buttons {
visibility: hidden;
position: absolute;
top: 24px;
right: 15px;
display: flex;
flex-direction: column;
justify-content: center;
}
.gpsp-card-buttons div {
width: 16px;
text-align: center;
cursor: pointer;
}
.issue-card:hover .gpsp-card-buttons {
visibility: visible;
}
.gpsp-column-points {
display: inline;
font-size: xx-small;
margin-left: 0.5em;
}
.gpsp-total-points {
font-weight: 400;
}
.gpsp-assignees-bar {
margin-top: 5px;
padding: 0 24px 0 16px;
display: flex;
flex-flow: row wrap;
}
.gpsp-assignees-bar > div {
margin: 0 0 0 10px;
}
.gpsp-assignees-bar > div > span {
margin-right: 5px;
}
`;
document.body.appendChild(sheet);
};
addStyle();
start();
window.addEventListener('statechange', () => {
setTimeout(updateStoryPointsHandler, 500);
});
})();