1
1
import re
2
2
from simple_logger .logger import get_logger
3
3
from jira import JIRA , JIRAError , Issue
4
- from retry import retry
4
+ from tenacity import retry , retry_if_exception_type , wait_fixed , stop_after_attempt
5
5
from apps .utils import all_python_files
6
6
from typing import List , Dict , Any
7
7
@@ -14,12 +14,13 @@ def __init__(self, token: str, url: str) -> None:
14
14
self .url = url
15
15
self .jira = JIRA (token_auth = self .token , options = {"server" : self .url })
16
16
17
- @retry (JIRAError , tries = 3 , delay = 2 )
17
+ @retry (retry = retry_if_exception_type ( JIRAError ), stop = stop_after_attempt ( 3 ), wait = wait_fixed ( 2 ) )
18
18
def get_issue (self , jira_id : str ) -> Issue :
19
+ LOGGER .info (f"Retry staistics: { self .get_issue .statistics } " )
19
20
return self .jira .issue (id = jira_id , fields = "status, issuetype, fixVersions" )
20
21
21
22
22
- def get_jira_ids_from_file_content (file_content : str ) -> List [str ]:
23
+ def get_jira_ids_from_file_content (file_content : str , issue_pattern : str ) -> List [str ]:
23
24
"""
24
25
Try to find all jira_utils tickets in a given file content.
25
26
Looking for the following patterns:
@@ -29,36 +30,45 @@ def get_jira_ids_from_file_content(file_content: str) -> List[str]:
29
30
30
31
Args:
31
32
file_content (str): The content of a given file.
33
+ issue_pattern (str): regex pattern for jira ids
32
34
33
35
Returns:
34
36
list: A list of jira tickets.
35
37
"""
36
- issue_pattern = r"([A-Z]+-[0-9]+)"
37
38
_pytest_jira_marker_bugs = re .findall (rf"pytest.mark.jira.*?{ issue_pattern } .*" , file_content , re .DOTALL )
38
- _is_jira_open = re .findall (rf"jira_id\s*=[\s*\"\']*{ issue_pattern } .*" , file_content )
39
- _jira_url_jiras = []
39
+ _jira_id_arguments = re .findall (rf"jira_id\s*=[\s*\"\']*{ issue_pattern } .*" , file_content )
40
40
_jira_url_jiras = re .findall (
41
41
rf"https://issues.redhat.com/browse/{ issue_pattern } " ,
42
42
file_content ,
43
43
)
44
- return list (set (_pytest_jira_marker_bugs + _is_jira_open + _jira_url_jiras ))
44
+ return list (set (_pytest_jira_marker_bugs + _jira_id_arguments + _jira_url_jiras ))
45
45
46
46
47
- def get_jiras_from_python_files () -> Dict [str , Any ]:
47
+ def get_jiras_from_python_files (issue_pattern : str ) -> Dict [str , Any ]:
48
48
"""
49
49
Get all python files from the current directory and get list of jira ids from each of them
50
50
51
+ Args:
52
+ issue_pattern (str): regex pattern for jira ids
53
+
54
+ Returns:
55
+ Dict: A dict of filenames and associated jira tickets.
56
+
51
57
Note: any line containing <skip-jira_utils-check> would be not be checked for presence of a jira id
52
58
"""
53
- jira_found = {}
59
+ jira_found : Dict [ str , list [ str ]] = {}
54
60
for filename in all_python_files ():
55
61
with open (filename ) as fd :
56
62
file_content = []
57
63
for line in fd .readlines ():
58
64
# if <skip-jira_utils-check> appears in a line, exclude that line from jira check
59
- if "<skip-jira_utils-check>" not in line :
65
+ if "<skip-jira_utils-check>" in line :
66
+ continue
67
+ else :
60
68
file_content .append (line )
61
- if unique_jiras := get_jira_ids_from_file_content (file_content = "\n " .join (file_content )):
69
+ if unique_jiras := get_jira_ids_from_file_content (
70
+ file_content = "\n " .join (file_content ), issue_pattern = issue_pattern
71
+ ):
62
72
jira_found [filename ] = unique_jiras
63
- LOGGER .info (f"File : { filename } , { unique_jiras } " )
73
+ LOGGER .info (f"Following jiras are found : { jira_found } " )
64
74
return jira_found
0 commit comments