Skip to content

Commit 50ad7c1

Browse files
authored
Merge pull request #5 from cobaltcore-dev/commit-lint
Commit lint
2 parents bbc02cd + 2d0662a commit 50ad7c1

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle, CommitRule
2+
3+
class BodyContainsOnBehalfOfSAPMarker(CommitRule):
4+
"""Enforce that each commit coming from an SAP contractor contains an
5+
"On-behalf-of SAP [email protected]" marker.
6+
"""
7+
8+
# A rule MUST have a human friendly name
9+
name = "body-requires-on-behalf-of-sap"
10+
11+
# A rule MUST have a *unique* id
12+
# We recommend starting with UC (for User-defined Commit-rule).
13+
id = "UC-sap"
14+
15+
# Lower-case list of contractors
16+
contractors = [
17+
"@cyberus-technology.de"
18+
]
19+
20+
# Marker followed by " [email protected]"
21+
marker = "On-behalf-of: SAP"
22+
23+
def validate(self, commit):
24+
if "@sap.com" in commit.author_email.lower():
25+
return
26+
27+
# Allow third-party open-source contributions
28+
if not any(contractor in commit.author_email.lower() for contractor in self.contractors):
29+
return
30+
31+
for line in commit.message.body:
32+
if line.startswith(self.marker) and "@sap.com" in line.lower():
33+
return
34+
35+
msg = f"Body does not contain a '{self.marker} [email protected]' line"
36+
return [RuleViolation(self.id, msg, line_nr=1)]

.github/workflows/commit-lint.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Commit Lint
2+
on: [ pull_request ]
3+
jobs:
4+
gitlint:
5+
name: Check commit messages
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Checkout repository
9+
uses: actions/checkout@v4
10+
with:
11+
ref: ${{ github.event.pull_request.head.sha }}
12+
fetch-depth: 0
13+
- name: Set up Python 3.10
14+
uses: actions/setup-python@v3
15+
with:
16+
python-version: "3.10"
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install --upgrade gitlint
21+
- name: Lint git commit messages
22+
run: |
23+
gitlint --commits origin/$GITHUB_BASE_REF..

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
result*
22
/.pre-commit-config.yaml
33
/.nixos-test-history
4+
5+
# Produced by gitlint
6+
__pycache__

.gitlint

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[general]
2+
extra-path=.config/gitlint/rules
3+
regex-style-search=true
4+
ignore=body-is-missing
5+
6+
[ignore-by-author-name]
7+
regex=dependabot
8+
ignore=all
9+
10+
# default 72
11+
[title-max-length]
12+
line-length=72
13+
14+
# default 80
15+
[body-max-line-length]
16+
line-length=72
17+
18+
# Empty bodies are fine
19+
[body-min-length]
20+
min-length=0

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
devShells.default = pkgs.mkShellNoCC {
2929
inherit (self.checks.${system}.pre-commit-check) shellHook;
3030
buildInputs = self.checks.${system}.pre-commit-check.enabledPackages;
31+
packages = with pkgs; [ gitlint ];
3132
};
3233

3334
lib = {

0 commit comments

Comments
 (0)