Skip to content

Commit

Permalink
Add more validation & rename vars to match trello nomenclature
Browse files Browse the repository at this point in the history
  • Loading branch information
Cole Brumley committed Sep 5, 2017
1 parent 9ff97c1 commit 95f900a
Showing 1 changed file with 66 additions and 26 deletions.
92 changes: 66 additions & 26 deletions client/trelloclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,76 +29,116 @@ func (c *TrelloClient) Initialize(cmd *cobra.Command) error {
}

// Apply creates the pre-populated Trello board
func (c *TrelloClient) Apply(t *tpl.Board) error {
func (c *TrelloClient) Apply(b *tpl.Board) error {
var (
thisBoard trello.Board
allLabels []*trello.Label
)

c.client = trello.NewClient(c.AppKey, c.Token)

boardArgs := map[string]string{
"name": t.Name,
"desc": t.Description,
"defaultLists": "false",
}

if len(t.Background) > 0 {
boardArgs["prefs_background"] = t.Background
boardArgs, err := validateBoardData(b)
if err != nil {
return err
}

if err := c.client.Post("boards", boardArgs, &thisBoard); err != nil {
return err
}
log.Printf("Created board %s", t.Name)
log.Printf("Created board %s", b.Name)

for _, lbl := range t.Labels {
for _, lbl := range b.Labels {
var label trello.Label
if len(lbl.Name) < 1 {
return fmt.Errorf("label name is required")
}
if err := c.client.Post("labels", map[string]string{
"name": lbl.Name,
"idBoard": thisBoard.ID,
"color": lbl.Color}, &label); err != nil {
log.Fatal(err)
return err
}
allLabels = append(allLabels, &label)
log.Printf("Created label %s", lbl.Name)
}

for laneName, laneStories := range t.Lists {
var lane trello.List
for listName, listCards := range b.Lists {
var thisList trello.List
if len(listName) < 1 {
return fmt.Errorf("list names are required")
}
if err := c.client.Post("lists", map[string]string{
"name": laneName,
"name": listName,
"idBoard": thisBoard.ID,
"pos": "bottom"}, &lane); err != nil {
"pos": "bottom"}, &thisList); err != nil {
return err
}
log.Printf("Created list %s", laneName)
log.Printf("Created list %s", listName)

for _, story := range laneStories {
for _, card := range listCards {
var (
wantLabels []string
thisCard trello.Card
)

for _, l := range allLabels {
for _, wantLbl := range story.Labels {
for _, wantLbl := range card.Labels {
if l.Name == wantLbl {
wantLabels = append(wantLabels, l.ID)
}
}
}

if err := c.client.Post("cards", map[string]string{
"idList": lane.ID,
"name": story.Name,
"desc": story.Description,
"idLabels": strings.Join(wantLabels, ","),
"pos": "bottom"}, &thisCard); err != nil {
if len(card.Name) < 1 {
return fmt.Errorf("card names are required")
}

cardData := map[string]string{
"idList": thisList.ID,
"name": card.Name,
"pos": "bottom",
}

if len(card.Description) > 0 {
cardData["desc"] = card.Description
}

if len(wantLabels) > 0 {
cardData["idLabels"] = strings.Join(wantLabels, ",")
}

if err := c.client.Post("cards", cardData, &thisCard); err != nil {
return err
}
log.Printf("Created card %s", story.Name)
log.Printf("Created card %s", card.Name)
}

}
return nil
}

func validateBoardData(b *tpl.Board) (boardData map[string]string, err error) {
err = nil
boardData = map[string]string{
"defaultLists": "false",
"defaultLabels": "false",
"prefs_voting": "members",
}

if len(b.Name) < 1 {
err = fmt.Errorf("board name is required")
return
}

boardData["name"] = b.Name

if len(b.Description) > 0 {
boardData["desc"] = b.Description
}

if len(b.Background) > 0 {
boardData["prefs_background"] = b.Background
}

return
}

0 comments on commit 95f900a

Please sign in to comment.