Skip to content

Commit

Permalink
ci EXPOSED-725 Enforce commit message rules
Browse files Browse the repository at this point in the history
1. Added `commit-message-validation.yml` that runs a bash script to check the commit messages using a regular expression. The `validate-commits` job will run when the pull request is first opened and on every push.
2. Added `pull-request-title-validation.yml` that runs a bash script to check the pull request's title using a regular expression. The `validate-PR-title` job will run on every edit of the pull request. It's not possible to isolate this to title edits only at the moment, so edits to other parts like the body and labels will also trigger this workflow.
  • Loading branch information
joc-a committed Feb 5, 2025
1 parent 753ba39 commit 30c9614
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/commit-message-validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This workflow ensures that all commit messages in a pull request adhere to the Conventional Commits specification.

name: Commit Message Validation

on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Triggers the workflow on pull request events targeting the main branch
pull_request:
branches:
- main
types:
- opened
- synchronize
- reopened

jobs:
validate-commits:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Validate commit messages
run: |
# Regex for Conventional Commits specification
COMMIT_REGEX="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([^\)]*\))?:\s?(EXPOSED-[0-9]+\s?)?.+$"
# Get all commits in the pull request (from base to head)
COMMITS=$(git log --format=%s --no-merges origin/main..${{ github.sha }})
# Initialize counters and store invalid commits
INVALID_COMMITS=()
VALID_COMMITS=()
echo "ℹ️ Checking if commit messages are following the Conventional Commits specification..."
# Loop through each commit message
IFS=$'\n'
for COMMIT_MSG in $COMMITS; do
# Check if commit message matches the regex
if [[ ! "$COMMIT_MSG" =~ $COMMIT_REGEX ]]; then
INVALID_COMMITS+=("$COMMIT_MSG")
echo -e "❌ $COMMIT_MSG"
else
VALID_COMMITS+=("$COMMIT_MSG")
echo -e "✅ $COMMIT_MSG"
fi
done
# If there are invalid commits, print the summary
if [ ${#INVALID_COMMITS[@]} -gt 0 ]; then
echo ""
echo "🛑 Some commit messages are not following the Conventional Commits specification."
echo ""
echo "Valid commit message format: <type>(<optional scope>): <subject>"
echo "Example: fix: Bug in insert"
echo "Please check https://www.conventionalcommits.org/en/v1.0.0/#summary"
exit 1
fi
echo "🎉 All commit messages are following the Conventional Commits specification."
45 changes: 45 additions & 0 deletions .github/workflows/pull-request-title-validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This workflow ensures that all commit messages in a pull request adhere to the Conventional Commits specification.

name: Pull Request Title Validation

on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Triggers the workflow on pull request events targeting the main branch
pull_request:
branches:
- main
types:
- opened
- synchronize
- edited
- reopened

jobs:
validate-PR-title:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Validate PR title
run: |
# Regex for Conventional Commits specification
PR_TITLE_REGEX="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([^\)]*\))?:\s?(EXPOSED-[0-9]+\s?)?.+$"
# Get the PR title
PR_TITLE="${{ github.event.pull_request.title }}"
# Check if PR title matches the regex
if [[ ! "$PR_TITLE" =~ $PR_TITLE_REGEX ]]; then
echo "❌ The PR title does not follow the Conventional Commits specification."
echo "PR Title -> $PR_TITLE"
echo "Valid PR title format: <type>(<optional scope>): <optional EXPOSED-<number>> <subject>"
echo "Example: fix: Bug in insert"
echo "Please check https://www.conventionalcommits.org/en/v1.0.0/#summary"
exit 1
else
echo "🎉 The PR title is following the Conventional Commits specification."
fi

0 comments on commit 30c9614

Please sign in to comment.