fix: importing trello attachments
Since jan 2021, trello requires authentication to access attachments. This commit passes the required auth headers to make downloading card attachments work. resolves https://github.com/go-vikunja/api/issues/6
This commit is contained in:
parent
57e5d10eee
commit
c3e0e6405a
3 changed files with 21 additions and 4 deletions
|
@ -26,10 +26,24 @@ import (
|
||||||
|
|
||||||
// DownloadFile downloads a file and returns its contents
|
// DownloadFile downloads a file and returns its contents
|
||||||
func DownloadFile(url string) (buf *bytes.Buffer, err error) {
|
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)
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if headers != nil {
|
||||||
|
for key, h := range headers {
|
||||||
|
for _, hh := range h {
|
||||||
|
req.Header.Add(key, hh)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hc := http.Client{}
|
hc := http.Client{}
|
||||||
resp, err := hc.Do(req)
|
resp, err := hc.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -38,6 +52,7 @@ func DownloadFile(url string) (buf *bytes.Buffer, err error) {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
buf = &bytes.Buffer{}
|
buf = &bytes.Buffer{}
|
||||||
_, err = buf.ReadFrom(resp.Body)
|
_, err = buf.ReadFrom(resp.Body)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,7 @@ func getTrelloData(token string) (trelloData []*trello.Board, err error) {
|
||||||
|
|
||||||
// Converts all previously obtained data from trello into the vikunja format.
|
// Converts all previously obtained data from trello into the vikunja format.
|
||||||
// `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.NamespaceWithListsAndTasks, err error) {
|
func convertTrelloDataToVikunja(trelloData []*trello.Board, token string) (fullVikunjaHierachie []*models.NamespaceWithListsAndTasks, err error) {
|
||||||
|
|
||||||
log.Debugf("[Trello Migration] ")
|
log.Debugf("[Trello Migration] ")
|
||||||
|
|
||||||
|
@ -254,7 +254,9 @@ func convertTrelloDataToVikunja(trelloData []*trello.Board) (fullVikunjaHierachi
|
||||||
|
|
||||||
log.Debugf("[Trello Migration] Downloading card attachment %s", attachment.ID)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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] Got all trello data for user %d", u.ID)
|
||||||
log.Debugf("[Trello Migration] Start converting 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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -359,7 +359,7 @@ func TestConvertTrelloToVikunja(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
hierachie, err := convertTrelloDataToVikunja(trelloData)
|
hierachie, err := convertTrelloDataToVikunja(trelloData, "")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, hierachie)
|
assert.NotNil(t, hierachie)
|
||||||
if diff, equal := messagediff.PrettyDiff(hierachie, expectedHierachie); !equal {
|
if diff, equal := messagediff.PrettyDiff(hierachie, expectedHierachie); !equal {
|
||||||
|
|
Loading…
Reference in a new issue