-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
270 lines (234 loc) · 6.82 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
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
let QA = document.getElementById("QA");
let Doc = document.getElementById("Documentation");
let Star = document.getElementById("Startest");
let Customer = document.getElementById("Customer");
let Infra = document.getElementById("Infra");
let Visible = document.getElementById("Visible");
let Invisible = document.getElementById("Invisible");
async function restore_options() {
let result = null;
result = await chrome.storage.sync.get(["workCategory"]);
let work_category = result.workCategory;
result = await chrome.storage.sync.get(["featureType"]);
let feature_type = result.featureType;
return [work_category, feature_type];
};
function updateTicket(payload) {
let jira = null;
let url = null;
// On-premise
jira = document.getElementsByName('ajs-issue-key');
if(typeof(jira) != 'undefined' && jira.length > 0) {
jira = jira[0].getAttribute('content');
} else {
jira = null;
}
// data-issue-key should be used when editing with in a filters window
if(jira == null) {
jira = document.getElementById("s-helper-panel-content");
if(jira != null && jira.length > 0) {
jira = jira.getAttribute('data-issue-key');
} else {
jira = null;
}
}
// if(typeof(jira) == 'undefined') {
// jira = jira.getAttribute('content');
// }
// Switch to cloud Jira
if(jira == null) {
title = document.title;
let issue_key = title.match(/\[(.+)\]/i);
if (issue_key.length > 0) {
jira = issue_key[1];
} else {
jira = null;
}
}
// On-premise
url = document.getElementsByName('ajs-base-url');
if (typeof(url) != 'undefined' && url.length > 0) {
url = url[0].getAttribute('content');
} else {
url = null;
}
// Switch to cloud Jira
if (url == null) {
url = document.baseURI;
let base_url = url.match(/(https:\/\/[^/]+)/i);
if (typeof(base_url) != 'undefined' && base_url.length > 0) {
url = base_url[1];
} else {
url = null;
}
}
if (jira == null || url == null) {
console.log("Not in Jira");
return;
}
function handleEvent(e) {
window.location.reload();
}
function addListeners(xhr) {
xhr.addEventListener("loadend", handleEvent);
}
const xhr = new XMLHttpRequest();
addListeners(xhr);
let endpoint = url + "/rest/api/2/issue/" + jira;
xhr.open("PUT", endpoint);
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.onreadystatechange = function () { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
// Request finished. Do processing here.
// console.log(xhr.responseText);
} else {
let text = null;
try {
text = JSON.parse(xhr.responseText);
} catch (e) {};
if (text != null && "errors" in text) {
alert(JSON.stringify(text["errors"]));
}
}
};
json = JSON.stringify(payload);
xhr.send(json);
}
QA.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: updateTicket,
args: [{
"update" : {
"labels" :
[
{ "add": "ManualTesting_NotRequired" }
]
}
}]
});
});
Doc.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: updateTicket,
args: [{
"update" : {
"labels" :
[
{ "add": "Documentation_NotRequired" }
]
}
}]
});
});
Star.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: updateTicket,
args: [{
"update" : {
"labels" :
[
{ "add": "QA-AutomatedTest-Not-Required" }
]
}
}]
});
});
Customer.addEventListener("click", async () => {
[work_category, feature_type] = await restore_options();
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: updateTicket,
args: [{
"update" : {
"labels" :
[
{ "add": "Documentation_NotRequired" },
{ "add": "QA-AutomatedTest-Not-Required" },
{ "add": "ManualTesting_NotRequired" }
]
},
"fields" : {
[work_category] : {"value" : "Development"},
[feature_type] : {"value" : "Customer Feature"}
}
}]
});
});
Infra.addEventListener("click", async () => {
[work_category, feature_type] = await restore_options();
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: updateTicket,
args: [{
"update" : {
"labels" :
[
{ "add": "QA-AutomatedTest-Not-Required" },
{ "add": "ManualTesting_NotRequired" }
]
},
"fields" : {
[work_category] : {"value" : "Development"},
[feature_type] : {"value" : "Infrastructure/Framework"}
}
}]
});
});
Visible.addEventListener("click", async () => {
[work_category, feature_type] = await restore_options();
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: updateTicket,
args: [{
"update" : {
"labels" :
[
{ "add": "Documentation_NotRequired" },
{ "add": "QA-AutomatedTest-Not-Required" },
{ "add": "ManualTesting_NotRequired" }
]
},
"fields" : {
[work_category] : {"value" : "Development"},
[feature_type] : {"value" : "Refactoring Customer Visible"}
}
}]
});
});
Invisible.addEventListener("click", async () => {
[work_category, feature_type] = await restore_options();
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: updateTicket,
args: [{
"update" : {
"labels" :
[
{ "add": "QA-AutomatedTest-Not-Required" },
{ "add": "ManualTesting_NotRequired" }
]
},
"fields" : {
[work_category] : {"value" : "Development"},
[feature_type] : {"value" : "Refactoring Non-Customer Visible"}
}
}]
});
});
document.querySelector('#go-to-options').addEventListener('click', function() {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL('options.html'));
}
});