From 61ee0bd5e24a9c9c41f12d5e83a93d5a6bec5c1f Mon Sep 17 00:00:00 2001 From: Elscrux Date: Wed, 10 Apr 2024 22:12:06 +0000 Subject: [PATCH] feat(migration): include non upload attachments from Trello (#2261) This makes the Trello migrator include attachments that are not file uploads. To include them in Vikunja without missing data, their text (usually links) will be appended to the Vikunja description. Co-authored-by: Elscrux Reviewed-on: https://kolaente.dev/vikunja/vikunja/pulls/2261 Reviewed-by: konrad Co-authored-by: Elscrux Co-committed-by: Elscrux --- pkg/modules/migration/trello/trello.go | 58 +++++++++++---------- pkg/modules/migration/trello/trello_test.go | 18 +++++-- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/pkg/modules/migration/trello/trello.go b/pkg/modules/migration/trello/trello.go index 84fe82458..510ec363e 100644 --- a/pkg/modules/migration/trello/trello.go +++ b/pkg/modules/migration/trello/trello.go @@ -336,37 +336,39 @@ func convertTrelloDataToVikunja(organizationName string, trelloData []*trello.Bo log.Debugf("[Trello Migration] Downloading %d card attachments from card %s", len(card.Attachments), card.ID) } for _, attachment := range card.Attachments { - if !attachment.IsUpload { // There are other types of attachments which are not files. We can only handle files. - log.Debugf("[Trello Migration] Attachment %s does not have a mime type, not downloading", attachment.ID) + if attachment.IsUpload { + // Download file and add it as attachment + log.Debugf("[Trello Migration] Downloading card attachment %s", attachment.ID) + + buf, err := migration.DownloadFileWithHeaders(attachment.URL, map[string][]string{ + "Authorization": {`OAuth oauth_consumer_key="` + config.MigrationTrelloKey.GetString() + `", oauth_token="` + token + `"`}, + }) + if err != nil { + return nil, err + } + + vikunjaAttachment := &models.TaskAttachment{ + File: &files.File{ + Name: attachment.Name, + Mime: attachment.MimeType, + Size: uint64(buf.Len()), + FileContent: buf.Bytes(), + }, + } + + if card.IDAttachmentCover != "" && card.IDAttachmentCover == attachment.ID { + vikunjaAttachment.ID = 42 + task.CoverImageAttachmentID = 42 + } + + task.Attachments = append(task.Attachments, vikunjaAttachment) + + log.Debugf("[Trello Migration] Downloaded card attachment %s", attachment.ID) continue } - log.Debugf("[Trello Migration] Downloading card attachment %s", attachment.ID) - - buf, err := migration.DownloadFileWithHeaders(attachment.URL, map[string][]string{ - "Authorization": {`OAuth oauth_consumer_key="` + config.MigrationTrelloKey.GetString() + `", oauth_token="` + token + `"`}, - }) - if err != nil { - return nil, err - } - - vikunjaAttachment := &models.TaskAttachment{ - File: &files.File{ - Name: attachment.Name, - Mime: attachment.MimeType, - Size: uint64(buf.Len()), - FileContent: buf.Bytes(), - }, - } - - if card.IDAttachmentCover != "" && card.IDAttachmentCover == attachment.ID { - vikunjaAttachment.ID = 42 - task.CoverImageAttachmentID = 42 - } - - task.Attachments = append(task.Attachments, vikunjaAttachment) - - log.Debugf("[Trello Migration] Downloaded card attachment %s", attachment.ID) + // Other links are not attachments in Vikunja, but we can add them to the description + task.Description += `

` + attachment.Name + "

\n" } // When the cover image was set manually, we need to add it as an attachment diff --git a/pkg/modules/migration/trello/trello_test.go b/pkg/modules/migration/trello/trello_test.go index 4d94f26ce..3e6ba1a6b 100644 --- a/pkg/modules/migration/trello/trello_test.go +++ b/pkg/modules/migration/trello/trello_test.go @@ -78,6 +78,13 @@ func getTestBoard(t *testing.T) ([]*trello.Board, time.Time) { MimeType: "image/jpg", URL: "https://vikunja.io/testimage.jpg", }, + { + ID: "7cc71b16f0c7a57bed3c94e9", + Name: "Website", + IsUpload: false, + MimeType: "", + URL: "https://vikunja.io", + }, }, }, { @@ -265,10 +272,11 @@ func TestConvertTrelloToVikunja(t *testing.T) { Tasks: []*models.TaskWithComments{ { Task: models.Task{ - Title: "Test Card 1", - Description: "

Card Description bold

\n", - BucketID: 1, - DueDate: time1, + Title: "Test Card 1", + Description: "

Card Description bold

\n" + + "

Website

\n", + BucketID: 1, + DueDate: time1, Labels: []*models.Label{ { Title: "Label 1", @@ -481,6 +489,6 @@ func TestCreateOrganizationMap(t *testing.T) { }, } if diff, equal := messagediff.PrettyDiff(organizationMap, expectedMap); !equal { - t.Errorf("converted trello data = %v,\nwant %v,\ndiff: %v", organizationMap, expectedMap, diff) + t.Errorf("converted organization map = %v,\nwant %v,\ndiff: %v", organizationMap, expectedMap, diff) } }