Skip to content

Commit

Permalink
Merge pull request #20 from Ctri-The-Third/zendesk_comments
Browse files Browse the repository at this point in the history
add methods
  • Loading branch information
Ctri-The-Third authored Dec 17, 2022
2 parents e34c9a5 + 883a6ea commit d1eed7d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
8 changes: 8 additions & 0 deletions serviceHelpers/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def __init__(self, config: JiraDetails) -> None:
}
self.logger = LO

def fetch_jira_ticket(self, key:str) -> JiraTicket:
"Takes a jira key or ID and gets the returned issue"
url = f"https://{self.host}/rest/api/2/issue/{key}"
results = _request_and_validate(url,self.headers)
ticket = JiraTicket().from_dict(results)
return ticket


def fetch_jira_tickets(self, jql) -> dict:
"takes a JQL string, encodes it, send its to Jira, and returns a dict of tickets, with the ticket ID (PRJ-123) as the dict key"

Expand Down
1 change: 1 addition & 0 deletions serviceHelpers/models/ZendeskTicket.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self, host) -> None:
self.requester_name = ""
self.requester = None
self.group_id = 0
self.comments = []
self.logger = logging.getLogger("zendeskHelper.zendeskTicket")
self.custom_fields = {}
pass
Expand Down
20 changes: 20 additions & 0 deletions serviceHelpers/zendesk.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ def __init__(self, host: str, api_key):
if host is None or api_key is None:
_LO.warning("Zendesk object initialised without necessary parameters!!")

def get_comments(self, ticket_id:int = None, ticket:ZendeskTicket = None):
if not ticket_id and not ticket:
self.logger.warning("No id/ticket passed to zendesk.get_comments")
return None
if ticket:
ticket_id = ticket.id

url = f"https://{self.host}/api/v2/tickets/{ticket_id}/comments?sort=-created_at"

try:
response = self._request_and_validate_paginated(url)
comments = response[0]["comments"]
if ticket is not None: #if we're searching by ticket, not by ticket_id
ticket.comments = comments
return ticket #return a ticket that now includes comments
return comments #return just the comments
except Exception as err:
self.logger.error("Unknown error when getting comments for ticket %s, %s", ticket_id,err)
return []

def search_for_tickets(self, search_string):
"""uses the zendesk search notation that's detailed here:
https://developer.zendesk.com/api-reference/ticketing/ticket-management/search/
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="hex-helpers",
version="2.7.5",
version="2.8.0",
description="A series of light helpers for `freshdesk`,`gmail`,`habitica`,`hue lights`,`jira`,`slack`,`trello`",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
6 changes: 6 additions & 0 deletions tests/test_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ def test_jira_init() -> Jira:
return test_instance


def test_fetch_one(caplog:LogCaptureFixture) -> JiraTicket():

jira_instance = test_jira_init()
ticket = jira_instance.fetch_jira_ticket("GSDSE-1")
print(ticket)

def test_fetch(caplog: LogCaptureFixture) -> JiraTicket:
"""Exceutes a JQL string (expects a single closed ticket return)"""
failures = 0
Expand Down
15 changes: 15 additions & 0 deletions tests/test_zendesk.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ def test_search_user(caplog):
assert isinstance(users[user_key], ZendeskUser)


def test_get_comments(caplog):

zd = test_init(caplog)

comments = zd.get_comments(-1)
assert len(comments) == 0

comments = zd.get_comments(1120539)
assert len(comments) == 2

tickets = zd.search_for_tickets("1120539")
for _,ticket in tickets.items():
ticket = zd.get_comments(ticket=ticket)
len( ticket.comments ) == 2

def test_user_init_and_invalid_handling(caplog):
"""verifies the error handling of a ZendeskUser object's initialisation"""

Expand Down

0 comments on commit d1eed7d

Please sign in to comment.