package main import ( "errors" "fmt" "github.com/adlio/trello" "github.com/rs/zerolog/log" "github.com/urfave/cli/v2" "time" ) func moveBacklogCardWithDate(cCtx *cli.Context) error { client := getTrelloClient() board, err := client.GetBoard(trelloBordID, trello.Defaults()) if err != nil { return fmt.Errorf("cant get bord: %w", err) } log.Debug().Interface("bord", board).Msg("get bord") lists, err := board.GetLists(trello.Defaults()) if err != nil { return fmt.Errorf("cant get losts for board: %w", err) } log.Debug().Interface("lists", lists).Msg("got lists") var backlogList *trello.List var todoList *trello.List for _, list := range lists { if list.Name == "Backlog" { backlogList = list } if list.Name == "ToDo" { todoList = list } } if backlogList == nil { return errors.New("cant find list with the text \"Backlog\"") } if todoList == nil { return errors.New("cant find list with the text \"ToDo\"") } cards, err := backlogList.GetCards() if err != nil { return fmt.Errorf("cant get card for Backlog list list: %w", err) } var cardsToMoveToTheEndOfTheList []*trello.Card for _, card := range cards { log.Debug().Str("name", card.Name).Msg("Found card") if card.Due == nil { for _, cardToMove := range cardsToMoveToTheEndOfTheList { err = cardToMove.MoveToBottomOfList() if err != nil { return fmt.Errorf("cant move card to end: %w", err) } } cardsToMoveToTheEndOfTheList = []*trello.Card{} continue } cardsToMoveToTheEndOfTheList = append(cardsToMoveToTheEndOfTheList, card) if card.Due.Before(time.Now()) { err = card.MoveToList(todoList.ID) if err != nil { return fmt.Errorf("cant move card to todo list: %w", err) } err = card.MoveToTopOfList() if err != nil { return fmt.Errorf("cant move card to top of todo list: %w", err) } _, err = card.AddComment(fmt.Sprintf("Due Date %v in Backlog reached, moved to todo!", card.Due)) if err != nil { return fmt.Errorf("cant add comment to card: %w", err) } err = card.Update(trello.Arguments{"due": "null"}) if err != nil { return fmt.Errorf("cant update card to remove due date: %w", err) } } else { msg := fmt.Sprintf("Saved for resubmission on %v", card.Due.Format(time.DateTime)) comments, err := card.GetCommentActions() if err != nil { return fmt.Errorf("cant get last comments: %w", err) } foundComment := false for _, comment := range comments { if comment.Data.Text == msg { foundComment = true continue } } if !foundComment { _, err = card.AddComment(msg) if err != nil { return fmt.Errorf("cant get comment to card: %w", err) } } } } return nil }