-
Notifications
You must be signed in to change notification settings - Fork 697
98 lines (81 loc) · 3.56 KB
/
commit-message-validation.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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
- edited
- reopened
jobs:
validate-commits:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action == 'opened') }}
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."
validate-PR-title:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' &&
(github.event.action == 'opened' ||
(github.event.action == 'edited' && github.event.changes.title)) }}
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