-
Notifications
You must be signed in to change notification settings - Fork 1
/
mergejs.js
158 lines (133 loc) · 6.09 KB
/
mergejs.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
const isMergeRequestPage = () => window.location.href.trim('/').endsWith('merge_requests');
const addBUCom = (elem) => elem.classList.add('bucom');
const handleWIP = (elem) => {
const elemWip = elem.querySelector('.merge-request-title-text a');
if (elemWip) {
if (elemWip.textContent.startsWith('WIP')) {
elem.classList.add('wip');
}
}
};
const handleCiFailed = (elem) => {
const elemCi = elem.querySelector('.merge-request-title .ci-status-icon-failed');
if (elemCi !== null) {
elemCi.parentElement.parentElement.parentElement.parentElement.classList.add('ci-failed');
}
};
const handleConflict = (elem) => {
const elemCi = elem.querySelector('.merge-request-title .fa-exclamation-triangle');
if (elemCi !== null) {
elemCi.parentElement.parentElement.parentElement.parentElement.parentElement.classList.add('conflict');
}
};
const handleCanMerge = (elem) => {
const elemThumbsup = elem.querySelector('.controls .fa.fa-thumbs-up');
const elemThumbsdown = elem.querySelector('.controls .fa.fa-thumbs-down');
const numThumbsup = null === elemThumbsup ? 0 : parseInt(elemThumbsup.parentElement.textContent.trim(), 10);
if (null !== elemThumbsdown) {
elem.classList.add('merge-denied');
}
if (null === elemThumbsdown) {
if (numThumbsup < 2) {
elem.classList.add('need-more-thumbs');
} else {
elem.classList.add('good-to-go');
}
}
};
const handleProjects = (elem) => {
const elemProjectName = elem.querySelector('.issuable-reference');
if (elemProjectName) {
const nameProjectFull = elemProjectName.textContent.split('!');
const nameProject = nameProjectFull.shift();
const colorBg = stringToColour(nameProject);
const colorFont = fontColorByBg(colorBg);
const colorRgb = 'rgba(' + hexToRgb(colorBg) + ',0.6)';
elem.querySelector('.issuable-reference').textContent = elemProjectName.textContent.replace('!', ' ! ');
elem.querySelector('.issuable-reference').style.cssText = 'background-color: ' + colorRgb + '!important; color: ' + colorFont + ' !important; text-shadow: 0 1px 1px ' + colorBg + ';';
}
};
const handleMyMerges = (elem) => {
const elemUser = document.querySelector('.header-user-dropdown-toggle');
const elemLinkAuthor = elem.querySelector('.issuable-info .author_link');
if (elemLinkAuthor && elemUser) {
const linkLoggedUser = elemUser.getAttribute('href');
const linkAuthor = elemLinkAuthor.getAttribute('href');
if (linkLoggedUser === linkAuthor) {
elem.classList.add('itsmemario');
}
const colorBg = stringToColour(linkAuthor);
const colorFont = fontColorByBg(colorBg);
const colorRgb = 'rgba(' + hexToRgb(colorBg) + ',0.6)';
elem.querySelector('.issuable-info .author_link').style.cssText = 'background-color: ' + colorRgb + '!important; color: ' + colorFont + ' !important; text-shadow: 0 1px 1px ' + colorBg + ';';
}
};
const stringToColour = (str) => {
let colour = '#'
if (str) {
let hash = 0
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash)
}
for (var i = 0; i < 3; i++) {
var value = (hash >> (i * 8)) & 0xFF;
colour += ('00' + value.toString(16)).substr(-2)
}
} else {
colour = "#FFFFFF"
}
return colour
};
const fontColorByBg = (hexcolor, dark = '#333333', light = '#FFFFFF') => {
var r = parseInt(hexcolor.substr(1,2),16);
var g = parseInt(hexcolor.substr(3,2),16);
var b = parseInt(hexcolor.substr(5,2),16);
var yiq = ((r*299)+(g*587)+(b*114))/1000;
return (yiq >= 155) ? dark : light;
};
const hexToRgb = (hex) => {
var bigint = parseInt(hex.substr(-6), 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;
return r + "," + g + "," + b;
};
const insertAfter = (newNode, target) => {
target.parentNode.insertBefore(newNode, target.nextSibiling);
};
const main = () => {
// const element = `<a href='/groups/mv.com/merge_requests'><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="m5 5.563v4.875c1.024.4 1.75 1.397 1.75 2.563 0 1.519-1.231 2.75-2.75 2.75-1.519 0-2.75-1.231-2.75-2.75 0-1.166.726-2.162 1.75-2.563v-4.875c-1.024-.4-1.75-1.397-1.75-2.563 0-1.519 1.231-2.75 2.75-2.75 1.519 0 2.75 1.231 2.75 2.75 0 1.166-.726 2.162-1.75 2.563m-1 8.687c.69 0 1.25-.56 1.25-1.25 0-.69-.56-1.25-1.25-1.25-.69 0-1.25.56-1.25 1.25 0 .69.56 1.25 1.25 1.25m0-10c.69 0 1.25-.56 1.25-1.25 0-.69-.56-1.25-1.25-1.25-.69 0-1.25.56-1.25 1.25 0 .69.56 1.25 1.25 1.25"></path><path d="m10.501 2c1.381.001 2.499 1.125 2.499 2.506v5.931c1.024.4 1.75 1.397 1.75 2.563 0 1.519-1.231 2.75-2.75 2.75-1.519 0-2.75-1.231-2.75-2.75 0-1.166.726-2.162 1.75-2.563v-5.931c0-.279-.225-.506-.499-.506v.926c0 .346-.244.474-.569.271l-2.952-1.844c-.314-.196-.325-.507 0-.71l2.952-1.844c.314-.196.569-.081.569.271v.93m1.499 12.25c.69 0 1.25-.56 1.25-1.25 0-.69-.56-1.25-1.25-1.25-.69 0-1.25.56-1.25 1.25 0 .69.56 1.25 1.25 1.25"></path></svg></a>`;
const element = `<a href='/groups/mv.com/merge_requests'><svg class="s16"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/assets/icons-f1e1e3187fbe3fe3aa42a17cb2558778f1a2ddc2a1914f0ea7ea59d27b4e425c.svg#git-merge"></use></svg></a>`;
const li = document.createElement('li');
li.innerHTML = element;
insertAfter(li, document.querySelector('.nav.navbar-nav .header-user'));
};
const mergeRequestPipeline = [
addBUCom,
handleWIP,
handleCanMerge,
handleMyMerges,
handleCiFailed,
handleConflict,
handleProjects
];
const mergeRequest = () => {
const elementos = document.querySelectorAll(".merge-request");
for (const elemento of elementos) {
mergeRequestPipeline.map(fn => fn(elemento));
}
};
const mainPipeline = [
main,
mergeRequest
];
const run = () => {
mainPipeline.map(pipeline => pipeline());
};
(function mainLoop() {
if (!document.body.classList.contains('superextensao')) {
document.body.classList.add('superextensao');
run();
}
setTimeout(mainLoop, 1000);
}());