|
| 1 | +name: commit lint |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +jobs: |
| 9 | + |
| 10 | + commit_lint: |
| 11 | + name: Run Lint Commit |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Check Commit Messages |
| 15 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 16 | + with: |
| 17 | + script: | |
| 18 | + const excludedBotIds = [ |
| 19 | + 49699333, // dependabot[bot] |
| 20 | + ]; |
| 21 | + const rules = [ |
| 22 | + { |
| 23 | + pattern: /^[^\r]*$/, |
| 24 | + error: "Commit message contains CRLF line breaks (only unix-style LF linebreaks are allowed)", |
| 25 | + }, |
| 26 | + { |
| 27 | + pattern: /^.+(\r?\n(\r?\n.*)*)?$/, |
| 28 | + error: "Empty line between commit title and body is missing", |
| 29 | + }, |
| 30 | + { |
| 31 | + pattern: /^.{0,72}(?:\r?\n(?:(.{0,72})|(.*?([a-z]+:\/\/)?(([a-zA-Z0-9_]|-)+\.)+[a-z]{2,}(:\d+)?([a-zA-Z_0-9@:%\+.~\?&/=]|-)+).*?))*$/, |
| 32 | + error: "Commit message lines are too long (maximum allowed is 72 characters, except for URLs)", |
| 33 | + }, |
| 34 | + { |
| 35 | + pattern: /^((?!^Merge branch )[\s\S])*$/, |
| 36 | + error: "Commit is a git merge commit, use the rebase command instead", |
| 37 | + }, |
| 38 | + { |
| 39 | + pattern: /^\S.*?\S: .+/, |
| 40 | + error: "Missing category in commit title (if this is a fix up of a previous commit, it should be squashed)", |
| 41 | + }, |
| 42 | + { |
| 43 | + pattern: /^\S.*?: [A-Z0-9]/, |
| 44 | + error: "First word of commit after the subsystem is not capitalized", |
| 45 | + }, |
| 46 | + { |
| 47 | + pattern: /^.+[^.\n](\r?\n.*)*$/, |
| 48 | + error: "Commit title ends in a period", |
| 49 | + }, |
| 50 | + { |
| 51 | + pattern: /^((?!Signed-off-by: )[\s\S])*$/, |
| 52 | + error: "Commit body contains a Signed-off-by tag", |
| 53 | + }, |
| 54 | + ]; |
| 55 | +
|
| 56 | + const { repository, pull_request } = context.payload; |
| 57 | +
|
| 58 | + // NOTE: This maxes out at 250 commits. If this becomes a problem, see: |
| 59 | + // https://octokit.github.io/rest.js/v18#pulls-list-commits |
| 60 | + const opts = github.rest.pulls.listCommits.endpoint.merge({ |
| 61 | + owner: repository.owner.login, |
| 62 | + repo: repository.name, |
| 63 | + pull_number: pull_request.number, |
| 64 | + }); |
| 65 | + const commits = await github.paginate(opts); |
| 66 | +
|
| 67 | + const errors = []; |
| 68 | + for (const { sha, commit: { message }, author } of commits) { |
| 69 | + if (author !== null && excludedBotIds.includes(author.id)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + const commitErrors = []; |
| 73 | + for (const { pattern, error } of rules) { |
| 74 | + if (!pattern.test(message)) { |
| 75 | + commitErrors.push(error); |
| 76 | + } |
| 77 | + } |
| 78 | + if (commitErrors.length > 0) { |
| 79 | + const title = message.split("\n")[0]; |
| 80 | + errors.push([`${title} (${sha}):`, ...commitErrors].join("\n ")); |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | + if (errors.length > 0) { |
| 85 | + core.setFailed(`One or more of the commits in this PR do not match the code submission policy:\n\n${errors.join("\n")}`); |
| 86 | + } |
| 87 | +
|
| 88 | + - name: Create PR comment on bad commit message |
| 89 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 90 | + if: ${{ failure() && !github.event.pull_request.draft }} |
| 91 | + with: |
| 92 | + script: | |
| 93 | + // If a comment already exists, skip |
| 94 | + const { data: comments } = await github.rest.issues.listComments({ |
| 95 | + issue_number: context.issue.number, |
| 96 | + owner: context.repo.owner, |
| 97 | + repo: context.repo.repo, |
| 98 | + }); |
| 99 | +
|
| 100 | + const existingComment = comments.find(comment => comment.user.login === 'github-actions[bot]' && comment.body.includes('commit messages in this PR')); |
| 101 | +
|
| 102 | + if (existingComment) { |
| 103 | + core.info('Preview comment already exists. Skipping...'); |
| 104 | + return; |
| 105 | + } |
| 106 | +
|
| 107 | + const body = `### ❌ One or more of the commit messages in this PR do not match the nginx-hugo-theme [git guidelines](https://github.com/nginxinc/nginx-hugo-theme/blob/main/CONTRIBUTING.md#git-guidelines), please check the 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.`; |
| 108 | +
|
| 109 | + await github.rest.issues.createComment({ |
| 110 | + issue_number: context.issue.number, |
| 111 | + owner: context.repo.owner, |
| 112 | + repo: context.repo.repo, |
| 113 | + body: body, |
| 114 | + }); |
0 commit comments