Added other Jira resources
This commit is contained in:
@@ -10,7 +10,7 @@ from core.Expando import Expando
|
||||
JIRA_ROOT = "https://altares.atlassian.net/rest/api/3"
|
||||
DEFAULT_HEADERS = {"Accept": "application/json"}
|
||||
DEFAULT_SEARCH_FIELDS = "summary,status,assignee"
|
||||
logger = logging.getLogger("jql")
|
||||
logger = logging.getLogger("Jira")
|
||||
|
||||
|
||||
class NotFound(Exception):
|
||||
@@ -18,8 +18,10 @@ class NotFound(Exception):
|
||||
|
||||
|
||||
class JiraRequestTypes(Enum):
|
||||
Issues = "issues"
|
||||
Search = "search"
|
||||
Issue = "issue"
|
||||
Comments = "comments"
|
||||
Versions = "versions"
|
||||
|
||||
|
||||
class Jira:
|
||||
@@ -41,7 +43,10 @@ class Jira:
|
||||
self.fields = fields
|
||||
|
||||
def test(self):
|
||||
logger.debug(f"test with no parameters")
|
||||
|
||||
url = f"{JIRA_ROOT}/myself"
|
||||
logger.debug(f" url: {url}")
|
||||
|
||||
response = requests.request(
|
||||
"GET",
|
||||
@@ -49,16 +54,21 @@ class Jira:
|
||||
headers=DEFAULT_HEADERS,
|
||||
auth=self.auth
|
||||
)
|
||||
logger.debug(f" response: {response}")
|
||||
logger.debug(f" response.text: {response.text}")
|
||||
|
||||
return response
|
||||
|
||||
def issue(self, issue_id: str) -> Expando:
|
||||
def issue(self, issue_id: str) -> list[Expando]:
|
||||
"""
|
||||
Retrieve an issue
|
||||
:param issue_id:
|
||||
:return:
|
||||
"""
|
||||
logger.debug(f"comments with {issue_id=}")
|
||||
|
||||
url = f"{JIRA_ROOT}/issue/{issue_id}"
|
||||
logger.debug(f" url: {url}")
|
||||
|
||||
response = requests.request(
|
||||
"GET",
|
||||
@@ -66,8 +76,10 @@ class Jira:
|
||||
headers=DEFAULT_HEADERS,
|
||||
auth=self.auth
|
||||
)
|
||||
logger.debug(f" response: {response}")
|
||||
logger.debug(f" response.text: {response.text}")
|
||||
|
||||
return Expando(json.loads(response.text))
|
||||
return [Expando(json.loads(response.text))]
|
||||
|
||||
def fields(self) -> list[Expando]:
|
||||
"""
|
||||
@@ -86,14 +98,14 @@ class Jira:
|
||||
as_dict = json.loads(response.text)
|
||||
return [Expando(field) for field in as_dict]
|
||||
|
||||
def issues(self, jql: str, fields=None) -> list[Expando]:
|
||||
def search(self, jql: str, fields=None) -> list[Expando]:
|
||||
"""
|
||||
Executes a JQL and returns the list of issues
|
||||
:param jql:
|
||||
:param fields: list of fields to retrieve
|
||||
:return:
|
||||
"""
|
||||
logger.debug(f"Processing jql '{jql}'")
|
||||
logger.debug(f"search with {jql=}, {fields=}")
|
||||
|
||||
if not jql:
|
||||
raise ValueError("Jql cannot be empty.")
|
||||
@@ -102,6 +114,7 @@ class Jira:
|
||||
fields = self.fields
|
||||
|
||||
url = f"{JIRA_ROOT}/search"
|
||||
logger.debug(f" url: {url}")
|
||||
|
||||
headers = DEFAULT_HEADERS.copy()
|
||||
headers["Content-Type"] = "application/json"
|
||||
@@ -113,15 +126,19 @@ class Jira:
|
||||
"maxResults": 500, # Does not seem to be used. It's always 100 !
|
||||
"startAt": 0
|
||||
}
|
||||
logger.debug(f" payload: {payload}")
|
||||
|
||||
result = []
|
||||
while True:
|
||||
logger.debug(f"Request startAt '{payload['startAt']}'")
|
||||
logger.debug(f" Request startAt '{payload['startAt']}'")
|
||||
response = requests.request("POST",
|
||||
url,
|
||||
data=json.dumps(payload),
|
||||
headers=headers,
|
||||
auth=self.auth)
|
||||
logger.debug(f" response: {response}")
|
||||
logger.debug(f" response.text: {response.text}")
|
||||
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception(self._format_error(response))
|
||||
@@ -130,6 +147,7 @@ class Jira:
|
||||
result += as_dict["issues"]
|
||||
|
||||
if as_dict["startAt"] + as_dict["maxResults"] >= as_dict["total"]:
|
||||
logger.debug(f" response: {response}")
|
||||
# We retrieve more than the total nuber of items
|
||||
break
|
||||
|
||||
@@ -143,12 +161,18 @@ class Jira:
|
||||
:param issue_id:
|
||||
:return:
|
||||
"""
|
||||
logger.debug(f"comments with {issue_id=}")
|
||||
|
||||
url = f"{JIRA_ROOT}/issue/{issue_id}/comment"
|
||||
logger.debug(f" url: {url}")
|
||||
|
||||
response = requests.request("GET",
|
||||
url,
|
||||
headers=DEFAULT_HEADERS,
|
||||
auth=self.auth)
|
||||
logger.debug(f" response: {response}")
|
||||
logger.debug(f" response.text: {response.text}")
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception(self._format_error(response))
|
||||
|
||||
@@ -156,6 +180,34 @@ class Jira:
|
||||
result = as_dict["comments"]
|
||||
return [Expando(issue) for issue in result]
|
||||
|
||||
def versions(self, project_key):
|
||||
"""
|
||||
Given a project name and a version name
|
||||
returns fixVersion number in JIRA
|
||||
:param project_key:
|
||||
:return:
|
||||
"""
|
||||
logger.debug(f"versions with {project_key=}")
|
||||
|
||||
url = f"{JIRA_ROOT}/project/{project_key}/versions"
|
||||
logger.debug(f" url: {url}")
|
||||
|
||||
response = requests.request(
|
||||
"GET",
|
||||
url,
|
||||
headers=DEFAULT_HEADERS,
|
||||
auth=self.auth
|
||||
)
|
||||
|
||||
logger.debug(f" response: {response}")
|
||||
logger.debug(f" response.text: {response.text}")
|
||||
|
||||
if response.status_code != 200:
|
||||
raise NotFound()
|
||||
|
||||
as_list = json.loads(response.text)
|
||||
return [Expando(version) for version in as_list]
|
||||
|
||||
def extract(self, jql, mappings, updates=None) -> list[dict]:
|
||||
"""
|
||||
Executes a jql and returns list of dict
|
||||
@@ -188,30 +240,6 @@ class Jira:
|
||||
row = {cvs_col: issue.get(jira_path) for jira_path, cvs_col in mappings.items() if cvs_col is not None}
|
||||
yield row
|
||||
|
||||
def get_versions(self, project_key):
|
||||
"""
|
||||
Given a project name and a version name
|
||||
returns fixVersion number in JIRA
|
||||
:param project_key:
|
||||
:param version_name:
|
||||
:return:
|
||||
"""
|
||||
|
||||
url = f"{JIRA_ROOT}/project/{project_key}/versions"
|
||||
|
||||
response = requests.request(
|
||||
"GET",
|
||||
url,
|
||||
headers=DEFAULT_HEADERS,
|
||||
auth=self.auth
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise NotFound()
|
||||
|
||||
as_list = json.loads(response.text)
|
||||
return [Expando(version) for version in as_list]
|
||||
|
||||
def get_version(self, project_key, version_name):
|
||||
"""
|
||||
Given a project name and a version name
|
||||
@@ -221,7 +249,7 @@ class Jira:
|
||||
:return:
|
||||
"""
|
||||
|
||||
for version in self.get_versions(project_key):
|
||||
for version in self.versions(project_key):
|
||||
if version.name == version_name:
|
||||
return version
|
||||
|
||||
|
||||
Reference in New Issue
Block a user