From ae63c633b40f8e0456fe5ef14612c26fe83cdff9 Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 6 Jun 2024 16:22:55 -0300 Subject: [PATCH] [ci] Add commit linter action --- .github/workflows/lint_commit.yml | 72 +++++++++++++++++++++++++++++++ laf | 2 +- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/lint_commit.yml diff --git a/.github/workflows/lint_commit.yml b/.github/workflows/lint_commit.yml new file mode 100644 index 000000000..0fcb54f7e --- /dev/null +++ b/.github/workflows/lint_commit.yml @@ -0,0 +1,72 @@ +# Based on SerenityOS commit linter: +# https://github.com/SerenityOS/serenity/blob/master/.github/workflows/lintcommits.yml + +name: Commit linter +on: [pull_request_target] +jobs: + lint_commits: + runs-on: ubuntu-22.04 + if: always() && github.repository == 'aseprite/aseprite' + steps: + - name: Lint PR commits + uses: actions/github-script@v7 + with: + script: | + const rules = [ + { + pattern: /^[^\r]*$/, + error: "Commit message contains CRLF line breaks (only unix-style LF linebreaks are allowed)", + }, + { + pattern: /^.+(\r?\n(\r?\n.*)*)?$/, + error: "Empty line between commit title and body is missing", + }, + { + pattern: /^.{0,72}(?:\r?\n(?:(.{0,72})|(.*?([a-z]+:\/\/)?(([a-zA-Z0-9_]|-)+\.)+[a-z]{2,}(:\d+)?([a-zA-Z_0-9@:%\+.~\?&/=]|-)+).*?))*$/, + error: "Commit message lines are too long (maximum allowed is 72 characters, except for URLs)", + }, + { + pattern: /^.+[^.\n](\r?\n.*)*$/, + error: "Commit title ends in a period", + }, + { + pattern: /^((?!Signed-off-by: )[\s\S])*$/, + error: "Commit body contains a Signed-off-by tag", + }, + ]; + + const { repository, pull_request } = context.payload; + + // 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); + + const errors = []; + for (const { sha, commit: { message } } of commits) { + 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 ")); + } + } + + 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")}`); + } + - name: Comment on PR + if: ${{ failure() && !github.event.pull_request.draft }} + uses: unsplash/comment-on-pr@a9bf050e744c8282dee4bb0dbcf063186d8316c4 + env: + GITHUB_TOKEN: ${{ secrets.LINT_COMMIT_TOKEN }} + with: + msg: "Hi there!\n\nOne or more of the commit messages in this PR do not match our [code submission policy](https://github.com/aseprite/aseprite/blob/main/CONTRIBUTING.md), 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." diff --git a/laf b/laf index c82fd98b2..7c2f03042 160000 --- a/laf +++ b/laf @@ -1 +1 @@ -Subproject commit c82fd98b275cb25122106797af00feabc5015c7f +Subproject commit 7c2f0304293f0e6a42059608558d012adc66f46c