-
-
Notifications
You must be signed in to change notification settings - Fork 90
148 lines (135 loc) · 4.69 KB
/
bad-commit-message-blocker.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
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
---
name: 📝 Git commit messages
on: # yamllint disable-line rule:truthy
pull_request:
env:
MAX_ALLOWED_PR_COMMITS: 50
jobs:
check-commit-message:
name: ✍
if: github.event.pull_request.user.type != 'Bot'
runs-on: ubuntu-22.04
steps:
- name: Get PR commits details
id: commits
run: |
COMMITS_NUMBER="$(\
2>http_headers curl --verbose --show-error \
--header 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' \
--header 'Accept: application/vnd.github.v3+json' \
'https://api.github.com/repos/${{
github.repository
}}/pulls/${{
github.event.pull_request.number
}}/commits?per_page=${{
env.MAX_ALLOWED_PR_COMMITS
}}' | jq length \
)"
echo "number=${COMMITS_NUMBER}" >> "${GITHUB_OUTPUT}"
if grep 'link:' http_headers
then
TOO_MANY=true
else
TOO_MANY=false
fi
echo "too-many=${TOO_MANY}" >> "${GITHUB_OUTPUT}"
echo check-range='${{
github.event.pull_request.base.sha
}}..${{
github.event.pull_request.head.sha
}}' >> "${GITHUB_OUTPUT}"
echo last-commit='${{
github.event.pull_request.head.sha
}}' >> "${GITHUB_OUTPUT}"
shell: bash
- name: Prepare helper commands
id: commands
run: |
echo gitlint-install='pip install --user gitlint' >> "${GITHUB_OUTPUT}"
echo gitlint-lint="\
python -m \
gitlint.cli \
--ignore-stdin \
--commits '${{ steps.commits.outputs.check-range }}'\
" >> "${GITHUB_OUTPUT}"
# -C ./.github/workflows/.gitlint
echo git-log="\
git log --color --no-patch \
'${{ steps.commits.outputs.check-range }}'\
" >> "${GITHUB_OUTPUT}"
shell: bash
# Ref:https://joe.gl/ombek/blog/pr-gitlint/
- name: Check out src from Git
uses: actions/checkout@v3
with:
fetch-depth: steps.commits.outputs.number
ref: ${{ github.event.pull_request.head.sha }}
- name: Log the commits to be checked
run: ${{ steps.commands.outputs.git-log }}
- name: Install gitlint
run: ${{ steps.commands.outputs.gitlint-install }}
- name: Check commit messages in the PR
shell: bash
run: |
>&2 echo Checking the following commit range: '${{
steps.commits.outputs.check-range
}}'
${{
steps.commands.outputs.gitlint-lint
}} 2>&1 | >&2 tee gitlint-results.txt
- name: Report check summary for too many commits
if: fromJSON(steps.commits.outputs.too-many)
run: >-
echo
'# This pull request has too many commits, only the last ${{
env.MAX_ALLOWED_PR_COMMITS
}} have been checked'
>>
"${GITHUB_STEP_SUMMARY}"
shell: bash
- name: Set a failing status because of too many commits
if: fromJSON(steps.commits.outputs.too-many)
run: exit 1
shell: bash
- name: Report gitlint check summary
if: always()
run: |
echo >> "${GITHUB_STEP_SUMMARY}"
echo '> **Note**' >> "${GITHUB_STEP_SUMMARY}"
echo \
'> If this check is failing, check the Gitlint rule violations,' \
'matching them with the corresponding commits listed down below.' \
'Fix the problems and force-push the pull request branch to clear' \
'this failure.' >> "${GITHUB_STEP_SUMMARY}"
echo >> "${GITHUB_STEP_SUMMARY}"
echo >> "${GITHUB_STEP_SUMMARY}"
echo '# Gitlint check output' >> "${GITHUB_STEP_SUMMARY}"
echo >> "${GITHUB_STEP_SUMMARY}"
echo '```console' >> "${GITHUB_STEP_SUMMARY}"
echo "$ ${{
steps.commands.outputs.gitlint-install
}}" >> "${GITHUB_STEP_SUMMARY}"
echo "$ ${{
steps.commands.outputs.gitlint-lint
}}" >> "${GITHUB_STEP_SUMMARY}"
if [[ \
"" != "$(cat gitlint-results.txt)" && \
! $(grep 'Commit' gitlint-results.txt) \
]]
then
echo 'Commit ${{
steps.commits.outputs.last-commit
}}:' >> "${GITHUB_STEP_SUMMARY}"
fi
cat gitlint-results.txt >> "${GITHUB_STEP_SUMMARY}"
echo '```' >> "${GITHUB_STEP_SUMMARY}"
echo >> "${GITHUB_STEP_SUMMARY}"
echo '# Checked commits' >> "${GITHUB_STEP_SUMMARY}"
echo >> "${GITHUB_STEP_SUMMARY}"
echo '```console' >> "${GITHUB_STEP_SUMMARY}"
${{
steps.commands.outputs.git-log
}} --no-color >> "${GITHUB_STEP_SUMMARY}"
echo '```' >> "${GITHUB_STEP_SUMMARY}"
shell: bash
...