-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
executable file
·495 lines (405 loc) · 15.6 KB
/
main.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
chrome.storage.local.get('options', function(items) {
var config = items.options;
// Only run if we are in the right :username:/:repo:
if ((config.username && config.repo) && window.location.pathname.indexOf( config.username + '/' + config.repo ) !== -1 ) {
$('body').addClass('gitban_repo');
var $firstButton = $('.reponav a:first');
var $openIssues = $();
var $inProgressIssues = $();
var $closedIssues = $();
var $board = $('<div class="gitban_board gitban_board_loading" />');
var $boardButton = $();
var $milestone = $([
'<div class="gitban_milestone">',
'<h4 class="gitban_milestone_name"><a href="https://github.com/' + config.username + '/' + config.repo + '/milestones" target="_blank">Milestone</a></h4>',
'<h5 class="milestone-meta">',
'<span class="milestone-meta-item"></span>',
'</h5>',
'<span class="milestone-meta-release"></span>',
'</div>'
].join(''));
var $progress = $([
'<div class="gitban_progress">',
'<div class="gitban_milestone">',
'<h4 class="gitban_milestone_name">Team Progress</h4>',
'</div>',
'<div class="ct-chart"></div>',
'</div>'
].join(''));
var $loader = $([
'<div class="context-loader large-format-loader is-context-loading">',
'<p><img height="64" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-128.gif" /></p>',
'<p>Loading...</p>',
'</div>'
].join(''));
// Append the milestone
$milestone.prependTo( $board );
// Remove the first class
$firstButton.removeClass('js-selected-navigation-item selected');
// Insert empty kanban board
$board.append( $loader, $progress )
.appendTo('#js-repo-pjax-container > .container');
// GitHub removes the nav elements on every load, so we
// have to check in a stupid way whether the button is still
// there and reload it if it's not
$(document.body).on('click', '.reponav a', function() {
var i = 0;
var addButtonInterval = setInterval(function() {
// Load button and stop checking
if( $('.gitban_button').length <= 0 ) {
window.clearInterval(addButtonInterval);
loadButton();
// Give up
} else if( i > 100 ) {
window.clearInterval(addButtonInterval);
}
i++;
}, 500);
});
// Insert Kanban Button
function loadButton() {
$boardButton = $firstButton.clone();
$boardButton.prependTo( $('.reponav') )
$boardButton.addClass('gitban_button')
.attr('aria-label', 'Kanban')
.html('<span class="octicon octicon-three-bars"></span> Kanban');
}
loadButton();
/**
* Markup for issue columns. Icons are octicons.
*
* @param {object} atts { className:, icon:, stage: }
* @return {object} jQuery Object
*/
function $issuesColMarkup( atts ) {
return $([
'<div class="gitban_issues gitban_issues_' + atts.className + '">',
'<div class="table-list-header">',
'<h5>',
'<span class="octicon octicon-' + atts.icon + '"></span>',
atts.stage,
'</h5>',
'<span class="gitban_count"></span>',
'</div>',
'<div class="gitban_issues_container"></div>',
'</div>'
].join(''));
}
/**
* Markup for individual issues.
*
* @param {object} issue #see https://developer.github.com/v3/issues/
* @return {object} jQuery Object
*/
function $issueMarkup( issue ) {
var avatar = (!!issue.assignee) ? issue.assignee.avatar_url : '';
var labels = '';
issue.labels.forEach(function(label) {
if( label.name !== 'in progress' ) {
labels += ' <span style="background:#' + label.color + '" class="label">' + label.name + '</span>';
}
});
return $([
'<div class="gitban_issue">',
'<div class="gitban_issue_data">',
'<img class="gitban_avatar" src="' + avatar + '" />',
'<h4>' + issue.title + '</h4>',
'<h5>#' + issue.number + '</h5>',
'<div class="gitban_tags">' + labels + '</div>',
'</div>',
'<span class="gitban_link"><a href="' + issue.html_url + '" target="_blank"></a></span>',
'</div>'
].join(''));
}
/**
* Close the Kanban board, restore normal github.
*/
function closeGitban() {
var $container = $('#js-repo-pjax-container > .container');
// var $container = $('.repo-container').closest('.container');
$board.hide();
$container.removeClass('show_gitban');
$('.gitban_button').find('a').removeClass('selected');
$('.repo-container').addClass('with-full-navigation');
$('.repository-content').show();
}
// Close if clicking anything but the kanban button
$('.repository-sidebar .sunken-menu-group li:not(.gitban_button)').on('click', closeGitban);
/**
* Clear all issues from a kanban column
*
* @param {object} $el jQuery Object for column
*/
function emptyKanbanCol( $el ) {
$el.find('.gitban_issues_container').empty();
}
/**
* Load the github issues for the current milestone.
*/
function loadGitHubIssues() {
$loader.show();
if( !$openIssues.length ) {
$openIssues = $issuesColMarkup({
className: 'open',
stage: 'Open',
icon: 'issue-opened'
});
$openIssues.insertBefore( $progress );
} else {
emptyKanbanCol( $openIssues );
}
if( !$inProgressIssues.length ) {
$inProgressIssues = $issuesColMarkup({
className: 'in_progress',
stage: 'In Progress',
icon: 'flame'
});
$inProgressIssues.insertBefore( $progress );
} else {
emptyKanbanCol( $inProgressIssues );
}
if( !$closedIssues.length ) {
$closedIssues = $issuesColMarkup({
className: 'closed',
stage: 'Closed',
icon: 'issue-closed'
});
$closedIssues.insertBefore( $progress );
} else {
emptyKanbanCol( $closedIssues );
}
var github = new Github({
token: config.token,
auth: 'oauth'
});
var repo = github.getRepo(config.username, config.repo);
var milestone;
var issues;
repo.listMilestones({state: 'open'}, function(err, milestones) {
if( err ) {
return;
}
milestones.sort(function(a, b) {
return new Date(a.due_on) > new Date(b.due_on);
});
milestone = milestones[0];
// Set the title
$milestone.find('.gitban_milestone_name a')
.text( 'Working On: ' + milestone.title );
function toHumanReadable( date ) {
var dayDiff = Math.floor( (new Date( date ) - new Date()) / 86400000 );
var timeLeft = ( dayDiff <= 7 ) ? dayDiff + ' days' : Math.floor(dayDiff / 7) + ' weeks';
return timeLeft;
}
var now = moment();
var dateFormat = 'YYYY-MM-DD';
var desktopReleaseTimeBase = moment('2015-12-01');
var websiteReleaseTimeBase = moment('2015-11-10');
var desktopRecurr = desktopReleaseTimeBase.recur().every(4).weeks();
desktopRecurr.fromDate(now);
var nextDesktopReleaseDate = desktopRecurr.next(1, dateFormat);
var websiteRecurr = websiteReleaseTimeBase.recur().every(2).weeks();
websiteRecurr.fromDate(now);
var nextWebsiteReleaseDate = websiteRecurr.next(1, dateFormat);
var dueHTML =
'<span class="octicon octicon-calendar"></span>' +
' Due in ' + toHumanReadable( milestone.due_on );
$milestone.find('.milestone-meta-item').html( dueHTML );
// Set the open / closed
$('.milestone-meta-release')
// .html(desktopReleaseHTML + ' - ' + websiteReleaseHTML);
.html('Next Desktop: <strong>' + toHumanReadable( nextDesktopReleaseDate ) + '</strong> - Next Web Release: <strong>' + toHumanReadable( nextWebsiteReleaseDate ) + '</strong>');
// Set open/closed issues
$milestone.find('.gitban_milestone_description p')
.html( milestone.description );
getIssues();
});
function getStoryPoints(issue) {
var points = 0;
var pointsPattern = /(\d{1,2})pts?/;
issue.labels.forEach(function(label) {
var matched = pointsPattern.exec(label.name);
if (matched) {
points = Number.parseInt(matched[1]);
}
});
return points;
}
/**
* Once we have our milestone load our issues.
*/
function getIssues() {
var closeMessageRegex = /((?:[Cc]los(?:e[sd]?|ing)|[Ff]ix(?:e[sd]|ing)?|[Rr]esolv(?:e[sd]?|ing)) +(?:(?:issues? +)?#\d+(?:(?:, *| +and +)?))+)/g;
var opts = { user: config.username, repo: config.repo, milestone: milestone.number, state: 'all' };
var issuesObj = github.getIssues(config.username, config.repo);
var colCount = [0,0,0];
var users = [];
// Sort issues into kanban columns
issuesObj.list(opts, function(err, data) {
if( err ) {
return;
}
issues = data;
issues.sort(function(a, b) {
return getStoryPoints(b) - getStoryPoints(a);
});
issues.forEach(function(issue) {
var $i = $issueMarkup( issue );
var $userProgress = $();
issue.points = getStoryPoints(issue);
if( !!issue.assignee && users.indexOf(issue.assignee.id) === -1 ) {
users.push(issue.assignee.id);
$userProgress = $issuesColMarkup({
className: 'progress',
stage: issue.assignee.login,
icon: 'pulse'
});
$userProgress.attr('user-id', issue.assignee.id);
$progress.append( $userProgress );
} else if( !!issue.assignee ) {
$userProgress = $('[user-id="' + issue.assignee.id + '"]');
}
// The issue is closed
if( issue.state === 'closed' ) {
$userProgress.find('.gitban_issues_container').append( $i.clone() );
$closedIssues.find('.gitban_issues_container').append( $i.clone() );
colCount[2]++;
// TODO: Don't hardcode "in progress" ?
} else if( issue.labels.map(function(l) { return l.name; }).indexOf('in progress') !== -1 ) {
$inProgressIssues.find('.gitban_issues_container').append( $i );
colCount[1]++;
// Otherwise it's open
} else {
$openIssues.find('.gitban_issues_container').append( $i );
colCount[0]++;
}
});
generateBurndown();
// Set 'total's for each column
var $count = $('.gitban_count');
colCount.forEach(function(num, i) {
$count.eq(i).text( num );
});
$loader.hide();
});
}
function generateBurndown() {
var endDate = new Date(milestone.due_on);
var startDate = new Date(milestone.due_on);
// Back this up two weeks
startDate.setDate( endDate.getDate() - 14 );
var now = new Date();
var diffDays = Math.ceil(Math.abs(startDate.getTime() - endDate.getTime()) / (1000 * 3600 * 24));
var diffDaysUntilNow = Math.ceil(Math.abs(startDate.getTime() - now.getTime()) / (1000 * 3600 * 24)) + 1;
var days = new Array(diffDays);
var points = new Array(days.length);
var untilNow = new Array(diffDaysUntilNow);
var cursor = startDate;
days.fill('day ');
days = days.map(function(d, i) {
return d + (i+1);
});
var allPoints = 0;
issues.forEach(function(issue) {
allPoints += issue.points;
});
untilNow.fill(allPoints);
issues.forEach(function(issue) {
if (issue.state === 'closed') {
var closedDate = new Date(issue.closed_at);
var offset = Math.ceil(Math.abs(startDate.getTime() - closedDate.getTime()) / (1000 * 3600 * 24));
untilNow.forEach(function(points, i) {
if (i >= offset) {
untilNow[i] -= issue.points;
}
})
}
})
points.fill(0);
points = points.map(function(d, i) {
return allPoints / days.length * (days.length - i);
});
var data = {
// A labels array that can contain any sort of values
labels: days,
// Our series array that contains series objects or in this case series data arrays
series: [
points, untilNow
]
};
var options = {
height: 400,
axisX: {
showLabel: true
}
};
// Create a new line chart object where as first parameter we pass in a selector
// that is resolving to our chart container element. The Second parameter
// is the actual data object.
new Chartist.Line('.ct-chart', data, options);
}
}
// Previously opened button
var $prevButton = $();
/**
* Show/Hide the kanban board when pressing the kanban board button
*
* @param {object} e $.Event
*/
function openBoard(e) {
var $container = $('#js-repo-pjax-container > .container')
// var $container = $('.repo-container').closest('.container');
$boardButton.addClass('selected');
$container.addClass('show_gitban');
loadGitHubIssues();
$container.addClass('show_gitban');
$('.repo-container').removeClass('with-full-navigation');
$('.repository-content').hide();
$board.show();
}
$('body').on('click', '.gitban_button', function(e) {
e.preventDefault();
e.stopPropagation();
// Redirecting (rather than reloading) helps avoid annoying GitHub behaviour which can
// break kanban anytime github makes a minor change to their markup.
window.location.href = 'https://github.com/' + config.username + '/' + config.repo;
});
// Open the board if we're at the right url
if( window.location.pathname === '/' + config.username + '/' + config.repo ) {
openBoard();
}
// Listen for templates to paste
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.data) {
insertTextAtCursor(request.data);
}
});
/**
* Insert the text at the cursor into the active element. Note that we're
* intentionally not appending or simply replacing the value of the editable
* field, as we want to allow normal pasting conventions. If you paste at the
* beginning, you shouldn't see all your text be replaced.
* Taken from:
* http://stackoverflow.com/questions/7404366/how-do-i-insert-some-text-where-the-cursor-is
*/
function insertTextAtCursor(text) {
var el = document.activeElement;
var val = el.value;
var endIndex;
var range;
var doc = el.ownerDocument;
if (typeof el.selectionStart === 'number' &&
typeof el.selectionEnd === 'number') {
endIndex = el.selectionEnd;
el.value = val.slice(0, endIndex) + text + val.slice(endIndex);
el.selectionStart = el.selectionEnd = endIndex + text.length;
} else if (doc.selection !== 'undefined' && doc.selection.createRange) {
el.focus();
range = doc.selection.createRange();
range.collapse(false);
range.text = text;
range.select();
}
}
}
});