Add the actual conversion
This commit is contained in:
parent
48c74deb83
commit
a5afe10f52
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
|
@ -382,18 +382,7 @@ func convertTodoistToVikunja(sync *sync) (fullVikunjaHierachie []*models.Namespa
|
||||||
// Only add the attachment if there's something to download
|
// Only add the attachment if there's something to download
|
||||||
if len(n.FileAttachment.FileURL) > 0 {
|
if len(n.FileAttachment.FileURL) > 0 {
|
||||||
// Download the attachment and put it in the file
|
// Download the attachment and put it in the file
|
||||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, n.FileAttachment.FileURL, nil)
|
buf, err := migration.DownloadFile(n.FileAttachment.FileURL)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ package trello
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.vikunja.io/api/pkg/config"
|
"code.vikunja.io/api/pkg/config"
|
||||||
|
"code.vikunja.io/api/pkg/files"
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
"code.vikunja.io/api/pkg/models"
|
"code.vikunja.io/api/pkg/models"
|
||||||
"code.vikunja.io/api/pkg/modules/migration"
|
"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.
|
// `trelloData` should contain all boards with their lists and cards respectively.
|
||||||
func convertTrelloDataToVikunja(trelloData []*trello.Board) (fullVikunjaHierachie []*models.NamespaceWithLists, err error) {
|
func convertTrelloDataToVikunja(trelloData []*trello.Board) (fullVikunjaHierachie []*models.NamespaceWithLists, err error) {
|
||||||
|
|
||||||
// Archived Board (closed)
|
fullVikunjaHierachie = []*models.NamespaceWithLists{
|
||||||
// Backgrounds
|
{
|
||||||
|
Namespace: models.Namespace{
|
||||||
|
Title: "Imported from Trello",
|
||||||
|
},
|
||||||
|
Lists: []*models.List{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Labels
|
var bucketID int64 = 1
|
||||||
// Checklists (as markdown in description)
|
for _, board := range trelloData {
|
||||||
// Due Date
|
list := &models.List{
|
||||||
// Reminders for due dates
|
Title: board.Name,
|
||||||
// Attachments
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -170,12 +170,10 @@ func TestConvertTrelloToVikunja(t *testing.T) {
|
||||||
DueDate: time1,
|
DueDate: time1,
|
||||||
Labels: []*models.Label{
|
Labels: []*models.Label{
|
||||||
{
|
{
|
||||||
ID: 11111,
|
|
||||||
Title: "Label 1",
|
Title: "Label 1",
|
||||||
HexColor: trelloColorMap["green"],
|
HexColor: trelloColorMap["green"],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: 22222,
|
|
||||||
Title: "Label 2",
|
Title: "Label 2",
|
||||||
HexColor: trelloColorMap["orange"],
|
HexColor: trelloColorMap["orange"],
|
||||||
},
|
},
|
||||||
|
@ -197,7 +195,6 @@ func TestConvertTrelloToVikunja(t *testing.T) {
|
||||||
Position: 127,
|
Position: 127,
|
||||||
Labels: []*models.Label{
|
Labels: []*models.Label{
|
||||||
{
|
{
|
||||||
ID: 22222,
|
|
||||||
Title: "Label 2",
|
Title: "Label 2",
|
||||||
HexColor: trelloColorMap["orange"],
|
HexColor: trelloColorMap["orange"],
|
||||||
},
|
},
|
||||||
|
@ -209,7 +206,6 @@ func TestConvertTrelloToVikunja(t *testing.T) {
|
||||||
Position: 111,
|
Position: 111,
|
||||||
Labels: []*models.Label{
|
Labels: []*models.Label{
|
||||||
{
|
{
|
||||||
ID: 3333,
|
|
||||||
Title: "Label 3",
|
Title: "Label 3",
|
||||||
HexColor: trelloColorMap["blue"],
|
HexColor: trelloColorMap["blue"],
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue