-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
144 lines (133 loc) · 4.24 KB
/
popup.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
var jiraIssue;
const baseURL = "https://technia.jira.com/browse/";
function ajax(type, url, data, callbacks={}) {
if (type === 'GET' && !Object.keys(callbacks).length){
callbacks = data;
}
let req = new XMLHttpRequest();
for (let event in callbacks) {
if (callbacks.hasOwnProperty(event)) {
req.addEventListener(event, callbacks[event]);
}
}
req.open(type, url);
req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
req.send(data);
}
function runner() {
let jiraIssueInput = document.getElementById('jiraIssue');
if (jiraIssueInput.value){
jiraIssue = jiraIssueInput.value.trim();
}
let jiraAttr = setJiraAttr(jiraIssue);
let project = jiraAttr.group;
let ticketNumber = jiraAttr.number;
if (project && ticketNumber) {
openJiraPage(project, ticketNumber);
} else {
openMyDashboard();
}
}
function setJiraAttr(jiraIssue) {
var project, ticketNumber, jiraArray;
if (/^[A-z]+-[0-9]+$/.test(jiraIssue)) {
jiraArray = jiraIssue.split("-", 2);
project = jiraArray[0];
ticketNumber = jiraArray[1];
localStorage.setItem('project', project);
localStorage.setItem('ticketNumber', ticketNumber);
} else if (/^[0-9]+$/.test(jiraIssue)) {
project = localStorage.getItem('project');
ticketNumber = jiraIssue;
localStorage.setItem('ticketNumber', ticketNumber);
} else {
project = localStorage.getItem('project');
ticketNumber = localStorage.getItem('ticketNumber');;
}
return {
group: project,
number: ticketNumber
};
}
function openJiraPage(project, ticketNumber) {
event.preventDefault();
chrome.tabs.create({
url: baseURL + project + '-' + ticketNumber
});
}
function browseIssue(jira) {
event.preventDefault();
chrome.tabs.create({
url: baseURL + jira
});
}
function openMyDashboard() {
event.preventDefault();
chrome.tabs.create({
url: 'https://technia.jira.com/secure/Dashboard.jspa'
});
}
function checkLoginStatus(callback) {
ajax('GET', 'https://technia.jira.com/rest/auth/1/session', {
load: function(){
let status = this.status;
let data = JSON.parse(this.responseText);
if (status === 200) {
callback(data.name);
} else {
document.getElementById('search').style.visibility = "hidden";
alertError();
}
}
});
}
function myOpenIssues(user) {
event.preventDefault();
let postData = JSON.stringify({
"jql": "assignee = \"" + user + "\" AND status != Resolved AND status != Closed AND status != Verified AND status != Done",
"startAt": 0,
"maxResults": 100,
"fields": [
"summary",
"status",
"assignee"
]
});
ajax('POST', 'https://technia.jira.com/rest/api/latest/search?', postData, {
load: function () {
let status = this.status;
let data = JSON.parse(this.responseText);
let jirasAssignedToUser = [];
let select = document.getElementById("jiraDropDown");
if (this.status !== 200) {
alertError();
return;
}
data.issues.forEach( function(issue) {
jirasAssignedToUser.push(issue.key);
});
jirasAssignedToUser.reverse();
jirasAssignedToUser.forEach( function(jira) {
let element = document.createElement("option");
element.textContent = jira;
element.value = jira;
select.appendChild(element);
});
}
});
}
function alertError() {
let element = document.getElementsByClassName("body")[0];
element.className += " errorAlert";
}
window.addEventListener('load', function() {
checkLoginStatus(function(user){
myOpenIssues(user);
let myIssuesDropdown = document.getElementById('jiraDropDown');
myIssuesDropdown.addEventListener('change', function() {
jiraIssue = myIssuesDropdown.value;
browseIssue(jiraIssue);
});
document.getElementById('runner').addEventListener('submit', runner);
});
});