From a5afe10f52727f23586efea497a14e9098ff2699 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 16 Dec 2020 23:19:40 +0100 Subject: [PATCH] Add the actual conversion --- pkg/modules/migration/helpers.go | 40 +++++++++ pkg/modules/migration/todoist/todoist.go | 13 +-- pkg/modules/migration/trello/trello.go | 95 +++++++++++++++++++-- pkg/modules/migration/trello/trello_test.go | 4 - 4 files changed, 129 insertions(+), 23 deletions(-) create mode 100644 pkg/modules/migration/helpers.go diff --git a/pkg/modules/migration/helpers.go b/pkg/modules/migration/helpers.go new file mode 100644 index 000000000..0a89fff54 --- /dev/null +++ b/pkg/modules/migration/helpers.go @@ -0,0 +1,40 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-2020 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package migration + +import ( + "bytes" + "context" + "net/http" +) + +// DownloadFile downloads a file and returns its contents +func DownloadFile(url string) (buf *bytes.Buffer, err error) { + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil) + if err != nil { + return nil, err + } + hc := http.Client{} + resp, err := hc.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + buf = &bytes.Buffer{} + _, err = buf.ReadFrom(resp.Body) + return +} diff --git a/pkg/modules/migration/todoist/todoist.go b/pkg/modules/migration/todoist/todoist.go index 38e42191f..b15459c41 100644 --- a/pkg/modules/migration/todoist/todoist.go +++ b/pkg/modules/migration/todoist/todoist.go @@ -382,18 +382,7 @@ func convertTodoistToVikunja(sync *sync) (fullVikunjaHierachie []*models.Namespa // Only add the attachment if there's something to download if len(n.FileAttachment.FileURL) > 0 { // Download the attachment and put it in the file - req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, n.FileAttachment.FileURL, nil) - if err != nil { - return nil, err - } - hc := http.Client{} - resp, err := hc.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - buf := &bytes.Buffer{} - _, err = buf.ReadFrom(resp.Body) + buf, err := migration.DownloadFile(n.FileAttachment.FileURL) if err != nil { return nil, err } diff --git a/pkg/modules/migration/trello/trello.go b/pkg/modules/migration/trello/trello.go index 80fadf8c3..46b61e27f 100644 --- a/pkg/modules/migration/trello/trello.go +++ b/pkg/modules/migration/trello/trello.go @@ -18,6 +18,7 @@ package trello import ( "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/modules/migration" @@ -123,14 +124,94 @@ func getTrelloData(token string) (trelloData []*trello.Board, err error) { // `trelloData` should contain all boards with their lists and cards respectively. func convertTrelloDataToVikunja(trelloData []*trello.Board) (fullVikunjaHierachie []*models.NamespaceWithLists, err error) { - // Archived Board (closed) - // Backgrounds + fullVikunjaHierachie = []*models.NamespaceWithLists{ + { + Namespace: models.Namespace{ + Title: "Imported from Trello", + }, + Lists: []*models.List{}, + }, + } - // Labels - // Checklists (as markdown in description) - // Due Date - // Reminders for due dates - // Attachments + var bucketID int64 = 1 + for _, board := range trelloData { + list := &models.List{ + Title: board.Name, + Description: board.Desc, + // Archived Board (closed) + IsArchived: board.Closed, + } + + // TODO: List Background + + for _, l := range board.Lists { + bucket := &models.Bucket{ + ID: bucketID, + Title: l.Name, + } + + for _, card := range l.Cards { + // The usual stuff: Title, description, position, bucket id + task := &models.Task{ + Title: card.Name, + Description: card.Desc, + Position: card.Pos, + BucketID: bucketID, + } + + if card.Due != nil { + task.DueDate = *card.Due + } + + // Checklists (as markdown in description) + //for _, checklist := range card.Checklists { + // + //} + + // Labels + for _, label := range card.Labels { + color, exists := trelloColorMap[label.Color] + if !exists { + log.Debugf("[Trello Migration] Color %s not mapped for trello card %s, not adding label", label.Color, card.ID) + continue + } + + task.Labels = append(task.Labels, &models.Label{ + Title: label.Name, + HexColor: color, + }) + } + + // Attachments + for _, attachment := range card.Attachments { + if attachment.MimeType == "" { // Attachments can also be not downloadable - the mime type is empty in that case. + continue + } + + buf, err := migration.DownloadFile(attachment.URL) + if err != nil { + return nil, err + } + + task.Attachments = append(task.Attachments, &models.TaskAttachment{ + File: &files.File{ + Name: attachment.Name, + Mime: attachment.MimeType, + Size: uint64(buf.Len()), + FileContent: buf.Bytes(), + }, + }) + } + + list.Tasks = append(list.Tasks, task) + } + + list.Buckets = append(list.Buckets, bucket) + bucketID++ + } + + fullVikunjaHierachie[0].Lists = append(fullVikunjaHierachie[0].Lists, list) + } return } diff --git a/pkg/modules/migration/trello/trello_test.go b/pkg/modules/migration/trello/trello_test.go index 5b8fe9e8e..5c717d018 100644 --- a/pkg/modules/migration/trello/trello_test.go +++ b/pkg/modules/migration/trello/trello_test.go @@ -170,12 +170,10 @@ func TestConvertTrelloToVikunja(t *testing.T) { DueDate: time1, Labels: []*models.Label{ { - ID: 11111, Title: "Label 1", HexColor: trelloColorMap["green"], }, { - ID: 22222, Title: "Label 2", HexColor: trelloColorMap["orange"], }, @@ -197,7 +195,6 @@ func TestConvertTrelloToVikunja(t *testing.T) { Position: 127, Labels: []*models.Label{ { - ID: 22222, Title: "Label 2", HexColor: trelloColorMap["orange"], }, @@ -209,7 +206,6 @@ func TestConvertTrelloToVikunja(t *testing.T) { Position: 111, Labels: []*models.Label{ { - ID: 3333, Title: "Label 3", HexColor: trelloColorMap["blue"], },