-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathgitlab.js
598 lines (487 loc) · 15.8 KB
/
gitlab.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
const fetch = require('node-fetch');
const FormData = require('form-data');
const { URL, URLSearchParams } = require('url');
const { spawn } = require('child_process');
const fs = require('fs').promises;
const fse = require('fs-extra');
const { resolve } = require('path');
const { ProxyAgent } = require('proxy-agent');
const { backOff } = require('exponential-backoff');
const { logger } = require('../logger');
const { fetchUploadData, download, gpuPresent } = require('../utils');
const { CI_JOB_ID, CI_PIPELINE_ID, IN_DOCKER } = process.env;
const API_VER = 'v4';
const MAX_COMMENT_SIZE = 1000000;
const ERROR_COMMENT_SIZE =
'GitLab Comment is too large, this is likely caused by the `--publish-native` flag causing the comment to pass the 1M character limit';
class Gitlab {
constructor(opts = {}) {
const { repo, token } = opts;
if (!token) throw new Error('token not found');
if (!repo) throw new Error('repo not found');
this.token = token;
this.repo = repo;
}
async projectPath() {
const repoBase = await this.repoBase();
const projectPath = encodeURIComponent(
this.repo.replace(repoBase, '').substr(1)
);
return projectPath;
}
async repoBase() {
if (this.detectedBase) return this.detectedBase;
const { origin, pathname } = new URL(this.repo);
const possibleBases = await Promise.all(
pathname
.split('/')
.filter(Boolean)
.map(async (_, index, array) => {
const components = [origin, ...array.slice(0, index)];
const path = components.join('/');
try {
if (
(await this.request({ url: `${path}/api/${API_VER}/version` }))
.version
)
return path;
} catch (error) {
return error;
}
})
);
this.detectedBase = possibleBases.find((base) => typeof base === 'string');
if (!this.detectedBase) {
if (possibleBases.length) throw possibleBases[0];
throw new Error('Invalid repository address');
}
return this.detectedBase;
}
async commitCommentCreate(opts = {}) {
const { commitSha, report } = opts;
if (report.length >= MAX_COMMENT_SIZE) throw new Error(ERROR_COMMENT_SIZE);
const projectPath = await this.projectPath();
const endpoint = `/projects/${projectPath}/repository/commits/${commitSha}/comments`;
const body = new URLSearchParams();
body.append('note', report);
await this.request({ endpoint, method: 'POST', body });
return `${this.repo}/-/commit/${commitSha}`;
}
async commitCommentUpdate(opts = {}) {
throw new Error('GitLab does not support comment updates!');
}
async commitComments(opts = {}) {
const { commitSha } = opts;
const projectPath = await this.projectPath();
const endpoint = `/projects/${projectPath}/repository/commits/${commitSha}/comments`;
const comments = await this.request({ endpoint, method: 'GET' });
return comments.map(({ id, note: body }) => {
return { id, body };
});
}
async commitPrs(opts = {}) {
const { commitSha } = opts;
const projectPath = await this.projectPath();
const endpoint = `/projects/${projectPath}/repository/commits/${commitSha}/merge_requests`;
const prs = await this.request({ endpoint, method: 'GET' });
return prs
.filter((pr) => pr.state === 'opened')
.map((pr) => {
const {
web_url: url,
source_branch: source,
target_branch: target
} = pr;
return {
url,
source,
target
};
});
}
async checkCreate() {
throw new Error('Gitlab does not support check!');
}
async upload(opts = {}) {
const { repo } = this;
const projectPath = await this.projectPath();
const endpoint = `/projects/${projectPath}/uploads`;
const { size, mime, data } = await fetchUploadData(opts);
const body = new FormData();
body.append('file', data);
const { url } = await this.request({ endpoint, method: 'POST', body });
return { uri: `${repo}${url}`, mime, size };
}
async runnerToken(body) {
const projectPath = await this.projectPath();
const legacyEndpoint = `/projects/${projectPath}`;
const endpoint = `/user/runners`;
const { id, runners_token: runnersToken } = await this.request({
endpoint: legacyEndpoint
});
if (runnersToken === null) {
if (!body) body = new URLSearchParams();
body.append('project_id', id);
body.append('runner_type', 'project_type');
return (await this.request({ endpoint, method: 'POST', body })).token;
}
return runnersToken;
}
async registerRunner(opts = {}) {
const { tags, name } = opts;
const endpoint = `/runners`;
const body = new URLSearchParams();
body.append('description', name);
body.append('tag_list', tags);
body.append('locked', 'true');
body.append('run_untagged', 'true');
body.append('access_level', 'not_protected');
const token = await this.runnerToken(new URLSearchParams(body));
if (token.startsWith('glrt-')) return { token };
body.append('token', token);
return await this.request({ endpoint, method: 'POST', body });
}
async unregisterRunner(opts = {}) {
const { runnerId } = opts;
const endpoint = `/runners/${runnerId}`;
return await this.request({ endpoint, method: 'DELETE', raw: true });
}
async startRunner(opts) {
const {
workdir,
idleTimeout,
single,
labels,
name,
dockerVolumes = [],
env
} = opts;
const gpu = await gpuPresent();
try {
const bin = resolve(workdir, 'gitlab-runner');
if (!(await fse.pathExists(bin))) {
const arch = process.platform === 'darwin' ? 'darwin' : 'linux';
const url = `https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-${arch}-amd64`;
await download({ url, path: bin });
await fs.chmod(bin, '777');
}
const { protocol, host } = new URL(this.repo);
const { token } = await this.registerRunner({ tags: labels, name });
let waitTimeout = idleTimeout;
if (idleTimeout === 'never') {
waitTimeout = '0';
}
let dockerVolumesTpl = '';
dockerVolumes.forEach((vol) => {
dockerVolumesTpl += `--docker-volumes ${vol} `;
});
const command = `${bin} --log-format="json" run-single \
--builds-dir "${workdir}" \
--cache-dir "${workdir}" \
--url "${protocol}//${host}" \
--name "${name}" \
--token "${token}" \
--wait-timeout ${waitTimeout} \
--executor "${IN_DOCKER ? 'shell' : 'docker'}" \
--docker-image "iterativeai/cml:${gpu ? 'latest-gpu' : 'latest'}" \
${gpu ? '--docker-runtime nvidia' : ''} \
${dockerVolumesTpl} \
${single ? '--max-builds 1' : ''}`;
return spawn(command, { shell: true, env });
} catch (err) {
if (err.message === 'Forbidden')
err.message +=
', check the permissions (scopes) of your GitLab token.\nSee https://cml.dev/doc/self-hosted-runners?tab=GitLab#personal-access-token';
throw new Error(`Failed preparing Gitlab runner: ${err.message}`);
}
}
async runners(opts = {}) {
const endpoint = `/runners?per_page=100`;
const runners = await this.request({ endpoint, method: 'GET' });
return await Promise.all(
runners.map(async ({ id, description, online }) => ({
id,
name: description,
busy:
(
await this.request({
endpoint: `/runners/${id}/jobs`,
method: 'GET'
})
).filter((job) => job.status === 'running').length > 0,
labels: (
await this.request({ endpoint: `/runners/${id}`, method: 'GET' })
).tag_list,
online
}))
);
}
async runnerById({ id } = {}) {
const {
description,
online,
tag_list: labels
} = await this.request({
endpoint: `/runners/${id}`,
method: 'GET'
});
return {
id,
name: description,
labels,
online
};
}
runnerLogPatterns() {
return {
ready: /Starting runner for/,
job_started: /"job":.+received/,
job_ended: /"duration_s":/,
job_ended_succeded: /"duration_s":.+Job succeeded/,
job: /"job":([0-9]+),"/
};
}
async prCreate(opts = {}) {
const projectPath = await this.projectPath();
const { source, target, title, description, skipCi, autoMerge } = opts;
const prTitle = skipCi ? title + ' [skip ci]' : title;
const endpoint = `/projects/${projectPath}/merge_requests`;
const body = new URLSearchParams();
body.append('source_branch', source);
body.append('target_branch', target);
body.append('title', prTitle);
body.append('description', description);
const { web_url: url, iid } = await this.request({
endpoint,
method: 'POST',
body
});
if (autoMerge)
await this.prAutoMerge({ pullRequestId: iid, mergeMode: autoMerge });
return url;
}
/**
* @param {{ pullRequestId: string }} param0
* @returns {Promise<void>}
*/
async prAutoMerge({ pullRequestId, mergeMode, mergeMessage }) {
if (mergeMode === 'rebase')
throw new Error(`Rebase auto-merge mode not implemented for GitLab`);
const projectPath = await this.projectPath();
const endpoint = `/projects/${projectPath}/merge_requests/${pullRequestId}/merge`;
const body = new URLSearchParams();
body.set('merge_when_pipeline_succeeds', true);
body.set('squash', mergeMode === 'squash');
if (mergeMessage) body.set(`${mergeMode}_commit_message`, mergeMessage);
try {
await backOff(() =>
this.request({
endpoint,
method: 'PUT',
body
})
);
} catch ({ message }) {
logger.warn(
`Failed to enable auto-merge: ${message}. Trying to merge immediately...`
);
body.set('merge_when_pipeline_succeeds', false);
this.request({
endpoint,
method: 'PUT',
body
});
}
}
async issueCommentUpsert(opts = {}) {
const projectPath = await this.projectPath();
const { issueId, report, id: commentId } = opts;
if (report.length >= MAX_COMMENT_SIZE) throw new Error(ERROR_COMMENT_SIZE);
const endpoint =
`/projects/${projectPath}/issues/${issueId}/notes` +
`${commentId ? '/' + commentId : ''}`;
const body = new URLSearchParams();
body.append('body', report);
const { id } = await this.request({
endpoint,
method: commentId ? 'PUT' : 'POST',
body
});
return `${this.repo}/-/issues/${issueId}#note_${id}`;
}
async issueCommentCreate(opts = {}) {
const { id, ...rest } = opts;
return this.issueCommentUpsert(rest);
}
async issueCommentUpdate(opts = {}) {
if (!opts.id) throw new Error('Id is missing updating comment');
return this.issueCommentUpsert(opts);
}
async issueComments(opts = {}) {
const projectPath = await this.projectPath();
const { issueId } = opts;
const endpoint = `/projects/${projectPath}/issues/${issueId}/notes`;
const comments = await this.request({
endpoint,
method: 'GET'
});
return comments.map(({ id, body }) => {
return { id, body };
});
}
async prCommentCreate(opts = {}) {
const projectPath = await this.projectPath();
const { report, prNumber } = opts;
if (report.length >= MAX_COMMENT_SIZE) throw new Error(ERROR_COMMENT_SIZE);
const endpoint = `/projects/${projectPath}/merge_requests/${prNumber}/notes`;
const body = new URLSearchParams();
body.append('body', report);
const { id } = await this.request({
endpoint,
method: 'POST',
body
});
return `${this.repo}/-/merge_requests/${prNumber}#note_${id}`;
}
async prCommentUpdate(opts = {}) {
const projectPath = await this.projectPath();
const { report, prNumber, id: commentId } = opts;
if (report.length >= MAX_COMMENT_SIZE) throw new Error(ERROR_COMMENT_SIZE);
const endpoint = `/projects/${projectPath}/merge_requests/${prNumber}/notes/${commentId}`;
const body = new URLSearchParams();
body.append('body', report);
const { id } = await this.request({
endpoint,
method: 'PUT',
body
});
return `${this.repo}/-/merge_requests/${prNumber}#note_${id}`;
}
async prComments(opts = {}) {
const projectPath = await this.projectPath();
const { prNumber } = opts;
const endpoint = `/projects/${projectPath}/merge_requests/${prNumber}/notes`;
const comments = await this.request({
endpoint,
method: 'GET'
});
return comments.map(({ id, body }) => {
return { id, body };
});
}
async prs(opts = {}) {
const projectPath = await this.projectPath();
const { state = 'opened' } = opts;
const endpoint = `/projects/${projectPath}/merge_requests?state=${state}`;
const prs = await this.request({ endpoint, method: 'GET' });
return prs.map((pr) => {
const { web_url: url, source_branch: source, target_branch: target } = pr;
return {
url,
source,
target
};
});
}
async pipelineRerun({ id = CI_PIPELINE_ID, jobId } = {}) {
const projectPath = await this.projectPath();
if (!id && jobId) {
({
pipeline: { id }
} = await this.request({
endpoint: `/projects/${projectPath}/jobs/${jobId}`
}));
}
const { status } = await this.request({
endpoint: `/projects/${projectPath}/pipelines/${id}`,
method: 'GET'
});
if (status === 'running') {
await this.request({
endpoint: `/projects/${projectPath}/pipelines/${id}/cancel`,
method: 'POST'
});
}
await this.request({
endpoint: `/projects/${projectPath}/pipelines/${id}/retry`,
method: 'POST'
});
}
async pipelineJobs(opts = {}) {
throw new Error('Not implemented');
}
async updateGitConfig({ userName, userEmail, remote } = {}) {
const repo = new URL(this.repo);
repo.password = this.token;
repo.username = 'token';
const commands = [
['git', 'config', 'user.name', userName || this.userName],
['git', 'config', 'user.email', userEmail || this.userEmail],
[
'git',
'remote',
'set-url',
remote,
repo.toString() + (repo.toString().endsWith('.git') ? '' : '.git')
]
];
return commands;
}
get workflowId() {
return CI_PIPELINE_ID;
}
get runId() {
return CI_JOB_ID;
}
get sha() {
return process.env.CI_COMMIT_SHA;
}
/**
* Returns the PR number if we're in a PR-related action event.
*/
get pr() {
if ('CI_MERGE_REQUEST_IID' in process.env) {
return process.env.CI_MERGE_REQUEST_IID;
}
return null;
}
get branch() {
if ('CI_COMMIT_BRANCH' in process.env) {
return process.env.CI_COMMIT_BRANCH;
}
return process.env.CI_COMMIT_REF_NAME;
}
get userEmail() {
return process.env.GITLAB_USER_EMAIL;
}
get userName() {
return process.env.GITLAB_USER_NAME;
}
async request(opts = {}) {
const { token } = this;
const { endpoint, method = 'GET', body, raw } = opts;
let { url } = opts;
if (endpoint) {
url = `${await this.repoBase()}/api/${API_VER}${endpoint}`;
}
if (!url) throw new Error('Gitlab API endpoint not found');
logger.debug(`Gitlab API request, method: ${method}, url: "${url}"`);
const headers = { 'PRIVATE-TOKEN': token, Accept: 'application/json' };
const response = await fetch(url, {
method,
headers,
body,
agent: new ProxyAgent()
});
if (!response.ok) {
logger.debug(`Response status is ${response.status}`);
throw new Error(response.statusText);
}
if (raw) return response;
return await response.json();
}
warn(message) {
logger.warn(message);
}
}
module.exports = Gitlab;