Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 12c8e2b

Browse files
committed
CI/CD: Added commit-linter workflow
1 parent 4178075 commit 12c8e2b

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

.github/workflows/lint-commits.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Commit linter
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
lint_commits:
10+
runs-on: ubuntu-20.04
11+
if: always()
12+
13+
steps:
14+
- name: Lint PR commits
15+
uses: actions/github-script@v7
16+
with:
17+
script: |
18+
const rules = [
19+
{
20+
pattern: /^[^\r]*$/,
21+
error: "Commit message contains CRLF line breaks (only unix-style LF linebreaks are allowed)",
22+
},
23+
{
24+
pattern: /^.+(\r?\n(\r?\n.*)*)?$/,
25+
error: "Empty line between commit title and body is missing",
26+
},
27+
{
28+
pattern: /^.{0,72}(?:\r?\n(?:(.{0,72})|(.*?([a-z]+:\/\/)?(([a-zA-Z0-9_]|-)+\.)+[a-z]{2,}(:\d+)?([a-zA-Z_0-9@:%\+.~\?&/=]|-)+).*?))*$/,
29+
error: "Commit message lines are too long (maximum allowed is 72 characters, except for URLs)",
30+
},
31+
{
32+
pattern: /^\S.*?\S: .+/,
33+
error: "Missing category in commit title (if this is a fix up of a previous commit, it should be squashed)",
34+
},
35+
{
36+
pattern: /^\S.*?: [A-Z0-9]/,
37+
error: "First word of commit after the subsystem is not capitalized",
38+
},
39+
{
40+
pattern: /^.+[^.\n](\r?\n.*)*$/,
41+
error: "Commit title ends in a period",
42+
},
43+
];
44+
45+
const { repository, pull_request } = context.payload;
46+
47+
// NOTE: This maxes out at 250 commits. If this becomes a problem, see:
48+
// https://octokit.github.io/rest.js/v18#pulls-list-commits
49+
const opts = github.rest.pulls.listCommits.endpoint.merge({
50+
owner: repository.owner.login,
51+
repo: repository.name,
52+
pull_number: pull_request.number,
53+
});
54+
const commits = await github.paginate(opts);
55+
56+
const errors = [];
57+
for (const { sha, commit: { message } } of commits) {
58+
const commitErrors = [];
59+
for (const { pattern, error } of rules) {
60+
if (!pattern.test(message)) {
61+
commitErrors.push(error);
62+
}
63+
}
64+
if (commitErrors.length > 0) {
65+
const title = message.split("\n")[0];
66+
errors.push([`${title} (${sha}):`, ...commitErrors].join("\n "));
67+
}
68+
}
69+
70+
if (errors.length > 0) {
71+
core.setFailed(`One or more of the commits in this PR do not match the code submission policy:\n\n${errors.join("\n")}`);
72+
}

0 commit comments

Comments
 (0)