grim/youtrack-import

Fix inline images. Fixes YI-41
draft
2020-08-18, Gary Kramlich
1e60d209bf11
Parents fa48081b3a1c
Children d8a71dd2be73
Fix inline images. Fixes YI-41
--- a/trac/tickets.go Mon Aug 17 22:44:53 2020 -0500
+++ b/trac/tickets.go Tue Aug 18 01:33:03 2020 -0500
@@ -121,6 +121,9 @@
// issue tracker links
debianBugRegex = regexp.MustCompile(`\[DebianBug:(\d+)(?:\s([^]]+))?\]`)
gnomeBugRegex = regexp.MustCompile(`\[GnomeBug:(\d+)(?:\s([^]]+))?\]`)
+
+ // inline image regex
+ inlineImageRegex = regexp.MustCompile(`\[\[Image\(([^)]+)\)\]\]`)
)
func convertLinks(text string) string {
@@ -181,8 +184,12 @@
// replace any ticket link references
comment = ticketLinkRegex.ReplaceAllString(comment, projectID+"-$1$2")
+ // clean up some custom links
comment = convertLinks(comment)
+ // fix images
+ comment = inlineImageRegex.ReplaceAllString(comment, "![]($1)")
+
return comment
}
--- a/youtrack/attachment.go Mon Aug 17 22:44:53 2020 -0500
+++ b/youtrack/attachment.go Tue Aug 18 01:33:03 2020 -0500
@@ -175,6 +175,8 @@
return nil
}
+// extractAttachments will look for any inline images to external pages, and if
+// found, will grab the content and add it as an attachment.
func extractAttachments(issue int, author, message string, created time.Time) (string, []*Attachment, error) {
pattern := `!\[(.*?)\]\((.+?)(\s+["'].*?["'])?\)`
regex := regexp.MustCompile(pattern)
@@ -201,11 +203,13 @@
resp, err := http.Get(uri)
if err != nil {
- return "", attachments, err
+ // these images could be links to anywhere, which can and will be
+ // broken, so we just ignore errors.
+ continue
}
if resp.StatusCode != http.StatusOK {
- return "", attachments, fmt.Errorf("unexpected status code %d", resp.StatusCode)
+ continue
}
attachment := NewAttachment(issue, author, created)