Skip to content

Commit

Permalink
switch to python
Browse files Browse the repository at this point in the history
test

Signed-off-by: Nikolay Martyanov <[email protected]>
  • Loading branch information
OhmSpectator committed Aug 27, 2024
1 parent cf0ea39 commit 80c81db
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
20 changes: 11 additions & 9 deletions .github/workflows/commit-messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ jobs:
git fetch origin ${{ github.event.pull_request.head.sha }}:${{ github.event.pull_request.head.ref }}
git checkout ${{ github.event.pull_request.head.ref }}
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '21'

- name: Install Commitlint
run: npm install @commitlint/config-conventional @commitlint/cli
- name: Create virtual environment
run: |
cd tools/check-commit-messages
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
- name: Run Commitlint
run: npx commitlint --from=origin/master --to=HEAD --config .commitlintrc.yml
- name: Lint commit messages
run: |
cd tools/check-commit-messages
source .venv/bin/activate
python check_commit_messages.py
1 change: 1 addition & 0 deletions tools/check-commit-messages/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv
63 changes: 63 additions & 0 deletions tools/check-commit-messages/check_commit_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
This script checks the commit messages in the current branch to ensure they follow the
recommended format. The recommended format is as follows:
* Commit message should have a subject and body
* An empty line should separate the subject and body
* The body should not be empty (it should contain more than just "Signed-off-by")
"""

import git
import sys

Check failure on line 10 in tools/check-commit-messages/check_commit_messages.py

View workflow job for this annotation

GitHub Actions / yetus

pylint:[C0411(wrong-import-order), ] standard import "import sys" should be placed before "import git"

def check_commit_message(commit):
message_lines = commit.message.strip().splitlines()

if len(message_lines) < 2:
return False, f"Commit {commit.hexsha} has no body."

subject = message_lines[0].strip()
second_line = message_lines[1].strip()

if not subject:
return False, f"Commit {commit.hexsha} has no subject."

# Check if the second line is empty (indicating a blank line between subject and body)
if second_line:
return False, f"Commit {commit.hexsha} does not have an empty line between the subject and body."

# Remove the subject and empty line to get the body
body_lines = [line.strip() for line in message_lines[2:] if line.strip()]

Check failure on line 29 in tools/check-commit-messages/check_commit_messages.py

View workflow job for this annotation

GitHub Actions / yetus

pylint:[C0301(line-too-long), ] Line too long (105/100)

# Remove lines that are just "Signed-off-by"
body_lines = [line for line in body_lines if not line.lower().startswith("signed-off-by")]

if not body_lines:
return False, f"Commit {commit.hexsha} has a body but only contains Signed-off-by."

return True, None


def main():
repo = git.Repo(search_parent_directories=True)

# Fetch the latest from origin/master to ensure we are comparing correctly

Check failure on line 43 in tools/check-commit-messages/check_commit_messages.py

View workflow job for this annotation

GitHub Actions / yetus

pylint:[C0116(missing-function-docstring), main] Missing function or method docstring
repo.remotes.origin.fetch('master')

commits = list(repo.iter_commits('origin/master..HEAD'))

if not commits:
print("No commits between origin/master and HEAD.")
sys.exit(1)

print(f"Checking {len(commits)} commits between origin/master and HEAD...")

for commit in commits:
valid, error_message = check_commit_message(commit)
if not valid:
print(error_message)
sys.exit(1)

print("All commits are valid.")

if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions tools/check-commit-messages/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gitdb==4.0.11
GitPython==3.1.43
smmap==5.0.1

0 comments on commit 80c81db

Please sign in to comment.