Skip to content

Commit

Permalink
CI: Add conventional commit checker (UEVE-DLL-2024-2025#19)
Browse files Browse the repository at this point in the history
* ci: add base script to check conventional commits

* CI: Add conventional commit checker

ci: add new ci to current ci jobs

fix: linting error related to new CI

ci: clean the code

chore: add contributor

ci: fix linting errors

ci: fix error when PR from fork

ci: fix linting errors
  • Loading branch information
ObstinateM authored and Sebb955 committed Oct 14, 2024
1 parent 17803fa commit 03c2509
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/scripts/commit_type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"types": {
"feat": {
"description": "A new feature",
"title": "Features"
},
"fix": {
"description": "A bug fix",
"title": "Bug Fixes"
},
"docs": {
"description": "Documentation only changes",
"title": "Documentation"
},
"style": {
"description": "Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)",
"title": "Styles"
},
"refactor": {
"description": "A code change that neither fixes a bug nor adds a feature",
"title": "Code Refactoring"
},
"perf": {
"description": "A code change that improves performance",
"title": "Performance Improvements"
},
"test": {
"description": "Adding missing tests or correcting existing tests",
"title": "Tests"
},
"build": {
"description": "Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)",
"title": "Builds"
},
"ci": {
"description": "Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)",
"title": "Continuous Integrations"
},
"chore": {
"description": "Other changes that don't modify src or test files",
"title": "Chores"
},
"revert": {
"description": "Reverts a previous commit",
"title": "Reverts"
}
}
}
56 changes: 56 additions & 0 deletions .github/scripts/validate_commit_msg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python
"""
This script validates commit messages based on the Conventional Commits specification.
It ensures that commit messages follow a predefined format defined in commit_type.json.
"""

import sys
import re
import json
import os

JSON_COMMIT_TYPE = "commit_type.json"

def load_commit_types():
"""
Load valid commit types from the commit_type.json file located in the same directory as
the script.
Returns:
list: A list of valid commit types in lowercase.
"""
# Get the directory where the script is located
script_dir = os.path.dirname(os.path.realpath(__file__))
json_file_path = os.path.join(script_dir, JSON_COMMIT_TYPE)

# Read the JSON file and extract commit types
with open(json_file_path, "r", encoding="utf-8") as file:
commit_types = json.load(file)
return [commit_type.lower() for commit_type in commit_types["types"].keys()]

def validate_commit_message(message, valid_types):
"""
Validate the commit message based on the given valid types and Conventional Commits format.
Args:
message (str): The commit message to validate.
valid_types (list): A list of valid commit types.
Returns:
bool: True if the commit message is valid, False otherwise.
"""
# Case insensitive match for commit type
pattern = r'^(' + '|'.join(valid_types) + r')(\(.+\))?: .{1,50}'
if re.match(pattern, message, re.IGNORECASE):
print("Commit message is valid.")
return True

print(f"Commit message ({message}) is invalid. Valid types are: {', '.join(valid_types)}")
return False

if __name__ == "__main__":
valid_commit_types = load_commit_types()
commit_message = sys.stdin.read().strip()

if not validate_commit_message(commit_message, valid_commit_types):
sys.exit(1)
17 changes: 17 additions & 0 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ on:

jobs:

lint_commits:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2 # Fetch at least 2 commits to ensure we get non-merge commits
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Validate commit messages
run: |
# Get the last non-merge commit and validate the message
git log --pretty=format:"%s" --no-merges -n 1 | python .github/scripts/validate_commit_msg.py
lint:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
- Rami, GOUAL, [email protected]
- Samy FERGUI, [email protected]
- Marwane, RACHAD, [email protected]
- Mathis, Beauville, [email protected]

0 comments on commit 03c2509

Please sign in to comment.