forked from rboufatis/chrome-extension-gitlab-mr-review
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contentscript.js
154 lines (131 loc) · 5.38 KB
/
contentscript.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
function injectScript(file, node) {
var th = document.getElementsByTagName(node)[0];
var s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.setAttribute('src', file);
th.appendChild(s);
}
window.addEventListener("message", function(event) {
if (event.source !== window || !event.data.type || 'GON_VAR' !== event.data.type)
return;
window.gon = event.data.gon;
boot();
}, false);
injectScript(chrome.extension.getURL('windowvar.js'), 'body');
function boot() {
if (typeof window.gon !== 'object') {
return;
}
if (window.gon.api_version !== 'v4') {
return console.error('Invalid gitlab api version.');
}
if (!window.gon.current_username) {
return console.error('No found username.');
}
parseMrLists();
}
function findProjectId() {
const projectResult = document.querySelectorAll('[data-project-id]')[0];
return projectResult && projectResult.getAttribute('data-project-id') || null;
}
function findGroupId() {
const groupResult = document.querySelectorAll('[data-group-id]')[0];
return groupResult && groupResult.getAttribute('data-group-id') || null;
}
function parseMrLists() {
var lists = document.getElementsByClassName('mr-list');
var mrs = [];
for (var i = 0; i < lists.length; i++) {
for (var j = 0; j < lists[i].children.length; j++) {
mrs.push({
id: parseInt(lists[i].children[j].getAttribute('data-id')),
iid: parseInt(lists[i].children[j].getElementsByClassName('issuable-reference')[0].innerText.split('!')[1]),
projectId: findProjectId(),
groupId: findGroupId(),
authorId: parseInt(lists[i].children[j].getElementsByClassName('issuable-authored')[0].getElementsByTagName('a')[0].getAttribute('data-user-id'))
});
}
}
parseMrs(mrs);
}
async function parseMrs(mrs) {
if (!mrs.length) {
return;
}
const type = mrs[0].projectId ? 'projects' : 'groups';
id = mrs[0].projectId || mrs[0].groupId;
if (type === 'groups') {
let total = [];
let nbResult = 100;
let i = 1;
// get the project id of each mr by getting the list of opened mrs in their group id
while(nbResult >= 100) {
const res = await getJson(`/api/v4/${type}/${id}/merge_requests?state=opened&view=simple&per_page=500&page=${i}`);
nbResult = res.length;
total = [...total, ...res];
i++;
}
mrs = mrs.map(mr => {
const moreInfos = total.find(re => re.id === mr.id)
return { ...mr, projectId: moreInfos.project_id };
})
}
// check which of the opened mr the connected gitlab used as approved with a thumbsup
await getJson(`/api/v4/${type}/${id}/merge_requests?my_reaction_emoji=thumbsup&state=opened&view=simple`).then((validatedOpenedMrs) => {
mrs = mrs.map(mr => {
const isValidated = validatedOpenedMrs.some(m => m.id === mr.id)
return { ...mr, isValidated };
})
})
mrs.forEach(mr => {
const mrIid = mr.iid;
const mrId = mr.id;
const authorId = mr.authorId;
const approvedCacheKey = window.gon.current_username + '_approved_' + mrIid;
const commentedCacheKey = window.gon.current_username + '_commented_' + mrIid;
const baseProjectUrl = '/api/v4/projects/' + mr.projectId + '/merge_requests';
chrome.storage.local.get([approvedCacheKey, commentedCacheKey], function (cachedValues) {
identifyYourMrs(mrId, authorId);
if (mr.isValidated) {
identifyMr(mrId, 'approved');
setLocalCacheValue(approvedCacheKey, true);
}
getJson(baseProjectUrl + '/' + mrIid + '/notes').then(function (res) {
const userHasCommented = res.filter(function (comment) {
return comment.author.username === window.gon.current_username;
}).length > 0;
if (userHasCommented) {
identifyMr(mrId, 'commented');
setLocalCacheValue(commentedCacheKey, true);
}
});
});
})
}
function identifyYourMrs(id, userId) {
const el = document.getElementById('merge_request_' + id);
if (window.gon.current_user_id === userId && el.getElementsByClassName('issuable-upvotes').length) {
el.getElementsByClassName('merge-request-title-text')[0].getElementsByTagName('a')[0].style.color = 'green';
}
}
function identifyMr(id, status) {
const el = document.getElementById('merge_request_' + id);
if ('approved' === status && el.getElementsByClassName('issuable-upvotes').length) {
el.getElementsByClassName('issuable-upvotes')[0].style.color = 'red';
} else if ('commented' === status && el.getElementsByClassName('issuable-comments').length) {
el.getElementsByClassName('issuable-comments')[0].getElementsByTagName('a')[0].style.color = 'red';
}
}
function getJson(url) {
return fetch(window.gon.gitlab_url.replace('http://','https://') + url, {
method: 'GET',
credentials: 'include'
}).then(function(res) {
return res.json();
});
}
function setLocalCacheValue(key, value) {
var cache = {};
cache[key] = value;
chrome.storage.local.set(cache)
}