-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathconventional-commits-lint.js
101 lines (86 loc) · 2.64 KB
/
conventional-commits-lint.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
"use strict";
const fs = require("fs");
const TITLE_PATTERN =
/^(?<prefix>[^:!(]+)(?<package>\([^)]+\))?(?<breaking>[!])?:.+$/;
const RELEASE_AS_DIRECTIVE = /^\s*Release-As:/im;
const BREAKING_CHANGE_DIRECTIVE = /^\s*BREAKING[ \t]+CHANGE:/im;
const ALLOWED_CONVENTIONAL_COMMIT_PREFIXES = [
"feat",
"fix",
"ci",
"docs",
"chore",
];
const object = process.argv[2];
const payload = JSON.parse(fs.readFileSync(process.stdin.fd, "utf-8"));
let validate = [];
if (object === "pr") {
validate.push({
title: payload.pull_request.title,
content: payload.pull_request.body,
});
} else if (object === "push") {
validate.push(
...payload.commits.map((commit) => ({
title: commit.message.split("\n")[0],
content: commit.message,
})),
);
} else {
console.error(
`Unknown object for first argument "${object}", use 'pr' or 'push'.`,
);
process.exit(0);
}
let failed = false;
validate.forEach((payload) => {
if (payload.title) {
const { groups } = payload.title.match(TITLE_PATTERN);
if (groups) {
if (groups.breaking) {
console.error(
`PRs are not allowed to declare breaking changes at this stage of the project. Please remove the ! in your PR title or commit message and adjust the functionality to be backward compatible.`,
);
failed = true;
}
if (
!ALLOWED_CONVENTIONAL_COMMIT_PREFIXES.find(
(prefix) => prefix === groups.prefix,
)
) {
console.error(
`PR (or a commit in it) is using a disallowed conventional commit prefix ("${groups.prefix}"). Only ${ALLOWED_CONVENTIONAL_COMMIT_PREFIXES.join(", ")} are allowed. Make sure the prefix is lowercase!`,
);
failed = true;
}
if (groups.package && groups.prefix !== "chore") {
console.warn(
"Avoid using package specifications in PR titles or commits except for the `chore` prefix.",
);
}
} else {
console.error(
"PR or commit title must match conventional commit structure.",
);
failed = true;
}
}
if (payload.content) {
if (payload.content.match(RELEASE_AS_DIRECTIVE)) {
console.error(
"PR descriptions or commit messages must not contain Release-As conventional commit directives.",
);
failed = true;
}
if (payload.content.match(BREAKING_CHANGE_DIRECTIVE)) {
console.error(
"PR descriptions or commit messages must not contain a BREAKING CHANGE conventional commit directive. Please adjust the functionality to be backward compatible.",
);
failed = true;
}
}
});
if (failed) {
process.exit(1);
}
process.exit(0);