Skip to content

Commit 6f05afa

Browse files
committed
Add commit message blocker as GitHub Action
1 parent daf6ea6 commit 6f05afa

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM python:3.7-slim-buster
2+
3+
RUN apt-get update
4+
RUN apt-get -y install git
5+
6+
RUN pip3 install textblob
7+
RUN python3 -m textblob.download_corpora
8+
# nltk_data ends up in /root so python can't find it
9+
RUN ln -s /root/nltk_data /usr/local/nltk_data
10+
11+
COPY entrypoint.sh /blocker/entrypoint.sh
12+
COPY bad_commit_message_blocker.py /blocker/bad_commit_message_blocker.py
13+
14+
ENTRYPOINT ["/blocker/entrypoint.sh"]

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A simple script, doing some natural language processing, to inhibit
66

77
## What?
88
A Python3 script, easy to integrate with various CI machinery
9-
(e.g. see [TravisCI example](https://github.com/platisd/bad-commit-message-blocker/blob/master/.travis.yml))
9+
(e.g. see [GitHub Actions example](#github-action))
1010
that is meant to keep bad commit messages out of a project. It verifies
1111
whether the [seven rules of a great Git commit message](https://chris.beams.io/posts/git-commit/)
1212
by Chris Beams, are being followed:
@@ -66,4 +66,31 @@ appropriate arguments:
6666
* `--subject-limit` (defaults to `50`) to set the subject line limit. E.g.:
6767
* `python3 bad_commit_message_blocker.py --subject-limit 80 --message "Add a really cool feature"`
6868
* `--body-limit` (defaults to `72`) to set the body line limit. E.g.:
69-
* `python3 bad_commit_message_blocker.py --body-limit 120 --message "Add a really cool feature"`
69+
* `python3 bad_commit_message_blocker.py --body-limit 120 --message "Add a really cool feature"`
70+
71+
## GitHub Action
72+
73+
Now you can use this script as part of your **GitHub Actions** CI pipeline.
74+
75+
An example configuration can be seen below:
76+
77+
```yaml
78+
name: Commit messages
79+
80+
on: [pull_request]
81+
82+
jobs:
83+
check-commit-message:
84+
runs-on: ubuntu-20.04
85+
steps:
86+
- name: Verify commit messages follow best practices in CI
87+
uses: platisd/bad-commit-message-blocker@master
88+
with:
89+
github_token: ${{ secrets.GITHUB_TOKEN }}
90+
# Optionally set the subject character limit (default `50`)
91+
subject_limit: 60
92+
# Optionally set the body character limit (default `72`)
93+
body_limit: 100
94+
# Optionally set the remote branch name to merge (default `master`)
95+
remote_branch: dev
96+
```

action.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: 'Bad commit message blocker'
2+
description: 'Inhibit commits with bad messages from getting merged'
3+
inputs:
4+
subject_limit:
5+
description: 'The maximum allowed length for a commit subject'
6+
required: true
7+
default: 50
8+
body_limit:
9+
description: 'The maximum allowed length for a line in the commit body'
10+
required: true
11+
default: 72
12+
remote_branch:
13+
description: 'The name of the remote branch you are trying to merge with'
14+
required: true
15+
default: 'master'
16+
github_token:
17+
description: 'The GitHub token'
18+
required: true
19+
runs:
20+
using: 'docker'
21+
image: 'Dockerfile'
22+
branding:
23+
icon: 'check'
24+
color: 'green'

entrypoint.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -eu
3+
4+
script_dir="$(dirname "$0")"
5+
cd $script_dir
6+
7+
eval git clone "https://${INPUT_GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" ${GITHUB_REPOSITORY}
8+
cd $GITHUB_REPOSITORY
9+
eval git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
10+
eval git fetch --all
11+
eval git checkout ${GITHUB_HEAD_REF:-$(basename ${GITHUB_REF})}
12+
13+
commits_since_master=$(git rev-list HEAD ^origin/${INPUT_REMOTE_BRANCH})
14+
15+
while read -r commit_hash; do
16+
commit_message="$(git log --format=%B -n 1 ${commit_hash})"
17+
python3 $script_dir/bad_commit_message_blocker.py \
18+
--message "${commit_message}" \
19+
--subject-limit "${INPUT_SUBJECT_LIMIT}" \
20+
--body-limit "${INPUT_BODY_LIMIT}"
21+
done <<< "$commits_since_master"

0 commit comments

Comments
 (0)