1+ import re
2+
3+ import click
14from simple_logger .logger import get_logger
25import shlex
36import subprocess
4- from pylero .exceptions import PyleroLibException
57
8+ from apps .utils import get_util_config
69
710LOGGER = get_logger (name = __name__ )
11+ AUTOMATED = "automated"
12+ NOT_AUTOMATED = "notautomated"
13+ APPROVED = "approved"
814
915
1016def 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
2633def 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
0 commit comments