Skip to content

Commit a094960

Browse files
committed
address review comments
1 parent 70202a0 commit a094960

File tree

7 files changed

+66
-34
lines changed

7 files changed

+66
-34
lines changed

apps/jira_utils/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pyutils-jira:
3434
jira_target_versions:
3535
- 1.0.0
3636
- 2.0.1
37+
issue_pattern: "([A-Z]+-[0-9]+)"
3738
```
3839
This would skip version checks on any jira ids associated with project ABC and DEF
3940
This would also check if the current repository branch is pointing to any jira card that is not targeted for 1.0.0 or 2.0.1 version

apps/jira_utils/get_jira_information.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,32 @@
3030
type=ListParamType(),
3131
required=False,
3232
)
33+
@click.option(
34+
"--jira-issue-pattern",
35+
help="Provide the regex for Jira ids, default is ([A-Z]+-[0-9]+)",
36+
type=click.STRING,
37+
default="([A-Z]+-[0-9]+)",
38+
)
3339
@click.option("--verbose", default=False, is_flag=True)
34-
def get_jira_mismatch(jira_cfg_file: Any, jira_target_versions: Any, verbose: bool) -> Any:
40+
def get_jira_mismatch(
41+
jira_cfg_file: str, jira_target_versions: list[str], jira_issue_pattern: str, verbose: bool
42+
) -> None:
3543
if verbose:
3644
LOGGER.setLevel(logging.DEBUG)
3745
else:
3846
logging.disable(logging.CRITICAL)
3947
config_dict = get_util_config(util_name="pyutils-jira", config_file_path=jira_cfg_file)
40-
if not (config_dict.get("url") and config_dict.get("token")):
41-
raise JiraInvalidConfigFileError("Jira config file must contain valid url and token.")
42-
jira_connector = JiraConnector(token=config_dict["token"], url=config_dict["url"])
48+
jira_url = config_dict.get("url")
49+
jira_token = config_dict.get("token")
50+
jira_issue_pattern = config_dict.get("issue_pattern", jira_issue_pattern)
51+
if not (jira_url and jira_token and jira_issue_pattern):
52+
raise JiraInvalidConfigFileError("Jira config file must contain valid url, token or issue pattern.")
53+
jira_connector = JiraConnector(token=jira_token, url=jira_url)
4354
jira_error: Dict[str, Dict[str, Any]] = {"status_mismatch": {}, "version_mismatch": {}, "connection_error": {}}
44-
resolved_status = config_dict.get("resolved_statuses", DEFAULT_RESOLVED_STATUS)
55+
resolved_status = config_dict.get("resolved_statuses", DEFAULT_RESOLVED_STATUS)
4556
jira_target_versions = jira_target_versions or config_dict.get("jira_target_versions", [])
4657
skip_project_ids = config_dict.get("skip_project_ids", [])
47-
for file_name in (jira_id_dict := get_jiras_from_python_files()):
58+
for file_name in (jira_id_dict := get_jiras_from_python_files(issue_pattern=jira_issue_pattern)):
4859
for jira_id in jira_id_dict[file_name]:
4960
try:
5061
# check closed status:

apps/jira_utils/jira_utils.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
from simple_logger.logger import get_logger
33
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
55
from apps.utils import all_python_files
66
from typing import List, Dict, Any
77

@@ -14,12 +14,13 @@ def __init__(self, token: str, url: str) -> None:
1414
self.url = url
1515
self.jira = JIRA(token_auth=self.token, options={"server": self.url})
1616

17-
@retry(JIRAError, tries=3, delay=2)
17+
@retry(retry=retry_if_exception_type(JIRAError), stop=stop_after_attempt(3), wait=wait_fixed(2))
1818
def get_issue(self, jira_id: str) -> Issue:
19+
LOGGER.info(f"Retry staistics: {self.get_issue.statistics}")
1920
return self.jira.issue(id=jira_id, fields="status, issuetype, fixVersions")
2021

2122

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]:
2324
"""
2425
Try to find all jira_utils tickets in a given file content.
2526
Looking for the following patterns:
@@ -29,36 +30,45 @@ def get_jira_ids_from_file_content(file_content: str) -> List[str]:
2930
3031
Args:
3132
file_content (str): The content of a given file.
33+
issue_pattern (str): regex pattern for jira ids
3234
3335
Returns:
3436
list: A list of jira tickets.
3537
"""
36-
issue_pattern = r"([A-Z]+-[0-9]+)"
3738
_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)
4040
_jira_url_jiras = re.findall(
4141
rf"https://issues.redhat.com/browse/{issue_pattern}",
4242
file_content,
4343
)
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))
4545

4646

47-
def get_jiras_from_python_files() -> Dict[str, Any]:
47+
def get_jiras_from_python_files(issue_pattern: str) -> Dict[str, Any]:
4848
"""
4949
Get all python files from the current directory and get list of jira ids from each of them
5050
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+
5157
Note: any line containing <skip-jira_utils-check> would be not be checked for presence of a jira id
5258
"""
53-
jira_found = {}
59+
jira_found: Dict[str, list[str]] = {}
5460
for filename in all_python_files():
5561
with open(filename) as fd:
5662
file_content = []
5763
for line in fd.readlines():
5864
# 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:
6068
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+
):
6272
jira_found[filename] = unique_jiras
63-
LOGGER.info(f"File: {filename}, {unique_jiras}")
73+
LOGGER.info(f"Following jiras are found: {jira_found}")
6474
return jira_found

apps/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ def all_python_files() -> Iterable[str]:
8181
if [_dir for _dir in exclude_dirs if _dir in root]:
8282
continue
8383
for filename in files:
84-
if filename.endswith(".py") and filename != os.path.split(__file__)[-1]:
84+
if filename.endswith(".py"):
8585
yield os.path.join(root, filename)

config.example.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ pyutils-polarion-verify-tc-requirements:
99
pyutils-polarion-set-automated:
1010
project_id: "ABCDEF"
1111
branch: "origin/main"
12+
pyutils-jira:
13+
url: <jira url>
14+
token: <jira token>
15+
resolved_statuses:
16+
- verified
17+
- release pending
18+
- closed
19+
issue_pattern: "([A-Z]+-[0-9]+)"

poetry.lock

Lines changed: 16 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pyhelper-utils = "^0.0.36"
5959
pytest-mock = "^3.14.0"
6060
pyyaml = "^6.0.1"
6161
jira = "^3.6.0"
62-
retry2 = "^0.9.5"
62+
tenacity = "^9.0.0"
6363

6464
[tool.poetry.group.dev.dependencies]
6565
ipdb = "^0.13.13"

0 commit comments

Comments
 (0)