-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab_label_proposal.js
More file actions
156 lines (128 loc) · 4.8 KB
/
gitlab_label_proposal.js
File metadata and controls
156 lines (128 loc) · 4.8 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
// ==UserScript==
// @name Gitlab label proposal
// @namespace https://www.synbioz.com/
// @version 0.3.0
// @description Label proposal for gitlab
// @author Martin Catty
// @include /^https:\/\/git\.synbioz\.com/.*/issues/\d
// @icon https://www.google.com/s2/favicons?domain=gitlab.com
// @run-at document-idle
// @grant none
// ==/UserScript==
(function() {
Array.prototype.compact = function() {
return this.filter(x => x !== undefined && x !== null);
};
const states = [
["to do", "doing"],
["doing", "to validate::functionally"],
["to validate::functionally", ["to do", "to validate::technically"]],
["to validate::technically", "to deploy::staging"],
["to deploy::staging", "to deploy::production"]
];
const header = `
<div class="title hide-collapsed gl-mb-3">
Labels proposal
</div>
`;
const initialStates = states.map(node => node[0]);
const transitions = new Map(states);
const label = document.querySelector(".labels");
const csrf = document.querySelector("meta[name=\"csrf-token\"]").getAttribute("content");
function renderLabelsProposal() {
const labelsToNodes = mapExistingLabelsToStates();
const nextLabels = Array.from(labelsToNodes.keys()).map(label => transitions.get(label)).compact().flat();
clearExistingLabelsProposal();
const html = buildNextLabelsHTML(nextLabels);
appendBlock(html);
registerLabelsProposalListener(labelsToNodes);
}
function clearExistingLabelsProposal() {
document.querySelectorAll(".labels-proposal").forEach(node => node.remove());
}
function registerLabelsProposalListener(labelsToNodes) {
document.querySelectorAll(".label-proposal").forEach(node => {
node.addEventListener("click", event => {
event.preventDefault();
const label = event.target.textContent.trim();
const url = `/api/v4/projects/${document.body.dataset.projectId}/issues/${document.body.dataset.pageTypeId}?add_labels=${encodeURIComponent(label)}`;
const headers = new Headers({
"Content-Type": "application/json",
"x-csrf-token": csrf,
Accept: "application/json"
});
fetch(url, {
method: "PUT",
headers: headers,
withCredentials: true
}).then(response => {
if (response.ok) {
node.remove();
// remove existing labels
labelsToNodes.forEach((nodes, key) => {
// there can be 2 nodes (including the scope), we only need one,
// they have the same parent
const node = nodes[0];
const cross = node.parentNode.nextElementSibling;
if (cross) {
const event = new Event("click");
cross.dispatchEvent(event);
}
});
}
});
});
});
}
// Create a map from existing labels, using this form :
// { "label" => [node1, node2] }
function mapExistingLabelsToStates() {
return Array.from(document.querySelectorAll(".labels .gl-label-text")).reduce((map, node) => {
const sibling = node.nextElementSibling;
let scope = "";
if (sibling !== undefined && sibling !== null) {
scope = "::" + sibling.textContent.trim();
}
const content = node.textContent.trim() + scope;
// we don't want to keep track of label's node not part
// of the states we are watching
if (initialStates.includes(content)) {
map.set(content, [node, sibling].compact());
}
return map;
}, new Map());
}
function buildNextLabelsHTML(nextLabels) {
return nextLabels.reduce((acc, label) => {
acc += `
<div class="hide-collapsed value issuable-show-labels js-value has-labels">
<span data-qa-selector="selected_label_content" data-qa-label-name="to do" class="gl-label gl-label-text-light"
style="--label-background-color:#D10069; --label-inset-border:inset 0 0 0 2px #D10069;">
<a href="#" class="gl-link gl-label-link">
<span class="gl-label-text label-proposal">
${label}
</span>
</a>
</span>
</div>
`;
return acc;
}, header);
}
function appendBlock(html) {
const block = document.createElement("div");
block.className = "block labels-proposal";
label.after(block);
block.innerHTML = html;
}
// re-render proposal when labels change
const observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
if (mutation.target.classList.contains("issuable-show-labels")) {
renderLabelsProposal();
}
}
});
observer.observe(label, { attributes: false, childList: true, subtree: true });
renderLabelsProposal();
})();