-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontentscript.js
240 lines (229 loc) · 6.15 KB
/
contentscript.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
// 2 TODO: Check JIRA api v3
// 2 TODO: Split on two content scripts ?
// 4 TODO: Seperate helper for api, add to plan for next version(need migration to npm architecture)
// 4 TODO: Check if exist better way to get jira current open task
if (
window.location.href.match(
/(?=.*(\.atlassian\.net\/jira|jira.*))(?=.*\?selectedIssue\=).*|(.atlassian.net|jira.*)\/browse/g
)
) {
setTimeout(() => chrome.runtime.sendMessage({ activeIcon: true }), 500);
}
chrome.runtime.onMessage.addListener((message) => {
if (message.action === "get_issue_name") {
const origin = window.location.origin;
const pathname = window.location.pathname;
const isJiraTaskPage = pathname.startsWith("/browse/");
const params = new URLSearchParams(window.location.search);
const selectedIssue = params.get("selectedIssue");
let url = "";
if (selectedIssue) {
url = createGetIssueUrl(origin, selectedIssue);
}
if (isJiraTaskPage) {
const issueKey = pathname.split("/")[2];
url = createGetIssueUrl(origin, issueKey);
}
if (url) {
(async () => {
const { copyWithCommand } = await getOptionsFromStorage([
"copyWithCommand",
]);
const [key, summary, issueType] = await fetchIssueData(url);
const branchName = await parseIssueResponse(key, summary, issueType);
const branchNameWithCommand = commandGenerator(
branchName,
copyWithCommand
);
copyToClipboard(branchNameWithCommand);
chrome.runtime.sendMessage({ doneIcon: true });
})();
}
}
});
function registryGenerator(text, option) {
const params = [
{ registry: "uppercase", command: text.toUpperCase() },
{ registry: "lowercase", command: text.toLowerCase() },
];
const generated = params.find(({ registry }) => registry === option);
return generated ? generated.command : text;
}
async function parseTilte(issueTitle, registry, regexpExclude) {
// 2 TODO: Better summary parsing, to massive
if (!issueTitle) return "";
const title = regexpExclude
? issueTitle.replace(
new RegExp(regexpExclude.pattern, regexpExclude.flag),
""
)
: issueTitle;
// 3 TODO: Replace foreign characters with normal
const cyrillicCheck = /[а-яА-ЯЁё]/.test(title);
let transliterated;
if (cyrillicCheck) transliterated = transliterate(title);
const clearTag = transliterated
? transliterated.replace(/[^а-яa-z0-9-.\/ ]*/gi, "")
: title.replace(/[^a-z0-9-.\/ ]*/gi, "");
const trimedTitle = clearTag.trim();
const titleWithoutSpaces = trimedTitle.replace(/[\/ ]/g, "-");
const trimMoreThatOneSlash = titleWithoutSpaces.replace(/-{2,}/g, "-");
const trimMoreThatOneDote = trimMoreThatOneSlash.replace(/\.{2,}/g, ".");
return registryGenerator(trimMoreThatOneDote, registry);
}
function copyToClipboard(textToCopy) {
// 4 TODO check clipboard API problem with no focus
const tempInput = document.createElement("input");
tempInput.style.cssText =
"opacity:0; position:fixed; width:1px; height:1px; top:0; left:0;";
tempInput.value = textToCopy;
document.body.appendChild(tempInput);
tempInput.focus();
tempInput.select();
try {
document.execCommand("selectAll");
document.execCommand("copy");
} catch (err) {
console.error(err);
// 3 TODO: Provide an alternative way to copy the table
}
tempInput.remove();
}
async function parseIssueResponse(key, summary, issueType) {
// 3 TODO: move string construct logic to seperate functions
const {
selectedFlow,
registry,
regexpExclude,
} = await getOptionsFromStorage([
"selectedFlow",
"registry",
"regexpExclude",
]);
const issueKey = registryGenerator(key, registry.key);
let branchName;
if (selectedFlow === "short") return issueKey;
if (selectedFlow === "id") return issueKey.split("-")[1];
const parsedTilte = await parseTilte(summary, registry.name, regexpExclude);
return generateBranchName(issueKey, parsedTilte, selectedFlow, issueType);
}
function commandGenerator(branchName, option) {
return option ? `git checkout -b "${branchName}"` : branchName;
}
function generateBranchName(issueKey, parsedTilte, selectedFlow, issueType) {
if (selectedFlow === "short") return issueKey;
if (selectedFlow === "gitlab") {
if (issueType === "Bug") {
return parsedTilte ? `hotfix/${issueKey}-${parsedTilte}` : "";
} else {
return parsedTilte ? `feature/${issueKey}-${parsedTilte}` : "";
}
} else {
return parsedTilte ? `${issueKey}-${parsedTilte}` : "";
}
}
async function fetchIssueData(url) {
try {
const response = await fetch(url);
const issueData = await response.json();
// 3 TODO: Check fetching issuetype by api
return [
issueData.key,
issueData.fields.summary,
issueData.fields.issuetype.name,
];
} catch (err) {
// 2 TODO: Check log errors for chrome extension, think about error messages
console.error("Failed to fetch issue: ", err);
}
}
function createGetIssueUrl(origin, issueKey) {
return `${origin}/rest/api/2/issue/${issueKey}`;
}
function getOptionsFromStorage(option) {
return new Promise((resolve, reject) => {
chrome.storage.sync.get(option, (options) => {
if (options) {
resolve(options);
} else {
reject(new Error());
}
});
});
}
const ru = {
Ё: "YO",
Й: "I",
Ц: "TS",
У: "U",
К: "K",
Е: "E",
Н: "N",
Г: "G",
Ш: "SH",
Щ: "SCH",
З: "Z",
Х: "H",
Ъ: "'",
ё: "yo",
й: "i",
ц: "ts",
у: "u",
к: "k",
е: "e",
н: "n",
г: "g",
ш: "sh",
щ: "sch",
з: "z",
х: "h",
ъ: "'",
Ф: "F",
Ы: "I",
В: "V",
А: "a",
П: "P",
Р: "R",
О: "O",
Л: "L",
Д: "D",
Ж: "ZH",
Э: "E",
ф: "f",
ы: "i",
в: "v",
а: "a",
п: "p",
р: "r",
о: "o",
л: "l",
д: "d",
ж: "zh",
э: "e",
Я: "Ya",
Ч: "CH",
С: "S",
М: "M",
И: "I",
Т: "T",
Ь: "'",
Б: "B",
Ю: "YU",
я: "ya",
ч: "ch",
с: "s",
м: "m",
и: "i",
т: "t",
ь: "'",
б: "b",
ю: "yu",
};
function transliterate(word) {
return word
.split("")
.map(function (char) {
return ru[char] || char;
})
.join("");
}