From ca0de680ad058e338d2081e2c2d77f864a0dc2fe Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 10 Mar 2024 16:30:01 +0100 Subject: [PATCH] fix(migration): import card covers when migrating from Trello --- go.mod | 2 ++ go.sum | 4 +-- .../migration/create_from_structure.go | 10 ++++++ pkg/modules/migration/trello/trello.go | 35 +++++++++++++++++-- pkg/modules/migration/trello/trello_test.go | 1 + 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 33d34fa71..bcb56100a 100644 --- a/go.mod +++ b/go.mod @@ -190,3 +190,5 @@ replace github.com/samedi/caldav-go => github.com/kolaente/caldav-go v3.0.1-0.20 go 1.21 toolchain go1.21.2 + +replace github.com/adlio/trello => github.com/kolaente/trello v1.8.1-0.20240310152004-14ccae2ddc51 diff --git a/go.sum b/go.sum index 9cf20eabf..5e6e8ac31 100644 --- a/go.sum +++ b/go.sum @@ -24,8 +24,6 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdko github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= github.com/ThreeDotsLabs/watermill v1.3.5 h1:50JEPEhMGZQMh08ct0tfO1PsgMOAOhV3zxK2WofkbXg= github.com/ThreeDotsLabs/watermill v1.3.5/go.mod h1:O/u/Ptyrk5MPTxSeWM5vzTtZcZfxXfO9PK9eXTYiFZY= -github.com/adlio/trello v1.10.0 h1:ia/rzoBwJJKr4IqnMlrU6n09CVqeyaahSkEVcV5/gPc= -github.com/adlio/trello v1.10.0/go.mod h1:I4Lti4jf2KxjTNgTqs5W3lLuE78QZZdYbbPnQQGwjOo= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= @@ -301,6 +299,8 @@ github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZY github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kolaente/caldav-go v3.0.1-0.20190610114120-2a4eb8b5dcc9+incompatible h1:q7DbyV+sFjEoTuuUdRDNl2nlyfztkZgxVVCV7JhzIkY= github.com/kolaente/caldav-go v3.0.1-0.20190610114120-2a4eb8b5dcc9+incompatible/go.mod h1:y1UhTNI4g0hVymJrI6yJ5/ohy09hNBeU8iJEZjgdDOw= +github.com/kolaente/trello v1.8.1-0.20240310152004-14ccae2ddc51 h1:R8xiJ/zSWOndiUjG03GmkkIm1O8MDKt2av0SeaIZy/c= +github.com/kolaente/trello v1.8.1-0.20240310152004-14ccae2ddc51/go.mod h1:I4Lti4jf2KxjTNgTqs5W3lLuE78QZZdYbbPnQQGwjOo= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= diff --git a/pkg/modules/migration/create_from_structure.go b/pkg/modules/migration/create_from_structure.go index 2fc965e9f..66bdeefa1 100644 --- a/pkg/modules/migration/create_from_structure.go +++ b/pkg/modules/migration/create_from_structure.go @@ -267,6 +267,8 @@ func createProjectWithEverything(s *xorm.Session, project *models.ProjectWithTas for _, a := range t.Attachments { // Check if we have a file to create if len(a.File.FileContent) > 0 { + oldID := a.ID + a.ID = 0 a.TaskID = t.ID fr := io.NopCloser(bytes.NewReader(a.File.FileContent)) err = a.NewAttachment(s, fr, a.File.Name, a.File.Size, user) @@ -274,6 +276,14 @@ func createProjectWithEverything(s *xorm.Session, project *models.ProjectWithTas return } log.Debugf("[creating structure] Created new attachment %d", a.ID) + + if t.CoverImageAttachmentID == oldID { + t.CoverImageAttachmentID = a.ID + err = t.Update(s, user) + if err != nil { + return + } + } } } diff --git a/pkg/modules/migration/trello/trello.go b/pkg/modules/migration/trello/trello.go index 3c44000fc..f1ca9b0ea 100644 --- a/pkg/modules/migration/trello/trello.go +++ b/pkg/modules/migration/trello/trello.go @@ -319,18 +319,49 @@ func convertTrelloDataToVikunja(trelloData []*trello.Board, token string) (fullV return nil, err } - task.Attachments = append(task.Attachments, &models.TaskAttachment{ + 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) } + // When the cover image was set manually, we need to add it as an attachment + if card.ManualCoverAttachment && len(card.Cover.Scaled) > 0 { + + cover := card.Cover.Scaled[len(card.Cover.Scaled)-1] + + buf, err := migration.DownloadFile(cover.URL) + if err != nil { + return nil, err + } + + coverAttachment := &models.TaskAttachment{ + ID: 43, + File: &files.File{ + Name: cover.ID + ".jpg", + Mime: "image/jpg", // Seems to always return jpg + Size: uint64(buf.Len()), + FileContent: buf.Bytes(), + }, + } + + task.Attachments = append(task.Attachments, coverAttachment) + task.CoverImageAttachmentID = coverAttachment.ID + } + project.Tasks = append(project.Tasks, &models.TaskWithComments{Task: *task}) } diff --git a/pkg/modules/migration/trello/trello_test.go b/pkg/modules/migration/trello/trello_test.go index cb1c59c10..cc7757ca3 100644 --- a/pkg/modules/migration/trello/trello_test.go +++ b/pkg/modules/migration/trello/trello_test.go @@ -69,6 +69,7 @@ func TestConvertTrelloToVikunja(t *testing.T) { }, Attachments: []*trello.Attachment{ { + ID: "5cc71b16f0c7a57bed3c94e9", Name: "Testimage.jpg", IsUpload: true, MimeType: "image/jpg",