-
-
Notifications
You must be signed in to change notification settings - Fork 0
161 lines (140 loc) · 5.87 KB
/
commit-lint.yml
File metadata and controls
161 lines (140 loc) · 5.87 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
name: Commit Lint
on:
pull_request:
branches: [main, develop]
types: [opened, synchronize, reopened, edited]
permissions:
contents: read
pull-requests: read
jobs:
lint-commits:
name: Validate Commit Messages
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@v2
with:
egress-policy: audit
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Validate commit messages
run: |
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}Validating commit messages...${NC}"
# Get the base branch
BASE_REF="${{ github.event.pull_request.base.sha }}"
HEAD_REF="${{ github.event.pull_request.head.sha }}"
# Get all commits in this PR
COMMITS=$(git log --pretty=format:"%H %s" "$BASE_REF".."$HEAD_REF")
# Conventional commit pattern
# Types: build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test
PATTERN="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z0-9_-]+\))?(!)?: .+"
INVALID_COMMITS=()
VALID_COUNT=0
TOTAL_COUNT=0
while IFS= read -r line; do
[[ -z "$line" ]] && continue
TOTAL_COUNT=$((TOTAL_COUNT + 1))
HASH=$(echo "$line" | awk '{print $1}')
MESSAGE=$(echo "$line" | cut -d' ' -f2-)
if [[ "$MESSAGE" =~ $PATTERN ]]; then
echo -e "${GREEN}✓${NC} $MESSAGE"
VALID_COUNT=$((VALID_COUNT + 1))
else
echo -e "${RED}✗${NC} $MESSAGE"
INVALID_COMMITS+=("$HASH: $MESSAGE")
fi
done <<< "$COMMITS"
echo ""
echo -e "${BLUE}Summary:${NC}"
echo -e " Total commits: $TOTAL_COUNT"
echo -e " Valid commits: ${GREEN}$VALID_COUNT${NC}"
echo -e " Invalid commits: ${RED}$((TOTAL_COUNT - VALID_COUNT))${NC}"
# If there are invalid commits, fail the check
if [ ${#INVALID_COMMITS[@]} -gt 0 ]; then
echo ""
echo -e "${RED}❌ Found invalid commit messages:${NC}"
echo ""
for commit in "${INVALID_COMMITS[@]}"; do
echo -e " ${RED}✗${NC} $commit"
done
echo ""
echo -e "${YELLOW}Commit messages must follow the Conventional Commits specification:${NC}"
echo ""
echo -e " Format: ${BLUE}type(scope): description${NC}"
echo ""
echo -e " Types:"
echo -e " ${GREEN}feat${NC} - New feature"
echo -e " ${GREEN}fix${NC} - Bug fix"
echo -e " ${GREEN}docs${NC} - Documentation changes"
echo -e " ${GREEN}style${NC} - Code style changes (formatting, etc.)"
echo -e " ${GREEN}refactor${NC} - Code refactoring"
echo -e " ${GREEN}perf${NC} - Performance improvements"
echo -e " ${GREEN}test${NC} - Test changes"
echo -e " ${GREEN}chore${NC} - Build process or auxiliary tool changes"
echo -e " ${GREEN}ci${NC} - CI configuration changes"
echo -e " ${GREEN}build${NC} - Build system changes"
echo -e " ${GREEN}revert${NC} - Revert a previous commit"
echo ""
echo -e " Examples:"
echo -e " ${BLUE}feat(auth): Add login functionality${NC}"
echo -e " ${BLUE}fix(api): Resolve null pointer exception${NC}"
echo -e " ${BLUE}docs(readme): Update README with installation steps${NC}"
echo -e " ${BLUE}feat(auth)!: Breaking change in auth flow${NC}"
echo ""
echo -e " For more info: ${BLUE}https://www.conventionalcommits.org${NC}"
# Add to step summary
{
echo "## ❌ Commit Message Validation Failed"
echo ""
echo "The following commits do not follow the Conventional Commits specification:"
echo ""
for commit in "${INVALID_COMMITS[@]}"; do
echo "- \`$commit\`"
done
echo ""
echo "### Required Format"
echo ""
echo "\`\`\`"
echo "type(scope): description"
echo "\`\`\`"
echo ""
echo "### Valid Types"
echo ""
echo "- \`feat\` - New feature"
echo "- \`fix\` - Bug fix"
echo "- \`docs\` - Documentation changes"
echo "- \`style\` - Code style changes"
echo "- \`refactor\` - Code refactoring"
echo "- \`perf\` - Performance improvements"
echo "- \`test\` - Test changes"
echo "- \`chore\` - Maintenance tasks"
echo "- \`ci\` - CI changes"
echo "- \`build\` - Build system changes"
echo ""
echo "### Examples"
echo ""
echo "- \`feat(auth): Add login functionality\`"
echo "- \`fix(api): Resolve null pointer exception\`"
echo "- \`docs(readme): Update README\`"
echo ""
echo "Learn more: [Conventional Commits](https://www.conventionalcommits.org)"
} >> $GITHUB_STEP_SUMMARY
exit 1
fi
echo ""
echo -e "${GREEN}✓ All commit messages are valid${NC}"
# Add success to step summary
{
echo "## ✅ Commit Message Validation Passed"
echo ""
echo "All $VALID_COUNT commit(s) follow the Conventional Commits specification."
} >> $GITHUB_STEP_SUMMARY
exit 0