Skip to content

Commit 3ca3704

Browse files
authored
Bugfix: trello state bugfix (#225)
* trello state bugfix refactor trello read * fix the comment issues
1 parent 8c8794d commit 3ca3704

File tree

7 files changed

+51
-26
lines changed

7 files changed

+51
-26
lines changed

docs/plugins/trello-github-integ_plugin.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ _This plugin depends on the following two environment variables:_
1111

1212
Set the values accordingly before using this plugin.
1313

14+
## 3 Tips:
15+
_Trello board description is managed by DevStream, please don't modify it._
16+
1417
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).
1518

1619
```yaml
@@ -32,6 +35,7 @@ tools:
3235
# integration tool name
3336
api:
3437
name: trello
38+
kanbanBoardName: kanban-name
3539
# main branch of the repo (to which branch the plugin will submit the workflows)
3640
branch: master
3741
```

examples/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ tools:
4545
repo: golang-demo
4646
api:
4747
name: trello
48+
kanbanBoardName: kanban-golang-demo
4849
branch: main
4950
- name: argocd-dev
5051
plugin:

internal/pkg/plugin/trellogithub/helper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,20 @@ func (gi *TrelloGithub) renderTemplate(workflow *Workflow) error {
6767

6868
func buildState(tg *TrelloGithub, ti *TrelloItemId) map[string]interface{} {
6969
res := make(map[string]interface{})
70-
res["workflowDir"] = fmt.Sprintf("repos/%s/%s/contents/.github/workflows", tg.options.Owner, tg.options.Repo)
70+
res["workflowDir"] = fmt.Sprintf("/repos/%s/%s/contents/.github/workflows", tg.options.Owner, tg.options.Repo)
7171
res["boardId"] = ti.boardId
7272
res["todoListId"] = ti.todoListId
7373
res["doingListId"] = ti.doingListId
7474
res["doneListId"] = ti.doneListId
7575
return res
7676
}
7777

78-
func (gi *TrelloGithub) buildReadState() (map[string]interface{}, error) {
78+
func (gi *TrelloGithub) buildReadState(api *Api) (map[string]interface{}, error) {
7979
c, err := trello.NewClient()
8080
if err != nil {
8181
return nil, err
8282
}
83-
listIds, err := c.GetBoardIdAndListId()
83+
listIds, err := c.GetBoardIdAndListId(gi.options.Owner, gi.options.Repo, api.KanbanBoardName)
8484
if err != nil {
8585
return nil, err
8686
}

internal/pkg/plugin/trellogithub/read.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func Read(options *map[string]interface{}) (map[string]interface{}, error) {
2323
for name, err := range retMap {
2424
if err != nil {
2525
errFlag = true
26-
log.Errorf("The workflow/file %s got some error: %s.", name, err)
26+
log.Errorf("The workflow/file %s got some error: %s", name, err)
2727
}
28-
log.Infof("The workflow/file %s is ok.", name)
28+
log.Infof("The workflow/file %s is ok", name)
2929
}
3030
if errFlag {
3131
return nil, nil
3232
}
3333

34-
return gis.buildReadState()
34+
return gis.buildReadState(api)
3535
}

internal/pkg/plugin/trellogithub/trellogithub.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ func (gi *TrelloGithub) CreateTrelloItems() (*TrelloItemId, error) {
8484
return nil, err
8585
}
8686

87-
boardName := gi.options.Repo
88-
board, err := c.CreateBoard(boardName)
87+
board, err := c.CreateBoard(gi.options.Api.KanbanBoardName, gi.options.Owner, gi.options.Repo)
8988
if err != nil {
9089
return nil, err
9190
}

internal/pkg/plugin/trellogithub/workflow.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ type Options struct {
2929
}
3030

3131
type Api struct {
32-
Name string
32+
Name string
33+
KanbanBoardName string
3334
}
3435

3536
// Workflow is the struct for a GitHub Actions Workflow.

pkg/util/trello/trello.go

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"os"
66

77
"github.com/adlio/trello"
8+
89
"github.com/merico-dev/stream/internal/pkg/log"
910
)
1011

1112
type Client struct {
1213
*trello.Client
1314
}
1415

16+
const DefaultListsNumber = 3
17+
1518
func NewClient() (*Client, error) {
1619
helpUrl := "https://docs.servicenow.com/bundle/quebec-it-asset-management/page/product/software-asset-management2/task/generate-trello-apikey-token.html"
1720
apiKey := os.Getenv("TRELLO_API_KEY")
@@ -25,11 +28,14 @@ func NewClient() (*Client, error) {
2528
}, nil
2629
}
2730

28-
func (c *Client) CreateBoard(boardName string) (*trello.Board, error) {
29-
if boardName == "" {
30-
return nil, fmt.Errorf("board name can't be empty")
31+
func (c *Client) CreateBoard(kanbanBoardName, owner, repo string) (*trello.Board, error) {
32+
if kanbanBoardName == "" {
33+
kanbanBoardName = fmt.Sprintf("%s/%s", owner, repo)
3134
}
32-
board := trello.NewBoard(boardName)
35+
36+
board := trello.NewBoard(kanbanBoardName)
37+
board.Desc = boardDesc(owner, repo)
38+
3339
err := c.Client.CreateBoard(&board, trello.Defaults())
3440
if err != nil {
3541
return nil, err
@@ -44,27 +50,41 @@ func (c *Client) CreateList(board *trello.Board, listName string) (*trello.List,
4450
return c.Client.CreateList(board, listName, trello.Defaults())
4551
}
4652

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

56+
res := make(map[string]interface{})
5057
bs, err := c.Client.GetMyBoards()
5158
if err != nil {
5259
return nil, err
5360
}
5461

5562
for _, b := range bs {
56-
lists, err := b.GetLists()
57-
if err != nil {
58-
return nil, err
63+
if checkTargetBoard(owner, repo, kanbanBoardName, b) {
64+
lists, err := b.GetLists()
65+
if err != nil {
66+
return nil, err
67+
}
68+
if len(lists) != DefaultListsNumber {
69+
log.Errorf("Unknown lists format: len==%d.", len(lists))
70+
return nil, fmt.Errorf("unknown lists format: len==%d", len(lists))
71+
}
72+
res["boardId"] = b.ID
73+
res["todoListId"] = lists[0].ID
74+
res["doingListId"] = lists[1].ID
75+
res["doneListId"] = lists[2].ID
5976
}
60-
if len(lists) != 3 {
61-
log.Errorf("Unknown lists format: len==%d.", len(lists))
62-
return nil, fmt.Errorf("unknown lists format: len==%d", len(lists))
63-
}
64-
res["boardId"] = b.ID
65-
res["todoListId"] = lists[0].ID
66-
res["doingListId"] = lists[1].ID
67-
res["doneListId"] = lists[2].ID
6877
}
6978
return res, nil
7079
}
80+
81+
func checkTargetBoard(owner, repo, kanbanBoardName string, b *trello.Board) bool {
82+
if b.Name == kanbanBoardName && b.Desc == boardDesc(owner, repo) {
83+
return true
84+
}
85+
return false
86+
}
87+
88+
func boardDesc(owner, repo string) string {
89+
return fmt.Sprintf("Description is managed by DevStream, please don't modify. %s/%s", owner, repo)
90+
}

0 commit comments

Comments
 (0)