Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: tests

on: push

jobs:
run_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Install dependencies
run: |
pip install six pytest
- run: pytest
25 changes: 23 additions & 2 deletions pre_ticket/tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,36 @@ def retrieve_ticket(branch, regex):


def is_ticket_in_message(contents, ticket):
for line in contents.splitlines():
"""
Checks if a specified ticket is present in the given message contents.

This function scans through each line of the provided message contents,
ignoring empty lines and comments (except for the first line), to determine
if the specified ticket is mentioned. It performs a case-insensitive search
for the ticket within the non-empty, non-comment lines.

The first line is an exception because some conventions assume that the task
number preceded by a hashtag will appear at the beginning of the commit
message: "#123 Commit Message".

Examples:
>>> is_ticket_in_message("This is a message with ticket #123", "#123")
True
>>> is_ticket_in_message("This is a message without a ticket", "#123")
False
>>> is_ticket_in_message("#123 This is a message with a ticket", "#123")
True
"""
for i, line in enumerate(contents.splitlines()):
stripped = line.strip().lower()

if stripped == "" or stripped.startswith("#"):
if stripped == "" or (stripped.startswith("#") and i != 0):
continue

if ticket.lower() in stripped:
return True

return False

def add_ticket_number(filename, regex, format_template):
branch = get_current_branch()
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
description="pre-commit hook for adding issue ticket number to your git commit messages",
install_requires=requirements,
tests_require=["pytest"],
name="pre_ticket",
version="1.0.0",
license="MIT license",
Expand Down
Empty file added tests/__init__.py
Empty file.
38 changes: 38 additions & 0 deletions tests/test_tickets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from pre_ticket.tickets import is_ticket_in_message
import pytest


with_ticket_1 = """#1234 Updated some code

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored.
"""

with_ticket_2 = """Updated some code

Ref: #1234

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored.
"""

without_ticket_1 = ""

without_ticket_2 = """Updated some code

# 1234
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored.
"""

@pytest.mark.parametrize(
("content", "expected_result"),
[
(without_ticket_1, False),
(without_ticket_2, False),
(with_ticket_1, True),
(with_ticket_2, True),
]
)
def test_is_ticket_in_message(content, expected_result):
assert is_ticket_in_message(content, "1234") is expected_result