1+ from __future__ import annotations
2+ import re
3+
4+ import click
15from simple_logger .logger import get_logger
26import shlex
37import subprocess
4- from pylero .exceptions import PyleroLibException
8+
9+ from apps .utils import get_util_config
510from typing import Dict , List
611
712LOGGER = get_logger (name = __name__ )
13+ AUTOMATED = "automated"
14+ NOT_AUTOMATED = "notautomated"
15+ APPROVED = "approved"
816
917
1018def git_diff () -> str :
@@ -15,33 +23,78 @@ def git_diff() -> str:
1523def git_diff_lines () -> Dict [str , List [str ]]:
1624 diff : Dict [str , List [str ]] = {}
1725 for line in git_diff ().splitlines ():
18- LOGGER .debug (line )
26+ LOGGER .info (line )
1927 if line .startswith ("+" ):
2028 diff .setdefault ("added" , []).append (line )
29+ if line .startswith ("-" ):
30+ diff .setdefault ("removed" , []).append (line )
2131 return diff
2232
2333
2434def validate_polarion_requirements (
2535 polarion_test_ids : List [str ],
2636 polarion_project_id : str ,
2737) -> List [str ]:
28- from pylero .work_item import TestCase , Requirement
29-
3038 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
3142
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 )
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 )
4758 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 commit comments