-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgitlab-issue-link.js
More file actions
67 lines (67 loc) · 2.91 KB
/
Copy pathgitlab-issue-link.js
File metadata and controls
67 lines (67 loc) · 2.91 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
// Adds a link to the related Drupal.org issue (with title) at the top of GitLab MR pages
(function addDrupalIssueLink() {
let src = chrome.runtime.getURL("common.js");
function tryAddLink() {
const issueRef = document.querySelector('a.ref-container[href^="/issue/"]');
if (!issueRef) return false;
import(src).then(({ utils }) => {
const match = issueRef.getAttribute("href").match(/\/issue\/[^-]+-(\d+)/);
if (!match) return;
const issueId = match[1];
fetch(`https://www.drupal.org/api-d7/node/${issueId}.json`)
.then((response) => response.json())
.then((data) => {
const title =
data && data.title
? data.title.trim().replace(/\s+/g, " ")
: `Drupal.org issue #${issueId}`;
const status = utils.getStatusForId(data.field_issue_status);
const infoDiv = document.createElement("div");
infoDiv.style.display = "block";
infoDiv.style.fontWeight = "bold";
infoDiv.style.marginBottom = "10px";
const link = document.createElement("a");
link.href = `https://drupal.org/i/${issueId}`;
link.textContent = `Drupal.org: ${title}`;
link.style.marginRight = "10px";
infoDiv.appendChild(link);
const contribLink = document.createElement("a");
contribLink.href = `https://new.drupal.org/contribution-record?source_link=https%3A//www.drupal.org/node/${issueId}`;
contribLink.textContent = `[Contribution record]`;
contribLink.style.marginRight = "10px";
infoDiv.appendChild(contribLink);
const statusSpan = document.createElement("span");
statusSpan.textContent = `Status: ${status}`;
statusSpan.style.marginRight = "10px";
infoDiv.appendChild(statusSpan);
const assigneeSpan = document.createElement("span");
assigneeSpan.textContent = "Assigned to: ";
infoDiv.appendChild(assigneeSpan);
let assigneeUri = null;
if (data.field_issue_assigned && data.field_issue_assigned.uri) {
assigneeUri = `${data.field_issue_assigned.uri}.json`;
}
if (assigneeUri) {
fetch(assigneeUri)
.then((response) => response.json())
.then((userData) => {
const name = userData.name ? userData.name : "Unknown";
assigneeSpan.textContent += name;
})
.catch(() => {
assigneeSpan.textContent += "Unknown";
});
} else {
assigneeSpan.textContent += "Unassigned";
}
document.body.insertBefore(infoDiv, document.body.firstChild);
});
});
return true;
}
if (tryAddLink()) return;
const observer = new MutationObserver(() => {
if (tryAddLink()) observer.disconnect();
});
observer.observe(document.body, { childList: true, subtree: true });
})();