Skip to content

Commit 07a9a66

Browse files
committed
Allow conventional commits
1 parent 4bf4604 commit 07a9a66

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
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-22.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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ def check(
167167
return all_rules_verified
168168

169169

170+
def strip_prefix(commit_message):
171+
if ":" in commit_message:
172+
return commit_message[commit_message.index(":") + 1 :].strip()
173+
return commit_message
174+
175+
170176
def main():
171177
parser_description = (
172178
"Bad commit message blocker: Avoid bad commit messages in your repository"
@@ -183,6 +189,13 @@ def main():
183189
help="The maximum allowed length for a line in the commit body",
184190
default=DEFAULT_BODY_LIMIT,
185191
)
192+
parser.add_argument(
193+
"--conventional-commit",
194+
help="Whether the commit message follows the conventional commit format,"
195+
" e.g. 'feat: add new feature'",
196+
action="store_true",
197+
default=False,
198+
)
186199
args = parser.parse_args()
187200

188201
commit_message = args.message.strip()
@@ -197,6 +210,9 @@ def main():
197210
+ CliColors.ENDC
198211
)
199212

213+
if args.conventional_commit:
214+
commit_message = strip_prefix(commit_message)
215+
200216
all_rules_verified = check(
201217
commit_message, int(args.subject_limit), int(args.body_limit)
202218
)

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
textblob==0.9.0
2+
pytest==8.3.2

0 commit comments

Comments
 (0)