Skip to content

Commit

Permalink
Merge pull request #13 from Ctri-The-Third/Zendesk_custom_fields
Browse files Browse the repository at this point in the history
Zendesk custom fields
  • Loading branch information
Ctri-The-Third authored Jun 11, 2022
2 parents 9bfc650 + cb3c57d commit 4fc8a85
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
17 changes: 13 additions & 4 deletions serviceHelpers/models/ZendeskTicket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
from datetime import datetime

from serviceHelpers.models.ZendeskUser import ZendeskUser

# 2021-11-25T12:00:15Z
_ZD_FORMAT = r"%Y-%m-%dT%H:%M:%SZ"

Expand All @@ -22,16 +24,18 @@ def __init__(self, host) -> None:
self.assignee_id = None
self.requester_id = None
self.requester_name = ""
self.requester = None
self.group_id = 0
self.lo = logging.getLogger("zendeskHelper.zendeskTicket")
self.logger = logging.getLogger("zendeskHelper.zendeskTicket")
self.custom_fields = {}
pass

def from_string(self, str):
"takes an unparsed string expected to be from the ZD API, parsed and loads it"
try:
self.from_dict(json.loads(str))
except Exception as e:
self.lo.error("Couldn't parse a ticket string into a dict")
self.logger.error("Couldn't parse a ticket string into a dict")
return self

def from_dict(self, source: dict):
Expand All @@ -54,7 +58,7 @@ def from_dict(self, source: dict):
else self.updated_ts
)
except ValueError as e:
self.lo.error(
self.logger.error(
"Date found but not parsed properly : %s", source["updated_at"]
)
self.summary = source["subject"] if "subject" in source else self.summary
Expand All @@ -68,7 +72,12 @@ def from_dict(self, source: dict):
self.group_id = source["group_id"] if "group_id" in source else self.group_id
self.status = source["status"] if "status" in source else self.status
self.priority = source["priority"] if "priority" in source else self.priority

for custom_field in source["custom_fields"]:
try:
if custom_field["value"] is not None:
self.custom_fields[custom_field["id"]] = custom_field["value"]
except KeyError as err:
self.logger.warning("Couldn't properly get a custom field - %s", err)
return self

def __str__(self) -> str:
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.3.0",
version="2.4.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
24 changes: 23 additions & 1 deletion tests/test_zendesk.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ZendeskOrganisation,
ZendeskUser,
ZendeskWorklog,
ZendeskTicket,
)

ZENDESK_HOST = os.environ.get("ZENDESK_HOST")
Expand Down Expand Up @@ -131,14 +132,18 @@ def test_user_init_and_invalid_handling(caplog):
def test_search_for_tickets(caplog):
"Check for tickets belonging to a specific group"
zend = zendesk(ZENDESK_HOST, ZENDESK_KEY)
search_str = "requester:[email protected]"
search_str = "1239674"

tickets = zend.search_for_tickets(search_string=search_str)

assert len(tickets) > 0

for entry in caplog.records:
assert entry.levelno < logging.ERROR

for _, ticket in tickets.items():
assert isinstance(ticket, ZendeskTicket)


def test_worklog_parse():
"check that if missing fields aren't supplied, the worklog is not reported as valid"
Expand Down Expand Up @@ -171,3 +176,20 @@ def test_get_worklogs_from_audit(caplog):
assert len(logs) > 0
for entry in caplog.records:
assert entry.levelno < logging.ERROR


def test_custom_fields():
"Fetches a test ticket and checks that there are appropriate custom fields populated"

target_id = 1239674

zend = zendesk(ZENDESK_HOST, ZENDESK_KEY)

tickets = zend.search_for_tickets(f"{target_id}")
ticket = tickets[1239674]
ticket: ZendeskTicket

assert isinstance(ticket.custom_fields, dict)
for field_id in ticket.custom_fields:
isinstance(ticket.custom_fields[field_id], (bool, str))
assert True

0 comments on commit 4fc8a85

Please sign in to comment.