Skip to content

Commit

Permalink
Allow conventional commits
Browse files Browse the repository at this point in the history
  • Loading branch information
platisd committed Aug 15, 2024
1 parent 4bf4604 commit 07a9a66
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
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-22.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
16 changes: 16 additions & 0 deletions bad_commit_message_blocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,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 +189,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'",
action="store_true",
default=False,
)
args = parser.parse_args()

commit_message = args.message.strip()
Expand All @@ -197,6 +210,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()
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
textblob==0.9.0
pytest==8.3.2

0 comments on commit 07a9a66

Please sign in to comment.