From 0e532d33d8c9cfb6b7b1ff5aa806a7352bf0fc2a Mon Sep 17 00:00:00 2001 From: Alexander Lais Date: Thu, 13 Jul 2023 08:43:41 +0200 Subject: [PATCH 1/2] ci: add commitlint configuration * breaking change handler * type enum according to our convention --- commitlint.config.js | 71 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 commitlint.config.js diff --git a/commitlint.config.js b/commitlint.config.js new file mode 100644 index 00000000..2efd7787 --- /dev/null +++ b/commitlint.config.js @@ -0,0 +1,71 @@ +module.exports = { + rules: { + // rules are defined as [ severity (2=error), "always"|"never", argument? ]. See + // https://commitlint.js.org/#/reference-rules?id=rules + + // log-commit-info is used for debugging. It outputs the dict passed to a plugin. + // 'log-commit-info': [2, 'always'], + // enforce that commits with a breaking change indicator ("!") must also contain an explanation of the breaking change. + 'breaking-change-indicator': [2, 'always'], + // enforce that only the types below are allowed + 'type-enum': [2, 'always', + [ + 'feat', + 'fix', + 'dep', + 'ci', + 'refactor', + 'doc', + 'test', + ] + ], + // enforce that a type is mandatory + 'type-empty': [2, 'never'], + // enforce the maximum subject length (without type and scope) + 'subject-max-length': [2, 'always', 75], + // enforce the maximum scope length + 'scope-max-length': [2, 'always', 25], + }, + parserPreset: { + parserOpts: { + // add matching groups for `!` before and after scope. Before scope is invalid, after scope is correct + // and enforces the "breaking-change" header (see below). + headerPattern: /^(\w*)(?:(!?)\(([\w\$\.\-\* ]*)\))?(!?)\: (.*)$/, + headerCorrespondence: ["type", "wrong_breaking", "scope", "breaking", "subject"], + }, + }, + plugins: [ + { + rules: { + // log-commit-info is used for debugging. It outputs the dict passed to a plugin. + // 'log-commit-info': (input) => { + // console.log(input) + // return [true, "see above"] + // }, + 'breaking-change-indicator': ({wrong_breaking, breaking, scope, body, footer}) => { + if (wrong_breaking) { + return [ + false, "The breaking change indicator `!` can only be used right before `:`, e.g. `feat(scope)!:`." + ] + } + + const rxBreakingChange = /^breaking(\s|-)change: .+/img + + // The marker "breaking change:" is treated as footer and "note" by commitlint, and not considered part of the body. + // When a breaking change indicator ("!") is there, we need to check body and footer to cover both variants (with and without "-"). + + // `commitOk` indicates, if the commit passes the rules. + const commitOk = + !breaking // if there is no "!" in the commit, we don't enforce a footer + || body?.match(rxBreakingChange) // if there is a "!", and a body exists, it must match the breaking change pattern + || footer?.match(rxBreakingChange) // if there is a "!", and a footer exists, it must match the breaking change pattern + + return [ + commitOk, + 'For a breaking change (indicated with `!` after the type), the commit must mention a trailer `breaking-change:` or `breaking change:` (case in-sensitive)' + ]; + }, + }, + }, + ], +}; \ No newline at end of file From d16d02adf08e088e5df4d51080da4e1b0761a732 Mon Sep 17 00:00:00 2001 From: Raghunath Deshpande Date: Wed, 5 Jul 2023 11:34:05 +0200 Subject: [PATCH 2/2] ci: run commitlint github action on PRs Co-authored-by: Soha Alboghdady Co-authored-by: Raghunath Deshpande --- .github/workflows/commitlint.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .github/workflows/commitlint.yml diff --git a/.github/workflows/commitlint.yml b/.github/workflows/commitlint.yml new file mode 100644 index 00000000..b48c7974 --- /dev/null +++ b/.github/workflows/commitlint.yml @@ -0,0 +1,21 @@ +name: CI + +on: + pull_request: + branches: + - main +jobs: + commitlint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Setup Node.js + uses: actions/setup-node@v2 + with: + node-version: '18.x' + - name: Install commitlint + run: npm install -g commitlint + - name: Validate PR commits with @commitlint/cli + run: npx commitlint --from ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} --to ${{ github.event.pull_request.head.sha }} --verbose