diff --git a/pkg/modules/migration/helpers.go b/pkg/modules/migration/helpers.go index cb208d21d8..6887b5593f 100644 --- a/pkg/modules/migration/helpers.go +++ b/pkg/modules/migration/helpers.go @@ -26,10 +26,24 @@ import ( // DownloadFile downloads a file and returns its contents func DownloadFile(url string) (buf *bytes.Buffer, err error) { + return DownloadFileWithHeaders(url, nil) +} + +// DownloadFileWithHeaders downloads a file and allows you to pass in headers +func DownloadFileWithHeaders(url string, headers http.Header) (buf *bytes.Buffer, err error) { req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil) if err != nil { return nil, err } + + if headers != nil { + for key, h := range headers { + for _, hh := range h { + req.Header.Add(key, hh) + } + } + } + hc := http.Client{} resp, err := hc.Do(req) if err != nil { @@ -38,6 +52,7 @@ func DownloadFile(url string) (buf *bytes.Buffer, err error) { defer resp.Body.Close() buf = &bytes.Buffer{} _, err = buf.ReadFrom(resp.Body) + return } diff --git a/pkg/modules/migration/trello/trello.go b/pkg/modules/migration/trello/trello.go index 57e40a3bae..c443d3c6c3 100644 --- a/pkg/modules/migration/trello/trello.go +++ b/pkg/modules/migration/trello/trello.go @@ -144,7 +144,7 @@ func getTrelloData(token string) (trelloData []*trello.Board, err error) { // Converts all previously obtained data from trello into the vikunja format. // `trelloData` should contain all boards with their lists and cards respectively. -func convertTrelloDataToVikunja(trelloData []*trello.Board) (fullVikunjaHierachie []*models.NamespaceWithListsAndTasks, err error) { +func convertTrelloDataToVikunja(trelloData []*trello.Board, token string) (fullVikunjaHierachie []*models.NamespaceWithListsAndTasks, err error) { log.Debugf("[Trello Migration] ") @@ -254,7 +254,9 @@ func convertTrelloDataToVikunja(trelloData []*trello.Board) (fullVikunjaHierachi log.Debugf("[Trello Migration] Downloading card attachment %s", attachment.ID) - buf, err := migration.DownloadFile(attachment.URL) + 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 } @@ -309,7 +311,7 @@ func (m *Migration) Migrate(u *user.User) (err error) { log.Debugf("[Trello Migration] Got all trello data for user %d", u.ID) log.Debugf("[Trello Migration] Start converting trello data for user %d", u.ID) - fullVikunjaHierachie, err := convertTrelloDataToVikunja(trelloData) + fullVikunjaHierachie, err := convertTrelloDataToVikunja(trelloData, m.Token) if err != nil { return } diff --git a/pkg/modules/migration/trello/trello_test.go b/pkg/modules/migration/trello/trello_test.go index e007e20d74..efb02bcfc1 100644 --- a/pkg/modules/migration/trello/trello_test.go +++ b/pkg/modules/migration/trello/trello_test.go @@ -359,7 +359,7 @@ func TestConvertTrelloToVikunja(t *testing.T) { }, } - hierachie, err := convertTrelloDataToVikunja(trelloData) + hierachie, err := convertTrelloDataToVikunja(trelloData, "") assert.NoError(t, err) assert.NotNil(t, hierachie) if diff, equal := messagediff.PrettyDiff(hierachie, expectedHierachie); !equal {