grim/youtrack-import

Parents 01d3a15e7c86
Children f858379a5b97
Scan markdown descriptions/comments for inline images and turn them into attachments. Fixes YI-1
--- a/bitbucket/converter.go Wed Jan 15 00:38:56 2020 -0600
+++ b/bitbucket/converter.go Wed Jan 15 04:41:04 2020 -0600
@@ -62,7 +62,7 @@
Markdown: true,
- Comments: []youtrack.Comment{},
+ Comments: []*youtrack.Comment{},
}
// TODO: check if state is resovled then set the Resolved field to the
@@ -86,15 +86,15 @@
return yt
}
-func (a *Archive) convertComments(userMap map[string]*youtrack.User, id int) []youtrack.Comment {
- comments := []youtrack.Comment{}
+func (a *Archive) convertComments(userMap map[string]*youtrack.User, id int) []*youtrack.Comment {
+ comments := []*youtrack.Comment{}
for _, comment := range a.Comments {
if comment.Issue != id {
continue
}
- var ytComment youtrack.Comment
+ var ytComment *youtrack.Comment
if comment.Content == "" {
// if we don't have a content we need to build it from the log
@@ -118,7 +118,7 @@
)
}
- ytComment = youtrack.Comment{
+ ytComment = &youtrack.Comment{
Author: author,
Created: log.CreatedOn,
Text: content,
@@ -132,7 +132,7 @@
author = user.Login
}
- ytComment = youtrack.Comment{
+ ytComment = &youtrack.Comment{
Author: author,
Text: comment.Content,
Created: comment.CreatedOn,
--- a/youtrack/attachment.go Wed Jan 15 00:38:56 2020 -0600
+++ b/youtrack/attachment.go Wed Jan 15 04:41:04 2020 -0600
@@ -9,6 +9,7 @@
"net/textproto"
"net/url"
"path/filepath"
+ "regexp"
"strings"
"time"
@@ -164,3 +165,48 @@
return nil
}
+
+func extractAttachments(issue int, author, message string, created time.Time) (string, []*Attachment, error) {
+ pattern := `!\[(.*?)\]\((.+?)(\s+["'].*?["'])?\)`
+ regex := regexp.MustCompile(pattern)
+
+ output := message
+ attachments := []*Attachment{}
+
+ matches := regex.FindAllStringSubmatch(message, -1)
+ for _, match := range matches {
+ old := match[0]
+ filename := match[1]
+ uri := match[2]
+
+ if filename == "" {
+ decoded, err := url.QueryUnescape(uri)
+ if err != nil {
+ return "", attachments, err
+ }
+
+ filename = filepath.Base(decoded)
+ }
+
+ img := fmt.Sprintf("![](%s)", filename)
+
+ resp, err := http.Get(uri)
+ if err != nil {
+ return "", attachments, err
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ return "", attachments, fmt.Errorf("unexpected status code %d", resp.StatusCode)
+ }
+
+ attachment := NewAttachment(issue, author, created)
+ if err := attachment.AddFile(resp.Body, filename); err != nil {
+ return "", attachments, err
+ }
+
+ output = strings.Replace(output, old, img, 1)
+ attachments = append(attachments, attachment)
+ }
+
+ return output, attachments, nil
+}
--- a/youtrack/comments.go Wed Jan 15 00:38:56 2020 -0600
+++ b/youtrack/comments.go Wed Jan 15 04:41:04 2020 -0600
@@ -13,8 +13,31 @@
Markdown bool
}
-func sortComments(comments []Comment) {
+func sortComments(comments []*Comment) {
sort.Slice(comments, func(i, j int) bool {
return comments[i].Created.Before(comments[j].Created)
})
}
+
+func (c *Comment) extractAttachments(i *Issue) error {
+ if !c.Markdown {
+ return nil
+ }
+
+ modified, attachments, err := extractAttachments(
+ i.Number,
+ c.Author,
+ c.Text,
+ c.Created,
+ )
+ if err != nil {
+ return err
+ }
+
+ if len(attachments) > 0 {
+ c.Text = modified
+ i.Attachments = append(i.Attachments, attachments...)
+ }
+
+ return nil
+}
--- a/youtrack/issue.go Wed Jan 15 00:38:56 2020 -0600
+++ b/youtrack/issue.go Wed Jan 15 04:41:04 2020 -0600
@@ -29,10 +29,37 @@
Type string `yt:"type"`
State string `yt:"state"`
- Comments []Comment
+ Comments []*Comment
Attachments []*Attachment
}
+func (i *Issue) extractAttachments() error {
+ if i.Markdown {
+ modified, attachments, err := extractAttachments(
+ i.Number,
+ i.Reporter,
+ i.Description,
+ i.Created,
+ )
+ if err != nil {
+ return err
+ }
+
+ if len(attachments) > 0 {
+ i.Description = modified
+ i.Attachments = append(i.Attachments, attachments...)
+ }
+ }
+
+ for _, comment := range i.Comments {
+ if err := comment.extractAttachments(i); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
func (i *Issue) encode() xmlIssue {
x := xmlIssue{}
@@ -104,6 +131,10 @@
issues.Issues = make([]xmlIssue, e-s)
for i := s; i < e; i++ {
+ if err := p.Issues[i].extractAttachments(); err != nil {
+ return err
+ }
+
issues.Issues[i-s] = p.Issues[i].encode()
}