From 484a16194d21bbfed0a79552f353eeb7db0168af Mon Sep 17 00:00:00 2001 From: Lorenzo Vagliano Date: Wed, 23 Oct 2024 12:33:51 +0200 Subject: [PATCH] workflows: Added commit message formating Signed-off-by: Lorenzo Vagliano --- .github/workflows/pre-commit.yml | 1 - .github/workflows/pull-request-main.yml | 3 +- .gitlint | 11 +++++++ .pre-commit-config.yaml | 9 ++++++ scripts/gitlint_rules/rules.py | 39 +++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 .gitlint create mode 100644 scripts/gitlint_rules/rules.py diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index e15ce6c8..92f229e2 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -24,4 +24,3 @@ jobs: - name: Run pre-commit uses: pre-commit/action@v3.0.0 - \ No newline at end of file diff --git a/.github/workflows/pull-request-main.yml b/.github/workflows/pull-request-main.yml index 1d8634a2..771e1698 100644 --- a/.github/workflows/pull-request-main.yml +++ b/.github/workflows/pull-request-main.yml @@ -3,7 +3,7 @@ name: Pull request main on: pull_request_target: branches: [main] - paths-ignore: [ "docs/**" ] + paths-ignore: ["docs/**"] jobs: test: @@ -17,4 +17,3 @@ jobs: with: ref: ${{ github.event.pull_request.head.sha }} secrets: inherit - \ No newline at end of file diff --git a/.gitlint b/.gitlint new file mode 100644 index 00000000..56d7f6a1 --- /dev/null +++ b/.gitlint @@ -0,0 +1,11 @@ +[general] +ignore=body-is-missing +extra-path=./scripts/gitlint_rules/ +debug=true +verbosity=3 + +[title-max-length] +line-length=50 + +[body-max-line-length] +line-length=72 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2fc4a58e..4c09835c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,6 +7,7 @@ repos: rev: "v2.7.1" hooks: - id: prettier + stages: [commit] - repo: https://github.com/pycqa/isort rev: "5.12.0" hooks: @@ -22,3 +23,11 @@ repos: - id: check-merge-conflict - id: end-of-file-fixer - id: trailing-whitespace + - repo: https://github.com/jorisroovers/gitlint + rev: "v0.17.0" + hooks: + - id: gitlint + language: python + entry: gitlint + stages: [commit-msg] + args: [--msg-filename] diff --git a/scripts/gitlint_rules/rules.py b/scripts/gitlint_rules/rules.py new file mode 100644 index 00000000..fb17440a --- /dev/null +++ b/scripts/gitlint_rules/rules.py @@ -0,0 +1,39 @@ +import re + +from gitlint.rules import CommitRule, RuleViolation + +class SignedOffBy(CommitRule): + """This rule will enforce that each commit contains a "Signed-off-by" line.""" + + name = "body-requires-signed-off-by" + id = "Workflows1" + + def validate(self, commit): + for line in commit.message.body: + if line.startswith("Signed-off-by"): + return + + msg = "Body does not contain a 'Signed-Off-By' line" + return [RuleViolation(self.id, msg, line_nr=1)] + + +class ApprovedSubject(CommitRule): + """Validate subject of each commit. + + This rule will enforce that each commit starts with a "module: text" format. + The 'module' can be any alphanumeric word, and the message must start with a colon followed by a space. + """ + + name = "approved-subject-in-title" + id = "Workflows2" + + MODULE_PATTERN = re.compile(r"^[a-zA-Z0-9_-]+: .+") + + def validate(self, commit): + title = commit.message.title + + if not self.MODULE_PATTERN.match(title): + msg = "Subject does not follow 'module: text' format" + return [RuleViolation(self.id, msg, line_nr=1)] + + return