-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-Gitlab-MergeRequests.js
314 lines (288 loc) · 11.2 KB
/
MMM-Gitlab-MergeRequests.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
Module.register("MMM-Gitlab-MergeRequests",{
// Default module config.
defaults: {
gitlabUrl: "https://gitlab.com",
accessToken: "",
tableClass: "small",
reloadInterval: 300,
maxEntries: 10,
maxTitleLength: 25,
wrapEvents: false, // wrap events to multiple lines breaking at maxTitleLength
fade: true,
fadePoint: 0.25,
state: "opened",
orderBy: "created_at",
sort: "desc",
scope: "created_by_me",
animationSpeed: 2000,
combineNames: false,
showNamespace: true,
milestone: "",
labels: "",
createdAfter: "",
createdBefore: "",
updatedAfter: "",
updatedBefore: "",
authorId: "",
authorUsername: "",
assigneeId: "",
approverIds: "",
approvedByIds: "",
sourceBranch: "",
targetBranch: "",
search: "",
in: "",
wip: "",
not: "",
environmnet: "",
deployedBefore: "",
deployedAfter: "",
},
// Define required scripts.
getStyles: function () {
return ["MMM-Gitlab-MergeRequests.css", "font-awesome.css"];
},
getScripts: function () {
return ["moment.js"];
},
start: function () {
Log.log("Starting module: " + this.name);
this.addGitlab(this.config);
this.mergeRequestData = [];
this.loaded = false;
},
// Override socket notification handler.
socketNotificationReceived: function (notification, payload) {
if (this.identifier !== payload.id) {
return;
}
if (notification === "MERGE_REQUESTS") {
this.mergeRequestData = payload.mergeRequests;
this.loaded = true;
if (this.config.broadcastEvents) {
this.broadcastEvents();
}
} else if (notification === "MR_FETCH_ERROR") {
Log.error("Merge Request Error. Could not fetch calendar: " + payload.url);
this.loaded = true;
} else if (notification === "MR_INCORRECT_URL") {
Log.error("Merge Request Error. Incorrect url: " + payload.url);
}
this.updateDom(this.config.animationSpeed);
},
// Override dom generator.
getDom: function() {
Log.log("getDom: " + this.name);
var mergeRequests = this.mergeRequestData;
var wrapper = document.createElement("table");
wrapper.className = this.config.tableClass;
if (mergeRequests.length === 0) {
wrapper.innerHTML = this.loaded ? "No matching Merge Requests." : "Loading Merge Requests";
wrapper.className = this.config.tableClass + " dimmed";
return wrapper;
}
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startFade = mergeRequests.length * this.config.fadePoint;
var fadeSteps = mergeRequests.length - startFade;
}
var currentFadeStep = 0;
for (var mr in mergeRequests){
var mergeRequest = mergeRequests[mr];
console.log(mergeRequest);
// var dateAsString = moment(mergeRequest.created_at, "x").format(this.config.dateFormat);
var mrWrapper = document.createElement("tr");
mrWrapper.className = "normal";
if (mr >= startFade) {
currentFadeStep = mr - startFade;
mrWrapper.style.opacity = 1 - (1 / fadeSteps) * currentFadeStep;
}
//author
var avatarWrapper = document.createElement("td");
var avatar = document.createElement("img");
avatar.src = mergeRequest.author.avatar_url;
avatar.alt = "Author"
avatar.className = "avatar"
avatarWrapper.appendChild(avatar);
mrWrapper.appendChild(avatarWrapper);
//title
var titleWrapper = document.createElement("td");
if(this.config.showNamespace){
var projectDiv = document.createElement("div");
projectDiv.innerHTML = this.titleTransform(mergeRequest.projectNamespace, this.config.wrapEvents, this.config.maxTitleLength, this.config.maxTitleLines);
projectDiv.className = "project"
titleWrapper.appendChild(projectDiv);
}
var titleDiv = document.createElement("div");
titleDiv.innerHTML = this.titleTransform(mergeRequest.title, this.config.wrapEvents, this.config.maxTitleLength, this.config.maxTitleLines);
titleDiv.className = "title"
titleWrapper.appendChild(titleDiv);
mrWrapper.appendChild(titleWrapper);
var timeWrapper = document.createElement("td");
moment.utc(mergeRequest.created_at,"YYYY-MM-DDTHH:mm:ss")
timeWrapper.innerHTML = this.capFirst(moment.utc(mergeRequest.created_at,"YYYY-MM-DDTHH:mm:ss").fromNow()); //gitlab returns time utc by default
timeWrapper.className = "time light";
mrWrapper.appendChild(timeWrapper);
var symbolWrapper = document.createElement("td");
symbolWrapper.className = "symbol align-right";
var symbol = document.createElement("span");
if(mergeRequest.merge_status === 'can_be_merged'){
symbol.className = "fa fa-fw fa-check-circle canMerge";
} else if(mergeRequest.merge_status === 'unchecked'){
//this means the merge request has no checks, so it is good to go
symbol.className = "fa fa-fw fa-check-circle canMerge";
} else if(mergeRequest.merge_status === 'cannot_be_merged_recheck' || mergeRequest.merge_status === 'cannot_be_merged'){
symbol.className = "fa fa-fw fa-times-circle canNotMerge";
}
symbolWrapper.appendChild(symbol);
mrWrapper.appendChild(symbolWrapper);
wrapper.appendChild(mrWrapper);
}
return wrapper;
},
getRequestUrl: function() {
let url = this.config.gitlabUrl;
if(!url.endsWith('/') && !url.endsWith('\\')){
url = url + "/api/v4/merge_requests?";
}
url = url + "page=1&per_page="+(this.config.maxEntries*2);
url = url + "&state="+this.config.state;
url = url + "&order_by="+this.config.orderBy;
url = url + "&sort="+this.config.sort;
url = url + "&scope="+this.config.scope;
url = url + "&with_merge_status_recheck=true";
if(this.config.milestone){
url = url + "&milestone="+this.config.milestone;
}
if(this.config.labels){
url = url + "&labels="+this.config.labels;
}
if(this.config.createdAfter){
url = url + "&created_after="+this.config.createdAfter;
}
if(this.config.createdBefore){
url = url + "&created_before="+this.config.createdBefore;
}
if(this.config.updatedAfter){
url = url + "&updated_after="+this.config.updatedAfter;
}
if(this.config.updatedBefore){
url = url + "&updated_before="+this.config.updatedBefore;
}
if(this.config.authorId){
url = url + "&author_id="+this.config.authorId;
}
if(this.config.authorUsername){
url = url + "&author_username="+this.config.authorUsername;
}
if(this.config.assigneeId){
url = url + "&assignee_id="+this.config.assigneeId;
}
if(this.config.approverIds){
url = url + "&approver_ids="+this.config.approverIds;
}
if(this.config.approvedByIds){
url = url + "&approved_by_ids="+this.config.approvedByIds;
}
if(this.config.sourceBranch){
url = url + "&source_branch="+this.config.sourceBranch;
}
if(this.config.targetBranch){
url = url + "&target_branch="+this.config.targetBranch;
}
if(this.config.search){
url = url + "&search="+this.config.search;
}
if(this.config.in){
url = url + "&in="+this.config.in;
}
if(this.config.wip){
url = url + "&wip="+this.config.wip;
}
if(this.config.not){
url = url + "¬="+this.config.not;
}
if(this.config.environmnet){
url = url + "&environmnet="+this.config.environmnet;
}
if(this.config.deployedBefore){
url = url + "&deployed_before="+this.config.deployedBefore;
}
if(this.config.deployedAfter){
url = url + "&deployed_after="+this.config.deployedAfter;
}
return url;
},
addGitlab: function (config) {
this.sendSocketNotification("ADD_GITLAB_INSTANCE", {
id: this.identifier,
config: config
});
},
titleTransform: function (string, wrapEvents, maxLength, maxTitleLines) {
if (typeof string !== "string") {
return "";
}
if (wrapEvents === true) {
var temp = "";
var currentLine = "";
var words = string.split(" ");
var line = 0;
for (var i = 0; i < words.length; i++) {
var word = words[i];
if (currentLine.length + word.length < (typeof maxLength === "number" ? maxLength : 25) - 1) {
// max - 1 to account for a space
currentLine += word + " ";
} else {
line++;
if (line > maxTitleLines - 1) {
if (i < words.length) {
currentLine += "…";
}
break;
}
if (currentLine.length > 0) {
temp += currentLine + "<br>" + word + " ";
} else {
temp += word + "<br>";
}
currentLine = "";
}
}
return (temp + currentLine).trim();
} else {
if (maxLength && typeof maxLength === "number" && string.length > maxLength) {
return string.trim().slice(0, maxLength) + "…";
} else {
return string.trim();
}
}
},
capFirst: function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
/**
* Broadcasts the events to all other modules for reuse.
* The all events available in one array, sorted on startdate.
*/
broadcastEvents: function () {
var eventList = [];
for (var url in this.calendarData) {
var calendar = this.calendarData[url];
for (var e in calendar) {
var event = cloneObject(calendar[e]);
event.symbol = this.symbolsForEvent(event);
event.calendarName = this.calendarNameForUrl(url);
event.color = this.colorForUrl(url);
delete event.url;
eventList.push(event);
}
}
eventList.sort(function (a, b) {
return a.startDate - b.startDate;
});
this.sendNotification("MERGE_REQUESTS", this.mergeRequestData);
}
});