From b7bd6e3817985e91890ab1c237c2023acb5369b2 Mon Sep 17 00:00:00 2001 From: Sean Smith Date: Wed, 2 Oct 2019 17:10:36 -0400 Subject: [PATCH] Respect list ordering for trello Ordering not respected because of the use of a map Switch to using slice Resolves #664 --- modules/trello/card.go | 5 +++++ modules/trello/client.go | 26 ++++++++++++++++---------- modules/trello/settings.go | 12 ++++++------ 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/modules/trello/card.go b/modules/trello/card.go index e1487c94..31f3467c 100644 --- a/modules/trello/card.go +++ b/modules/trello/card.go @@ -6,3 +6,8 @@ type TrelloCard struct { List string Description string } + +type TrelloList struct { + ID string + Name string +} diff --git a/modules/trello/client.go b/modules/trello/client.go index e010ce1b..6df6cc22 100644 --- a/modules/trello/client.go +++ b/modules/trello/client.go @@ -6,13 +6,13 @@ import ( "github.com/adlio/trello" ) -func GetCards(client *trello.Client, username string, boardName string, lists map[string]string) (*SearchResult, error) { +func GetCards(client *trello.Client, username string, boardName string, listNames []string) (*SearchResult, error) { boardID, err := getBoardID(client, username, boardName) if err != nil { return nil, err } - lists, err = getListIDs(client, boardID, lists) + lists, err := getLists(client, boardID, listNames) if err != nil { return nil, err } @@ -20,8 +20,8 @@ func GetCards(client *trello.Client, username string, boardName string, lists ma searchResult := &SearchResult{Total: 0} searchResult.TrelloCards = make(map[string][]TrelloCard) - for listName, listID := range lists { - cards, err := getCardsOnList(client, listID) + for _, list := range lists { + cards, err := getCardsOnList(client, list.ID) if err != nil { return nil, err } @@ -32,14 +32,14 @@ func GetCards(client *trello.Client, username string, boardName string, lists ma for _, card := range cards { trelloCard := TrelloCard{ ID: card.ID, - List: listName, + List: list.Name, Name: card.Name, Description: card.Desc, } cardArray = append(cardArray, trelloCard) } - searchResult.TrelloCards[listName] = cardArray + searchResult.TrelloCards[list.Name] = cardArray } return searchResult, nil @@ -65,7 +65,13 @@ func getBoardID(client *trello.Client, username, boardName string) (string, erro return "", fmt.Errorf("could not find board with name %s", boardName) } -func getListIDs(client *trello.Client, boardID string, lists map[string]string) (map[string]string, error) { +func getLists(client *trello.Client, boardID string, listNames []string) ([]TrelloList, error) { + comparison := make(map[string]string, len(listNames)) + results := []TrelloList{} + //convert to a map for comparison + for _, item := range listNames { + comparison[item] = "" + } board, err := client.GetBoard(boardID, trello.Defaults()) if err != nil { return nil, err @@ -77,12 +83,12 @@ func getListIDs(client *trello.Client, boardID string, lists map[string]string) } for _, list := range boardLists { - if _, ok := lists[list.Name]; ok { - lists[list.Name] = list.ID + if _, ok := comparison[list.Name]; ok { + results = append(results, TrelloList{ID: list.ID, Name: list.Name}) } } - return lists, nil + return results, nil } func getCardsOnList(client *trello.Client, listID string) ([]*trello.Card, error) { diff --git a/modules/trello/settings.go b/modules/trello/settings.go index e43c7bad..aa36a7dc 100644 --- a/modules/trello/settings.go +++ b/modules/trello/settings.go @@ -18,7 +18,7 @@ type Settings struct { accessToken string apiKey string board string - list map[string]string + list []string username string } @@ -32,18 +32,18 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co username: ymlConfig.UString("username"), } - settings.list = mapifyList(ymlConfig, globalConfig) + settings.list = buildLists(ymlConfig, globalConfig) return &settings } -func mapifyList(ymlConfig *config.Config, globalConfig *config.Config) map[string]string { - lists := make(map[string]string) +func buildLists(ymlConfig *config.Config, globalConfig *config.Config) []string { + lists := []string{} // Single list list, err := ymlConfig.String("list") if err == nil { - lists[list] = list + lists = append(lists, list) return lists } @@ -51,7 +51,7 @@ func mapifyList(ymlConfig *config.Config, globalConfig *config.Config) map[strin listList := ymlConfig.UList("list") for _, listName := range listList { if list, ok := listName.(string); ok { - lists[list] = list + lists = append(lists, list) } }