Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support update ticket jira and jira on-prem #1588

Merged
merged 24 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b8d070c
feat: support update ticket jira and jira on-prem
ezhil56x Aug 11, 2024
0baccef
feat: support update ticket jira and jira on-prem
ezhil56x Aug 11, 2024
cb6179e
Merge branch 'keephq:main' into jira-update
ezhil56x Aug 12, 2024
061e8a4
Update keep/providers/jiraonprem_provider/jiraonprem_provider.py
ezhil56x Aug 12, 2024
90f2b78
Update keep/providers/jiraonprem_provider/jiraonprem_provider.py
ezhil56x Aug 12, 2024
3ef1109
Update keep/providers/jiraonprem_provider/jiraonprem_provider.py
ezhil56x Aug 12, 2024
a000575
Update keep/providers/jiraonprem_provider/jiraonprem_provider.py
ezhil56x Aug 12, 2024
78d029b
feat: support update ticket jira and jira on-prem
ezhil56x Aug 12, 2024
09ee4dc
feat: support update ticket jira and jira on-prem
ezhil56x Aug 12, 2024
6f5b89e
Merge branch 'main' into jira-update
ezhil56x Aug 12, 2024
7f9f5c5
Merge branch 'keephq:main' into jira-update
ezhil56x Aug 18, 2024
a0b20d4
feat: support update ticket jira and jira on-prem
ezhil56x Aug 18, 2024
f5e76ea
Merge branch 'keephq:main' into jira-update
ezhil56x Aug 19, 2024
860be28
Merge branch 'main' into jira-update
talboren Aug 21, 2024
d473b31
Merge branch 'main' into jira-update
talboren Aug 25, 2024
4f9ea9c
Update keep/providers/jiraonprem_provider/jiraonprem_provider.py
talboren Aug 25, 2024
e1d1580
Merge branch 'main' into jira-update
shahargl Aug 26, 2024
ac952b7
Merge branch 'keephq:main' into jira-update
ezhil56x Aug 27, 2024
68e601a
Merge branch 'keephq:main' into jira-update
ezhil56x Aug 30, 2024
6d64f6f
feat: support update ticket jira and jira on-prem
ezhil56x Aug 30, 2024
c59d5dc
feat: support update ticket jira and jira on-prem
ezhil56x Aug 30, 2024
5476ff3
feat: support update ticket jira and jira on-prem
ezhil56x Aug 30, 2024
49c1fd1
Merge branch 'main' into jira-update
talboren Aug 30, 2024
f700207
Merge branch 'main' into jira-update
talboren Sep 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions keep/providers/jira_provider/jira_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,61 @@ def __create_issue(
return {"issue": response.json()}
except Exception as e:
raise ProviderException(f"Failed to create an issue: {e}")

def __update_issue(
self,
ticket_id: str,
summary: str,
description: str = "",
labels: List[str] = None,
components: List[str] = None,
custom_fields: dict = None,
**kwargs: dict,
):
talboren marked this conversation as resolved.
Show resolved Hide resolved
"""
Helper method to update an issue in jira.
"""
try:
self.logger.info("Updating an issue...")

url = self.__get_url(paths=["issue", ticket_id])

update = { }

if summary:
update["summary"] = [{"set": summary}]

if description:
update["description"] = [{"set": description}]

if components:
update["components"] = [{"set": component} for component in components]

if labels:
update["labels"] = [{"set": label} for label in labels]

if custom_fields:
update.update(custom_fields)

request_body = { "update": update }

response = requests.put(
url=url, json=request_body, auth=self.__get_auth(), verify=False
)

try:
if response.status_code != 204:
response.raise_for_status()
except Exception:
self.logger.exception(
"Failed to update an issue", extra=response.text
)
raise ProviderException("Failed to update an issue")
self.logger.info("Updated an issue!")
return {"ticket_id": ticket_id}

except Exception as e:
raise ProviderException(f"Failed to update an issue: {e}")

def _extract_project_key_from_board_name(self, board_name: str):
boards_response = requests.get(
Expand All @@ -328,6 +383,19 @@ def _extract_project_key_from_board_name(self, board_name: str):
)
else:
raise Exception("Could not fetch boards: " + boards_response.text)

def _extract_issue_key_from_ticket_id(self, ticket_id: str):
issue_key = requests.get(
f"{self.jira_host}/rest/api/2/issue/{ticket_id}",
auth=self.__get_auth(),
headers={"Accept": "application/json"},
verify=False,
)

if issue_key.status_code == 200:
return issue_key.json()["key"]
else:
raise Exception("Could not fetch issue key: " + issue_key.text)
talboren marked this conversation as resolved.
Show resolved Hide resolved

def _notify(
self,
Expand All @@ -336,6 +404,7 @@ def _notify(
issue_type: str = "",
project_key: str = "",
board_name: str = "",
ticket_id: str = None,
labels: List[str] = None,
components: List[str] = None,
custom_fields: dict = None,
Expand All @@ -356,6 +425,25 @@ def _notify(
labels = json.loads(labels.replace("'", '"'))
try:
self.logger.info("Notifying jira...")

if ticket_id:
result = self.__update_issue(
ticket_id=ticket_id,
summary=summary,
description=description,
labels=labels,
components=components,
custom_fields=custom_fields,
**kwargs,
)

issue_key = self._extract_issue_key_from_ticket_id(ticket_id)

result["ticket_url"] = f"{self.jira_host}/browse/{issue_key}"

self.logger.info("Notified jira!")
return result

result = self.__create_issue(
project_key=project_key,
summary=summary,
Expand Down
102 changes: 102 additions & 0 deletions keep/providers/jiraonprem_provider/jiraonprem_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,69 @@ def __create_issue(
return {"issue": response.json()}
talboren marked this conversation as resolved.
Show resolved Hide resolved
except Exception as e:
raise ProviderException(f"Failed to create an issue: {e}")

def __update_issue(
self,
ticket_id: str,
ezhil56x marked this conversation as resolved.
Show resolved Hide resolved
summary: str,
talboren marked this conversation as resolved.
Show resolved Hide resolved
description: str = "",
priority: str = "Medium",
labels: List[str] = None,
components: List[str] = None,
custom_fields: dict = None,
**kwargs: dict,
):
"""
Helper method to update an issue in jira.
"""
try:
self.logger.info("Updating an issue...")

url = self.__get_url(paths=["issue", ticket_id])

update = { }

if summary:
update["summary"] = [{"set": summary}]

if description:
update["description"] = [{"set": description}]

if priority:
update["priority"] = [{"set": {"name": priority}}]

if components:
update["components"] = [{"set": [{"name": component} for component in components]}]

if labels:
update["labels"] = [{"set": label} for label in labels]

if custom_fields:
update.update(custom_fields)

request_body = {"update": update}

response = requests.put(
url=url,
json=request_body,
headers=self.__get_auth_header(),
verify=False,
timeout=10,
)

try:
if response.status_code != 204:
response.raise_for_status()
except Exception:
self.logger.exception(
"Failed to update an issue", extra=response.text
)
raise ProviderException("Failed to update an issue")
self.logger.info("Updated an issue!")
return {"ticket_id": ticket_id}

except Exception as e:
raise ProviderException(f"Failed to update an issue: {e}")

def _extract_project_key_from_board_name(self, board_name: str):
headers = {
Expand Down Expand Up @@ -385,6 +448,24 @@ def _extract_project_key_from_board_name(self, board_name: str):
)
else:
raise Exception("Could not fetch boards: " + boards_response.text)

def _extract_issue_key_from_ticket_id(self, ticket_id: str):
headers = {
"Accept": "application/json",
}
headers.update(self.__get_auth_header())

issue_key = requests.get(
f"{self.jira_host}/rest/api/2/issue/{ticket_id}",
headers=headers,
verify=False,
timeout=10,
)

if issue_key.status_code == 200:
return issue_key.json()["key"]
else:
raise Exception("Could not fetch issue key: " + issue_key.text)
talboren marked this conversation as resolved.
Show resolved Hide resolved

def _notify(
self,
Expand All @@ -393,6 +474,7 @@ def _notify(
issue_type: str = "",
project_key: str = "",
board_name: str = "",
ticket_id: str = None,
ezhil56x marked this conversation as resolved.
Show resolved Hide resolved
labels: List[str] = None,
components: List[str] = None,
custom_fields: dict = None,
Expand All @@ -414,6 +496,26 @@ def _notify(
labels = json.loads(labels.replace("'", '"'))
try:
self.logger.info("Notifying jira...")

if ticket_id:
ezhil56x marked this conversation as resolved.
Show resolved Hide resolved
result = self.__update_issue(
ticket_id=ticket_id,
ezhil56x marked this conversation as resolved.
Show resolved Hide resolved
summary=summary,
description=description,
labels=labels,
components=components,
custom_fields=custom_fields,
priority=priority,
**kwargs,
)

issue_key = self._extract_issue_key_from_ticket_id(ticket_id)
talboren marked this conversation as resolved.
Show resolved Hide resolved

result["ticket_url"] = f"{self.jira_host}/browse/{issue_key}"

self.logger.info("Notified jira!")
talboren marked this conversation as resolved.
Show resolved Hide resolved
return result

result = self.__create_issue(
project_key=project_key,
summary=summary,
Expand Down
Loading