-
-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathlint-commit-messages.mjs
62 lines (47 loc) · 1.87 KB
/
lint-commit-messages.mjs
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
import * as process from 'node:process';
import * as child_process from 'node:child_process';
import { promisify } from 'node:util';
import * as assert from 'node:assert';
import { Octokit } from '@octokit/core';
import chalk from 'chalk';
if (!process.env.CI_PULL_REQUEST) {
// not a PR
process.exit(0);
}
const execAsync = promisify(child_process.exec);
const octokit = new Octokit({ auth: process.env.GH_TOKEN });
const PULL_REQUEST_NUMBER = Number(process.env.CI_PULL_REQUEST.slice(process.env.CI_PULL_REQUEST.lastIndexOf('/') + 1));
assert.ok(!Number.isNaN(PULL_REQUEST_NUMBER), `Could not find valid PR Number: ${PULL_REQUEST_NUMBER}`);
const OPTIONS = {
owner: 'stoplightio',
repo: 'spectral',
id: PULL_REQUEST_NUMBER,
};
const pullRequest = await octokit.request('GET /repos/{owner}/{repo}/pulls/{id}', OPTIONS);
assert.ok(pullRequest.status >= 200 && pullRequest.status < 300);
await lint(pullRequest.data.title, 'Your PR title is invalid');
const autoMergeCommitTitle = pullRequest.data.auto_merge?.commit_title;
if (typeof autoMergeCommitTitle === 'string') {
await lint(pullRequest.data.title, 'Your auto-merge message is invalid');
}
const commits = await octokit.request('GET /repos/{owner}/{repo}/pulls/{id}/commits', OPTIONS);
assert.ok(commits.status >= 200 && commits.status < 300);
for (const { commit } of commits.data) {
if (Array.isArray(commit.parents) && commit.parents.length > 1) {
// possibly a merge commit, carry on
continue;
}
await lint(commit.message);
}
process.stdout.write(chalk.green('✔️ All good. Your commit messages are valid.\n'));
async function lint(title, header) {
try {
await execAsync(`echo "${title}" | yarn commitlint`);
} catch (e) {
if (header) {
process.stderr.write(chalk.redBright(`✖ ${header}\n`));
}
process.stderr.write(chalk.redBright(e.stdout ?? e.message));
process.exit(1);
}
}