Skip to content

Commit

Permalink
Allow conventional commits
Browse files Browse the repository at this point in the history
Some projects use 'conventional commits' so let's make it
easier for them to use this Action,
by ignoring the subject/tag.
  • Loading branch information
platisd committed Aug 15, 2024
1 parent 29f6a11 commit ab8015b
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 8 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Python package

on: [push]

jobs:
build:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
python -m textblob.download_corpora
- name: Run tests
run: |
python -m pytest bad_commit_message_blocker_tests.py
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__pycache__
__pycache__
.venv
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ jobs:
body_limit: 100
# Optionally set the remote branch name to merge (default `master`)
remote_branch: dev
# Optionally allow "conventional commits" (default `false`)
allow_conventional_commits: true
```
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ inputs:
description: 'The maximum allowed length for a line in the commit body'
required: true
default: 72
conventional_commit:
description: 'Whether to allow conventional commits, e.g. "feat: add new feature"'
required: false
default: false
remote_branch:
description: 'The name of the remote branch you are trying to merge with'
required: true
Expand Down
26 changes: 20 additions & 6 deletions bad_commit_message_blocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ def check_subject_uses_imperative(commit_message):
third_person_blob = TextBlob(third_person_prefix + first_line)
non_third_person_blob = TextBlob(non_third_person_prefix + first_line)

first_word, third_person_result = third_person_blob.tags[
words_in_third_person_prefix_blob
]
_, non_third_person_result = non_third_person_blob.tags[
words_in_non_third_person_prefix_blob
]
tags = third_person_blob.tags
first_word, third_person_result = tags[words_in_third_person_prefix_blob]
tags = non_third_person_blob.tags
_, non_third_person_result = tags[words_in_non_third_person_prefix_blob]

# We need to determine whether the first word is a non-third person verb
# when parsed in a non-third person blob. However, there were some
Expand Down Expand Up @@ -167,6 +165,12 @@ def check(
return all_rules_verified


def strip_prefix(commit_message):
if ":" in commit_message:
return commit_message[commit_message.index(":") + 1 :].strip()
return commit_message


def main():
parser_description = (
"Bad commit message blocker: Avoid bad commit messages in your repository"
Expand All @@ -183,6 +187,13 @@ def main():
help="The maximum allowed length for a line in the commit body",
default=DEFAULT_BODY_LIMIT,
)
parser.add_argument(
"--conventional-commit",
help="Whether the commit message follows the conventional commit format,"
" e.g. 'feat: add new feature'",
type=bool,
default=False,
)
args = parser.parse_args()

commit_message = args.message.strip()
Expand All @@ -197,6 +208,9 @@ def main():
+ CliColors.ENDC
)

if args.conventional_commit:
commit_message = strip_prefix(commit_message)

all_rules_verified = check(
commit_message, int(args.subject_limit), int(args.body_limit)
)
Expand Down
10 changes: 10 additions & 0 deletions bad_commit_message_blocker_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ def test_checkBodyExplainsWhatAndWhy_WhenCalled_WillReturnTrue(self):
test_input = "Something that does not matter"
self.assertTrue(blocker.check_body_explains_what_and_why(test_input))

def test_stripPrefix_WhenColonInMessage_WillReturnEverythingAfterColon(self):
test_input = "feat: add new feature"
expected_output = "add new feature"
self.assertEqual(blocker.strip_prefix(test_input), expected_output)

def test_stripPrefix_WhenNoColonInMessage_WillReturnWholeMessage(self):
test_input = "add new feature"
expected_output = "add new feature"
self.assertEqual(blocker.strip_prefix(test_input), expected_output)


if __name__ == "__main__":
unittest.main()
3 changes: 2 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ while read -r commit_hash; do
python3 $script_dir/bad_commit_message_blocker.py \
--message "${commit_message}" \
--subject-limit "${INPUT_SUBJECT_LIMIT}" \
--body-limit "${INPUT_BODY_LIMIT}"
--body-limit "${INPUT_BODY_LIMIT}" \
--conventional-commit "${INPUT_CONVENTIONAL_COMMIT}"
done <<< "$commits_since_master"
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
textblob==0.18.0.post0
pytest==8.3.2

0 comments on commit ab8015b

Please sign in to comment.