diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..11da20d --- /dev/null +++ b/.github/workflows/tests.yml @@ -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 diff --git a/pre_ticket/tickets.py b/pre_ticket/tickets.py index 2b4e477..5f685fc 100644 --- a/pre_ticket/tickets.py +++ b/pre_ticket/tickets.py @@ -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() diff --git a/setup.py b/setup.py index 21825ee..96da6a0 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tickets.py b/tests/test_tickets.py new file mode 100644 index 0000000..4b378fa --- /dev/null +++ b/tests/test_tickets.py @@ -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