-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
277 lines (228 loc) · 8.5 KB
/
bundle.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
(function() {
var XbYoutrack = {
version: "0.2.5",
renderUi: renderUi,
token: null
}
var event = new CustomEvent('xbYoutrack.loaded', { detail: XbYoutrack });
document.dispatchEvent(event);
function renderUi(options) {
XbYoutrack.token = options.token;
XbYoutrack.fetch = options.fetch;
var wrapper = document.createElement("td");
wrapper.style.paddingLeft = '30px';
var btn = document.createElement("input");
btn.value = "Create YouTrack Ticket";
btn.type = 'button';
btn.classList.add('submit');
btn.onclick = onClick;
wrapper.append(btn);
findButtonPlacement().append(wrapper);
}
async function onClick(event) {
var info = await gatherInfoObject();
var href = convertToUrl(info);
createClientValue(function (){
window.open(href);
});
}
function findButtonPlacement() {
return document.querySelector('input[value="Update"]').closest('tr');
}
async function gatherInfoObject() {
var info = {
project: "WD",
summary: getSummary(),
description: await getDescription(),
};
var params = new URLSearchParams(info);
var attrFields = gatherAttributeFields();
console.log(attrFields);
attrFields.forEach(item => params.append('c', item));
var textFields = gatherTextFields();
console.log(textFields);
textFields.forEach(item => params.append('textFields', item));
console.log(params);
return params;
}
function gatherAttributeFields() {
var startDate = getStartDate();
var verificationDate = getVerificationDate();
var dueDate = getDueDate();
var estimation = getEstimation();
var assignee = getAssignee();
var fields = [
'State ' + getState(),
'Client ' + getClient()
];
if (startDate) {
fields.push('Start Date ' + startDate);
}
if (verificationDate) {
fields.push('Verification date ' + verificationDate);
}
if (dueDate) {
fields.push('Due Date ' + dueDate);
}
if (estimation) {
fields.push('Estimation ' + estimation);
}
if (assignee) {
fields.push('Assignee ' + assignee);
}
return fields;
}
function gatherTextFields() {
var fields = [
{id: '171-1', value: getExternalLinksText()},
{id: '171-4', value: getRepoText()}
];
return fields.map(item => new URLSearchParams(item).toString());
}
function getSummary() {
var regex = /.*?\((.*)\)/;
var input = document.querySelector('input[name="description"]');
var matches = regex.exec(input.value);
return matches && matches.length > 1 ? matches[1] : input.value;
}
async function getDescription() {
// TODO: Figure out how to provide
return ""
let content = await retrieveSpecContent(getSpecUrl())
let spec = filterConstraints(content)
let markdown = convertToMarkdown(spec)
return markdown
}
function retrieveSpecContent(url) {
return new Promise((resolve, reject) => {
if (typeof XbYoutrack.fetch === 'undefined') {
reject(null)
}
XbYoutrack.fetch({
method: "GET",
url: url,
onload: function(response) {
let doc = document.createElement('html')
doc.innerHTML = response.responseText
let spec = doc.querySelector('table.MessageBorder td:first-child')
resolve(spec)
}
})
})
}
function filterConstraints(element) {
var result = document.createElement('div')
var currentIndex = 0
var current = element.childNodes[currentIndex]
const headings = ["H1","H2","H3","H4","H5","H6"]
const isConstraintHeading = (elem) => { return elem.textContent.toLowerCase().includes('constraints') && headings.includes(elem.tagName) }
while (current && !isConstraintHeading(current)) {
result.appendChild(current.cloneNode(true))
currentIndex++
current = element.childNodes[currentIndex]
}
return result
}
function convertToMarkdown(element) {
if (typeof window.TurndownService !== "undefined") {
var converter = window.TurndownService()
return converter.turndown(element)
}
return element.innerText
}
function getSpecUrl() {
var specLink = document.querySelector('a[href*="area=projects&target=specification&"]');
return specLink.href + "&read_only_mode=yes"
}
function getSpecLink() {
var specLink = getSpecUrl()
return specLink ? "Specification: " + specLink : "";
}
function getExternalLinksText() {
return "Task: " + location.href + '\n' + getSpecLink();
}
function getRepoText() {
return '';
}
function getAssignee() {
var select = document.getElementById('u_id');
var assignee = select.options[select.selectedIndex].text.split(",");
return assignee && assignee.length > 1 ? assignee[0] : null;
}
function getState() {
var select = document.getElementById('status');
switch (select.value) {
case 'W':
return 'In Progress';
case 'Q':
return 'Quality Assurance';
case 'E':
case 'X':
return 'Canceled';
case 'V':
return 'Waiting';
case 'T':
return 'Completed';
case 'S':
default:
return 'Scheduled';
}
}
function getStartDate() {
var input = document.getElementById('date_start-data');
return input ? formatDate(input.value) : null;
}
function getDueDate() {
var input = document.getElementById('date_end-data');
return input ? formatDate(input.value) : null;
}
function getVerificationDate() {
var input = document.getElementById('verification_date-data');
return input ? formatDate(input.value) : null;
}
function getEstimation() {
var input = document.querySelector('input[name="hours"]');
var value = parseInt(input.value);
return value ? value + 'h' : null;
}
function getClient() {
var element = document.querySelectorAll('.SubjectInfo_Light')[0];
// Removing <span class="online_company" title="Online last hour">•</span>
return element.innerText.replace("•", "").trim();
}
function formatDate(str) {
var date = new Date(str);
return String(date.getFullYear()) + '-' + String(date.getMonth()+1).padStart(2,'0') + '-' + String(date.getDate()).padStart(2,'0');
}
function convertToUrl(params) {
var baseUrl = "https://xcart.myjetbrains.com/youtrack/";
const url = new URL('newIssue?' + params.toString(), baseUrl);
return url.href;
}
function createClientValue(callback) {
var data = JSON.stringify({
"name": getClient(),
"color": {
"id": "0",
"$type": "FieldStyle"
},
"description": "",
"$type": "EnumBundleElement"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
callback();
}
});
xhr.open("POST", "https://xcart.myjetbrains.com/youtrack/api/admin/customFieldSettings/bundles/enum/77-4/values?$top=-1&fields=$type,archived,assembleDate,avatarUrl,color%28id%29,description,fullName,hasRunningJob,id,isResolved,issueRelatedGroup%28icon%29,localizedName,login,name,ordinal,owner%28id,login,ringId%29,releaseDate,released,ringId,showLocalizedNameInAdmin,teamForProject%28ringId%29,usersCount");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer " + XbYoutrack.token);
xhr.setRequestHeader("Accept", "*/*");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.send(data);
}
})();