Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
PATCH: Fix case when there's no html_url key
Browse files Browse the repository at this point in the history
  • Loading branch information
jonodrew committed May 2, 2024
1 parent c2405a2 commit 2a74700
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
38 changes: 21 additions & 17 deletions lambda_/zendesk_backup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def save_support(ticket_ids: Optional[list] = None):

@dataclasses.dataclass
class ZendeskObject:
html_url: str
html_url: Optional[str]
id: str

to_dict = dataclasses.asdict
Expand All @@ -133,18 +133,18 @@ def get_relations(subject: ObjectTypes) -> dict[str, ObjectTypes]:


def extract_substructure(object_type: ObjectTypes, zendesk_object: ZendeskObject, parent_id: str, parent_key: str,
article_ids: list) -> tuple[dict, str]:
article_ids: list) -> Optional[tuple[dict, str]]:
relations = get_relations(object_type)
parent_type = relations["parent"]
substructure = {}
parent_type_id = f"{parent_type}_id"
if zendesk_object.__getattribute__(parent_type_id) == parent_id:
object_ref = get_key(zendesk_object.to_dict())
if object_ref:
object_key = f"{parent_key}/{object_ref}"
if article_ids == [] or zendesk_object.id in article_ids:
substructure = {object_key: zendesk_object.to_dict()}
return substructure, object_key
return substructure, object_key
return None


def extract_helpcenter(article_ids: list) -> dict[str, dict]:
Expand All @@ -166,25 +166,29 @@ def extract_helpcenter(article_ids: list) -> dict[str, dict]:

sections = zenpy_client().help_center.sections(category_id=category.id)
for section in sections:
section_file, section_key = extract_substructure(
section_output = extract_substructure(
object_type="section",
zendesk_object=section,
parent_id=category.id,
parent_key=category_key,
article_ids=article_ids
)
files.update(section_file)

articles = zenpy_client().help_center.articles(section_id=section.id)
for article in articles:
article_file, _ = extract_substructure(
object_type="article",
zendesk_object=article,
parent_id=section.id,
parent_key=section_key,
article_ids=article_ids,
)
files.update(article_file)
if section_output:
section_file, section_key = section_output
files.update(section_file)

articles = zenpy_client().help_center.articles(section_id=section.id)
for article in articles:
article_output = extract_substructure(
object_type="article",
zendesk_object=article,
parent_id=section.id,
parent_key=section_key,
article_ids=article_ids,
)
if article_output:
article_file, _ = article_output
files.update(article_file)
return files


Expand Down
27 changes: 27 additions & 0 deletions tests/test_zendesk_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,33 @@ def test_save_helpcenter(s3_client: Mock, zenpy_client: Mock):
)


@mock.patch("lambda_.zendesk_backup.main.zenpy_client")
@mock.patch("lambda_.zendesk_backup.main.s3_client")
def test_save_helpcenter_when_no_key(s3_client: Mock, zenpy_client: Mock):
category = ZendeskCategory(html_url="", id="category_id")
section = ZendeskSection(category_id=category.id, html_url="", id="section_id")
article = ZendeskArticle(html_url="", section_id=section.id, id="article_id")
zenpy_client.return_value.help_center.categories.return_value = [category]
zenpy_client.return_value.help_center.sections.return_value = [section]
zenpy_client.return_value.help_center.articles.return_value = [article]
zendesk_backup.save_helpcentre()
s3_put: Mock = s3_client.return_value.put_object
s3_put.assert_not_called()


def test_extract_substructure_when_no_key():
section = ZendeskSection(category_id="category_id", id="section_id", html_url="")
section_output = zendesk_backup.extract_substructure(
"section",
section,
parent_id="category_id",
parent_key="parent/key",
article_ids=[]
)
assert section_output is None



@dataclasses.dataclass
class ZendeskCategory(ZendeskObject):
id: str
Expand Down

0 comments on commit 2a74700

Please sign in to comment.