Skip to content

Commit 8cf2db4

Browse files
author
Christian Riedel
committed
improve tag searching in commit message
Prior only the first of the tag was checked. Now all occurances are iterated over until a fullmatch is found, which then is optionally online verified.
1 parent 4d2483e commit 8cf2db4

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/commit_msg_jira_hook/check_jira_tag.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,31 @@ def main(ctx, jira_tag: str, jira_url: str, verify: bool, commit_msg_file: str)
8787

8888
c_msg_cleaned = "\n".join(c_msg_lines)
8989

90+
regex_str = r"(" + jira_tag.upper() + r")-?([0-9]+)?"
91+
9092
#: Extract tag from commit msg
91-
extract = re.search(r"(" + jira_tag.upper() + r")-?([0-9]+)?", c_msg_cleaned)
93+
extract = re.search(regex_str, c_msg_cleaned)
9294

9395
#: Check if tag is in commit msg
9496
if extract is None:
9597
click.echo(f"'{jira_tag.upper()}' tag not found in commit message.")
9698
ctx.abort()
9799

98-
#: Check if tag has a number
99100
if extract.group(2) is None: # type: ignore
100101
click.echo(f"'{jira_tag.upper()}' tag but no number found in commit message.")
101102
ctx.abort()
102103

103-
#: Get tag from extract
104-
issue = str(extract.group(0)) # type: ignore
104+
for match in re.finditer(regex_str, c_msg_cleaned):
105+
#: Check if tag has a number
106+
if match.group(2) is None: # type: ignore
107+
#: This tag has no number, try next
108+
continue
109+
#: Get tag from extract
110+
issue = str(match.group(0)) # type: ignore
111+
break
112+
else:
113+
click.echo(f"'{jira_tag.upper()}' tag but no number found in commit message.")
114+
ctx.abort()
105115

106116
#: Exit with 0 when online check is disabled
107117
if not verify:

tests/test_check_jira_tag.py

+21
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,24 @@ def test_no_verify(mock_ini, mock_commit_msg_file, cli_runner):
209209
)
210210

211211
assert result.exit_code == 0
212+
213+
214+
@pytest.mark.parametrize(
215+
"commit_msg",
216+
[
217+
"TAG message TAG123",
218+
"TAG message TAG-123",
219+
"message #TAG #TAG-123 #TAG",
220+
],
221+
)
222+
def test_all_tag_finds_are_search_for_fullmatch(
223+
commit_msg, mock_ini, mock_commit_msg_file, cli_runner
224+
):
225+
"""Assert next tag occurance is checked if prior one is missing number."""
226+
mock_commit_msg_file.write_text(commit_msg)
227+
228+
result = cli_runner.invoke(
229+
main, ["--jira-tag=TAG", "--no-verify", str(mock_commit_msg_file)]
230+
)
231+
232+
assert result.exit_code == 0

0 commit comments

Comments
 (0)