4242_TITLE_MAX_SIZE = 255
4343_TEXT_MAX_SIZE = 32767
4444
45+ AttachmentUploadResult = Tuple [Attachment , Optional [str ], Optional [str ]]
46+
4547
4648class JiraTrackerClientError (TrackerClientError ):
4749 """A Jira tracker client error."""
@@ -488,45 +490,47 @@ def _add_comment(
488490
489491 def _replace_attachments_references (
490492 self ,
491- uploads : List [Tuple [ Attachment , str ] ],
493+ uploads : List [AttachmentUploadResult ],
492494 referencing_texts : List [str ],
493495 unique_name_prefix : str ,
494496 ) -> List [str ]:
495- for attachment , upload_url in uploads :
496- referencing_texts = [
497- (
498- text
499- # use the unique attachment name for thumbnails otherwise jira is much confused
500- .replace (
501- f"[{ attachment .original_name } |{ attachment .url } ]" ,
502- (
503- f"[{ self ._make_attachment_unique_name (prefix = unique_name_prefix , attachment = attachment )} "
504- f"|{ attachment .url } ]"
505- ),
506- )
507- .replace (
508- f"!{ attachment .original_name } |{ attachment .url } !" ,
509- (
510- f"!{ self ._make_attachment_unique_name (prefix = unique_name_prefix , attachment = attachment )} "
511- f"|{ attachment .url } !"
512- ),
513- )
497+ for attachment , upload_url , error_message in uploads :
498+ # use the unique attachment name for thumbnails otherwise jira is much confused
499+ unique_name = self ._make_attachment_unique_name (prefix = unique_name_prefix , attachment = attachment )
500+ if upload_url :
501+ referencing_texts = [
502+ text .replace (f"[{ attachment .original_name } |{ attachment .url } ]" , f"[{ unique_name } |{ attachment .url } ]" )
503+ .replace (f"[{ attachment .original_name } ]({ attachment .url } )" , f"[{ unique_name } |{ attachment .url } ]" )
504+ .replace (f"!{ attachment .original_name } |{ attachment .url } !" , f"!{ unique_name } |{ attachment .url } !" )
514505 .replace (
515506 attachment .url ,
516507 upload_url ,
517508 )
518- )
519- for text in referencing_texts
520- ]
509+ for text in referencing_texts
510+ ]
511+ elif error_message :
512+ referencing_texts = [
513+ (
514+ text
515+ # replace attachment with the error message
516+ .replace (f"[{ unique_name } |{ attachment .url } ]" , error_message )
517+ .replace (f"[{ unique_name } ]({ attachment .url } )" , error_message )
518+ .replace (f"!{ unique_name } |{ attachment .url } !" , error_message )
519+ .replace (f"[{ attachment .original_name } |{ attachment .url } ]" , error_message )
520+ .replace (f"[{ attachment .original_name } ]({ attachment .url } )" , error_message )
521+ .replace (f"!{ attachment .original_name } |{ attachment .url } !" , error_message )
522+ )
523+ for text in referencing_texts
524+ ]
521525 return referencing_texts
522526
523527 def _upload_attachments (
524528 self ,
525529 issue : JIRAIssue ,
526530 attachments : List [Attachment ],
527531 unique_name_prefix : str ,
528- ) -> List [Tuple [ Attachment , str ] ]:
529- uploads = []
532+ ) -> List [AttachmentUploadResult ]:
533+ uploads : List [ AttachmentUploadResult ] = []
530534 for attachment in attachments :
531535 # give each attachment a unique name so there's no confusion when jira displays a thumbnail
532536 filename = self ._make_attachment_unique_name (prefix = unique_name_prefix , attachment = attachment )
@@ -537,19 +541,28 @@ def _upload_attachments(
537541 attachment = CustomBytesIO (buffer = attachment .data ), # using CustomBytesIO despite jira expectations
538542 )
539543 except JIRAError as e :
540- raise JiraTrackerClientError (
541- f"Unable to upload attachments for project { self .configuration .project } to JIRA" ,
542- ) from e
543- issue_attachment_url = quote (
544- issue_attachment .content ,
545- safe = ":/?&=" ,
546- )
547- uploads .append (
548- (
549- attachment ,
550- issue_attachment_url ,
551- ),
552- )
544+ uploads .append (
545+ (
546+ attachment ,
547+ None ,
548+ (
549+ f'(Attachment "{ attachment .original_name } " not available due to upload error: '
550+ f'{ getattr (e , "text" , "Unknown error" )} )'
551+ ),
552+ ),
553+ )
554+ else :
555+ url = quote (
556+ issue_attachment .content ,
557+ safe = ":/?&=" ,
558+ )
559+ uploads .append (
560+ (
561+ attachment ,
562+ url ,
563+ None ,
564+ )
565+ )
553566 return uploads
554567
555568 @classmethod
0 commit comments