Skip to content

Commit 5245a91

Browse files
authored
base PR for set polarion as automated (#30)
* base PR for set polarion as automated * updated based on reviews * remove extra test
1 parent 079eca2 commit 5245a91

File tree

7 files changed

+151
-51
lines changed

7 files changed

+151
-51
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import logging
2+
import os
3+
from simple_logger.logger import get_logger
4+
5+
import click
6+
7+
from apps.polarion.polarion_utils import get_polarion_project_id, find_polarion_ids, update_polarion_ids
8+
9+
LOGGER = get_logger(name=__name__)
10+
11+
12+
@click.command()
13+
@click.option(
14+
"--config-file-path",
15+
help="Provide absolute path to the config file. Any CLI option(s) would override YAML file",
16+
type=click.Path(),
17+
default=os.path.expanduser("~/.config/python-utility-scripts/config.yaml"),
18+
)
19+
@click.option("--project-id", "-p", help="Provide the polarion project id")
20+
@click.option("--verbosity", default=False, is_flag=True)
21+
def polarion_approve_automate(config_file_path, project_id, verbosity):
22+
if verbosity:
23+
LOGGER.setLevel(logging.DEBUG)
24+
removed_polarions = {}
25+
added_polarions = {}
26+
polarion_project_id = get_polarion_project_id(
27+
project_id=project_id, config_file_path=config_file_path, util_name="pyutils-polarion-set-automated"
28+
)
29+
added_ids = find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added")
30+
LOGGER.debug(f"Following polarion ids were added: {added_ids}")
31+
if removed_ids := set(find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="removed")) - set(
32+
added_ids
33+
):
34+
LOGGER.debug(f"Following polarion ids were removed: {removed_ids}")
35+
removed_polarions = update_polarion_ids(
36+
polarion_ids=removed_ids, project_id=polarion_project_id, is_automated=False
37+
)
38+
LOGGER.error(f"Following polarion ids marked not automated: {removed_polarions.get('updated')}")
39+
40+
if added_polarions := update_polarion_ids(
41+
polarion_ids=added_ids, project_id=polarion_project_id, is_automated=True, is_approved=True
42+
):
43+
LOGGER.debug(f"Following polarion ids were marked automated and approved: {added_polarions.get('updated')}")
44+
if removed_polarions.get("failed") or added_polarions.get("failed"):
45+
LOGGER.error(
46+
f"Following polarion ids updates failed. Removed ids: {removed_polarions.get('failed')}"
47+
f"Added ids:: {added_polarions.get('failed')}"
48+
)
49+
raise click.Abort()
50+
51+
52+
if __name__ == "__main__":
53+
polarion_approve_automate()

apps/polarion/polarion_utils.py

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import re
2+
3+
import click
14
from simple_logger.logger import get_logger
25
import shlex
36
import subprocess
4-
from pylero.exceptions import PyleroLibException
57

8+
from apps.utils import get_util_config
69

710
LOGGER = get_logger(name=__name__)
11+
AUTOMATED = "automated"
12+
NOT_AUTOMATED = "notautomated"
13+
APPROVED = "approved"
814

915

1016
def git_diff():
@@ -19,31 +25,74 @@ def git_diff_lines():
1925
LOGGER.debug(line)
2026
if line.startswith("+"):
2127
diff.setdefault("added", []).append(line)
22-
28+
if line.startswith("-"):
29+
diff.setdefault("removed", []).append(line)
2330
return diff
2431

2532

2633
def validate_polarion_requirements(
27-
polarion_test_ids,
2834
polarion_project_id,
35+
polarion_test_ids,
2936
):
30-
from pylero.work_item import TestCase, Requirement
31-
3237
tests_with_missing_requirements = []
38+
if polarion_test_ids:
39+
from pylero.work_item import TestCase, Requirement
40+
from pylero.exceptions import PyleroLibException
3341

34-
for _id in polarion_test_ids:
35-
has_req = False
36-
LOGGER.debug(f"Checking if {_id} verifies any requirement")
37-
tc = TestCase(project_id=polarion_project_id, work_item_id=_id)
38-
for link in tc.linked_work_items:
39-
try:
40-
Requirement(project_id=polarion_project_id, work_item_id=link.work_item_id)
41-
has_req = True
42-
break
43-
except PyleroLibException:
44-
continue
45-
46-
if not has_req:
47-
LOGGER.error(f"{_id}: Is missing requirement")
48-
tests_with_missing_requirements.append(_id)
42+
for _id in polarion_test_ids:
43+
has_req = False
44+
LOGGER.debug(f"Checking if {_id} verifies any requirement")
45+
tc = TestCase(project_id=polarion_project_id, work_item_id=_id)
46+
for link in tc.linked_work_items:
47+
try:
48+
Requirement(project_id=polarion_project_id, work_item_id=link.work_item_id)
49+
has_req = True
50+
break
51+
except PyleroLibException:
52+
continue
53+
54+
if not has_req:
55+
LOGGER.error(f"{_id}: Is missing requirement")
56+
tests_with_missing_requirements.append(_id)
4957
return tests_with_missing_requirements
58+
59+
60+
def find_polarion_ids(polarion_project_id, string_to_match):
61+
return re.findall(
62+
rf"pytest.mark.polarion.*({polarion_project_id}-[0-9]+)",
63+
"\n".join(git_diff_lines().get(string_to_match, [])),
64+
re.MULTILINE | re.IGNORECASE,
65+
)
66+
67+
68+
def get_polarion_project_id(project_id, config_file_path, util_name):
69+
polarion_project_id = project_id or get_util_config(util_name=util_name, config_file_path=config_file_path).get(
70+
"project_id"
71+
)
72+
if not polarion_project_id:
73+
LOGGER.error("Polarion project id must be passed via config file or command line")
74+
raise click.Abort()
75+
return polarion_project_id
76+
77+
78+
def update_polarion_ids(project_id, is_automated, polarion_ids, is_approved=False):
79+
updated_ids = {}
80+
if polarion_ids:
81+
automation_status = AUTOMATED if is_automated else NOT_AUTOMATED
82+
83+
from pylero.work_item import TestCase
84+
from pylero.exceptions import PyleroLibException
85+
86+
for id in polarion_ids:
87+
try:
88+
tc = TestCase(project_id=project_id, work_item_id=id)
89+
tc.caseautomation = automation_status
90+
if is_approved:
91+
tc.status = APPROVED
92+
tc.update()
93+
LOGGER.debug(f"Polarion {id}: marked as: {automation_status}, approved status set: {is_approved}")
94+
updated_ids.setdefault("updated", []).append(id)
95+
except PyleroLibException as polarion_exception:
96+
LOGGER.warning(f"{id}: {polarion_exception}")
97+
updated_ids.setdefault("failed", []).append(id)
98+
return updated_ids

apps/polarion/polarion_verify_tc_requirements.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import re
21
import logging
32
from simple_logger.logger import get_logger
43
import os
54
import click
6-
from apps.polarion.polarion_utils import (
7-
git_diff_lines,
8-
validate_polarion_requirements,
9-
)
10-
from apps.utils import get_util_config
5+
from apps.polarion.polarion_utils import validate_polarion_requirements, find_polarion_ids, get_polarion_project_id
116

127
LOGGER = get_logger(name="polarion-verify-tc-requirements")
138

@@ -24,27 +19,16 @@
2419
def has_verify(config_file_path, project_id, verbosity):
2520
if verbosity:
2621
LOGGER.setLevel(logging.DEBUG)
27-
28-
polarion_project_id = project_id or get_util_config(
29-
util_name="pyutils-polarion-verify-tc-requirements",
30-
config_file_path=config_file_path,
31-
).get("project_id")
32-
33-
if not polarion_project_id:
34-
LOGGER.error("Polarion project id must be passed via config file or command line")
35-
raise click.Abort()
36-
37-
if added_ids := re.findall(
38-
rf"pytest.mark.polarion.*({polarion_project_id}-[0-9]+)",
39-
"\n".join(git_diff_lines().get("added", [])),
40-
re.MULTILINE | re.IGNORECASE,
41-
):
22+
polarion_project_id = get_polarion_project_id(
23+
project_id=project_id, config_file_path=config_file_path, util_name="pyutils-polarion-verify-tc-requirements"
24+
)
25+
if added_ids := find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added"):
4226
LOGGER.debug(f"Checking following ids: {added_ids}")
4327
if tests_with_missing_requirements := validate_polarion_requirements(
4428
polarion_test_ids=added_ids,
4529
polarion_project_id=polarion_project_id,
4630
):
47-
click.echo(f"TestCases with missing requirement: {tests_with_missing_requirements}")
31+
LOGGER.error(f"TestCases with missing requirement: {tests_with_missing_requirements}")
4832
raise click.Abort()
4933

5034

config.example.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ pyutils-unusedcode:
44
exclude_function_prefix:
55
- "my_exclude_function_prefix"
66
pyutils-polarion-verify-tc-requirements:
7-
project_id: "ABC"
7+
project_id: "ABCDEF"
8+
pyutils-polarion-set-automated:
9+
project_id: "ABCDEF"

poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ packages = [{ include = "apps" }]
3838
[tool.poetry.scripts]
3939
pyutils-unusedcode = "apps.unused_code.unused_code:get_unused_functions"
4040
pyutils-polarion-verify-tc-requirements = "apps.polarion.polarion_verify_tc_requirements:has_verify"
41-
41+
pyutils-polarion-set-automated = "apps.polarion.polarion_set_automated:polarion_approve_automate"
4242

4343
[tool.poetry.dependencies]
4444
python = "^3.8"
@@ -58,11 +58,6 @@ enable = true
5858
pattern = "((?P<epoch>\\d+)!)?(?P<base>\\d+(\\.\\d+)*)"
5959

6060

61-
62-
63-
64-
65-
6661
[tool.poetry.group.test.dependencies]
6762
pytest = "^8.0.0"
6863
pytest-cov = "^4.1.0"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import shlex
2+
import subprocess
3+
from pyhelper_utils.shell import run_command
4+
5+
BASE_COMMAND = "poetry run python apps/polarion/polarion_set_automated.py"
6+
7+
8+
def test_missing_project_id_set_automated():
9+
rc, _, err = run_command(
10+
command=shlex.split(BASE_COMMAND),
11+
verify_stderr=False,
12+
check=False,
13+
capture_output=False,
14+
stderr=subprocess.PIPE,
15+
)
16+
assert "Polarion project id must be passed via config file or command line" in err
17+
assert not rc

0 commit comments

Comments
 (0)