/
gitlab_merge_requests.1m.js
executable file
Β·553 lines (495 loc) Β· 22 KB
/
gitlab_merge_requests.1m.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#!/usr/bin/env /usr/local/bin/node
// jshint asi:true
// jshint esversion: 8
// <xbar.title>GitLab Merge Requests</xbar.title>
// <xbar.version>v1.0</xbar.version>
// <xbar.author>Simeon Cheeseman</xbar.author>
// <xbar.author.github>simeonc</xbar.author.github>
// <xbar.desc>List of your assigned and created merge requests.
// The menu bar shows a count of MRs requiring your approval (π), WIP MRs (π οΈ), Un-mergable MRs (β), MRs with failed pipelines (β οΈ), MRs with unresolved discussions (π§), MRs with running pipeline (π), MRs pending approval (π¬) and Approved/Ready to Merge (βοΈ).
// Each MR in the dropdown is grouped by project and displays `<Unmergable> "Title"; "Pipeline Status" : "Approval Status"`, clicking on an MR opens it in the browser</xbar.desc>
// <xbar.dependencies>node.js</xbar.dependencies>
// <xbar.image>https://i.imgur.com/t0TtQXO.png</xbar.image>
// <xbar.var>string(GITLAB_API_TOKEN=""): Gitlab token with read_user and read_api scopes. https://gitlab.com/profile/personal_access_tokens</xbar.var>
// <xbar.var>string(GITLAB_HOST="gitlab.com"): The domain your instance is hosted on. Leave the default if using gitlab.com</xbar.var>
// <xbar.var>number(FONT_SIZE=15): Font size of the MR title and status</xbar.var>
// <xbar.var>number(PROJECT_FONT_SIZE=13): Font size of the project heading</xbar.var>
// <xbar.var>select(DARK_MODE="auto"): Choose dark mode settings [auto, dark, light]</xbar.var>
/**
* Information
*
* This was inspired by the work of Shelton Koskie (@eightygrit), who made the
* first version of the "GITLAB Projects" for API v4.
*
* @see GitLab API Documentation https://docs.gitlab.com/ee/api/README.html
* @see Create GitLab Access Token https://gitlab.com/profile/personal_access_tokens
* @see BitBar Node Module Docs https://github.com/sindresorhus/bitbar
*/
const gitlab_domain = process.env.GITLAB_HOST;
const private_token = process.env.GITLAB_API_TOKEN;
const font_size = process.env.FONT_SIZE;
// <xbar.var>boolean(MENU_BAR_SHOW_NEW_MR=true): Show new MR count and icon in the menubar, these are branches that have been pushed today but have no connected MR</xbar.var>
// <xbar.var>boolean(MENU_BAR_SHOW_REQUESTED_REVIEWER=true): show requested reviews count and icon in the menubar</xbar.var>
// <xbar.var>boolean(MENU_BAR_SHOW_WIP=true): show Work In Progress/Draft count and icon in the menubar</xbar.var>
// <xbar.var>boolean(MENU_BAR_SHOW_UNMERGABLE=true): show Unmergable MR count and icon in the menubar</xbar.var>
// <xbar.var>boolean(MENU_BAR_SHOW_UNRESOLVED_DISCUSSION=true): show MRs with unresolved discussion count and icon in the menubar</xbar.var>
// <xbar.var>boolean(MENU_BAR_SHOW_APPROVED=true): show Approved with passing CI MR count and icon in the menubar</xbar.var>
// <xbar.var>boolean(MENU_BAR_SHOW_CI_FAILED=true): show Failed pipeline count and icon in the menubar</xbar.var>
// <xbar.var>boolean(MENU_BAR_SHOW_CI_RUNNING=true): show Pipeline running count and icon in the menubar</xbar.var>
// <xbar.var>boolean(MENU_BAR_SHOW_CI_PENDING=true): show Pending pipeline count and icon in the menubar</xbar.var>
/**
* Change this array to choose which statuses show and in what order on the menu bar.
*
* @type {string[]}
*/
const MENU_BAR_ORDER = [
process.env.MENU_BAR_SHOW_NEW_MR === "true" ? "newMR" : false,
process.env.MENU_BAR_SHOW_REQUESTED_REVIEWER === "true" ? "reviewer" : false,
process.env.MENU_BAR_SHOW_WIP === "true" ? "wip" : false,
process.env.MENU_BAR_SHOW_UNMERGABLE === "true" ? "unmergable" : false,
process.env.MENU_BAR_SHOW_CI_FAILED === "true" ? "failed" : false,
process.env.MENU_BAR_SHOW_UNRESOLVED === "true" ? "unresolved" : false,
process.env.MENU_BAR_SHOW_CI_RUNNING === "true" ? "ciRunning" : false,
process.env.MENU_BAR_SHOW_CI_PENDING === "true" ? "pending" : false,
process.env.MENU_BAR_SHOW_APPROVED === "true" ? "approved" : false,
].filter((k) => !!k);
/**
* MAX length of the title string in the toolbar
*
* @type {number}
*/
const MAX_LENGTH = 60;
/////////////////////////////////////////////////////////////////////////
// Do not edit below this line unless you know what you're doing. :) //
///////////////////////////////////////////////////////////////////////
const fs = require("fs");
const path = require("path");
const SETTINGS_FILE_PATH = path.resolve(
__dirname,
"./.gitlab_merge_requests.settings.json"
);
let bitbar;
// Verify bitbar node module is available or try to install it globally.
try {
bitbar = require("bitbar");
} catch (e) {
try {
bitbar = globalRequire("bitbar");
} catch (e) {
installBitbarModule();
// Not catching error if one is thrown.
bitbar = globalRequire("bitbar");
}
}
const textColor = (() => {
switch (process.env.DARK_MODE) {
case "dark":
return "white";
case "light":
return "black";
case "auto":
default:
return bitbar.darkMode ? "white" : "black";
}
})();
// Converts the gitlab status to emoji
// see https://emojipedia.org/ for customisation
function stateIcon(status) {
return {
created: "π€",
pending: "π€",
running: "π",
failed: "β οΈ",
success: "β
",
skipped: "π",
manual: "β―οΈ",
}[status];
}
const gitlabIconBase64 =
"iVBORw0KGgoAAAANSUhEUgAAAEMAAABACAYAAABBXsrdAAAAAXNSR0IArs4c6QAAAIRlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAACQAAAAAQAAAJAAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAAEOgAwAEAAAAAQAAAEAAAAAAB/P4oQAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAAVlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdX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PA0ByWnTgdwFSQhpsk5snCjzuVOOm/GSRTvFKPQMrRuPwAISUmT7wz4WvVkuPut37DHPYu/m3x5jJ8PLjMoP6uKS91gJGtVxpH8C92rdRD8PuzU95gpeI2GkxRxPQzBp2COcKbCRSWTbwflrAlw6CKsSRd1Wcl0IF7M4DfT53o0VjFGtO7UYqbNv2NjH8/sGX8bmithup+LX4kstd4bBgYF8FaCYicRzry49pkgE3yTUcQwfE4BkDegALcdip8GGKUZZGRYB62CtcWiVTiIMEZQsjuw0+3OAlgNrhLn7Y+jLx15Cj1Cl5AtB3Ccq88ayCshemXDiGZaDq7l/8/QENzkzCMmdmd0BIfgl8yVKI0ABs4qksHOXwqi2OY0Smy6kKPDrUYJhwIONaVGlXvGzJqHLwHBbIZPCI0EgiA0FIwEVbVr1LgRpFdE9nD30T/l86U7oMxfAUigl8EOlksR3yFcSzsCDrMKLIHWkWF1iveoUYp6qQp1Gn96uIh5Jbs+3HP0z4wNtL7E8pL1GnVvqJtcTSi3f0WL2nXCF2SlfWu/A6W/h5okMK8hCJ51evJkqPPnkDVvQry4xcQo1Ky+G0GyYA0c67vRniM/IF+3ryuj9pb/LHG1dRrxbl4so1IwAuF+2p3mjkZ7j3zfFdwW+P6beoPGx0IVpENXkFAh5bhC0I4geZdOxSV7WBekn0AwSB7F/PkGgjLPu2UkwDDbyExXRKXcSHeH6Ql+ovPBHnlV5Ng/1YWVm1y73IdKqsXt02+ab6iR8Wlag7TeW6LbJXw+Une6TaJQPLpmb+mHa6fcE3c63M/zd9JXOS5595G80+wTxYrPd2+Y+fbaF4q/7L47eUe3Sp5viDvdxrtOhbb89waW9xWvbpxHBtUEEA8Oft842l9DU9Yk1+hqvm4i0ESgiUATgSYCTQSaCDQRaCLQRODGQOB/dFm0a9MwhJUAAAAASUVORK5CYII=";
function request(path) {
const httpTransport = require("https");
const responseEncoding = "utf8";
const httpOptions = {
hostname: gitlab_domain,
port: "443",
path,
method: "GET",
headers: { "PRIVATE-TOKEN": private_token },
};
httpOptions.headers["User-Agent"] =
"bitbar/gitlab_projects - node " + process.version;
return new Promise((resolve, reject) => {
const request = httpTransport
.request(httpOptions, (res) => {
let responseBufs = [];
let responseStr = "";
res
.on("data", (chunk) => {
if (Buffer.isBuffer(chunk)) {
responseBufs.push(chunk);
} else {
responseStr = responseStr + chunk;
}
})
.on("end", () => {
if (responseBufs.length > 0) {
responseStr = Buffer.concat(responseBufs).toString(responseEncoding);
}
resolve(JSON.parse(responseStr));
});
})
.setTimeout(0)
.on("error", (error) => {
reject(error);
});
request.write("");
request.end();
});
}
const cachedProjectNames = {};
async function registerProject(project_id) {
if (!cachedProjectNames[project_id]) {
const project = await request(`/api/v4/projects/${project_id}`);
cachedProjectNames[project_id] = project.name_with_namespace;
}
}
async function getSettings() {
let settings = {};
if (fs.existsSync(SETTINGS_FILE_PATH)) {
settings = JSON.parse(fs.readFileSync(SETTINGS_FILE_PATH));
}
if (!settings.userId) {
const myUser = await request("/api/v4/user");
settings.userId = myUser.id;
fs.writeFileSync(
SETTINGS_FILE_PATH,
JSON.stringify({
settings,
})
);
}
return settings;
}
async function getMRs() {
const { userId, todaysUUID, todaysExcludedEvents = [] } = await getSettings();
const statusCount = {};
function incrementStatusCount(key) {
statusCount[key] = statusCount[key] || 0;
statusCount[key] += 1;
}
const mergeRequestsByProjectId = {};
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
const yesterdayParam = `${yesterday.getFullYear()}-${
yesterday.getMonth() + 1
}-${yesterday.getDate()}`;
let excludedEvents = [];
if (todaysUUID === yesterdayParam) {
excludedEvents = todaysExcludedEvents;
}
const [todaysEvents, createdMRs, assignedMRs, approvalMRs] =
await Promise.all([
request(
`/api/v4/users/${userId}/events?after=${yesterdayParam}&action=pushed`
),
request("/api/v4/merge_requests?scope=created_by_me&state=opened"),
request("/api/v4/merge_requests?scope=assigned_to_me&state=opened"),
request(
`/api/v4/merge_requests?scope=all&state=opened&approver_ids[]=${userId}`
),
request(
`/api/v4/merge_requests?scope=all&state=opened&reviewer_id[]=${userId}`
),
]);
const allMRs = assignedMRs
.concat(approvalMRs, createdMRs)
.reduce((uniqueMRs, currentMR) => {
if (!uniqueMRs.find(({ id }) => id === currentMR.id)) {
uniqueMRs.push(currentMR);
}
return uniqueMRs;
}, []);
if (allMRs.length === 0) {
// Hide when no open MRs
return bitbar([
{
image: gitlabIconBase64,
text: `0 MR`,
color: textColor,
dropdown: true,
},
]);
}
await Promise.all(
allMRs
.map((mergeRequest) => {
const { project_id, iid } = mergeRequest;
return Promise.all([
Promise.resolve(mergeRequest),
request(
`/api/v4/projects/${project_id}/merge_requests/${iid}/pipelines`
),
request(
`/api/v4/projects/${project_id}/merge_requests/${iid}/approval_state`
),
registerProject(project_id),
]);
})
.map(async (promises) => {
const [mergeRequest, pipelines, approvals] = await promises;
const {
web_url,
merge_status,
title,
project_id,
iid,
blocking_discussions_resolved,
work_in_progress,
assignee,
assignees,
author,
source_branch,
} = mergeRequest;
excludedEvents.push(`${project_id}_${source_branch}`);
const isReviewer =
(!author || author.id !== userId) &&
(!assignee || assignee.id !== userId) &&
(!assignees || !assignees.find(({ id }) => id === userId));
mergeRequestsByProjectId[project_id] =
mergeRequestsByProjectId[project_id] || [];
let pipelineStatus = "";
if (pipelines.length) {
pipelineStatus = stateIcon(pipelines[0].status);
}
const isApproved = approvals.rules.reduce(
(isApproved, { approved }) => isApproved && approved,
true
);
const hasCurrentUserApproved = approvals.rules.reduce(
(currentUserHasApproved, { approved_by }) =>
currentUserHasApproved ||
!!approved_by.find(({ id }) => id === userId),
false
);
const canMerge = merge_status === "can_be_merged";
if (
isReviewer &&
(work_in_progress ||
!canMerge ||
isApproved ||
hasCurrentUserApproved)
)
return;
let mergeStatus = " ";
if (!canMerge) {
mergeStatus = "β ";
} else if (isReviewer) {
mergeStatus = "π ";
}
let approvalStatus = "π¬";
if (work_in_progress) {
approvalStatus = "π οΈ";
} else if (!blocking_discussions_resolved) {
approvalStatus = "π§";
} else if (isApproved) {
approvalStatus = "βοΈ";
}
if (isReviewer) {
incrementStatusCount("reviewer");
} else if (work_in_progress) {
incrementStatusCount("wip");
} else if (merge_status !== "can_be_merged") {
incrementStatusCount("unmergable");
} else if (pipelines.length && pipelines[0].status === "failed") {
incrementStatusCount("failed");
} else if (!blocking_discussions_resolved) {
incrementStatusCount("unresolved");
} else if (pipelines.length && pipelines[0].status === "running") {
incrementStatusCount("ciRunning");
} else if (!isApproved) {
incrementStatusCount("pending");
} else {
incrementStatusCount("approved");
}
let statusString = "";
if (pipelineStatus || approvals.rules.length) {
const statusStrings = [];
if (pipelineStatus) statusStrings.push(pipelineStatus);
if (approvals.rules.length) statusStrings.push(approvalStatus);
statusString = ` ${statusStrings.join(" ")}`;
}
let trimmedTitle = title.substring(0, MAX_LENGTH);
if (title.length > MAX_LENGTH) {
trimmedTitle += "...";
}
mergeRequestsByProjectId[project_id].push(
`${mergeStatus}!${iid}: ${trimmedTitle}${statusString} | href="${web_url}" size=${font_size}`
);
})
);
for (let i = 0; i < todaysEvents.length; i += 1) {
const {
project_id,
push_data: { action, ref, ref_type, commit_to },
} = todaysEvents[i];
if (excludedEvents.indexOf(`${project_id}_${ref}`) !== -1) continue;
if (!(action === "created" && ref_type === "branch")) continue;
const mrCheck = await request(
`/api/v4/projects/${project_id}/repository/commits/${commit_to}/merge_requests`
);
if (mrCheck.length) {
excludedEvents.push(`${project_id}_${ref}`);
continue;
}
await registerProject(project_id);
const { web_url } = await request(`/api/v4/projects/${project_id}`);
const params = [
`merge_request%5Bsource_branch%5D=${ref}`,
`merge_request%5Bsource_project_id%5D=${project_id}`,
`merge_request%5Btarget_project_id%5D=${project_id}`,
];
const createMRUrl = `${web_url}/-/merge_requests/new?${params.join("&")}`;
mergeRequestsByProjectId[project_id] =
mergeRequestsByProjectId[project_id] || [];
mergeRequestsByProjectId[project_id].unshift(
`π: ${ref} | href="${createMRUrl}" size=${font_size}`
);
incrementStatusCount("newMR");
}
const content = [];
const statusMap = {
reviewer: "π",
unmergable: "β",
failed: "β οΈ",
ciRunning: "π",
pending: "π¬",
approved: "βοΈ",
wip: "π οΈ",
unresolved: "π§",
newMR: "π",
};
const statusString = MENU_BAR_ORDER.map((key) => {
if (statusCount[key]) {
return `${statusMap[key]}[${statusCount[key]}]`;
}
})
.filter((s) => !!s)
.join(", ");
content.push({
image: gitlabIconBase64,
text: `${statusString}`,
color: textColor,
dropdown: true,
});
Object.keys(mergeRequestsByProjectId).forEach((projectId) => {
if (!mergeRequestsByProjectId[projectId].length) return;
content.push(bitbar.separator);
content.push({
text: cachedProjectNames[projectId],
size: process.env.PROJECT_FONT_SIZE,
});
mergeRequestsByProjectId[projectId].forEach((text) =>
text ? content.push({ text }) : null
);
});
// Update settings file for speed later
fs.writeFileSync(
SETTINGS_FILE_PATH,
JSON.stringify({
userId,
todaysUUID: yesterdayParam,
todaysExcludedEvents: excludedEvents.reduce((uniqueEvents, event) => {
if (uniqueEvents.indexOf(event) === -1) {
uniqueEvents.push(event);
}
return uniqueEvents;
}, []),
})
);
try {
bitbar(content);
} catch (error) {
console.log(content);
throw error;
}
}
getMRs().catch((error) => {
console.error(error);
});
// These utility functions taken from another plugin;
// https://github.com/matryer/xbar-plugins/blob/f1004a74a0d887b9655c71a1d52ba4e02b37fd77/Dev/Gitlab/gitlab_projects.js
/**
* Sets up the ability to require global node packages.
*
* @return {object} Returns the required node package object
*/
function globalRequire(packageName) {
const childProcess = require("child_process");
const path = require("path");
const fs = require("fs");
const env = Object.assign({}, process.env);
env.PATH = path.resolve("/usr/local/bin") + ":" + env.PATH;
const globalNodeModulesDir =
childProcess
.execSync(npmBin() + " root -g", { env: env })
.toString()
.trim() + "/";
let packageDir = path.join(globalNodeModulesDir, packageName, "/");
//find package required by older versions of npm
if (!fs.existsSync(packageDir)) {
packageDir = path.join(
globalNodeModulesDir,
"npm/node_modules/",
packageName
);
}
// Package not found
if (!fs.existsSync(packageDir)) {
throw new Error("Cannot find global module '" + packageName + "'");
}
const packageMeta = JSON.parse(
fs.readFileSync(path.join(packageDir, "package.json")).toString()
);
const main = path.join(packageDir, packageMeta.files[0]);
return require(main);
}
/**
* Installs Bitbar node module if it doesn't exits.
*
* @see BitBar node module on github https://github.com/sindresorhus/bitbar
*/
function installBitbarModule() {
// Allows one to run the npm command as if on the command line.
const childProcess = require("child_process");
const execSync = childProcess.execSync;
const path = require("path");
const env = Object.assign({}, process.env);
env.PATH = path.resolve("/usr/local/bin") + ":" + env.PATH;
// Get the path to npm bin
const npm = npmBin();
// The install command
const cmd = npm + " install -g bitbar@1";
console.log("Installing the BitBar Node module...");
execSync(cmd, {
cwd: process.cwd(),
env: env,
})
.toString("utf8")
.trim();
console.log("Installation complete.");
}
/**
* Gets the path to your npm executable.
*
* @return {string} The full path to your npm executable
*/
function npmBin() {
const path = require("path");
const childProcess = require("child_process");
const execSync = childProcess.execSync;
const env = Object.assign({}, process.env);
env.PATH = path.resolve("/usr/local/bin") + ":" + env.PATH;
// Get the path to npm bin
return execSync("which npm", { env: env }).toString("utf8").trim();
}