-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
89 lines (72 loc) · 2.61 KB
/
content.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
const TicketTypeMap = {
"Bug": "bugfix",
"Technical debt": "bugfix",
"Story": "feature",
"Sub-task": "feature",
"Task": "feature",
// Add other mappings as needed
};
function getCategoryForTicketType(ticketType) {
return TicketTypeMap[ticketType] || null;
}
function getErrorMessage(ticketID, ticketTitle, ticketType, ticketCategory) {
let errorMessage = null;
const errorMessages = [];
if (!ticketID) {
errorMessages.push('There is no Ticket ID on this page.');
}
if (!ticketTitle) {
errorMessages.push('There is no Ticket Title on this page.');
}
if (!ticketType) {
errorMessages.push('There is no Ticket Type on this page.');
}
if (ticketType && !ticketCategory) {
errorMessages.push(`There is no mapping defined for Ticket Type "${ticketType}"`);
}
if (errorMessages.length > 0) {
errorMessage = 'Error: \n' + errorMessages.join('\n');
}
return errorMessage;
}
const replaceGermanChars = (str) => {
return str
.replace(/ü/g, 'ue')
.replace(/ö/g, 'oe')
.replace(/ä/g, 'ae')
.replace(/ß/g, 'ss');
};
function getBranchName() {
const ticketIDElement = document.getElementById('key-val');
const ticketTitleElement = document.getElementById('summary-val');
const ticketTypeElement = document.getElementById('type-val');
const ticketID = ticketIDElement ? ticketIDElement.getAttribute('data-issue-key') : null;
const ticketTitle = ticketTitleElement ? ticketTitleElement.textContent.trim() : null;
const ticketType = ticketTypeElement ? ticketTypeElement.textContent.trim() : null;
const ticketCategory = getCategoryForTicketType(ticketType);
console.log('ticketID:', ticketID);
console.log('ticketTitle:', ticketTitle);
console.log('ticketType:', ticketType);
console.log('ticketCategory:', ticketCategory);
let branchName = null;
let errorMessage = null;
if (ticketID && ticketTitle && ticketCategory) {
const sanitizedTitle = replaceGermanChars(ticketTitle.toLowerCase())
.replace(/[^a-z0-9\s]/g, '') // Remove non-alphanumeric characters
.replace(/\s+/g, '-'); // Replace spaces with hyphens
branchName = `${ticketCategory}/${ticketID}-${sanitizedTitle}`;
console.log('branchName:', branchName);
} else {
errorMessage = getErrorMessage(ticketID, ticketTitle, ticketType, ticketCategory);
}
return { branchName, errorMessage };
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getBranchName") {
const { branchName, errorMessage } = getBranchName();;
sendResponse({
branchName: branchName,
errorMessage: errorMessage
});
}
});