[pre-commit.ci] pre-commit autoupdate #37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Commit linter | |
| on: | |
| pull_request: | |
| branches: | |
| - main | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| lint_commits: | |
| runs-on: ubuntu-20.04 | |
| if: always() | |
| 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: /^\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", | |
| }, | |
| { | |
| pattern: /^.+[^.\n](\r?\n.*)*$/, | |
| error: "Commit title ends in a period", | |
| }, | |
| ]; | |
| 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")}`); | |
| } |