Skip to content

Commit 3bd8dd6

Browse files
authored
polarion_approve_automate() should compare commit hashes to get the git diff. (#153)
* mark automated can't work with branch with the branch being deleted after PR merge * address comments * updates based on review
1 parent a3cb689 commit 3bd8dd6

File tree

3 files changed

+78
-15
lines changed

3 files changed

+78
-15
lines changed

apps/polarion/polarion_set_automated.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@ def approve_tests(polarion_project_id: str, added_ids: list[str]) -> dict[str, l
2020

2121

2222
def remove_approved_tests(
23-
polarion_project_id: str, branch: str, added_ids: list[str] | None = None
23+
polarion_project_id: str,
24+
branch: str | None = None,
25+
previous_commit: str | None = None,
26+
current_commit: str | None = None,
27+
added_ids: list[str] | None = None,
2428
) -> dict[str, list[str]]:
2529
removed_polarions = {}
2630
added_ids = added_ids or []
2731
if removed_ids := set(
28-
find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="removed", branch=branch)
32+
find_polarion_ids(
33+
polarion_project_id=polarion_project_id,
34+
string_to_match="removed",
35+
branch=branch,
36+
previous_commit=previous_commit,
37+
current_commit=current_commit,
38+
)
2939
) - set(added_ids):
3040
LOGGER.info(f"Following polarion ids were removed: {removed_ids}")
3141
removed_polarions = update_polarion_ids(
@@ -43,9 +53,12 @@ def remove_approved_tests(
4353
default=os.path.expanduser("~/.config/python-utility-scripts/config.yaml"),
4454
)
4555
@click.option("--project-id", "-p", help="Provide the polarion project id")
46-
@click.option("--branch", "-b", help="Provide the github remote branch to run against", default="origin/main")
56+
@click.option("--previous-commit", "-p", help="Provide previous-commit to compare against", required=True)
57+
@click.option("--current-commit", "-c", help="Provide current-commit to compare with", required=True)
4758
@click.option("--verbose", default=False, is_flag=True)
48-
def polarion_approve_automate(config_file_path: str, project_id: str, branch: str, verbose: bool) -> None:
59+
def polarion_approve_automate(
60+
config_file_path: str, project_id: str, previous_commit: str, current_commit: str, verbose: bool
61+
) -> None:
4962
if verbose:
5063
LOGGER.setLevel(logging.DEBUG)
5164
# since the utilities are in apps.polarion.polarion_utils, we need to change log level
@@ -56,12 +69,21 @@ def polarion_approve_automate(config_file_path: str, project_id: str, branch: st
5669
config_file_path=config_file_path, util_name="pyutils-polarion-set-automated"
5770
)
5871
added_polarions = {}
59-
if added_ids := find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added", branch=branch):
72+
if added_ids := find_polarion_ids(
73+
polarion_project_id=polarion_project_id,
74+
string_to_match="added",
75+
branch=None,
76+
previous_commit=previous_commit,
77+
current_commit=current_commit,
78+
):
6079
added_polarions = approve_tests(polarion_project_id=polarion_project_id, added_ids=added_ids)
6180
LOGGER.debug(f"Following polarion ids were marked automated and approved: {added_polarions.get('updated')}")
6281

6382
removed_polarions = remove_approved_tests(
64-
polarion_project_id=polarion_project_id, added_ids=added_ids, branch=branch
83+
polarion_project_id=polarion_project_id,
84+
added_ids=added_ids,
85+
previous_commit=previous_commit,
86+
current_commit=current_commit,
6587
)
6688
if removed_polarions.get("failed") or added_polarions.get("failed"):
6789
error = "Following polarion ids updates failed."

apps/polarion/polarion_utils.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,28 @@
1515
APPROVED = "approved"
1616

1717

18-
def git_diff(branch: str) -> str:
19-
data = subprocess.check_output(shlex.split(f"git diff {branch} HEAD"))
18+
def git_diff(branch: str | None = None, current_commit: str | None = None, previous_commit: str | None = None) -> str:
19+
if branch and (previous_commit or current_commit):
20+
LOGGER.error("Branch and Previous or current commit are mutually exclusive command line options.")
21+
sys.exit(1)
22+
23+
# Sanitize inputs to prevent command injection
24+
if branch:
25+
sanitized_branch = shlex.quote(branch)
26+
diff_command = f"git diff {sanitized_branch} HEAD"
27+
else:
28+
sanitized_previous = shlex.quote(previous_commit) if previous_commit else ""
29+
sanitized_current = shlex.quote(current_commit) if current_commit else ""
30+
diff_command = f"git diff {sanitized_previous} {sanitized_current}"
31+
data = subprocess.check_output(shlex.split(diff_command))
2032
return data.decode()
2133

2234

23-
def git_diff_lines(branch: str) -> dict[str, list[str]]:
35+
def git_diff_lines(
36+
branch: str | None = None, previous_commit: str | None = None, current_commit: str | None = None
37+
) -> dict[str, list[str]]:
2438
diff: dict[str, list[str]] = {}
25-
for line in git_diff(branch=branch).splitlines():
39+
for line in git_diff(branch=branch, current_commit=current_commit, previous_commit=previous_commit).splitlines():
2640
LOGGER.debug(line)
2741
if line.startswith("+"):
2842
diff.setdefault("added", []).append(line)
@@ -43,7 +57,12 @@ def validate_polarion_requirements(
4357
for _id in polarion_test_ids:
4458
has_req = False
4559
LOGGER.debug(f"Checking if {_id} verifies any requirement")
46-
tc = TestCase(project_id=polarion_project_id, work_item_id=_id)
60+
try:
61+
tc = TestCase(project_id=polarion_project_id, work_item_id=_id)
62+
except PyleroLibException:
63+
LOGGER.error(f"{_id}: Test case not found.")
64+
tests_with_missing_requirements.append(_id)
65+
continue
4766
for link in tc.linked_work_items:
4867
try:
4968
Requirement(project_id=polarion_project_id, work_item_id=link.work_item_id)
@@ -53,15 +72,25 @@ def validate_polarion_requirements(
5372
continue
5473

5574
if not has_req:
56-
LOGGER.error(f"{_id}: Is missing requirement")
75+
LOGGER.error(f"{_id}: does not have associated requirement.")
5776
tests_with_missing_requirements.append(_id)
5877
return tests_with_missing_requirements
5978

6079

61-
def find_polarion_ids(polarion_project_id: str, string_to_match: str, branch: str) -> list[str]:
80+
def find_polarion_ids(
81+
polarion_project_id: str,
82+
string_to_match: str,
83+
branch: str | None = None,
84+
previous_commit: str | None = None,
85+
current_commit: str | None = None,
86+
) -> list[str]:
6287
return re.findall(
6388
rf"pytest.mark.polarion.*({polarion_project_id}-[0-9]+)",
64-
"\n".join(git_diff_lines(branch=branch).get(string_to_match, [])),
89+
"\n".join(
90+
git_diff_lines(branch=branch, previous_commit=previous_commit, current_commit=current_commit).get(
91+
string_to_match, []
92+
)
93+
),
6594
re.MULTILINE | re.IGNORECASE,
6695
)
6796

tests/polarion/test_polarion_automated.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,25 @@
66
BASE_COMMAND = "poetry run python apps/polarion/polarion_set_automated.py --verbose"
77

88

9-
def test_missing_project_id_set_automated():
9+
def test_missing_required_params_set_automated():
1010
rc, _, err = run_command(
1111
command=shlex.split(BASE_COMMAND),
1212
verify_stderr=False,
1313
check=False,
1414
capture_output=False,
1515
stderr=subprocess.PIPE,
1616
)
17+
assert "Missing option" in err
18+
assert not rc
19+
20+
21+
def test_missing_project_id_set_automated():
22+
rc, _, err = run_command(
23+
command=shlex.split(f"{BASE_COMMAND} --previous-commit commit1 --current-commit commit2"),
24+
verify_stderr=False,
25+
check=False,
26+
capture_output=False,
27+
stderr=subprocess.PIPE,
28+
)
1729
assert "Polarion project id must be passed via config file or command line" in err
1830
assert not rc

0 commit comments

Comments
 (0)