-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
270 lines (221 loc) · 7.9 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
(function() {
var JiraYoutrack = {
version: "0.0.6",
renderUi: renderUi,
converter: null,
token: null
}
var stylesheet = `
.css-button, .css-button:visited, .css-button:disabled, .css-button[disabled] {
-webkit-box-align: baseline;
align-items: baseline;
border-width: 0px;
border-radius: 3px;
box-sizing: border-box;
display: inline-flex;
font-size: inherit;
font-style: normal;
font-family: inherit;
font-weight: 500;
max-width: 100%;
position: relative;
text-align: center;
text-decoration: none;
transition: background 0.1s ease-out 0s, box-shadow 0.15s cubic-bezier(0.47, 0.03, 0.49, 1.38) 0s;
white-space: nowrap;
background: rgba(9, 30, 66, 0.04);
cursor: pointer;
height: 2.28571em;
line-height: 2.28571em;
padding: 0px 10px;
vertical-align: middle;
width: auto;
-webkit-box-pack: center;
justify-content: center;
outline: none;
margin: 0px;
color: rgb(66, 82, 110) !important;
}
.css-button:hover {
text-decoration: inherit;
transition: background 0s ease-out 0s, box-shadow 0.15s cubic-bezier(0.47, 0.03, 0.49, 1.38) 0s;
background: rgba(9, 30, 66, 0.08);
}
.css-button:active, .css-button:focus {
transition: background 0s ease-out 0s, box-shadow 0s cubic-bezier(0.47, 0.03, 0.49, 1.38) 0s;
background: rgba(179, 212, 255, 0.6);
color: rgb(0, 82, 204) !important;
}
`
var event = new CustomEvent('jiraYoutrack.loaded', { detail: JiraYoutrack });
document.dispatchEvent(event);
function renderUi(options) {
JiraYoutrack.token = options.token;
JiraYoutrack.fetch = options.fetch;
JiraYoutrack.converter = options.converter;
addStylesheet();
findButtonPlacement().append(renderButton());
}
function renderButton() {
var wrapper = document.createElement("div");
wrapper.setAttribute("role", "presentation");
var span = document.createElement("span");
var btn = document.createElement("button");
btn.type = "button";
btn.classList.add('css-button');
btn.onclick = onClick;
var label = document.createElement("span");
label.textContent = "Copy to YT";
btn.append(label);
span.append(btn);
wrapper.append(span);
return wrapper;
}
function addStylesheet() {
var style = document.createElement("style");
style.innerHTML = stylesheet;
document.getElementsByTagName("head")[0].appendChild(style);
}
async function onClick(event) {
var info = await gatherInfoObject();
var href = convertToUrl(info);
window.open(href);
//createClientValue(function (){
//});
}
function findButtonPlacement() {
return document.querySelector('div[data-test-id="issue.views.issue-details.issue-layout.left-most-column"] button[aria-label="Attach"]').closest('div[role="presentation"]').parentElement;
}
async function gatherInfoObject() {
var info = {
project: "WD",
summary: getSummary(),
description: 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 element = document.querySelector('h1[data-test-id="issue.views.issue-base.foundation.summary.heading"]');
return element ? element.textContent : "Jira issue";
}
function getDescription() {
let content = retrieveSpecContent()
let markdown = convertToMarkdown(content)
return markdown
}
function retrieveSpecContent() {
var container = document.querySelector('div[data-test-id="issue.views.field.rich-text.description"] div.ak-renderer-document');
return container.innerHTML;
}
function convertToMarkdown(element) {
if (typeof JiraYoutrack.converter !== "undefined") {
return JiraYoutrack.converter.turndown(element)
}
return element.innerText
}
function getExternalLinksText() {
return "Jira Ticket: " + location.href + '\n';
}
function getRepoText() {
return '';
}
function getAssignee() {
return null;
}
function getState() {
return 'Submitted';
}
function getStartDate() {
return null;
}
function getDueDate() {
return null;
}
function getVerificationDate() {
return null;
}
function getEstimation() {
var value = "4";
return value ? value + 'h' : null;
}
function getClient() {
return "Internal project";
}
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 " + JiraYoutrack.token);
xhr.setRequestHeader("Accept", "*/*");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.send(data);
}
})();