Skip to content

Commit 429575f

Browse files
committed
ci: EXPOSED-725 Enforce commit message rules
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.
1 parent 753ba39 commit 429575f

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This workflow ensures that all commit messages in a pull request adhere to the Conventional Commits specification.
2+
3+
name: Commit Message Validation
4+
5+
on:
6+
# Allows you to run this workflow manually from the Actions tab
7+
workflow_dispatch:
8+
9+
# Triggers the workflow on pull request events targeting the main branch
10+
pull_request:
11+
branches:
12+
- main
13+
types:
14+
- opened
15+
- synchronize
16+
- reopened
17+
18+
jobs:
19+
validate-commits:
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- name: Checkout code
24+
uses: actions/checkout@v3
25+
with:
26+
fetch-depth: 0
27+
28+
- name: Validate commit messages
29+
run: |
30+
# Regex for Conventional Commits specification
31+
COMMIT_REGEX="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([^\)]*\))?:\s?(EXPOSED-[0-9]+\s?)?.+$"
32+
33+
# Get all commits in the pull request (from base to head)
34+
COMMITS=$(git log --format=%s --no-merges origin/main..${{ github.sha }})
35+
36+
# Initialize counters and store invalid commits
37+
INVALID_COMMITS=()
38+
VALID_COMMITS=()
39+
40+
echo "ℹ️ Checking if commit messages are following the Conventional Commits specification..."
41+
42+
# Loop through each commit message
43+
IFS=$'\n'
44+
for COMMIT_MSG in $COMMITS; do
45+
# Check if commit message matches the regex
46+
if [[ ! "$COMMIT_MSG" =~ $COMMIT_REGEX ]]; then
47+
INVALID_COMMITS+=("$COMMIT_MSG")
48+
echo -e "❌ $COMMIT_MSG"
49+
else
50+
VALID_COMMITS+=("$COMMIT_MSG")
51+
echo -e "✅ $COMMIT_MSG"
52+
fi
53+
done
54+
55+
# If there are invalid commits, print the summary
56+
if [ ${#INVALID_COMMITS[@]} -gt 0 ]; then
57+
echo ""
58+
echo "🛑 Some commit messages are not following the Conventional Commits specification."
59+
echo ""
60+
echo "Valid commit message format: <type>(<optional scope>): <subject>"
61+
echo "Example: fix: Bug in insert"
62+
echo "Please check https://www.conventionalcommits.org/en/v1.0.0/#summary"
63+
exit 1
64+
fi
65+
66+
echo "🎉 All commit messages are following the Conventional Commits specification."
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This workflow ensures that all commit messages in a pull request adhere to the Conventional Commits specification.
2+
3+
name: Pull Request Title Validation
4+
5+
on:
6+
# Allows you to run this workflow manually from the Actions tab
7+
workflow_dispatch:
8+
9+
# Triggers the workflow on pull request events targeting the main branch
10+
pull_request:
11+
branches:
12+
- main
13+
types:
14+
- opened
15+
- synchronize
16+
- edited
17+
- reopened
18+
19+
jobs:
20+
validate-PR-title:
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v3
26+
27+
- name: Validate PR title
28+
run: |
29+
# Regex for Conventional Commits specification
30+
PR_TITLE_REGEX="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([^\)]*\))?:\s?(EXPOSED-[0-9]+\s?)?.+$"
31+
32+
# Get the PR title
33+
PR_TITLE="${{ github.event.pull_request.title }}"
34+
35+
# Check if PR title matches the regex
36+
if [[ ! "$PR_TITLE" =~ $PR_TITLE_REGEX ]]; then
37+
echo "❌ The PR title does not follow the Conventional Commits specification."
38+
echo "PR Title -> $PR_TITLE"
39+
echo "Valid PR title format: <type>(<optional scope>): <optional EXPOSED-<number>> <subject>"
40+
echo "Example: fix: Bug in insert"
41+
echo "Please check https://www.conventionalcommits.org/en/v1.0.0/#summary"
42+
exit 1
43+
else
44+
echo "🎉 The PR title is following the Conventional Commits specification."
45+
fi

0 commit comments

Comments
 (0)