From f726ec8226935fd11c1478e2b8b1cb0f4ba55159 Mon Sep 17 00:00:00 2001 From: MoritzWeber Date: Wed, 24 Jan 2024 17:35:18 +0100 Subject: [PATCH] build: Use `commitlint` to enfore conventional commits `commitlint` has a better CLI, which allows to enforce conventional commits in a GitHub Actions pipeline. --- .commitlintrc.yml | 17 ++++++++ .github/workflows/commits.yml | 57 +++++++++++++++++++++++++++ .pre-commit-config.yaml | 7 ++-- git-conventional-commits.json | 17 -------- git-conventional-commits.json.license | 2 - 5 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 .commitlintrc.yml create mode 100644 .github/workflows/commits.yml delete mode 100644 git-conventional-commits.json delete mode 100644 git-conventional-commits.json.license diff --git a/.commitlintrc.yml b/.commitlintrc.yml new file mode 100644 index 000000000..479890a0f --- /dev/null +++ b/.commitlintrc.yml @@ -0,0 +1,17 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG and contributors +# SPDX-License-Identifier: CC0-1.0 +rules: + body-leading-blank: [2, always] + footer-leading-blank: [2, always] + scope-case: [2, always, lower-case] + subject-case: [2, always, sentence-case] + subject-empty: [2, never] + subject-full-stop: [2, never, .] + subject-max-length: [2, always, 72] + type-empty: [2, never] + type-enum: + [ + 2, + always, + [build, chore, ci, docs, feat, fix, merge, perf, refactor, revert, test], + ] diff --git a/.github/workflows/commits.yml b/.github/workflows/commits.yml new file mode 100644 index 000000000..e41710ade --- /dev/null +++ b/.github/workflows/commits.yml @@ -0,0 +1,57 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG and contributors +# SPDX-License-Identifier: CC0-1.0 + +name: commits + +on: + pull_request: + branches: [main] + +jobs: + conventional-commits: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Install commitlint + run: npm install -g @commitlint/cli + - name: Validate commit messages + id: conventional-commits + env: + SHA_FROM: ${{ github.event.pull_request.base.sha }} + SHA_TO: ${{ github.event.pull_request.head.sha }} + run: | + delim="_EOF_$(uuidgen)" + echo "validation-result<<$delim" >> "$GITHUB_OUTPUT" + r=0 + commitlint --from "$SHA_FROM" --to "$SHA_TO" >> "$GITHUB_OUTPUT" 2>&1 || r=$? + echo "$delim" >> "$GITHUB_OUTPUT" + exit $r + - name: Post comment if validation failed + if: always() && steps.conventional-commits.outcome == 'failure' + uses: actions/github-script@v6 + env: + TEXT: |- + The pull request does not conform to the conventional commit specification. Please ensure that your commit messages follow the spec: . + We also strongly recommend that you set up your development environment with pre-commit, as described in our [Developer documentation](https://dsd-dbs.github.io/capella-collab-manager/development/). This will run all the important checks right before you commit your changes, and avoids lengthy CI wait time and round trips. + + This is the commit validation log: + ``` + ${{ steps.conventional-commits.outputs.validation-result }} + ``` + + Here are some examples of valid commit messages: + ``` + build: Bump frontend versions + docs(user): Add model creation workflow + feat: Add a monitoring dashboard + ``` + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: process.env.TEXT + }) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b5683db4b..b04338acc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -153,10 +153,11 @@ repos: types: [] exclude: '.+\.spec(-helper)?\.ts$' types_or: ["html", "ts"] - - repo: https://github.com/qoomon/git-conventional-commits - rev: v2.6.7 + - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook + rev: v9.11.0 hooks: - - id: conventional-commits + - id: commitlint + stages: [commit-msg] - repo: https://github.com/asottile/pyupgrade rev: v3.15.0 hooks: diff --git a/git-conventional-commits.json b/git-conventional-commits.json deleted file mode 100644 index 94ef04cc7..000000000 --- a/git-conventional-commits.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "convention" : { - "commitTypes": [ - "build", - "chore", - "ci", - "docs", - "feat", - "fix", - "perf", - "refactor", - "revert", - "test" - ], - "commitScopes": [] - } -} diff --git a/git-conventional-commits.json.license b/git-conventional-commits.json.license deleted file mode 100644 index 7ea22469b..000000000 --- a/git-conventional-commits.json.license +++ /dev/null @@ -1,2 +0,0 @@ -SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors -SPDX-License-Identifier: Apache-2.0