-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.js
More file actions
623 lines (562 loc) · 22.3 KB
/
board.js
File metadata and controls
623 lines (562 loc) · 22.3 KB
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
$(document).ready(function () {
var ISSUE_BOARD_PAGE = "swimlanes.html";
var OAUTH_KEY_VALUE = "";
var OAUTH_STORAGE_KEY = "oath_key";
var IS_GIT_LOGGED_IN = false;
var STORED_REPO_KEY = "preferredRepo";
var PREFERRED_REPO_VALUE = "";
var STORED_MILESTONE_KEY = "preferredMileStone";
var STORED_MILESTONE_VALUE = "";
var LABEL_1_KEY = "label_1";
var LABEL_2_KEY = "label_2";
var LABEL_3_KEY = "label_3";
var LABEL_4_KEY = "label_4";
var LABEL_1_VALUE = "";
var LABEL_2_VALUE = "";
var LABEL_3_VALUE = "";
var LABEL_4_VALUE = "";
var MILESTONE = [];
var MILESTONE_ISSUE = [];
var REPOSITORY = [];
var ISSUE = [];
var LABEL = [];
var GET_ALL_REPO_URL = "https://api.github.com/user/repos";
var GET_STORED_VALUES_FOR_BOARD = "getBoardParameters";
var GET_PREFERRED_REPO = "getPreferredRepo";
var OKAY = "ok";
var SAVE_PARAMETER_ACTION = "saveParameters";
var LEVEL1_ISSUE = [];
var LEVEL2_ISSUE = [];
var LEVEL3_ISSUE = [];
var LEVEL4_ISSUE = [];
var repoObject;
actionForIssueBoardPage();
/**
* Function to perform actions for issue board page.
* Actions include check git oauth key,value in local storage.
* Perform other actions on basis of this value.
* @author nimesh
**/
function actionForIssueBoardPage() {
chrome.runtime.sendMessage({action: GET_STORED_VALUES_FOR_BOARD}, function (response) {
if (response) {
var resonseValue = response.oauthValue;
if (resonseValue != "" || resonseValue != null) {
OAUTH_KEY_VALUE = resonseValue[OAUTH_STORAGE_KEY];
if (OAUTH_KEY_VALUE != "" || !OAUTH_KEY_VALUE) {
IS_GIT_LOGGED_IN = true;
}
PREFERRED_REPO_VALUE = resonseValue[STORED_REPO_KEY];
STORED_MILESTONE_VALUE = resonseValue[STORED_MILESTONE_KEY];
LABEL_1_VALUE = resonseValue[LABEL_1_KEY];
LABEL_2_VALUE = resonseValue[LABEL_2_KEY];
LABEL_3_VALUE = resonseValue[LABEL_3_KEY];
LABEL_4_VALUE = resonseValue[LABEL_4_KEY];
}
if (OAUTH_KEY_VALUE != "" || !OAUTH_KEY_VALUE) {
$("#accessed-content").show();
$("#page-wrapper-access-token").slideToggle("fast");
//call actions for issue page
getGithubRequiredVariables();
} else {
$("#page-wrapper-access-token").show();
$("#accessed-content").hide();
}
}
});
}
/**
* Method to get all required git variables like milestone,repository
* @author nimesh
**/
function getGithubRequiredVariables() {
if (IS_GIT_LOGGED_IN) {
getAllGitRepository(true);
}
}
/**
* Method to get list of all repository
* @author nimesh
**/
function getAllGitRepository(isOldValueSet) {
$.ajax({
url: GET_ALL_REPO_URL,
type: "GET",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "token " + OAUTH_KEY_VALUE)
},
success: function (data) {
var s = $("#repoId");
$.each(data, function (index) {
REPOSITORY[this.id] = this;
$('<option>').val(this.id).text(this.name).attr("data-owner", this.owner).attr("data-repoobject", this).appendTo('#repoId');
});
if (isOldValueSet && PREFERRED_REPO_VALUE) {
$('#repoId').val(PREFERRED_REPO_VALUE);
repoObject = REPOSITORY[PREFERRED_REPO_VALUE];
$("#add-issue").attr("href", "https://github.com/" + repoObject.full_name +"/issues/new");
getAllMileStone(repoObject, true);
getAllLabel(repoObject, true);
getClosedIssueBasedOnMileStone(repoObject);
}
}
});
}
/**
* Method to get list of all Milestone
* @param repoObject A javascript object containing attributes owner(owner organization) and name(name of the repo).
* @author nimesh
**/
function getAllMileStone(repoObject, isOldValueSet) {
var mileStoneApiUrl = "https://api.github.com/repos/" + repoObject.full_name + "/milestones";
$.ajax({
url: mileStoneApiUrl,
type: "GET",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "token " + OAUTH_KEY_VALUE)
},
success: function (data) {
$.each(data, function (index) {
MILESTONE[this.id] = this;
$('<option>').val(this.id).text(this.title).attr("data-milestoneobject", this).appendTo('#mileStone');
});
if (isOldValueSet && STORED_MILESTONE_VALUE)
$('#mileStone').val(STORED_MILESTONE_VALUE);
if (isOldValueSet) {
getIssueBasedOnMileStone(STORED_MILESTONE_VALUE, repoObject);
}
}
});
}
/**
* Method to get all labes for a repo.
* @param repoObject A javascript object containing attributes owner(owner organization) and name(name of the repo).
* @author nimesh
* @modifiedby Rajisha Khadka
**/
function getAllLabel(repoObject, isOldValueSet) {
var labelApiUrl = "https://api.github.com/repos/" + repoObject.full_name + "/labels";
$.ajax({
url: labelApiUrl,
type: "GET",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "token " + OAUTH_KEY_VALUE)
},
success: function (data) {
$('#lane1,#lane2, #lane3, #lane4').empty().append('<option>Select</option>');
$.each(data, function (index) {
LABEL[index] = this;
$('<option>').val(this.name).text(this.name).attr("data-label", this).appendTo('#lane1');
$('<option>').val(this.name).text(this.name).attr("data-label", this).appendTo('#lane2');
$('<option>').val(this.name).text(this.name).attr("data-label", this).appendTo('#lane3');
$('<option>').val(this.name).text(this.name).attr("data-label", this).appendTo('#lane4');
});
if (isOldValueSet) {
if (LABEL_1_VALUE)
$('#lane1').val(LABEL_1_VALUE);
if (LABEL_2_VALUE)
$('#lane2').val(LABEL_2_VALUE);
if (LABEL_3_VALUE)
$('#lane3').val(LABEL_3_VALUE);
if (LABEL_4_VALUE)
$('#lane4').val(LABEL_4_VALUE);
}
}
});
}
/**
* Method to get specific Issue for that repo.
* @param repoObject A javascript object containing attributes owner(owner organization) and name(name of the repo).
* @param issueNumber number of issue
* @author sanjaya
**/
function getSpecificIssues(repoObject, issueNumber) {
var issueApiUrl = "https://api.github.com/repos/" + repoObject.owner + "/" + repoObject.name + "/issues/" + issueNumber;
$.ajax({
url: issueApiUrl,
type: "GET",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "token " + OAUTH_KEY_VALUE)
},
success: function (data) {
}
});
}
/**
* Method to edit specific Issue for that repo.
* @param repoObject A javascript object containing attributes owner(owner organization) and name(name of the repo).
* @param issueNumber number of issue to edit
* @param updateData eg: {
"title": "Found a bug",
"body": "I'm having a problem with this.",
"assignee": "octocat",
"milestone": 1,
"state": "open",
"labels": [
"Label1",
"Label2"
]
}
* @author sanjaya
**/
function editSpecificIssues(repoObject, issueNumber, updateData) {
var issueApiUrl = "https://api.github.com/repos/" + repoObject.full_name + "/issues/" + issueNumber;
var newLabel = '{"labels": ["' + updateData + '"]}';
responseStatus = true;
$.ajax({
url: issueApiUrl,
type: "PATCH",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "token " + OAUTH_KEY_VALUE)
},
data: newLabel,
success: function (data) {
if(data.message === 'Not Found') {
responseStatus = false;
}
}
});
return responseStatus;
}
/**
* Method to save issue board page preferred parameters like repository,milestone,labels.
* @author nimesh
* @param value The value to be saved in local storage.
* @param key The key in which value is to be saved.
**/
function saveIssueBoardPageParameters(key, valueToSave) {
chrome.runtime.sendMessage({
action: SAVE_PARAMETER_ACTION,
valueObj: valueToSave,
key: key
}, function (response) {
if (response.status == OKAY) {
return true
}
});
}
//Defines Draggable Element
$(".draggable").draggable({
revert: "invalid",
zIndex: 10000,
appendTo: "body",
stack: ".draggable"
});
//Defines Droppable Element
$(".droppable").droppable({
tolerance: "pointer",
drop: function (event, ui) {
var draggableNumber = ui.draggable.attr("data-number");
var laneId = $(this).attr("id");
var updateData = "";
//@modifiedBy Mukesh
var status = '';
var draggableId = $(this).attr('id');
var draggableLane = $(ui.draggable.parents()[0]).attr('id');
// prevent further processing and revert position if the block is drag on same lane
if (draggableLane == laneId) {
ui.draggable.draggable({revert:true});
return false;
}
//@modifiedBy Bipen
switch (laneId) {
case "lane1_swim" :
updateData = LABEL_1_VALUE;
break;
case "lane2_swim" :
updateData = LABEL_2_VALUE;
break;
case "lane3_swim" :
updateData = LABEL_3_VALUE;
break;
case "lane4_swim" :
updateData = LABEL_4_VALUE;
break;
case "lane5_swim" :
break;
default :
break;
}
//@modifiedBy Mukesh
// reset the position of the element to zero (so it fits in the drop box)
ui.draggable.appendTo($(this)).css({"left":0, "top":0});
if(laneId === 'lane5_swim') {
status = closeSpecificIssues(repoObject, draggableNumber);
} else {
status = editSpecificIssues(repoObject,draggableNumber , updateData);
}
if(!status) {
ui.draggable.draggable({revert:true});
} else {
updateNewLaneIssueNumber(laneId);
updateOldLaneIssueNumber(draggableLane);
}
}
});
// Returns an array of numbers located in the string
function getNumbers(input) {
var numbers = input.match(/[0-9]+/g);
return numbers[0];
}
function updateNewLaneIssueNumber(input)
{
var nummbers = getNumbers(input);
var lane_count = "#lane"+ nummbers +"-count";
var total_issues = $(lane_count).text();
var new_total_issues = parseInt(total_issues) + 1;
var old_lane_issues =
$(lane_count).text(new_total_issues);
}
function updateOldLaneIssueNumber(input)
{
var nummbers = getNumbers(input);
var lane_count = "#lane"+ nummbers +"-count";
var total_issues = $(lane_count).text();
var new_total_issues = parseInt(total_issues) - 1;
$(lane_count).text(new_total_issues);
}
//Setting Form Interceptor
$("#github-user-setting").on("submit", function (event) {
$(".alert").hide();
event.preventDefault();
if ($("#repoId").val() != null && $("#lane1").val() != null && $("#lane2").val() != null && $("#lane3").val() != null && $("#lane4").val() != null) {
$("#github-user-setting .alert-success").show();
//Save token to Local Storage
saveIssueBoardPageParameters(STORED_REPO_KEY, $("#repoId").val());
saveIssueBoardPageParameters(STORED_MILESTONE_KEY, $("#mileStone").val());
saveIssueBoardPageParameters(LABEL_1_KEY, $("#lane1").val());
saveIssueBoardPageParameters(LABEL_2_KEY, $("#lane2").val());
saveIssueBoardPageParameters(LABEL_3_KEY, $("#lane3").val());
saveIssueBoardPageParameters(LABEL_4_KEY, $("#lane4").val());
location.reload();
} else {
$("#github-user-setting .alert-danger").show();
return false;
}
});
//Access Token Form Interceptor
$("#form-access-token").on("submit", function (event) {
event.preventDefault();
var accessToken = $("#access-token").val();
if (!accessToken) {
$(".at-error").show();
return false;
}
//Save Token to Local Storage
saveIssueBoardPageParameters(OAUTH_STORAGE_KEY, accessToken);
location.reload();
});
/**
* Change event for Repo. Fetch Milestone and Label based on currently selected Repo.
* @author adhpawal
*/
$("#repoId").on("change", function () {
var element = $(this).val();
var repoObject = REPOSITORY[element];
getAllMileStone(repoObject);
getAllLabel(repoObject);
});
$('#lane1').on("change", function () {
var element = $("#repoId").val();
var repoObject = REPOSITORY[element];
})
function getIssueBasedOnMileStone(mileStone, repoObject) {
var issueApiUrl = "https://api.github.com/repos/" + repoObject.full_name + "/issues";
$.ajax({
url: issueApiUrl,
type: "GET",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "token " + OAUTH_KEY_VALUE)
},
success: function (data) {
$.each(data, function (index) {
ISSUE[index] = this;
if (this.mileStone == STORED_MILESTONE_VALUE) {
MILESTONE_ISSUE[this.id] = this;
}
});
// MILESTONE_ISSUE=STORED_MILESTONE_VALUE?MILESTONE_ISSUE:ISSUE;
divideIssueByLabel(ISSUE);
}
});
}
/**
* @author adhpawal
*/
function divideIssueByLabel(issueList) {
$("#lane1-label").html(LABEL_1_VALUE);
$("#lane2-label").html(LABEL_2_VALUE);
$("#lane3-label").html(LABEL_3_VALUE);
$("#lane4-label").html(LABEL_4_VALUE);
// debugger;
$(issueList).each(function () {
var issuePriority = 1;
$(this.labels).each(function () {
issuePriority = getIssuePriority(issuePriority, this.name);
});
setIssuePriorityOrder(issuePriority, this);
});
appendIssueToRespectiveLane(LEVEL1_ISSUE, "lane1_swim");
$("#lane1-count").html(LEVEL1_ISSUE.length);
appendIssueToRespectiveLane(LEVEL2_ISSUE, "lane2_swim");
$("#lane2-count").html(LEVEL2_ISSUE.length);
appendIssueToRespectiveLane(LEVEL3_ISSUE, "lane3_swim");
$("#lane3-count").html(LEVEL3_ISSUE.length);
appendIssueToRespectiveLane(LEVEL4_ISSUE, "lane4_swim");
$("#lane4-count").html(LEVEL4_ISSUE.length);
}
/**
* @author adhpawal
* @param currentPriority
* @param labelName
* @returns {number}
*/
function getIssuePriority(currentPriority, labelName) {
var priorityNewValue = 0;
if (labelName == LABEL_4_VALUE) {
priorityNewValue = 4;
} else if (labelName == LABEL_3_VALUE) {
priorityNewValue = 3;
} else if (labelName == LABEL_2_VALUE) {
priorityNewValue = 2;
} else if (labelName == LABEL_1_VALUE) {
priorityNewValue = 1;
}
return Math.max(currentPriority, priorityNewValue);
}
/**
* @author adhpawal
* @param currentPriority
* @param currentIssue
* @returns {number}
*/
function setIssuePriorityOrder(currentPriority, currentIssue) {
if (currentPriority == 4) {
LEVEL4_ISSUE.push(currentIssue);
} else if (currentPriority == 3) {
LEVEL3_ISSUE.push(currentIssue);
} else if (currentPriority == 2) {
LEVEL2_ISSUE.push(currentIssue);
} else if (currentPriority == 1) {
LEVEL1_ISSUE.push(currentIssue);
}
}
/**
* @author adhpawal
*/
function appendIssueToRespectiveLane(issueList, lane, draggable) {
var issueFormat = "";
//@modifiedby bipen only enable draggable if issue in not closed
var draggableClassName = "",
addImageblock = "";
if( typeof draggable === 'undefined') {
draggableClassName = "draggable";
addImageblock = '<p><a href="#" class="uploadfile">Add image </a></p>';
}
//@modifiedby bipen
$(issueList).each(function () {
// if it is not a pull request dispaly the issue in the board
if(!this.pull_request){
//@modifiedby bipen check if there is assignee in issue if not display repo users avatar
var avatar = (this.assignee == null) ? "user.png" : this.assignee.avatar_url;
issueFormat += '<div class="panel panel-default ' + draggableClassName + '" data-number="' + this.number + '"> <div class="panel-heading"> <h3 class="panel-title"><img alt="Issue Detail" src="' + avatar + '" width="20" height="20"/> <a href="' + this.html_url + '" target="_blank"># Issue ' + this.number + '</a></h3> </div><div class="panel-body"> ' + this.title + addImageblock + ' </div></div>';
}
//@TODO Decide where to display pull request or not
});
$("#" + lane).html(issueFormat);
$(".draggable").draggable({
revert: function (event, ui) {
return !event;
},
zIndex: 10000,
appendTo: "body",
stack: ".draggable"
});
}
$("#toggle").click(function () {
$("#page-wrapper-access-token").slideToggle("slow");
});
$(document).on('click','.uploadfile',function(){
var uploadURL = 'http://localhost/html/GithubUtils/upload.php';
var issueNumber = $(this).closest(".draggable").data('number')
console.log(issueNumber);
/* Creates an `input[type="file]` */
var fileChooser = document.createElement('input');
fileChooser.type = 'file';
fileChooser.addEventListener('change', function () {
var file = fileChooser.files[0];
var formData = new FormData();
formData.append("image", file);
var xhr = new XMLHttpRequest();
xhr.open('POST', uploadURL, true);
xhr.addEventListener('readystatechange', function (evt) {
if (xhr.readyState==4 && xhr.status==200)
{
var response = JSON.parse(xhr.responseText);
createIssueComment(issueNumber, response.filename);
}
});
xhr.send(formData);
form.reset(); // <-- Resets the input so we do get a `change` event,
// even if the user chooses the same file
});
/* Wrap it in a form for resetting */
var form = document.createElement('form');
form.appendChild(fileChooser);
$(fileChooser).click();
});
//@addedby bipen
// function to get all closed issue and display it in closed swimlanes.
function getClosedIssueBasedOnMileStone(repoObject) {
var issueApiUrl = "https://api.github.com/repos/" + repoObject.full_name + "/issues";
$.ajax({
url: issueApiUrl,
type: "GET",
data: {state: 'closed'},
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "token " + OAUTH_KEY_VALUE)
},
success: function (data) {
appendIssueToRespectiveLane(data, "lane5_swim", false);
$("#lane5-count").html(data.length);
}
});
}
function closeSpecificIssues(repoObject, issueNumber) {
var issueApiUrl = "https://api.github.com/repos/" + repoObject.full_name + "/issues/" + issueNumber;
var newLabel = '{"state": "close", "labels" : []}';
var responseStatus = true;
$.ajax({
url: issueApiUrl,
type: "post",
data: newLabel,
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "token " + OAUTH_KEY_VALUE)
},
success: function (data) {
if(data.message === 'Not Found') {
responseStatus = false;
}
}
});
return responseStatus;
}
function createIssueComment(issueNumber, filename){
var issueApiUrl = "https://api.github.com/repos/" + repoObject.full_name + "/issues/" + issueNumber + "/comments";
var comment_messsage = "Attached Files with name: " + filename;
var newComment = '{"body": "'+ comment_messsage +'"}';
$.ajax({
url: issueApiUrl,
type: "post",
data: newComment,
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "token " + OAUTH_KEY_VALUE)
},
success: function (data) {
if(data) {
alert('File uploaded Successfully and added to comments.');
}
}
});
}
});