python-utility-scripts 0.0.19__tar.gz → 0.0.21__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-utility-scripts
3
- Version: 0.0.19
3
+ Version: 0.0.21
4
4
  Summary: Python utility scripts
5
5
  Home-page: https://github.com/RedHatQE/python-utility-scripts
6
6
  License: Apache-2.0
@@ -15,7 +15,7 @@ Classifier: Programming Language :: Python :: 3.9
15
15
  Classifier: Programming Language :: Python :: 3.10
16
16
  Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3.12
18
- Requires-Dist: pyhelper-utils (>=0.0.19,<0.0.20)
18
+ Requires-Dist: pyhelper-utils (>=0.0.21,<0.0.22)
19
19
  Requires-Dist: pylero (>=0.1.0,<0.2.0)
20
20
  Requires-Dist: pytest-mock (>=3.14.0,<4.0.0)
21
21
  Requires-Dist: python-simple-logger (>=1.0.8,<2.0.0)
@@ -0,0 +1,6 @@
1
+ class PolarionTestCaseApprovalError(Exception):
2
+ pass
3
+
4
+
5
+ class PolarionTestCaseWithoutRequirementError(Exception):
6
+ pass
@@ -0,0 +1,71 @@
1
+ from __future__ import annotations
2
+ import logging
3
+ import os
4
+ from simple_logger.logger import get_logger
5
+
6
+ import click
7
+
8
+ from apps.polarion.exceptions import PolarionTestCaseApprovalError
9
+ from apps.polarion.polarion_utils import get_polarion_project_id, find_polarion_ids, update_polarion_ids
10
+ from typing import List, Dict, Optional
11
+
12
+
13
+ LOGGER = get_logger(name=__name__)
14
+
15
+
16
+ def approve_tests(polarion_project_id: str, added_ids: List[str]) -> Dict[str, List[str]]:
17
+ LOGGER.debug(f"Following polarion ids were added: {added_ids}")
18
+ return update_polarion_ids(
19
+ polarion_ids=list(added_ids), project_id=polarion_project_id, is_automated=True, is_approved=True
20
+ )
21
+
22
+
23
+ def remove_approved_tests(polarion_project_id: str, added_ids: Optional[List[str]] = None) -> Dict[str, List[str]]:
24
+ removed_polarions = {}
25
+ added_ids = added_ids or []
26
+ if removed_ids := set(find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="removed")) - set(
27
+ added_ids
28
+ ):
29
+ LOGGER.info(f"Following polarion ids were removed: {removed_ids}")
30
+ removed_polarions = update_polarion_ids(
31
+ polarion_ids=list(removed_ids), project_id=polarion_project_id, is_automated=False
32
+ )
33
+ LOGGER.error(f"Following polarion ids marked not automated: {removed_polarions.get('updated')}")
34
+ return removed_polarions
35
+
36
+
37
+ @click.command()
38
+ @click.option(
39
+ "--config-file-path",
40
+ help="Provide absolute path to the config file. Any CLI option(s) would override YAML file",
41
+ type=click.Path(),
42
+ default=os.path.expanduser("~/.config/python-utility-scripts/config.yaml"),
43
+ )
44
+ @click.option("--project-id", "-p", help="Provide the polarion project id")
45
+ @click.option("--verbose", default=False, is_flag=True)
46
+ def polarion_approve_automate(config_file_path: str, project_id: str, verbose: bool) -> None:
47
+ if verbose:
48
+ LOGGER.setLevel(logging.INFO)
49
+ else:
50
+ logging.disable(logging.ERROR)
51
+ polarion_project_id = project_id or get_polarion_project_id(
52
+ config_file_path=config_file_path, util_name="pyutils-polarion-set-automated"
53
+ )
54
+ added_polarions = {}
55
+ if added_ids := find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added"):
56
+ added_polarions = approve_tests(polarion_project_id=polarion_project_id, added_ids=added_ids)
57
+ LOGGER.info(f"Following polarion ids were marked automated and approved: {added_polarions.get('updated')}")
58
+
59
+ removed_polarions = remove_approved_tests(polarion_project_id=polarion_project_id, added_ids=added_ids)
60
+ if removed_polarions.get("failed") or added_polarions.get("failed"):
61
+ error = "Following polarion ids updates failed."
62
+ if removed_polarions.get("failed"):
63
+ error += f" Removed ids: {removed_polarions.get('failed')}."
64
+ if added_polarions.get("failed"):
65
+ error += f" Added ids:: {added_polarions.get('failed')}."
66
+ LOGGER.error(error)
67
+ raise PolarionTestCaseApprovalError(error)
68
+
69
+
70
+ if __name__ == "__main__":
71
+ polarion_approve_automate()
@@ -0,0 +1,100 @@
1
+ from __future__ import annotations
2
+ import re
3
+
4
+ import click
5
+ from simple_logger.logger import get_logger
6
+ import shlex
7
+ import subprocess
8
+
9
+ from apps.utils import get_util_config
10
+ from typing import Dict, List
11
+
12
+ LOGGER = get_logger(name=__name__)
13
+ AUTOMATED = "automated"
14
+ NOT_AUTOMATED = "notautomated"
15
+ APPROVED = "approved"
16
+
17
+
18
+ def git_diff() -> str:
19
+ data = subprocess.check_output(shlex.split("git diff HEAD^-1"))
20
+ return data.decode()
21
+
22
+
23
+ def git_diff_lines() -> Dict[str, List[str]]:
24
+ diff: Dict[str, List[str]] = {}
25
+ for line in git_diff().splitlines():
26
+ LOGGER.info(line)
27
+ if line.startswith("+"):
28
+ diff.setdefault("added", []).append(line)
29
+ if line.startswith("-"):
30
+ diff.setdefault("removed", []).append(line)
31
+ return diff
32
+
33
+
34
+ def validate_polarion_requirements(
35
+ polarion_test_ids: List[str],
36
+ polarion_project_id: str,
37
+ ) -> List[str]:
38
+ tests_with_missing_requirements: List[str] = []
39
+ if polarion_test_ids:
40
+ from pylero.work_item import TestCase, Requirement
41
+ from pylero.exceptions import PyleroLibException
42
+
43
+ for _id in polarion_test_ids:
44
+ has_req = False
45
+ LOGGER.info(f"Checking if {_id} verifies any requirement")
46
+ tc = TestCase(project_id=polarion_project_id, work_item_id=_id)
47
+ for link in tc.linked_work_items:
48
+ try:
49
+ Requirement(project_id=polarion_project_id, work_item_id=link.work_item_id)
50
+ has_req = True
51
+ break
52
+ except PyleroLibException:
53
+ continue
54
+
55
+ if not has_req:
56
+ LOGGER.error(f"{_id}: Is missing requirement")
57
+ tests_with_missing_requirements.append(_id)
58
+ return tests_with_missing_requirements
59
+
60
+
61
+ def find_polarion_ids(polarion_project_id: str, string_to_match: str) -> List[str]:
62
+ return re.findall(
63
+ rf"pytest.mark.polarion.*({polarion_project_id}-[0-9]+)",
64
+ "\n".join(git_diff_lines().get(string_to_match, [])),
65
+ re.MULTILINE | re.IGNORECASE,
66
+ )
67
+
68
+
69
+ def get_polarion_project_id(util_name: str, config_file_path: str) -> str:
70
+ polarion_project_id = get_util_config(util_name=util_name, config_file_path=config_file_path).get("project_id")
71
+ if not polarion_project_id:
72
+ LOGGER.error("Polarion project id must be passed via config file or command line")
73
+ raise click.Abort()
74
+ return polarion_project_id
75
+
76
+
77
+ def update_polarion_ids(
78
+ project_id: str, is_automated: bool, polarion_ids: List[str], is_approved: bool = False
79
+ ) -> Dict[str, List[str]]:
80
+ updated_ids: Dict[str, List[str]] = {}
81
+ if polarion_ids:
82
+ automation_status = AUTOMATED if is_automated else NOT_AUTOMATED
83
+
84
+ from pylero.work_item import TestCase
85
+ from pylero.exceptions import PyleroLibException
86
+
87
+ for id in polarion_ids:
88
+ try:
89
+ tc = TestCase(project_id=project_id, work_item_id=id)
90
+ tc.caseautomation = automation_status
91
+ if is_approved:
92
+ tc.status = APPROVED
93
+ tc.update()
94
+ LOGGER.info(f"Polarion {id}: marked as: {automation_status}, approved status set: {is_approved}")
95
+ updated_ids.setdefault("updated", []).append(id)
96
+ except PyleroLibException as polarion_exception:
97
+ error = f"{id}: {polarion_exception}"
98
+ LOGGER.error(error)
99
+ updated_ids.setdefault("failed", []).append(error)
100
+ return updated_ids
@@ -0,0 +1,41 @@
1
+ import logging
2
+ from simple_logger.logger import get_logger
3
+ import os
4
+ import click
5
+
6
+ from apps.polarion.exceptions import PolarionTestCaseWithoutRequirementError
7
+ from apps.polarion.polarion_utils import validate_polarion_requirements, find_polarion_ids, get_polarion_project_id
8
+
9
+ LOGGER = get_logger(name="polarion-verify-tc-requirements")
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("--verbose", default=False, is_flag=True)
21
+ def has_verify(config_file_path: str, project_id: str, verbose: bool) -> None:
22
+ if verbose:
23
+ LOGGER.setLevel(logging.INFO)
24
+ else:
25
+ logging.disable(logging.ERROR)
26
+ polarion_project_id = project_id or get_polarion_project_id(
27
+ config_file_path=config_file_path, util_name="pyutils-polarion-verify-tc-requirements"
28
+ )
29
+ if added_ids := find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added"):
30
+ LOGGER.info(f"Checking following ids: {added_ids}")
31
+ if tests_with_missing_requirements := validate_polarion_requirements(
32
+ polarion_test_ids=added_ids,
33
+ polarion_project_id=polarion_project_id,
34
+ ):
35
+ raise PolarionTestCaseWithoutRequirementError(
36
+ f"TestCases with missing requirement: {tests_with_missing_requirements}"
37
+ )
38
+
39
+
40
+ if __name__ == "__main__":
41
+ has_verify()
@@ -2,7 +2,7 @@
2
2
  omit = ["tests/*"]
3
3
 
4
4
  [tool.coverage.report]
5
- fail_under = 75
5
+ fail_under = 60
6
6
  skip_empty = true
7
7
 
8
8
  [tool.coverage.html]
@@ -29,7 +29,7 @@ exclude = [".git", ".venv", ".mypy_cache", ".tox", "__pycache__"]
29
29
 
30
30
  [tool.poetry]
31
31
  name = "python-utility-scripts"
32
- version = "0.0.19"
32
+ version = "0.0.21"
33
33
  description = "Python utility scripts"
34
34
  authors = ["dbasunag <dbasunag@redhat.com>"]
35
35
  license = "Apache License 2.0"
@@ -47,12 +47,13 @@ packages = [{ include = "apps" }]
47
47
  [tool.poetry.scripts]
48
48
  pyutils-unusedcode = "apps.unused_code.unused_code:get_unused_functions"
49
49
  pyutils-polarion-verify-tc-requirements = "apps.polarion.polarion_verify_tc_requirements:has_verify"
50
+ pyutils-polarion-set-automated = "apps.polarion.polarion_set_automated:polarion_approve_automate"
50
51
 
51
52
  [tool.poetry.dependencies]
52
53
  python = "^3.8"
53
54
  python-simple-logger = "^1.0.8"
54
55
  pylero = "^0.1.0"
55
- pyhelper-utils = "^0.0.19"
56
+ pyhelper-utils = "^0.0.21"
56
57
  pytest-mock = "^3.14.0"
57
58
  pyyaml = "^6.0.1"
58
59
 
@@ -65,6 +66,7 @@ ipython = "*"
65
66
  enable = false
66
67
  pattern = "((?P<epoch>\\d+)!)?(?P<base>\\d+(\\.\\d+)*)"
67
68
 
69
+
68
70
  [tool.poetry.group.test.dependencies]
69
71
  pytest = "^8.0.0"
70
72
  pytest-cov = "^5.0.0"
@@ -1,47 +0,0 @@
1
- from simple_logger.logger import get_logger
2
- import shlex
3
- import subprocess
4
- from pylero.exceptions import PyleroLibException
5
- from typing import Dict, List
6
-
7
- LOGGER = get_logger(name=__name__)
8
-
9
-
10
- def git_diff() -> str:
11
- data = subprocess.check_output(shlex.split("git diff HEAD^-1"))
12
- return data.decode()
13
-
14
-
15
- def git_diff_lines() -> Dict[str, List[str]]:
16
- diff: Dict[str, List[str]] = {}
17
- for line in git_diff().splitlines():
18
- LOGGER.debug(line)
19
- if line.startswith("+"):
20
- diff.setdefault("added", []).append(line)
21
- return diff
22
-
23
-
24
- def validate_polarion_requirements(
25
- polarion_test_ids: List[str],
26
- polarion_project_id: str,
27
- ) -> List[str]:
28
- from pylero.work_item import TestCase, Requirement
29
-
30
- tests_with_missing_requirements: List[str] = []
31
-
32
- for _id in polarion_test_ids:
33
- has_req = False
34
- LOGGER.debug(f"Checking if {_id} verifies any requirement")
35
- tc = TestCase(project_id=polarion_project_id, work_item_id=_id)
36
- for link in tc.linked_work_items:
37
- try:
38
- Requirement(project_id=polarion_project_id, work_item_id=link.work_item_id)
39
- has_req = True
40
- break
41
- except PyleroLibException:
42
- continue
43
-
44
- if not has_req:
45
- LOGGER.error(f"{_id}: Is missing requirement")
46
- tests_with_missing_requirements.append(_id)
47
- return tests_with_missing_requirements
@@ -1,52 +0,0 @@
1
- import re
2
- import logging
3
- from simple_logger.logger import get_logger
4
- import os
5
- 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
11
-
12
- LOGGER = get_logger(name="polarion-verify-tc-requirements")
13
-
14
-
15
- @click.command()
16
- @click.option(
17
- "--config-file-path",
18
- help="Provide absolute path to the config file. Any CLI option(s) would override YAML file",
19
- type=click.Path(),
20
- default=os.path.expanduser("~/.config/python-utility-scripts/config.yaml"),
21
- )
22
- @click.option("--project-id", "-p", help="Provide the polarion project id")
23
- @click.option("--verbosity", default=False, is_flag=True)
24
- def has_verify(config_file_path: str, project_id: str, verbosity: bool) -> None:
25
- if verbosity:
26
- 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
- ):
42
- LOGGER.debug(f"Checking following ids: {added_ids}")
43
- if tests_with_missing_requirements := validate_polarion_requirements(
44
- polarion_test_ids=added_ids,
45
- polarion_project_id=polarion_project_id,
46
- ):
47
- click.echo(f"TestCases with missing requirement: {tests_with_missing_requirements}")
48
- raise click.Abort()
49
-
50
-
51
- if __name__ == "__main__":
52
- has_verify()