feat(api): upgrade JIRA
feat(api): changed code to support new JIRA SDK feat(api): JIRA helper return error to UI feat(api): JIRA catch 401
This commit is contained in:
parent
3b60856063
commit
1743665769
3 changed files with 21 additions and 18 deletions
|
|
@ -5,19 +5,20 @@ import requests
|
|||
from jira import JIRA
|
||||
from jira.exceptions import JIRAError
|
||||
from requests.auth import HTTPBasicAuth
|
||||
from starlette import status
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
fields = "id, summary, description, creator, reporter, created, assignee, status, updated, comment, issuetype, labels"
|
||||
|
||||
|
||||
class JiraManager:
|
||||
# retries = 5
|
||||
retries = 0
|
||||
|
||||
def __init__(self, url, username, password, project_id=None):
|
||||
self._config = {"JIRA_PROJECT_ID": project_id, "JIRA_URL": url, "JIRA_USERNAME": username,
|
||||
"JIRA_PASSWORD": password}
|
||||
try:
|
||||
self._jira = JIRA({'server': url}, basic_auth=(username, password), logging=True, max_retries=1)
|
||||
self._jira = JIRA(url, basic_auth=(username, password), logging=True, max_retries=1)
|
||||
except Exception as e:
|
||||
print("!!! JIRA AUTH ERROR")
|
||||
print(e)
|
||||
|
|
@ -34,7 +35,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.get_projects()
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
projects_dict_list = []
|
||||
for project in projects:
|
||||
projects_dict_list.append(self.__parser_project_info(project))
|
||||
|
|
@ -50,7 +51,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.get_project()
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
return self.__parser_project_info(project)
|
||||
|
||||
def get_issues(self, sql: str, offset: int = 0):
|
||||
|
|
@ -66,7 +67,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.get_issues(sql, offset)
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
|
||||
issue_dict_list = []
|
||||
for issue in issues:
|
||||
|
|
@ -86,7 +87,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.get_issue(issue_id)
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
return self.__parser_issue_info(issue)
|
||||
|
||||
def get_issue_v3(self, issue_id: str):
|
||||
|
|
@ -106,7 +107,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.get_issue_v3(issue_id)
|
||||
print(f"=>Error {e}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
return self.__parser_issue_info(issue.json())
|
||||
|
||||
def create_issue(self, issue_dict):
|
||||
|
|
@ -120,7 +121,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.create_issue(issue_dict)
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
|
||||
def close_issue(self, issue):
|
||||
try:
|
||||
|
|
@ -132,7 +133,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.close_issue(issue)
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
|
||||
def assign_issue(self, issue_id, account_id) -> bool:
|
||||
try:
|
||||
|
|
@ -143,7 +144,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.assign_issue(issue_id, account_id)
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
|
||||
def add_comment(self, issue_id: str, comment: str):
|
||||
try:
|
||||
|
|
@ -154,7 +155,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.add_comment(issue_id, comment)
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
return self.__parser_comment_info(comment)
|
||||
|
||||
def add_comment_v3(self, issue_id: str, comment: str):
|
||||
|
|
@ -191,7 +192,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.add_comment_v3(issue_id, comment)
|
||||
print(f"=>Error {e}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
return self.__parser_comment_info(comment_response.json())
|
||||
|
||||
def get_comments(self, issueKey):
|
||||
|
|
@ -207,7 +208,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.get_comments(issueKey)
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
|
||||
def get_meta(self):
|
||||
meta = {}
|
||||
|
|
@ -217,14 +218,16 @@ class JiraManager:
|
|||
|
||||
def get_assignable_users(self):
|
||||
try:
|
||||
users = self._jira.search_assignable_users_for_issues('', project=self._config['JIRA_PROJECT_ID'])
|
||||
users = self._jira.search_assignable_users_for_issues(project=self._config['JIRA_PROJECT_ID'], query="*")
|
||||
except JIRAError as e:
|
||||
self.retries -= 1
|
||||
if (e.status_code // 100) == 4 and self.retries > 0:
|
||||
time.sleep(1)
|
||||
return self.get_assignable_users()
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
if e.status_code == 401:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="JIRA: 401 Unauthorized")
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
users_dict = []
|
||||
for user in users:
|
||||
users_dict.append({
|
||||
|
|
@ -245,7 +248,7 @@ class JiraManager:
|
|||
time.sleep(1)
|
||||
return self.get_issue_types()
|
||||
print(f"=>Error {e.text}")
|
||||
raise e
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"JIRA: {e.text}")
|
||||
types_dict = []
|
||||
for type in types:
|
||||
if not type.subtask and not type.name.lower() == "epic":
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ boto3==1.16.1
|
|||
pyjwt==1.7.1
|
||||
psycopg2-binary==2.8.6
|
||||
elasticsearch==7.9.1
|
||||
jira==2.0.0
|
||||
jira==3.1.1
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ boto3==1.16.1
|
|||
pyjwt==1.7.1
|
||||
psycopg2-binary==2.8.6
|
||||
elasticsearch==7.9.1
|
||||
jira==2.0.0
|
||||
jira==3.1.1
|
||||
clickhouse-driver==0.2.2
|
||||
python3-saml==1.12.0
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue