Skip to content
Merged
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
Empty file added 18
Empty file.
21 changes: 11 additions & 10 deletions apps/jira_utils/jira_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def get_jira_information(
file_name: str,
) -> tuple[str, str]:
jira_error_string = ""
re_compile = r"(?<![\d.])\d+\.\d+(?:\.(?:\d+|z))?\b"
re_compile = rf"(?<![\d.])\d+\.\d+(?:\.(?:\d+|z))?|{target_version_str}\b"

try:
# check resolved status:
Expand All @@ -116,20 +116,21 @@ def get_jira_information(
if skip_project_ids and jira_id.startswith(tuple(skip_project_ids)):
return file_name, jira_error_string

# If a bug has a fix version, extract it using regex
if (jira_fix_versions := jira_issue_metadata.fixVersions) and (
found_version := re.findall(re_compile, jira_fix_versions[0].name)
):
current_target_version = found_version[0]
current_target_versions = [target_version_str]
# If a bug has fix version(s), extract using regex
if jira_fix_versions := jira_issue_metadata.fixVersions:
jira_fix_versions = ",".join([jira_fix_version.name for jira_fix_version in jira_fix_versions])
current_target_versions = re.findall(re_compile, jira_fix_versions)

else:
current_target_version = target_version_str
if any([version in jira_target_versions for version in current_target_versions]):
return file_name, jira_error_string

if not any([current_target_version == version for version in jira_target_versions]):
else:
jira_error_string += (
f"{jira_id} target version: {current_target_version}, does not match expected "
f"{jira_id} target versions: {current_target_versions}, do not match expected "
f"version {jira_target_versions}."
)

except JIRAError as exp:
jira_error_string += f"{jira_id} JiraError status code: {exp.status_code}, details: {exp.text}]."

Expand Down
258 changes: 166 additions & 92 deletions tests/jira_utils/test_jira_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess

import pytest
from jira.exceptions import JIRAError
from pyhelper_utils.shell import run_command
from simple_logger.logger import get_logger

Expand Down Expand Up @@ -73,64 +74,128 @@ def test_process_jira_command_line_config_file_valid_config(mocker):


@pytest.mark.parametrize(
"jira_id, resolved_status, jira_target_versions, target_version_str, file_name, "
"skip_project_ids, expected_jira_error_string, "
"test_jira_version",
"test_params",
[
# Test case 1: Issue with no jira target versions and not resolved status
("issue1", ["resolved"], [], "1.0", "file1.txt", [], "", "1.0"),
# Test case 2: Issue with no jira target versions, but resolved status
(
"issue2",
["open"],
[],
"1.0",
"file2.txt",
[],
"issue2 current status: open is resolved.",
"1.0",
),
# Test case 3: Issue with no jira target versions, default resolved status
("issue3", [], [], "", "file3.txt", [], "", "1.1"),
# Test case 4: Issue with not resolved state, but matching jira target version
("issue4", ["resolved"], ["1.0"], "1.0", "file4.txt", [], "", "1.0"),
# Test case 5: Issue with not resolved state, and not matching jira target version
(
"issue5",
["resolved"],
["1.1"],
"1.0",
"file5.txt",
[],
"issue5 target version: 1.0, does not match expected version ['1.1'].",
"1.0",
),
# Test case 6: Issue that would be skipped for version check because of skip
("issue6", ["resolved"], ["1.0"], "1.0", "file6.txt", ["issue"], "", "1.1"),
# Test case 7: Issue that would be skipped for version check but fail resolved check
(
"issue7",
["open"],
["1.0"],
"1.0",
"file6.txt",
["issue"],
"issue7 current status: open is resolved.",
"1.1",
),
# Test case 8: Issue with unresolved state, and matching jira z target version
("issue8", [], ["1.2.z"], "1.2,z", "file4.txt", [], "", "1.2.z"),
# Test case 9: Issue with unresolved state, and jira z target version not matching expected versions
(
"issue8",
[],
["1.2.3"],
"",
"file4.txt",
[],
"issue8 target version: 1.2.z, does not match expected version ['1.2.3'].",
"1.2.z",
),
{ # Test case 1: Issue with no jira target versions and not resolved status
"jira_id": "issue1",
"resolved_status": ["resolved"],
"jira_target_versions": [],
"target_version_str": "1.0",
"file_name": "file1.txt",
"skip_project_ids": [],
"expected_jira_error_string": "",
"test_jira_versions": ["1.0"],
},
{ # Test case 2: Issue with no jira target versions, but resolved status
"jira_id": "issue2",
"resolved_status": ["open"],
"jira_target_versions": [],
"target_version_str": "1.0",
"file_name": "file2.txt",
"skip_project_ids": [],
"expected_jira_error_string": "issue2 current status: open is resolved.",
"test_jira_versions": ["1.0"],
},
{ # Test case 3: Issue with no jira target versions, default resolved status
"jira_id": "issue3",
"resolved_status": [],
"jira_target_versions": [],
"target_version_str": "",
"file_name": "file3.txt",
"skip_project_ids": [],
"expected_jira_error_string": "",
"test_jira_versions": ["1.1"],
},
{ # Test case 4: Issue with not resolved state, but matching jira target version
"jira_id": "issue4",
"resolved_status": ["resolved"],
"jira_target_versions": ["1.0"],
"target_version_str": "1.0",
"file_name": "file4.txt",
"skip_project_ids": [],
"expected_jira_error_string": "",
"test_jira_versions": ["1.0"],
},
{ # Test case 5: Issue with not resolved state, and not matching jira target version
"jira_id": "issue5",
"resolved_status": ["resolved"],
"jira_target_versions": ["1.1"],
"target_version_str": "1.0",
"file_name": "file5.txt",
"skip_project_ids": [],
"expected_jira_error_string": "issue5 target versions: ['1.0'], do not match expected version ['1.1'].",
"test_jira_versions": ["1.0"],
},
{ # Test case 6: Issue that would be skipped for version check because of skip
"jira_id": "issue6",
"resolved_status": ["resolved"],
"jira_target_versions": ["1.0"],
"target_version_str": "1.0",
"file_name": "file6.txt",
"skip_project_ids": ["issue"],
"expected_jira_error_string": "",
"test_jira_versions": ["1.1"],
},
{ # Test case 7: Issue that would be skipped for version check but fail resolved check
"jira_id": "issue7",
"resolved_status": ["open"],
"jira_target_versions": ["1.0"],
"target_version_str": "1.0",
"file_name": "file6.txt",
"skip_project_ids": ["issue"],
"expected_jira_error_string": "issue7 current status: open is resolved.",
"test_jira_versions": ["1.1"],
},
{ # Test case 8: Issue with unresolved state, and matching jira z target version
"jira_id": "issue8",
"resolved_status": [],
"jira_target_versions": ["1.2.z"],
"target_version_str": "1.2.z",
"file_name": "file4.txt",
"skip_project_ids": [],
"expected_jira_error_string": "",
"test_jira_versions": ["1.2.z"],
},
{ # Test case 9: Issue with unresolved state, and jira z target version not matching expected versions
"jira_id": "issue9",
"resolved_status": [],
"jira_target_versions": ["1.2.3"],
"target_version_str": "1.2.z",
"file_name": "file4.txt",
"skip_project_ids": [],
"expected_jira_error_string": "issue9 target versions: ['1.2.z'], do not match expected version ['1.2.3'].",
"test_jira_versions": ["1.2.z"],
},
{ # Test case 10: Issue with unresolved state, and matching jira z target versions
"jira_id": "issue10",
"resolved_status": [],
"jira_target_versions": ["1.2.z", "1.3.z"],
"target_version_str": "",
"file_name": "file4.txt",
"skip_project_ids": [],
"expected_jira_error_string": "",
"test_jira_versions": ["1.2.z", "1.4.0"],
},
{ # Test case 11: Issue with unresolved state, and jira target version not matching expected versions
"jira_id": "issue11",
"resolved_status": [],
"jira_target_versions": ["1.2.3"],
"target_version_str": "",
"file_name": "file4.txt",
"skip_project_ids": [],
"expected_jira_error_string": "issue11 target versions: ['1.2.z', '1.4.0'], do not match expected version ['1.2.3'].",
"test_jira_versions": ["1.2.z", "1.4.0"],
},
{ # Test case 12: Issue with unresolved state, and jira target versions not matching expected versions
"jira_id": "issue12",
"resolved_status": [],
"jira_target_versions": ["1.2.3", "1.5.0"],
"target_version_str": "",
"file_name": "file4.txt",
"skip_project_ids": [],
"expected_jira_error_string": "issue12 target versions: ['1.2.z', '1.4.0'], do not match expected version ['1.2.3', '1.5.0'].",
"test_jira_versions": ["1.2.z", "1.4.0"],
},
],
ids=[
"test_no_jira_versions_no_resolved_status",
Expand All @@ -142,51 +207,41 @@ def test_process_jira_command_line_config_file_valid_config(mocker):
"test_skip_version_check_fail_status_check",
"test_matching_target_z_version",
"test_non_matching_target_z_version",
"test_matching_target_version_with_versions",
"test_non_matching_target_version_with_versions",
"test_matching_target_versions_with_versions",
],
)
def test_get_jira_information(
mocker,
jira_id,
resolved_status,
jira_target_versions,
target_version_str,
file_name,
skip_project_ids,
expected_jira_error_string,
test_jira_version,
):
def test_get_jira_information(mocker, test_params):
mock_jira = mocker.MagicMock()
mock_jira.fields.status.name = "open"
jira_version = mocker.MagicMock()
jira_version.name = test_jira_version
mock_jira.fixVersions = [jira_version]
mock_jira.fixVersions = [mocker.MagicMock()]
mocker.patch("apps.jira_utils.jira_information.get_issue", return_value=mock_jira)

if jira_target_versions:
if test_jira_versions := test_params.get("test_jira_versions"):
mocker.patch(
"apps.jira_utils.jira_information.re.findall",
return_value=[test_jira_version],
)
result = get_jira_information(
jira_object=mock_jira,
jira_id=jira_id,
resolved_status=resolved_status,
target_version_str="1.0",
skip_project_ids=skip_project_ids,
jira_target_versions=jira_target_versions,
file_name=file_name,
)
else:
result = get_jira_information(
jira_object=mock_jira,
jira_id=jira_id,
resolved_status=resolved_status,
target_version_str="1.0",
skip_project_ids=skip_project_ids,
jira_target_versions=jira_target_versions,
file_name=file_name,
return_value=test_jira_versions,
)

jira_id = test_params.get("jira_id")
resolved_status = test_params.get("resolved_status")
target_version_str = test_params.get("target_version_str")
skip_project_ids = test_params.get("skip_project_ids")
jira_target_versions = test_params.get("jira_target_versions")
file_name = test_params.get("file_name")
expected_jira_error_string = test_params.get("expected_jira_error_string")

result = get_jira_information(
jira_object=mock_jira,
jira_id=jira_id,
resolved_status=resolved_status,
target_version_str=target_version_str,
skip_project_ids=skip_project_ids,
jira_target_versions=jira_target_versions,
file_name=file_name,
)

assert result == (file_name, expected_jira_error_string)


Expand Down Expand Up @@ -232,3 +287,22 @@ def test_get_jira_ids_from_file_content(content_and_expected):
jira_url="https://example.com",
)
assert jira_ids == content_and_expected["expected"]


def test_jira_api_error(mocker):
mock_jira = mocker.MagicMock()
mocker.patch(
"apps.jira_utils.jira_information.get_issue", side_effect=JIRAError(status_code=404, text="Issue not found")
)

result = get_jira_information(
jira_object=mock_jira,
jira_id="404",
resolved_status=[],
target_version_str="",
skip_project_ids=[],
jira_target_versions=[],
file_name="",
)

assert result[1] == "404 JiraError status code: 404, details: Issue not found]."