-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathcml.js
executable file
·596 lines (499 loc) · 14.6 KB
/
cml.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
const { execSync, spawnSync } = require('child_process');
const gitUrlParse = require('git-url-parse');
const stripAuth = require('strip-url-auth');
const globby = require('globby');
const git = require('simple-git')('./');
const path = require('path');
const fs = require('fs').promises;
const chokidar = require('chokidar');
const winston = require('winston');
const remark = require('remark');
const visit = require('unist-util-visit');
const Gitlab = require('./drivers/gitlab');
const Github = require('./drivers/github');
const BitbucketCloud = require('./drivers/bitbucket_cloud');
const {
upload,
exec,
watermarkUri,
preventcacheUri,
waitForever
} = require('./utils');
const { GITHUB_REPOSITORY, CI_PROJECT_URL, BITBUCKET_REPO_UUID } = process.env;
const GIT_USER_NAME = 'Olivaw[bot]';
const GIT_USER_EMAIL = '[email protected]';
const GIT_REMOTE = 'origin';
const GITHUB = 'github';
const GITLAB = 'gitlab';
const BB = 'bitbucket';
const watcher = chokidar.watch([], {
persistent: true,
followSymlinks: true,
disableGlobbing: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 2000,
pollInterval: 500
}
});
const uriNoTrailingSlash = (uri) => {
return uri.endsWith('/') ? uri.substr(0, uri.length - 1) : uri;
};
const gitRemoteUrl = (opts = {}) => {
const { remote = GIT_REMOTE } = opts;
const url = gitUrlParse(
execSync(`git config --get remote.${remote}.url`).toString('utf8')
);
return stripAuth(url.toString(url.protocol === 'http' ? 'http' : 'https'));
};
const inferToken = () => {
const {
REPO_TOKEN,
repo_token: repoToken,
GITHUB_TOKEN,
GITLAB_TOKEN,
BITBUCKET_TOKEN
} = process.env;
return (
REPO_TOKEN || repoToken || GITHUB_TOKEN || GITLAB_TOKEN || BITBUCKET_TOKEN
);
};
const inferDriver = (opts = {}) => {
const { repo } = opts;
if (repo) {
if (repo.includes('github.com')) return GITHUB;
if (repo.includes('gitlab.com')) return GITLAB;
if (/bitbucket\.(com|org)/.test(repo)) return BB;
}
if (GITHUB_REPOSITORY) return GITHUB;
if (CI_PROJECT_URL) return GITLAB;
if (BITBUCKET_REPO_UUID) return BB;
};
const fixGitSafeDirectory = () => {
const gitConfigSafeDirectory = (value) =>
spawnSync(
'git',
[
'config',
'--global',
value ? '--add' : '--get-all',
'safe.directory',
value
],
{
encoding: 'utf8'
}
).stdout;
const addSafeDirectory = (directory) =>
gitConfigSafeDirectory()
.split(/[\r\n]+/)
.includes(directory) || gitConfigSafeDirectory(directory);
// Fix for git>=2.36.0
addSafeDirectory('*');
// Fix for git^2.35.2
addSafeDirectory('/');
for (
let root, dir = process.cwd();
root !== dir;
{ root, dir } = path.parse(dir)
) {
addSafeDirectory(dir);
}
};
class CML {
constructor(opts = {}) {
fixGitSafeDirectory(); // https://github.com/iterative/cml/issues/970
const { driver, repo, token } = opts;
this.repo = uriNoTrailingSlash(repo || gitRemoteUrl()).replace(
/\.git$/,
''
);
this.token = token || inferToken();
this.driver = driver || inferDriver({ repo: this.repo });
}
async revParse({ ref = 'HEAD' } = {}) {
try {
return await exec(`git rev-parse ${ref}`);
} catch (err) {
winston.warn(
'Failed to obtain SHA. Perhaps not in the correct git folder'
);
}
}
async triggerSha() {
const { sha } = this.getDriver();
return sha || (await this.revParse());
}
async branch() {
const { branch } = this.getDriver();
return branch || (await exec(`git branch --show-current`));
}
getDriver() {
const { driver, repo, token } = this;
if (!driver) throw new Error('driver not set');
if (driver === GITHUB) return new Github({ repo, token });
if (driver === GITLAB) return new Gitlab({ repo, token });
if (driver === BB) return new BitbucketCloud({ repo, token });
throw new Error(`driver ${driver} unknown!`);
}
async commentCreate(opts = {}) {
const triggerSha = await this.triggerSha();
const {
commitSha: inCommitSha = triggerSha,
rmWatermark,
update,
pr,
publish,
publishUrl,
markdownFile,
report: testReport,
watch,
triggerFile
} = opts;
const commitSha =
(await this.revParse({ ref: inCommitSha })) || inCommitSha;
if (rmWatermark && update)
throw new Error('watermarks are mandatory for updateable comments');
const watermark = rmWatermark
? ''
: '![CML watermark](https://raw.githubusercontent.com/iterative/cml/master/assets/watermark.svg)';
let userReport = testReport;
try {
if (!userReport) {
userReport = await fs.readFile(markdownFile, 'utf-8');
}
} catch (err) {
if (!watch) throw err;
}
let report = `${userReport}\n\n${watermark}`;
const drv = this.getDriver();
const publishLocalFiles = async (tree) => {
const nodes = [];
visit(tree, ['definition', 'image', 'link'], (node) => nodes.push(node));
const visitor = async (node) => {
if (node.url && node.alt !== 'CML watermark') {
const absolutePath = path.resolve(
path.dirname(markdownFile),
node.url
);
if (!triggerFile && watch) watcher.add(absolutePath);
try {
node.url = await this.publish({
...opts,
path: absolutePath,
url: publishUrl
});
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
}
};
await Promise.all(nodes.map(visitor));
};
if (publish) {
report = (
await remark()
.use(() => publishLocalFiles)
.process(report)
)
.toString()
.replace(/\\&(.+)=/g, '&$1=');
}
if (watch) {
let first = true;
let lock = false;
watcher.add(triggerFile || markdownFile);
watcher.on('all', async (event, path) => {
if (lock) return;
lock = true;
try {
winston.info(`watcher event: ${event} ${path}`);
await this.commentCreate({
...opts,
update: update || !first,
watch: false
});
if (event !== 'unlink' && path === triggerFile) {
await fs.unlink(triggerFile);
}
} catch (err) {
winston.warn(err);
}
first = false;
lock = false;
});
winston.info('watching for file changes...');
await waitForever();
}
let comment;
const updatableComment = (comments) => {
return comments.reverse().find(({ body }) => {
return body.includes('watermark.svg');
});
};
const isBB = this.driver === BB;
if (pr || isBB) {
let commentUrl;
if (commitSha !== triggerSha)
winston.info(
`Looking for PR associated with --commit-sha="${inCommitSha}".\nSee https://cml.dev/doc/ref/send-comment.`
);
const longReport = `${commitSha.substr(0, 7)}\n\n${report}`;
const [commitPr = {}] = await drv.commitPrs({ commitSha });
const { url } = commitPr;
if (!url && !isBB)
throw new Error(`PR for commit sha "${inCommitSha}" not found`);
if (url) {
const [prNumber] = url.split('/').slice(-1);
if (update)
comment = updatableComment(await drv.prComments({ prNumber }));
if (update && comment) {
commentUrl = await drv.prCommentUpdate({
report: longReport,
id: comment.id,
prNumber
});
} else
commentUrl = await drv.prCommentCreate({
report: longReport,
prNumber
});
if (this.driver !== 'bitbucket') return commentUrl;
}
}
if (update) {
comment = updatableComment(await drv.commitComments({ commitSha }));
if (comment)
return await drv.commentUpdate({
report,
id: comment.id,
commitSha
});
}
return await drv.commentCreate({
report,
commitSha
});
}
async checkCreate(opts = {}) {
const { headSha = await this.triggerSha() } = opts;
return await this.getDriver().checkCreate({ ...opts, headSha });
}
async publish(opts = {}) {
const { title = '', md, native, rmWatermark } = opts;
let mime, uri;
if (native) {
({ mime, uri } = await this.getDriver().upload(opts));
} else {
({ mime, uri } = await upload(opts));
}
if (!rmWatermark) {
const [, type] = mime.split('/');
uri = watermarkUri({ uri, type });
}
uri = preventcacheUri({ uri });
if (md && mime.match('(image|video)/.*'))
return `![](${uri}${title ? ` "${title}"` : ''})`;
if (md) return `[${title}](${uri})`;
return uri;
}
async runnerToken() {
return await this.getDriver().runnerToken();
}
async parseRunnerLog(opts = {}) {
let { data, name } = opts;
if (!data) return [];
data = data.toString('utf8');
const parseId = (key) => {
if (patterns[key]) {
const regex = patterns[key];
const matches = regex.exec(data) || [];
return matches[1];
}
};
const driver = await this.getDriver();
const logs = [];
const patterns = driver.runnerLogPatterns();
for (const status of ['ready', 'job_started', 'job_ended']) {
const regex = patterns[status];
if (regex.test(data)) {
const date = new Date();
const log = {
status,
date: date.toISOString(),
repo: this.repo
};
if (status === 'job_started') {
log.job = parseId('job');
log.pipeline = parseId('pipeline');
if (name && this.driver === GITHUB) {
const { id: runnerId } = await this.runnerByName({ name });
const { id } = await driver.runnerJob({ runnerId });
log.job = id;
}
}
if (status === 'job_ended')
log.success = patterns.job_ended_succeded.test(data);
log.level = log.success ? 'info' : 'error';
logs.push(log);
}
}
return logs;
}
async startRunner(opts = {}) {
return await this.getDriver().startRunner(opts);
}
async registerRunner(opts = {}) {
return await this.getDriver().registerRunner(opts);
}
async unregisterRunner(opts = {}) {
const { id: runnerId } = (await this.runnerByName(opts)) || {};
if (!runnerId) throw new Error(`Runner not found`);
return await this.getDriver().unregisterRunner({ runnerId, ...opts });
}
async runners(opts = {}) {
return await this.getDriver().runners(opts);
}
async runnerByName(opts = {}) {
let { name, runners } = opts;
if (!runners) runners = await this.runners(opts);
return runners.find((runner) => runner.name === name);
}
async runnerById(opts = {}) {
return await this.getDriver().runnerById(opts);
}
async runnersByLabels(opts = {}) {
let { labels, runners } = opts;
if (!runners) runners = await this.runners(opts);
return runners.filter((runner) =>
labels.split(',').every((label) => runner.labels.includes(label))
);
}
async runnerJob({ name, status = 'running' } = {}) {
return await this.getDriver().runnerJob({ status, name });
}
async repoTokenCheck() {
try {
await this.runnerToken();
} catch (err) {
if (err.message === 'Bad credentials')
err.message += ', REPO_TOKEN should be a personal access token';
throw err;
}
}
async ci(opts = {}) {
const {
unshallow = false,
userEmail = GIT_USER_EMAIL,
userName = GIT_USER_NAME,
remote = GIT_REMOTE
} = opts;
const driver = this.getDriver();
await exec(await driver.updateGitConfig({ userName, userEmail, remote }));
if (unshallow) {
if ((await exec('git rev-parse --is-shallow-repository')) === 'true') {
await exec('git fetch --unshallow');
}
}
await exec('git fetch --all');
}
async prCreate(opts = {}) {
const driver = this.getDriver();
const {
remote = GIT_REMOTE,
globs = ['dvc.lock', '.gitignore'],
md,
skipCi,
branch,
message,
title,
body,
merge,
rebase,
squash
} = opts;
await this.ci(opts);
const renderPr = (url) => {
if (md)
return `[CML's ${
this.driver === GITLAB ? 'Merge' : 'Pull'
} Request](${url})`;
return url;
};
const { files } = await git.status();
if (!files.length) {
winston.warn('No files changed. Nothing to do.');
return;
}
const paths = (await globby(globs)).filter((path) =>
files.map((file) => file.path).includes(path)
);
if (!paths.length) {
winston.warn('Input files are not affected. Nothing to do.');
return;
}
const sha = await this.triggerSha();
const shaShort = sha.substr(0, 8);
const target = await this.branch();
const source = branch || `${target}-cml-pr-${shaShort}`;
const branchExists = (
await exec(
`git ls-remote $(git config --get remote.${remote}.url) ${source}`
)
).includes(source);
if (branchExists) {
const prs = await driver.prs();
const { url } =
prs.find(
(pr) => source.endsWith(pr.source) && target.endsWith(pr.target)
) || {};
if (url) return renderPr(url);
} else {
await exec(`git fetch ${remote} ${sha}`);
await exec(`git checkout -B ${target} ${sha}`);
await exec(`git checkout -b ${source}`);
await exec(`git add ${paths.join(' ')}`);
let commitMessage = message || `CML PR for ${shaShort}`;
if (skipCi || (!message && !(merge || rebase || squash))) {
commitMessage += ' [skip ci]';
}
await exec(`git commit -m "${commitMessage}"`);
await exec(`git push --set-upstream ${remote} ${source}`);
}
const url = await driver.prCreate({
source,
target,
title: title || `CML PR for ${target} ${shaShort}`,
description:
body ||
`
Automated commits for ${this.repo}/commit/${sha} created by CML.
`,
skipCi,
autoMerge: merge
? 'merge'
: rebase
? 'rebase'
: squash
? 'squash'
: undefined
});
return renderPr(url);
}
async pipelineRerun(opts) {
return await this.getDriver().pipelineRerun(opts);
}
async pipelineJobs(opts) {
return await this.getDriver().pipelineJobs(opts);
}
logError(e) {
winston.error(e.message);
}
}
module.exports = {
CML,
default: CML,
GIT_USER_EMAIL,
GIT_USER_NAME,
GIT_REMOTE
};