Skip to content

Commit

Permalink
Merge branch 'release/2.8.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Honel committed Feb 28, 2024
2 parents 58b09e9 + a5ea2cf commit ec8e497
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Since data is pulled from YWH platform to your server, only regular outbound web
- improved Python versions support (>=3.7 to <=3.12)
- removed the GUI from the default installation (use `pip install 'ywh2bt[gui]'` to include the GUI)
- fixed an issue with github when the title of an issue is longer than 255 characters
- fixed an issue with jira image previews when multiple attached images have the same name
- v2.7:
- added synchronization of "fix verification" logs when "Upload status updates" is checked
- fixed an issue with jira when scope contains special markdown characters
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ywh2bt"
version = "2.8.0"
version = "2.8.1"
description = "ywh2bt - YesWeHack to Bug Tracker"
readme = "README.md"
authors = ["m.honel <m.honel@yeswehack.com>"]
Expand Down
77 changes: 52 additions & 25 deletions ywh2bt/core/api/trackers/jira/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class JiraTrackerClient(TrackerClient[JiraConfiguration]):
_attachments_list_description_item_jira_template = Template("- [${name}|${url}]")
_attachments_list_description_item_markdown_template = Template("- [${name}](${url})")

_report_attachment_name_prefix: str = "R"
_comment_attachment_name_prefix: str = "C"

def __init__(
self,
configuration: JiraConfiguration,
Expand Down Expand Up @@ -222,6 +225,7 @@ def send_report(
title="*Attachments*:",
item_template=self._attachments_list_description_item_jira_template,
attachments=report.attachments,
unique_name_prefix=self._report_attachment_name_prefix,
)
markdown_description = ""
description_attachment = None
Expand All @@ -235,6 +239,7 @@ def send_report(
title="**Attachments**:",
item_template=self._attachments_list_description_item_markdown_template,
attachments=report.attachments,
unique_name_prefix=self._report_attachment_name_prefix,
)
report_copy = deepcopy(report)
report_copy.description_html = (
Expand All @@ -251,6 +256,7 @@ def send_report(
description_attachment,
*report.attachments,
],
unique_name_prefix=self._report_attachment_name_prefix,
)
jira_issue = self._create_issue(
title=title,
Expand All @@ -260,11 +266,13 @@ def send_report(
uploads=self._upload_attachments(
issue=jira_issue,
attachments=report.attachments,
unique_name_prefix=self._report_attachment_name_prefix,
),
referencing_texts=[
description,
markdown_description,
],
unique_name_prefix=self._report_attachment_name_prefix,
)
if description_attachment:
description_attachment.data_loader = lambda: bytes(markdown_description, "utf-8")
Expand All @@ -274,10 +282,12 @@ def send_report(
attachments=[
description_attachment,
],
unique_name_prefix=self._report_attachment_name_prefix,
),
referencing_texts=[
description,
],
unique_name_prefix=self._report_attachment_name_prefix,
)[0]
jira_issue.update(
description=description,
Expand Down Expand Up @@ -405,6 +415,7 @@ def _add_comment(
title="*Attachments*:",
item_template=self._attachments_list_description_item_jira_template,
attachments=log.attachments,
unique_name_prefix=self._comment_attachment_name_prefix,
)
markdown_description = ""
body_attachment = None
Expand All @@ -418,6 +429,7 @@ def _add_comment(
title="**Attachments**:",
item_template=self._attachments_list_description_item_markdown_template,
attachments=log.attachments,
unique_name_prefix=self._comment_attachment_name_prefix,
)
log_copy = deepcopy(log)
log_copy.message_html = (
Expand All @@ -432,16 +444,19 @@ def _add_comment(
body_attachment,
*log.attachments,
],
unique_name_prefix=self._comment_attachment_name_prefix,
)
comment_body, markdown_description = self._replace_attachments_references(
uploads=self._upload_attachments(
issue=issue,
attachments=log.attachments,
unique_name_prefix=self._comment_attachment_name_prefix,
),
referencing_texts=[
comment_body,
markdown_description,
],
unique_name_prefix=self._comment_attachment_name_prefix,
)
if body_attachment:
body_attachment.data_loader = lambda: bytes(markdown_description, "utf-8")
Expand All @@ -451,10 +466,12 @@ def _add_comment(
attachments=[
body_attachment,
],
unique_name_prefix=self._comment_attachment_name_prefix,
),
referencing_texts=[
comment_body,
],
unique_name_prefix=self._comment_attachment_name_prefix,
)[0]
try:
return cast(
Expand All @@ -469,36 +486,35 @@ def _add_comment(
f"Unable to add JIRA comment for issue {issue} in project {self.configuration.project}",
) from e

def _upload_attachments_and_substitute_references(
self,
issue: JIRAIssue,
attachments: List[Attachment],
referencing_texts: List[str],
) -> List[str]:
uploads = self._upload_attachments(
issue=issue,
attachments=attachments,
)
for attachment, upload_url in uploads:
referencing_texts = [
text.replace(
attachment.url,
upload_url,
)
for text in referencing_texts
]
return referencing_texts

def _replace_attachments_references(
self,
uploads: List[Tuple[Attachment, str]],
referencing_texts: List[str],
unique_name_prefix: str,
) -> List[str]:
for attachment, upload_url in uploads:
referencing_texts = [
text.replace(
attachment.url,
upload_url,
(
text
# use the unique attachment name for thumbnails otherwise jira is much confused
.replace(
f"[{attachment.original_name}|{attachment.url}]",
(
f"[{self._make_attachment_unique_name(prefix=unique_name_prefix, attachment=attachment)}"
f"|{attachment.url}]"
),
)
.replace(
f"!{attachment.original_name}|{attachment.url}!",
(
f"!{self._make_attachment_unique_name(prefix=unique_name_prefix, attachment=attachment)}"
f"|{attachment.url}!"
),
)
.replace(
attachment.url,
upload_url,
)
)
for text in referencing_texts
]
Expand All @@ -508,13 +524,16 @@ def _upload_attachments(
self,
issue: JIRAIssue,
attachments: List[Attachment],
unique_name_prefix: str,
) -> List[Tuple[Attachment, str]]:
uploads = []
for attachment in attachments:
# give each attachment a unique name so there's no confusion when jira displays a thumbnail
filename = self._make_attachment_unique_name(prefix=unique_name_prefix, attachment=attachment)
try:
issue_attachment = self._get_client().add_attachment(
issue=issue.key,
filename=attachment.original_name,
filename=filename,
attachment=CustomBytesIO(buffer=attachment.data), # using CustomBytesIO despite jira expectations
)
except JIRAError as e:
Expand All @@ -533,11 +552,16 @@ def _upload_attachments(
)
return uploads

@classmethod
def _make_attachment_unique_name(cls, prefix: str, attachment: Attachment) -> str:
return f"{prefix}-{attachment.attachment_id}-{attachment.original_name}"

def _get_attachments_list_description(
self,
title: str,
item_template: Template,
attachments: List[Attachment],
unique_name_prefix: str,
) -> str:
attachments_lines = []
if attachments:
Expand All @@ -548,7 +572,10 @@ def _get_attachments_list_description(
for attachment in attachments:
attachments_lines.append(
item_template.substitute(
name=attachment.original_name,
name=self._make_attachment_unique_name(
prefix=unique_name_prefix,
attachment=attachment,
),
url=attachment.url,
),
)
Expand Down

0 comments on commit ec8e497

Please sign in to comment.