2021-05-22 22:15:05 +00:00
name : Commit linter
on : [ pull_request_target]
2021-07-27 19:37:17 +00:00
# Make sure to update Meta/lint-commit.sh to match this script when adding new checks!
2021-11-28 18:59:23 +00:00
# (… but don't accept overlong 'fixup!' commit descriptions.)
2021-07-27 19:37:17 +00:00
2021-05-22 22:15:05 +00:00
jobs :
lint_commits :
2022-05-23 20:12:57 +00:00
runs-on : ubuntu-22.04
2021-07-09 19:36:39 +00:00
if : always() && github.repository == 'SerenityOS/serenity'
2021-05-22 22:15:05 +00:00
steps :
2021-10-28 02:33:39 +00:00
- name : Lint PR commits
2022-11-25 17:28:34 +00:00
uses : actions/github-script@v6
2021-05-22 22:15:05 +00:00
with :
2021-10-28 02:33:39 +00:00
script : |
2023-01-18 13:33:45 +00:00
const excludedBotIds = [
49699333 , // dependabot[bot]
] ;
2021-10-28 02:33:39 +00:00
const rules = [
{
pattern : /^[^\r]*$/,
error : "Commit message contains CRLF line breaks (only unix-style LF linebreaks are allowed)" ,
},
2022-01-11 21:31:08 +00:00
{
pattern : /^.+(\r?\n(\r?\n.*)*)?$/,
error : "Empty line between commit title and body is missing" ,
},
2021-10-28 02:33:39 +00:00
{
2021-11-18 07:15:00 +00:00
pattern : /^.{0,72}(?:\r?\n(?:(.{0,72})|(.*?([a-z]+:\/\/)?(([a-zA-Z0-9_]|-)+\.)+[a-z]{2,}(:\d+)?([a-zA-Z_0-9@:%\+.~\?&/=]|-)+).*?))*$/,
2021-10-28 02:33:39 +00:00
error : "Commit message lines are too long (maximum allowed is 72 characters, except for URLs)" ,
},
2022-11-19 15:53:54 +00:00
{
2022-12-10 17:17:11 +00:00
pattern : /^((?!^Merge branch )[\s\S])*$/,
2022-11-19 15:53:54 +00:00
error : "Commit is a git merge commit, use the rebase command instead" ,
},
2021-10-28 02:33:39 +00:00
{
pattern: /^\S.*?\S : .+/,
error : "Missing category in commit title (if this is a fix up of a previous commit, it should be squashed)" ,
},
{
pattern: /^\S.*? : [ A-Z0-9]/,
error : "First word of commit after the subsystem is not capitalized" ,
},
{
2021-11-18 07:15:00 +00:00
pattern : /^.+[^.\n](\r?\n.*)*$/,
2021-10-28 02:33:39 +00:00
error : "Commit title ends in a period" ,
},
{
pattern: /^((?!Signed-off-by : )[\s\S])*$/,
error : "Commit body contains a Signed-off-by tag" ,
},
] ;
2021-05-22 22:15:05 +00:00
2021-10-28 02:33:39 +00:00
const { repository, pull_request } = context.payload;
2021-05-22 22:15:05 +00:00
2021-10-28 02:33:39 +00:00
// NOTE : This maxes out at 250 commits. If this becomes a problem, see :
// https://octokit.github.io/rest.js/v18#pulls-list-commits
const opts = github.rest.pulls.listCommits.endpoint.merge({
owner : repository.owner.login,
repo : repository.name,
pull_number : pull_request.number,
});
const commits = await github.paginate(opts);
2021-06-16 13:41:17 +00:00
2021-10-28 02:33:39 +00:00
const errors = [];
2023-01-18 13:33:45 +00:00
for (const { sha, commit: { message }, author } of commits) {
2023-01-19 23:32:02 +00:00
if (author !== null && excludedBotIds.includes(author.id)) {
2023-01-18 13:33:45 +00:00
continue;
}
2021-10-28 02:33:39 +00:00
const commitErrors = [];
for (const { pattern, error } of rules) {
if (!pattern.test(message)) {
commitErrors.push(error);
}
}
if (commitErrors.length > 0) {
const title = message.split("\n")[0];
errors.push([`${title} (${sha}):`, ...commitErrors].join("\n "));
}
}
2021-05-22 22:15:05 +00:00
2021-10-28 02:33:39 +00:00
if (errors.length > 0) {
core.setFailed(`One or more of the commits in this PR do not match the code submission policy:\n\n${errors.join("\n")}`);
}
2021-09-07 19:36:17 +00:00
2021-05-22 22:15:05 +00:00
- name : Comment on PR
2021-07-09 19:36:39 +00:00
if : ${{ failure() && !github.event.pull_request.draft }}
2021-05-23 22:37:45 +00:00
uses : IdanHo/comment-on-pr@5f51df338210754f519f721f8320d8f72525a4d0
2021-05-22 22:15:05 +00:00
env :
2023-01-06 17:19:42 +00:00
GITHUB_TOKEN : ${{ secrets.BUGGIEBOT_TOKEN }}
2021-05-22 22:15:05 +00:00
with :
2022-01-07 02:40:23 +00:00
msg : "Hello!\n\nOne or more of the commit messages in this PR do not match the SerenityOS [code submission policy](https://github.com/SerenityOS/serenity/blob/master/CONTRIBUTING.md#code-submission-policy), please check the `lint_commits` CI job for more details on which commits were flagged and why.\nPlease do not close this PR and open another, instead modify your commit message(s) with [git commit --amend](https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/changing-a-commit-message) and force push those changes to update this PR."