From fa695559bebca18bfceb5629570a4f379f2387c3 Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Fri, 9 Aug 2024 14:53:53 -0300 Subject: [PATCH 1/2] ci: add conventional commits enforcement --- .../workflows/conventional-commits-lint.js | 92 +++++++++++++++++++ .github/workflows/conventional-commits.yml | 43 +++++++++ 2 files changed, 135 insertions(+) create mode 100644 .github/workflows/conventional-commits-lint.js create mode 100644 .github/workflows/conventional-commits.yml diff --git a/.github/workflows/conventional-commits-lint.js b/.github/workflows/conventional-commits-lint.js new file mode 100644 index 00000000..62930939 --- /dev/null +++ b/.github/workflows/conventional-commits-lint.js @@ -0,0 +1,92 @@ +"use strict"; + +const fs = require("fs"); + +const TITLE_PATTERN = + /^(?[^:!(]+)(?\([^)]+\))?(?[!])?:.+$/; +const RELEASE_AS_DIRECTIVE = /^\s*Release-As:/im; +const BREAKING_CHANGE_DIRECTIVE = /^\s*BREAKING[ \t]+CHANGE:/im; + +const ALLOWED_CONVENTIONAL_COMMIT_PREFIXES = [ + "revert", + "feat", + "fix", + "ci", + "docs", + "chore", + "style", + "test", + "refactor", +]; + +const object = process.argv[2]; +const payload = JSON.parse(fs.readFileSync(process.stdin.fd, "utf-8")); + +let validate = []; + +if (object === "pr") { + validate.push({ + title: payload.pull_request.title, + content: payload.pull_request.body, + }); +} else if (object === "push") { + validate.push( + ...payload.commits + .map((commit) => ({ + title: commit.message.split("\n")[0], + content: commit.message, + })) + .filter(({ title }) => !title.startsWith("Merge branch ") && !title.startsWith("Revert ")), + ); +} else { + console.error( + `Unknown object for first argument "${object}", use 'pr' or 'push'.`, + ); + process.exit(0); +} + +let failed = false; + +validate.forEach((payload) => { + if (payload.title) { + const match = payload.title.match(TITLE_PATTERN); + if (!match) { + return + } + + const { groups } = match + + if (groups) { + if ( + !ALLOWED_CONVENTIONAL_COMMIT_PREFIXES.find( + (prefix) => prefix === groups.prefix, + ) + ) { + console.error( + `PR (or a commit in it) is using a disallowed conventional commit prefix ("${groups.prefix}"). Only ${ALLOWED_CONVENTIONAL_COMMIT_PREFIXES.join(", ")} are allowed. Make sure the prefix is lowercase!`, + ); + failed = true; + } + } else { + console.error( + "PR or commit title must match conventional commit structure.", + ); + failed = true; + } + } + + if (payload.content) { + if (payload.content.match(RELEASE_AS_DIRECTIVE)) { + console.error( + "PR descriptions or commit messages must not contain Release-As conventional commit directives.", + ); + failed = true; + } + } +}); + +if (failed) { + process.exit(1); +} + +process.exit(0); \ No newline at end of file diff --git a/.github/workflows/conventional-commits.yml b/.github/workflows/conventional-commits.yml new file mode 100644 index 00000000..ab722080 --- /dev/null +++ b/.github/workflows/conventional-commits.yml @@ -0,0 +1,43 @@ +name: Check pull requests + +on: + push: + branches-ignore: # Run the checks on all branches but the protected ones + - main + - release/* + + pull_request_target: + branches: + - main + - release/* + types: + - opened + - edited + - reopened + - ready_for_review + +jobs: + check-conventional-commits: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + sparse-checkout: | + .github + + - if: ${{ github.event_name == 'pull_request_target' }} + run: | + set -ex + + node .github/workflows/conventional-commits-lint.js pr < Date: Sun, 11 Aug 2024 10:40:47 +0000 Subject: [PATCH 2/2] ci: format conventional commit files --- .github/workflows/conventional-commits-lint.js | 2 +- .github/workflows/conventional-commits.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/conventional-commits-lint.js b/.github/workflows/conventional-commits-lint.js index 62930939..35db22f5 100644 --- a/.github/workflows/conventional-commits-lint.js +++ b/.github/workflows/conventional-commits-lint.js @@ -89,4 +89,4 @@ if (failed) { process.exit(1); } -process.exit(0); \ No newline at end of file +process.exit(0); diff --git a/.github/workflows/conventional-commits.yml b/.github/workflows/conventional-commits.yml index ab722080..71e0e1d0 100644 --- a/.github/workflows/conventional-commits.yml +++ b/.github/workflows/conventional-commits.yml @@ -40,4 +40,4 @@ jobs: node .github/workflows/conventional-commits-lint.js push <