I can have different types of JIRA query
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import json
|
||||
import logging
|
||||
from enum import Enum
|
||||
|
||||
import requests
|
||||
from requests.auth import HTTPBasicAuth
|
||||
|
||||
from core.Expando import Expando
|
||||
|
||||
JIRA_ROOT = "https://altares.atlassian.net/rest/api/2"
|
||||
JIRA_ROOT = "https://altares.atlassian.net/rest/api/3"
|
||||
DEFAULT_HEADERS = {"Accept": "application/json"}
|
||||
|
||||
logger = logging.getLogger("jql")
|
||||
@@ -16,6 +17,11 @@ class NotFound(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class JiraRequestTypes(Enum):
|
||||
Issues = "issues"
|
||||
Comments = "comments"
|
||||
|
||||
|
||||
class Jira:
|
||||
"""Manage default operation to JIRA"""
|
||||
|
||||
@@ -67,7 +73,7 @@ class Jira:
|
||||
as_dict = json.loads(response.text)
|
||||
return [Expando(field) for field in as_dict]
|
||||
|
||||
def jql(self, jql: str, fields="summary,status,assignee") -> list[Expando]:
|
||||
def issues(self, jql: str, fields="summary,status,assignee") -> list[Expando]:
|
||||
"""
|
||||
Executes a JQL and returns the list of issues
|
||||
:param jql:
|
||||
@@ -91,13 +97,11 @@ class Jira:
|
||||
result = []
|
||||
while True:
|
||||
logger.debug(f"Request startAt '{payload['startAt']}'")
|
||||
response = requests.request(
|
||||
"POST",
|
||||
url,
|
||||
data=json.dumps(payload),
|
||||
headers=headers,
|
||||
auth=self.auth
|
||||
)
|
||||
response = requests.request("POST",
|
||||
url,
|
||||
data=json.dumps(payload),
|
||||
headers=headers,
|
||||
auth=self.auth)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception(self._format_error(response))
|
||||
@@ -113,6 +117,25 @@ class Jira:
|
||||
|
||||
return [Expando(issue) for issue in result]
|
||||
|
||||
def comments(self, issue_id: str) -> list[Expando]:
|
||||
"""
|
||||
Retrieve the list of comments for an issue
|
||||
:param issue_id:
|
||||
:return:
|
||||
"""
|
||||
url = f"{JIRA_ROOT}/issue/{issue_id}/comment"
|
||||
|
||||
response = requests.request("GET",
|
||||
url,
|
||||
headers=DEFAULT_HEADERS,
|
||||
auth=self.auth)
|
||||
if response.status_code != 200:
|
||||
raise Exception(self._format_error(response))
|
||||
|
||||
as_dict = json.loads(response.text)
|
||||
result = as_dict["comments"]
|
||||
return [Expando(issue) for issue in result]
|
||||
|
||||
def extract(self, jql, mappings, updates=None) -> list[dict]:
|
||||
"""
|
||||
Executes a jql and returns list of dict
|
||||
@@ -134,7 +157,7 @@ class Jira:
|
||||
# retrieve the list of requested fields from what was asked in the mapping
|
||||
jira_fields = [_get_field(mapping) for mapping in mappings]
|
||||
as_string = ", ".join(jira_fields)
|
||||
issues = self.jql(jql, as_string)
|
||||
issues = self.issues(jql, as_string)
|
||||
|
||||
for issue in issues:
|
||||
# apply updates if needed
|
||||
@@ -222,4 +245,4 @@ class Jira:
|
||||
error_messages = json.loads(response.text)["errorMessages"]
|
||||
else:
|
||||
error_messages = response.text
|
||||
return f"Error {response.status_code} : {response.reason} : {error_messages}"
|
||||
return f"Error {response.status_code} : {response.reason} : {error_messages}"
|
||||
|
||||
Reference in New Issue
Block a user