Skip to content

Commit 3ebf7f7

Browse files
committed
Allow conventional commits
1 parent 4bf4604 commit 3ebf7f7

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

.github/workflows/test-action.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Python package
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-20.04
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
- name: Set up Python
12+
uses: actions/setup-python@v5
13+
with:
14+
python-version: "3.12"
15+
- name: Install dependencies
16+
run: |
17+
python -m pip install --upgrade pip
18+
pip install -r requirements.txt
19+
python -m textblob.download_corpora
20+
- name: Run tests
21+
run: |
22+
python -m pytest bad_commit_message_blocker_tests.py

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
__pycache__
1+
__pycache__
2+
.venv

bad_commit_message_blocker.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,10 @@ def check_subject_uses_imperative(commit_message):
9797
third_person_blob = TextBlob(third_person_prefix + first_line)
9898
non_third_person_blob = TextBlob(non_third_person_prefix + first_line)
9999

100-
first_word, third_person_result = third_person_blob.tags[
101-
words_in_third_person_prefix_blob
102-
]
103-
_, non_third_person_result = non_third_person_blob.tags[
104-
words_in_non_third_person_prefix_blob
105-
]
100+
tags = third_person_blob.tags
101+
first_word, third_person_result = tags[words_in_third_person_prefix_blob]
102+
tags = non_third_person_blob.tags
103+
_, non_third_person_result = tags[words_in_non_third_person_prefix_blob]
106104

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

169167

168+
def strip_prefix(commit_message):
169+
if ":" in commit_message:
170+
return commit_message[commit_message.index(":") + 1 :].strip()
171+
return commit_message
172+
173+
170174
def main():
171175
parser_description = (
172176
"Bad commit message blocker: Avoid bad commit messages in your repository"
@@ -183,6 +187,13 @@ def main():
183187
help="The maximum allowed length for a line in the commit body",
184188
default=DEFAULT_BODY_LIMIT,
185189
)
190+
parser.add_argument(
191+
"--conventional-commit",
192+
help="Whether the commit message follows the conventional commit format,"
193+
" e.g. 'feat: add new feature'",
194+
action="store_true",
195+
default=False,
196+
)
186197
args = parser.parse_args()
187198

188199
commit_message = args.message.strip()
@@ -197,6 +208,9 @@ def main():
197208
+ CliColors.ENDC
198209
)
199210

211+
if args.conventional_commit:
212+
commit_message = strip_prefix(commit_message)
213+
200214
all_rules_verified = check(
201215
commit_message, int(args.subject_limit), int(args.body_limit)
202216
)

bad_commit_message_blocker_tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ def test_checkBodyExplainsWhatAndWhy_WhenCalled_WillReturnTrue(self):
144144
test_input = "Something that does not matter"
145145
self.assertTrue(blocker.check_body_explains_what_and_why(test_input))
146146

147+
def test_stripPrefix_WhenColonInMessage_WillReturnEverythingAfterColon(self):
148+
test_input = "feat: add new feature"
149+
expected_output = "add new feature"
150+
self.assertEqual(blocker.strip_prefix(test_input), expected_output)
151+
152+
def test_stripPrefix_WhenNoColonInMessage_WillReturnWholeMessage(self):
153+
test_input = "add new feature"
154+
expected_output = "add new feature"
155+
self.assertEqual(blocker.strip_prefix(test_input), expected_output)
156+
147157

148158
if __name__ == "__main__":
149159
unittest.main()

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nltk==3.8.1
2+
textblob==0.9.0
3+
pytest==8.3.2

0 commit comments

Comments
 (0)