Skip to content

Commit

Permalink
Bugfix: trello state bugfix (devstream-io#225)
Browse files Browse the repository at this point in the history
* trello state bugfix

refactor trello read

* fix the comment issues
  • Loading branch information
lfbdev authored Feb 24, 2022
1 parent 8c8794d commit 3ca3704
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 26 deletions.
4 changes: 4 additions & 0 deletions docs/plugins/trello-github-integ_plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ _This plugin depends on the following two environment variables:_

Set the values accordingly before using this plugin.

## 3 Tips:
_Trello board description is managed by DevStream, please don't modify it._

To create a Trello API key and token, see [here](https://docs.servicenow.com/bundle/quebec-it-asset-management/page/product/software-asset-management2/task/generate-trello-apikey-token.html).

```yaml
Expand All @@ -32,6 +35,7 @@ tools:
# integration tool name
api:
name: trello
kanbanBoardName: kanban-name
# main branch of the repo (to which branch the plugin will submit the workflows)
branch: master
```
Expand Down
1 change: 1 addition & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tools:
repo: golang-demo
api:
name: trello
kanbanBoardName: kanban-golang-demo
branch: main
- name: argocd-dev
plugin:
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/plugin/trellogithub/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ func (gi *TrelloGithub) renderTemplate(workflow *Workflow) error {

func buildState(tg *TrelloGithub, ti *TrelloItemId) map[string]interface{} {
res := make(map[string]interface{})
res["workflowDir"] = fmt.Sprintf("repos/%s/%s/contents/.github/workflows", tg.options.Owner, tg.options.Repo)
res["workflowDir"] = fmt.Sprintf("/repos/%s/%s/contents/.github/workflows", tg.options.Owner, tg.options.Repo)
res["boardId"] = ti.boardId
res["todoListId"] = ti.todoListId
res["doingListId"] = ti.doingListId
res["doneListId"] = ti.doneListId
return res
}

func (gi *TrelloGithub) buildReadState() (map[string]interface{}, error) {
func (gi *TrelloGithub) buildReadState(api *Api) (map[string]interface{}, error) {
c, err := trello.NewClient()
if err != nil {
return nil, err
}
listIds, err := c.GetBoardIdAndListId()
listIds, err := c.GetBoardIdAndListId(gi.options.Owner, gi.options.Repo, api.KanbanBoardName)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/plugin/trellogithub/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func Read(options *map[string]interface{}) (map[string]interface{}, error) {
for name, err := range retMap {
if err != nil {
errFlag = true
log.Errorf("The workflow/file %s got some error: %s.", name, err)
log.Errorf("The workflow/file %s got some error: %s", name, err)
}
log.Infof("The workflow/file %s is ok.", name)
log.Infof("The workflow/file %s is ok", name)
}
if errFlag {
return nil, nil
}

return gis.buildReadState()
return gis.buildReadState(api)
}
3 changes: 1 addition & 2 deletions internal/pkg/plugin/trellogithub/trellogithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ func (gi *TrelloGithub) CreateTrelloItems() (*TrelloItemId, error) {
return nil, err
}

boardName := gi.options.Repo
board, err := c.CreateBoard(boardName)
board, err := c.CreateBoard(gi.options.Api.KanbanBoardName, gi.options.Owner, gi.options.Repo)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/plugin/trellogithub/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type Options struct {
}

type Api struct {
Name string
Name string
KanbanBoardName string
}

// Workflow is the struct for a GitHub Actions Workflow.
Expand Down
54 changes: 37 additions & 17 deletions pkg/util/trello/trello.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"os"

"github.com/adlio/trello"

"github.com/merico-dev/stream/internal/pkg/log"
)

type Client struct {
*trello.Client
}

const DefaultListsNumber = 3

func NewClient() (*Client, error) {
helpUrl := "https://docs.servicenow.com/bundle/quebec-it-asset-management/page/product/software-asset-management2/task/generate-trello-apikey-token.html"
apiKey := os.Getenv("TRELLO_API_KEY")
Expand All @@ -25,11 +28,14 @@ func NewClient() (*Client, error) {
}, nil
}

func (c *Client) CreateBoard(boardName string) (*trello.Board, error) {
if boardName == "" {
return nil, fmt.Errorf("board name can't be empty")
func (c *Client) CreateBoard(kanbanBoardName, owner, repo string) (*trello.Board, error) {
if kanbanBoardName == "" {
kanbanBoardName = fmt.Sprintf("%s/%s", owner, repo)
}
board := trello.NewBoard(boardName)

board := trello.NewBoard(kanbanBoardName)
board.Desc = boardDesc(owner, repo)

err := c.Client.CreateBoard(&board, trello.Defaults())
if err != nil {
return nil, err
Expand All @@ -44,27 +50,41 @@ func (c *Client) CreateList(board *trello.Board, listName string) (*trello.List,
return c.Client.CreateList(board, listName, trello.Defaults())
}

func (c *Client) GetBoardIdAndListId() (map[string]interface{}, error) {
res := make(map[string]interface{})
// GetBoardIdAndListId get the board, which board name == kanbanBoardName, and board desc == owner/repo
func (c *Client) GetBoardIdAndListId(owner, repo, kanbanBoardName string) (map[string]interface{}, error) {

res := make(map[string]interface{})
bs, err := c.Client.GetMyBoards()
if err != nil {
return nil, err
}

for _, b := range bs {
lists, err := b.GetLists()
if err != nil {
return nil, err
if checkTargetBoard(owner, repo, kanbanBoardName, b) {
lists, err := b.GetLists()
if err != nil {
return nil, err
}
if len(lists) != DefaultListsNumber {
log.Errorf("Unknown lists format: len==%d.", len(lists))
return nil, fmt.Errorf("unknown lists format: len==%d", len(lists))
}
res["boardId"] = b.ID
res["todoListId"] = lists[0].ID
res["doingListId"] = lists[1].ID
res["doneListId"] = lists[2].ID
}
if len(lists) != 3 {
log.Errorf("Unknown lists format: len==%d.", len(lists))
return nil, fmt.Errorf("unknown lists format: len==%d", len(lists))
}
res["boardId"] = b.ID
res["todoListId"] = lists[0].ID
res["doingListId"] = lists[1].ID
res["doneListId"] = lists[2].ID
}
return res, nil
}

func checkTargetBoard(owner, repo, kanbanBoardName string, b *trello.Board) bool {
if b.Name == kanbanBoardName && b.Desc == boardDesc(owner, repo) {
return true
}
return false
}

func boardDesc(owner, repo string) string {
return fmt.Sprintf("Description is managed by DevStream, please don't modify. %s/%s", owner, repo)
}

0 comments on commit 3ca3704

Please sign in to comment.