| 1 | name: ✒️ Conventional Commits |
| 2 | |
| 3 | on: |
| 4 | pull_request_target: |
| 5 | paths-ignore: |
| 6 | - 'doc/**' |
| 7 | - '*.md' |
| 8 | branches: |
| 9 | - master |
| 10 | - release/** |
| 11 | |
| 12 | permissions: |
| 13 | contents: read |
| 14 | pull-requests: write |
| 15 | |
| 16 | jobs: |
| 17 | conventional-commits: |
| 18 | name: 📜 Conventional Commits |
| 19 | runs-on: ubuntu-latest |
| 20 | steps: |
| 21 | - name: 🔎 Check PR commit messages follow Conventional Commits |
| 22 | uses: actions/github-script@v8 |
| 23 | with: |
| 24 | script: | |
| 25 | const owner = context.repo.owner; |
| 26 | const repo = context.repo.repo; |
| 27 | const pull_number = context.payload.pull_request.number; |
| 28 | |
| 29 | const marker = "<!-- conventional-commits-check -->"; |
| 30 | |
| 31 | // Conventional Commits v1.0.0 summary line regex |
| 32 | const re = /^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\([^)]+\))?(!)?:\s\S.+/; |
| 33 | |
| 34 | const commits = await github.paginate( |
| 35 | github.rest.pulls.listCommits, |
| 36 | { owner, repo, pull_number, per_page: 100 } |
| 37 | ); |
| 38 | |
| 39 | const invalid = []; |
| 40 | for (const c of commits) { |
| 41 | const summary = c.commit.message.split("\n")[0]; |
| 42 | if (!re.test(summary)) { |
| 43 | invalid.push({ |
| 44 | sha: c.sha.substring(0, 7), |
| 45 | message: summary, |
| 46 | }); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Find existing bot comment (if any) |
| 51 | const comments = await github.paginate( |
| 52 | github.rest.issues.listComments, |
| 53 | { owner, repo, issue_number: pull_number } |
| 54 | ); |
| 55 | |
| 56 | const existing = comments.find( |
| 57 | c => c.user?.type === "Bot" && c.body?.includes(marker) |
| 58 | ); |
| 59 | |
| 60 | if (invalid.length > 0) { |
| 61 | const body = |
| 62 | `${marker} |
| 63 | ### ❌ Conventional Commits check failed |
| 64 | |
| 65 | One or more commit messages in this PR do **not** follow the |
| 66 | [Conventional Commits v1.0.0](https://www.conventionalcommits.org/en/v1.0.0/) specification. |
| 67 | |
| 68 | **Invalid commits:** |
| 69 | ${invalid.map(i => `- \`${i.sha}\` — ${i.message}`).join("\n")} |
| 70 | |
| 71 | **Expected format** |
| 72 | \`\`\` |
| 73 | type(scope?)!?: subject |
| 74 | \`\`\` |
| 75 | |
| 76 | **Examples** |
| 77 | - \`feat: add user login\` |
| 78 | - \`fix(api): handle null response\` |
| 79 | - \`chore!: drop node 16 support\` |
| 80 | |
| 81 | Please fix the commit messages (e.g. via \`git rebase -i\`) and push again.`; |
| 82 | |
| 83 | if (existing) { |
| 84 | await github.rest.issues.updateComment({ |
| 85 | owner, |
| 86 | repo, |
| 87 | comment_id: existing.id, |
| 88 | body, |
| 89 | }); |
| 90 | } else { |
| 91 | await github.rest.issues.createComment({ |
| 92 | owner, |
| 93 | repo, |
| 94 | issue_number: pull_number, |
| 95 | body, |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | core.setFailed( |
| 100 | `${invalid.length} commit(s) do not follow Conventional Commits` |
| 101 | ); |
| 102 | } else { |
| 103 | // If previously failed, clean up the comment |
| 104 | if (existing) { |
| 105 | await github.rest.issues.updateComment({ |
| 106 | owner, |
| 107 | repo, |
| 108 | comment_id: existing.id, |
| 109 | body: `${marker} |
| 110 | ### ✅ Conventional Commits check passed |
| 111 | |
| 112 | All commit messages in this PR now follow the Conventional Commits specification. 🎉`, |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | core.info(`All ${commits.length} commit(s) follow Conventional Commits.`); |
| 117 | } |