-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
251 lines (221 loc) Β· 7.92 KB
/
Copy pathscript.js
File metadata and controls
251 lines (221 loc) Β· 7.92 KB
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
const DEFAULT_CONFIG = {
branchSelector: 'a[href*="odoo-dev"]',
pullRequestBodySelector: '.js-command-palette-pull-body',
};
const loadConfig = (callback) => {
try {
if (!chrome || !chrome.storage || !chrome.storage.sync) {
callback(DEFAULT_CONFIG);
return;
}
} catch (e) {
// `chrome` not available (e.g. running directly in page) β use defaults
callback(DEFAULT_CONFIG);
return;
}
chrome.storage.sync.get(DEFAULT_CONFIG, (items) => {
// In case of any error, silently fall back to defaults
if (chrome.runtime && chrome.runtime.lastError) {
console.warn("Odoo github extension: using default config", chrome.runtime.lastError);
callback(DEFAULT_CONFIG);
return;
}
callback(items || DEFAULT_CONFIG);
});
};
const extractId = (text, prefix) => {
const match = text?.match(new RegExp(`${prefix}-(\\d+)`, "i"));
return match ? match[1] : null;
};
const findId = (text, prefixes) => {
return prefixes.map(prefix => extractId(text, prefix)).find(Boolean)
}
const linkifyIdsInNode = (root, prefixes) => {
if (!root) return;
const walker = document.createTreeWalker(
root,
NodeFilter.SHOW_TEXT,
{
acceptNode(node) {
if (!node.nodeValue || !node.nodeValue.match(/(opw-\d+|task-\d+)/i)) {
return NodeFilter.FILTER_REJECT;
}
const parent = node.parentElement;
if (!parent) return NodeFilter.FILTER_REJECT;
// Avoid code blocks / existing links / our own links
if (["CODE", "PRE", "A"].includes(parent.tagName)) {
return NodeFilter.FILTER_REJECT;
}
if (parent.closest(".odoo-task-linkified")) {
return NodeFilter.FILTER_REJECT;
}
return NodeFilter.FILTER_ACCEPT;
},
},
false
);
const regex = /(opw-(\d+)|task-(\d+))/gi;
const toProcess = [];
let current;
while ((current = walker.nextNode())) {
toProcess.push(current);
}
toProcess.forEach((textNode) => {
const original = textNode.nodeValue;
const frag = document.createDocumentFragment();
let lastIndex = 0;
let match;
while ((match = regex.exec(original))) {
const full = match[0]; // e.g. "opw-1234"
const rawId = match[2] || match[3]; // digits
if (match.index > lastIndex) {
frag.appendChild(document.createTextNode(original.slice(lastIndex, match.index)));
}
const link = document.createElement("a");
link.textContent = full;
link.href = `https://www.odoo.com/odoo/all-tasks/${rawId}`;
link.target = "_blank";
link.rel = "noopener noreferrer";
link.classList.add("odoo-task-linkified");
Object.assign(link.style, {
color: "#58a6ff",
textDecoration: "underline",
cursor: "pointer",
});
frag.appendChild(link);
lastIndex = regex.lastIndex;
}
if (lastIndex < original.length) {
frag.appendChild(document.createTextNode(original.slice(lastIndex)));
}
textNode.parentNode.replaceChild(frag, textNode);
});
};
const getRunbotBatchUrl = () => {
const link = document.querySelector('a[href*="runbot.odoo.com/runbot/batch/"]');
if (!link) return null;
const href = link.getAttribute("href");
const match = href.match(/(https:\/\/runbot\.odoo\.com\/runbot\/batch\/\d+)/);
return match ? match[1] : null;
};
const createButtons = (ticketId, runbotTicketId, branchName, runbotBatchUrl) => {
const showTaskButton = ticketId || runbotTicketId;
const buttons = [];
if (showTaskButton) {
const openTaskButton = document.createElement("a");
openTaskButton.className = "odoo-task-button";
openTaskButton.textContent = "π Task";
openTaskButton.title = "Open corresponding Odoo task";
Object.assign(openTaskButton.style, {
background: "#714B67",
color: "white",
border: "none",
borderRadius: "6px",
padding: "5px 12px",
fontSize: "12px",
fontWeight: "500",
marginLeft: "8px",
cursor: "pointer",
transition: "background 0.2s ease, transform 0.1s ease",
});
const openTaskListener = () => {
let url;
if (ticketId) {
url = `https://www.odoo.com/odoo/all-tasks/${ticketId}`;
} else {
url = `https://runbot.odoo.com/odoo/runbot.build.error/${runbotTicketId}`
}
window.open(url, "_blank");
}
openTaskButton.addEventListener("click", openTaskListener);
buttons.push(openTaskButton);
}
const odooCopyButton = document.createElement("a");
odooCopyButton.className = "odoo-branch-button";
odooCopyButton.textContent = "π Branch";
odooCopyButton.title = "Copy branch name";
Object.assign(odooCopyButton.style, {
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
marginLeft: "8px",
padding: "4px 10px",
fontSize: "12px",
fontWeight: "500",
textDecoration: "none",
color: "#ffffff",
background: "#238636",
border: "1px solid rgba(240,246,252,0.1)",
borderRadius: "6px",
cursor: "pointer",
});
odooCopyButton.addEventListener("click", async () => {
try {
await navigator.clipboard.writeText(branchName);
odooCopyButton.textContent = "β
Copied!";
setTimeout(() => (odooCopyButton.textContent = "π Branch"), 1500);
} catch (err) {
console.error(err);
}
});
buttons.push(odooCopyButton);
if (runbotBatchUrl) {
const runbotButton = document.createElement("a");
runbotButton.className = "odoo-runbot-button";
runbotButton.textContent = "π€ Runbot";
runbotButton.title = "Open Runbot batch";
runbotButton.href = runbotBatchUrl;
runbotButton.target = "_blank";
runbotButton.rel = "noopener noreferrer";
Object.assign(runbotButton.style, {
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
marginLeft: "8px",
padding: "4px 10px",
fontSize: "12px",
fontWeight: "500",
textDecoration: "none",
color: "#ffffff",
background: "#1f6feb",
border: "1px solid rgba(240,246,252,0.1)",
borderRadius: "6px",
cursor: "pointer",
});
buttons.push(runbotButton);
}
return buttons;
};
const run = (config) => {
try {
const { branchSelector, pullRequestBodySelector } = config || DEFAULT_CONFIG;
const [branchElement, stickyBranchElement] = document.querySelectorAll(branchSelector);
const pullRequestBodyElement = document.querySelector(pullRequestBodySelector);
const pullRequestBody = pullRequestBodyElement?.textContent;
const branchTitle = (branchElement?.getAttribute("href") || stickyBranchElement?.getAttribute("href"))?.split("/").pop();
const ticketId = findId(branchTitle, ["opw", "task"]) || findId(pullRequestBody, ["opw", "task"]);
const runbotTicketId = findId(branchTitle, ["runbot"]) || findId(pullRequestBody, ["runbot"]);
const runbotBatchUrl = getRunbotBatchUrl();
// Make opw-XXX / task-XXX in the PR body clickable (handles multiple IDs)
if (pullRequestBodyElement) {
linkifyIdsInNode(pullRequestBodyElement, ["opw", "task"]);
}
// Add buttons to main header
if (branchElement && !branchElement.closest("div").querySelector(".odoo-task-button, .odoo-branch-button")) {
const container = branchElement;
const buttons = createButtons(ticketId, runbotTicketId, branchTitle, runbotBatchUrl);
buttons.forEach(button => container.closest("div").lastElementChild.after(button));
}
// Add buttons to sticky header
if (stickyBranchElement && !stickyBranchElement.closest("div").querySelector(".odoo-task-button, .odoo-branch-button")) {
const stickyContainer = stickyBranchElement;
const buttons = createButtons(ticketId, runbotTicketId, branchTitle, runbotBatchUrl);
buttons.forEach(button => stickyContainer.closest("div").lastElementChild.after(button));
}
} catch (error) {
console.log("Odoo github extension", error)
}
}
window.addEventListener('load', () => {
loadConfig(run);
});