From ac7c12712047d278a70bdda98d4f8c2004183624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Chojnacki?= Date: Wed, 6 Nov 2024 17:20:01 +0100 Subject: [PATCH 1/2] Fix leading ticket number --- .github/workflows/tests.yml | 16 ++++++++++++++++ pre_ticket/tickets.py | 5 +++-- setup.py | 1 + tests/__init__.py | 0 tests/test_tickets.py | 38 +++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/tests.yml create mode 100644 tests/__init__.py create mode 100644 tests/test_tickets.py 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..cf970d1 100644 --- a/pre_ticket/tickets.py +++ b/pre_ticket/tickets.py @@ -18,15 +18,16 @@ def retrieve_ticket(branch, regex): def is_ticket_in_message(contents, ticket): - for line in contents.splitlines(): + 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 From 4701d8a01bdcc77caec1d789f8756289fc633cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Chojnacki?= Date: Thu, 7 Nov 2024 17:33:01 +0100 Subject: [PATCH 2/2] Add docstring --- pre_ticket/tickets.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pre_ticket/tickets.py b/pre_ticket/tickets.py index cf970d1..5f685fc 100644 --- a/pre_ticket/tickets.py +++ b/pre_ticket/tickets.py @@ -18,6 +18,26 @@ def retrieve_ticket(branch, regex): def is_ticket_in_message(contents, ticket): + """ + 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()