Skip to content

Commit 1830b71

Browse files
authored
github can have multiple commits, so get diff with branch instead (#66)
* github can have multiple commit, so get diff with branch instead * updates based on review comments
1 parent 6110827 commit 1830b71

File tree

5 files changed

+62
-20
lines changed

5 files changed

+62
-20
lines changed

apps/polarion/README.md

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This script uses [pylero](https://github.com/RedHatQE/pylero) and expects .pyler
99
## Usage
1010
```bash
1111
pyutils-polarion-verify-tc-requirements --help
12-
pyutils-polarion-verify-tc-requirements --project_id <project_id>
12+
pyutils-polarion-verify-tc-requirements --project_id <project_id> --branch <remote_branch_to_compare_to>
1313
```
1414

1515
## Config file
@@ -20,13 +20,47 @@ To specify polation project id for polarion scripts, it can be added to the conf
2020
### Example:
2121

2222
```yaml
23-
pyutils-polarion-verify-tc-requirements :
23+
pyutils-polarion-verify-tc-requirements:
2424
project_id: "<project_id>"
25+
branch: "<remote_branch_to_compare_to>"
2526
```
26-
This would run the polarion requirement check against Polarion project <project_id>
27+
This would run the polarion requirement check against Polarion project <project_id> and remote branch <remote_branch_to_compare_to>
2728
28-
To run from CLI with `--project-id`
29+
To run from CLI with `--project-id` and `--branch`
2930

3031
```bash
31-
pyutils-polarion-verify-tc-requirements --project-id 'my_project_id'
32+
pyutils-polarion-verify-tc-requirements --project-id 'my_project_id' --branch "origin/main"
33+
```
34+
35+
36+
## pyutils-polarion-set-automated
37+
Utility to mark newly added test cases in a pytest repository as approved automatically.
38+
39+
## Requirements
40+
This script uses [pylero](https://github.com/RedHatQE/pylero) and expects .pylero config file to be present in current directory or user's home directory.
41+
42+
## Usage
43+
```bash
44+
pyutils-polarion-set-automated --help
45+
pyutils-polarion-set-automated --project_id <project_id> --branch <remote_branch_to_compare_to>
46+
```
47+
48+
## Config file
49+
To specify polation project id for polarion scripts, it can be added to the config file:
50+
`~/.config/python-utility-scripts/config.yaml`
51+
52+
53+
### Example:
54+
55+
```yaml
56+
pyutils-polarion-set-automated:
57+
project_id: "<project_id>"
58+
branch: "<remote_branch_to_compare_to>"
59+
```
60+
This would run the polarion requirement check against Polarion project <project_id> and remote branch <remote_branch_to_compare_to>
61+
62+
To run from CLI with `--project-id` and `--branch`
63+
64+
```bash
65+
pyutils-polarion-set-automated --project-id 'my_project_id' --branch "origin/main"
3266
```

apps/polarion/polarion_set_automated.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ def approve_tests(polarion_project_id: str, added_ids: List[str]) -> Dict[str, L
2020
)
2121

2222

23-
def remove_approved_tests(polarion_project_id: str, added_ids: Optional[List[str]] = None) -> Dict[str, List[str]]:
23+
def remove_approved_tests(
24+
polarion_project_id: str, branch: str, added_ids: Optional[List[str]] = None
25+
) -> Dict[str, List[str]]:
2426
removed_polarions = {}
2527
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-
):
28+
if removed_ids := set(
29+
find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="removed", branch=branch)
30+
) - set(added_ids):
2931
LOGGER.info(f"Following polarion ids were removed: {removed_ids}")
3032
removed_polarions = update_polarion_ids(
3133
polarion_ids=list(removed_ids), project_id=polarion_project_id, is_automated=False
@@ -42,8 +44,9 @@ def remove_approved_tests(polarion_project_id: str, added_ids: Optional[List[str
4244
default=os.path.expanduser("~/.config/python-utility-scripts/config.yaml"),
4345
)
4446
@click.option("--project-id", "-p", help="Provide the polarion project id")
47+
@click.option("--branch", "-b", help="Provide the github remote branch to run against", default="origin/main")
4548
@click.option("--verbose", default=False, is_flag=True)
46-
def polarion_approve_automate(config_file_path: str, project_id: str, verbose: bool) -> None:
49+
def polarion_approve_automate(config_file_path: str, project_id: str, branch: str, verbose: bool) -> None:
4750
if verbose:
4851
LOGGER.setLevel(logging.INFO)
4952
else:
@@ -52,11 +55,13 @@ def polarion_approve_automate(config_file_path: str, project_id: str, verbose: b
5255
config_file_path=config_file_path, util_name="pyutils-polarion-set-automated"
5356
)
5457
added_polarions = {}
55-
if added_ids := find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added"):
58+
if added_ids := find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added", branch=branch):
5659
added_polarions = approve_tests(polarion_project_id=polarion_project_id, added_ids=added_ids)
5760
LOGGER.info(f"Following polarion ids were marked automated and approved: {added_polarions.get('updated')}")
5861

59-
removed_polarions = remove_approved_tests(polarion_project_id=polarion_project_id, added_ids=added_ids)
62+
removed_polarions = remove_approved_tests(
63+
polarion_project_id=polarion_project_id, added_ids=added_ids, branch=branch
64+
)
6065
if removed_polarions.get("failed") or added_polarions.get("failed"):
6166
error = "Following polarion ids updates failed."
6267
if removed_polarions.get("failed"):

apps/polarion/polarion_utils.py

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

1717

18-
def git_diff() -> str:
19-
data = subprocess.check_output(shlex.split("git diff HEAD^-1"))
18+
def git_diff(branch: str) -> str:
19+
data = subprocess.check_output(shlex.split(f"git diff {branch} HEAD"))
2020
return data.decode()
2121

2222

23-
def git_diff_lines() -> Dict[str, List[str]]:
23+
def git_diff_lines(branch: str) -> Dict[str, List[str]]:
2424
diff: Dict[str, List[str]] = {}
25-
for line in git_diff().splitlines():
25+
for line in git_diff(branch=branch).splitlines():
2626
LOGGER.info(line)
2727
if line.startswith("+"):
2828
diff.setdefault("added", []).append(line)
@@ -58,10 +58,10 @@ def validate_polarion_requirements(
5858
return tests_with_missing_requirements
5959

6060

61-
def find_polarion_ids(polarion_project_id: str, string_to_match: str) -> List[str]:
61+
def find_polarion_ids(polarion_project_id: str, string_to_match: str, branch: str) -> List[str]:
6262
return re.findall(
6363
rf"pytest.mark.polarion.*({polarion_project_id}-[0-9]+)",
64-
"\n".join(git_diff_lines().get(string_to_match, [])),
64+
"\n".join(git_diff_lines(branch=branch).get(string_to_match, [])),
6565
re.MULTILINE | re.IGNORECASE,
6666
)
6767

apps/polarion/polarion_verify_tc_requirements.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
default=os.path.expanduser("~/.config/python-utility-scripts/config.yaml"),
1818
)
1919
@click.option("--project-id", "-p", help="Provide the polarion project id")
20+
@click.option("--branch", "-b", help="Provide the github remote branch to run against", default="origin/main")
2021
@click.option("--verbose", default=False, is_flag=True)
21-
def has_verify(config_file_path: str, project_id: str, verbose: bool) -> None:
22+
def has_verify(config_file_path: str, project_id: str, branch: str, verbose: bool) -> None:
2223
if verbose:
2324
LOGGER.setLevel(logging.INFO)
2425
else:
2526
logging.disable(logging.ERROR)
2627
polarion_project_id = project_id or get_polarion_project_id(
2728
config_file_path=config_file_path, util_name="pyutils-polarion-verify-tc-requirements"
2829
)
29-
if added_ids := find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added"):
30+
if added_ids := find_polarion_ids(polarion_project_id=polarion_project_id, string_to_match="added", branch=branch):
3031
LOGGER.info(f"Checking following ids: {added_ids}")
3132
if tests_with_missing_requirements := validate_polarion_requirements(
3233
polarion_test_ids=added_ids,

config.example.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ pyutils-unusedcode:
55
- "my_exclude_function_prefix"
66
pyutils-polarion-verify-tc-requirements:
77
project_id: "ABCDEF"
8+
branch: "origin/main"
89
pyutils-polarion-set-automated:
910
project_id: "ABCDEF"
11+
branch: "origin/main"

0 commit comments

Comments
 (0)