Skip to content

Commit 76ac5ba

Browse files
authored
Merge pull request #323 from merico-dev/feat-output-ref
feat: add plugin outputs reference
2 parents 2b62496 + 5a1a3c9 commit 76ac5ba

File tree

19 files changed

+514
-215
lines changed

19 files changed

+514
-215
lines changed
Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
## 1 `trello-github-integ` Plugin
22

3-
This plugin creates a new Trello board and integrate it with your GitHub repo.
3+
This plugin creates a new GitHub Actions workflow(trello-github-integration) and uploads it to your GitHub repo.
44

55
## 2 Usage:
66

7-
_This plugin depends on the following two environment variables:_
8-
9-
- TRELLO_API_KEY
10-
- TRELLO_TOKEN
11-
12-
Set the values accordingly before using this plugin.
13-
14-
If you don't know how to create these two tokens, check out:
15-
- [Generate a Trello API key and token](https://docs.servicenow.com/bundle/quebec-it-asset-management/page/product/software-asset-management2/task/generate-trello-apikey-token.html)
16-
17-
## 3 Tips:
18-
_Trello board description is managed by DevStream, please don't modify it._
19-
20-
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).
7+
_This plugin depends on the plugin `trello`:_
218

229
```yaml
2310
tools:
@@ -30,20 +17,58 @@ tools:
3017
# checkout the version from the GitHub releases
3118
version: 0.2.0
3219
# optional; if specified, dtm will make sure the dependency is applied first before handling this tool.
33-
dependsOn: [ "TOOL1_NAME.TOOL1_KIND", "TOOL2_NAME.TOOL2_KIND" ]
20+
dependsOn: [ "TRELLO_INSTANCE_NAME.trello" ]
3421
# options for the plugin
3522
options:
3623
# the repo's owner. It should be case-sensitive here; strictly use your GitHub user name; please change the value below.
3724
owner: YOUR_GITHUB_USERNAME
3825
# the repo where you'd like to setup GitHub Actions; please change the value below.
3926
repo: YOUR_REPO_NAME
40-
# integration tool name
41-
api:
42-
name: trello
43-
# name of the Trello kanban board
44-
kanbanBoardName: kanban-name
27+
# reference parameters come from dependency, their usage will be explained later
28+
boardId: ${{ TRELLO_INSTANCE_NAME.trello.outputs.boardId }}
29+
todoListId: ${{ TRELLO_INSTANCE_NAME.trello.outputs.todoListId }}
30+
doingListId: ${{ TRELLO_INSTANCE_NAME.trello.outputs.doingListId }}
31+
doneListId: ${{ TRELLO_INSTANCE_NAME.trello.outputs.doneListId }}
4532
# main branch of the repo (to which branch the plugin will submit the workflows)
4633
branch: main
4734
```
4835
49-
Currently, all the parameters in the example above are mandatory.
36+
## 3. Use Together with the `trello` Plugin
37+
38+
This plugin can be used together with the `trello` plugin (see document [here](./trello_plugin.md).)
39+
40+
See the example below:
41+
42+
```yaml
43+
---
44+
tools:
45+
- name: trello_init_demo
46+
plugin:
47+
kind: trello
48+
version: 0.2.0
49+
options:
50+
owner: lfbdev
51+
repo: golang-demo
52+
kanbanBoardName: kanban-name
53+
- name: trello_github_integ_demo
54+
plugin:
55+
kind: trello-github-integ
56+
version: 0.2.0
57+
dependsOn: ["trello_init_demo.trello"]
58+
options:
59+
owner: lfbdev
60+
repo: golang-demo
61+
boardId: ${{ trello_init_demo.trello.outputs.boardId }}
62+
todoListId: ${{ trello_init_demo.trello.outputs.todoListId }}
63+
doingListId: ${{ trello_init_demo.trello.outputs.doingListId }}
64+
doneListId: ${{ trello_init_demo.trello.outputs.doneListId }}
65+
branch: main
66+
```
67+
68+
In the example above:
69+
70+
- We put `trello_init_demo.trello` as a dependency by using the `dependsOn` keyword.
71+
- We use `trello_init_demo.trello`'s output as input for the `trello_github_integ_demo` plugin.
72+
73+
Pay attention to the `${{ xxx }}` part in the example. `${{ TOOL_NAME.TOOL_KIND.outputs.var}}` is the syntax for using an output.
74+

docs/plugins/trello_plugin.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,32 @@ See the example below:
4444
```yaml
4545
---
4646
tools:
47-
- name: default
47+
- name: trello_init_demo
4848
plugin:
4949
kind: trello
5050
version: 0.2.0
5151
options:
5252
owner: lfbdev
5353
repo: golang-demo
5454
kanbanBoardName: kanban-name
55-
- name: default_trello_github
55+
- name: trello_github_integ_demo
5656
plugin:
5757
kind: trello-github-integ
5858
version: 0.2.0
59-
dependsOn: ["default.trello"]
59+
dependsOn: ["trello_init_demo.trello"]
6060
options:
6161
owner: lfbdev
6262
repo: golang-demo
63-
api:
64-
name: trello
65-
boardId: ${{ default.trello.outputs.bid }}
66-
todoListId: ${{ default.trello.outputs.todoid }}
67-
doingListId: ${{ default.trello.outputs.doingid }}
68-
doneListId: ${{ default.trello.outputs.doneid }}
63+
boardId: ${{ trello_init_demo.trello.outputs.boardId }}
64+
todoListId: ${{ trello_init_demo.trello.outputs.todoListId }}
65+
doingListId: ${{ trello_init_demo.trello.outputs.doingListId }}
66+
doneListId: ${{ trello_init_demo.trello.outputs.doneListId }}
6967
branch: main
7068
```
7169

7270
In the example above:
7371

74-
- We put `default.trello` as dependency by using the `dependsOn` keyword.
72+
- We put `default.trello` as a dependency by using the `dependsOn` keyword.
7573
- We use `default.trello`'s output as input for the `default_trello_github` plugin.
7674

7775
Pay attention to the `${{ xxx }}` part in the example. `${{ TOOL_NAME.TOOL_KIND.outputs.var}}` is the syntax for using an output.

internal/pkg/plugin/trello/helper.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ func buildReadState(options *Options) (map[string]interface{}, error) {
3737
return nil, err
3838
}
3939

40+
output := make(map[string]interface{})
41+
output["boardId"] = fmt.Sprint(listIds["boardId"])
42+
output["todoListId"] = fmt.Sprint(listIds["todoListId"])
43+
output["doingListId"] = fmt.Sprint(listIds["doingListId"])
44+
output["doneListId"] = fmt.Sprint(listIds["doneListId"])
45+
46+
listIds["outputs"] = output
4047
return listIds, nil
4148
}
4249

internal/pkg/plugin/trello/trello.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ func CreateTrelloBoard(options *Options) (*TrelloItemId, error) {
2828
return nil, err
2929
}
3030

31+
exist, err := c.CheckBoardExists(options.Owner, options.Repo, options.KanbanBoardName)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
if exist {
37+
log.Infof("Board already exists, owner: %s, repo: %s, kanbanName: %s.", options.Owner, options.Repo, options.KanbanBoardName)
38+
listIds, err := c.GetBoardIdAndListId(options.Owner, options.Repo, options.KanbanBoardName)
39+
if err != nil {
40+
return nil, err
41+
}
42+
return &TrelloItemId{
43+
boardId: fmt.Sprint(listIds["boardId"]),
44+
todoListId: fmt.Sprint(listIds["todoListId"]),
45+
doingListId: fmt.Sprint(listIds["doingListId"]),
46+
doneListId: fmt.Sprint(listIds["doneListId"]),
47+
}, nil
48+
}
49+
3150
board, err := c.CreateBoard(options.KanbanBoardName, options.Owner, options.Repo)
3251
if err != nil {
3352
return nil, err

internal/pkg/plugin/trellogithub/create.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,24 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
1111
return nil, err
1212
}
1313

14-
api := tg.GetApi()
15-
log.Infof("API is: %s.", api.Name)
16-
ws := defaultWorkflows.GetWorkflowByNameVersionTypeString(api.Name)
17-
18-
for _, w := range ws {
19-
if err := tg.renderTemplate(w); err != nil {
20-
return nil, err
21-
}
22-
if err := tg.client.AddWorkflow(w, tg.options.Branch); err != nil {
23-
return nil, err
24-
}
14+
if err := tg.client.AddWorkflow(trelloWorkflow, tg.options.Branch); err != nil {
15+
return nil, err
2516
}
17+
2618
log.Success("Adding workflow file succeeded.")
27-
trelloIds, err := tg.CreateTrelloItems()
28-
if err != nil {
29-
return nil, err
19+
20+
trelloItemId := &TrelloItemId{
21+
boardId: tg.options.BoardId,
22+
todoListId: tg.options.todoListId,
23+
doingListId: tg.options.doingListId,
24+
doneListId: tg.options.doneListId,
3025
}
31-
log.Success("Creating trello board succeeded.")
32-
if err := tg.AddTrelloIdSecret(trelloIds); err != nil {
26+
27+
if err := tg.AddTrelloIdSecret(trelloItemId); err != nil {
3328
return nil, err
3429
}
3530

3631
log.Success("Adding secret keys for trello succeeded.")
3732

38-
return buildState(tg, trelloIds), nil
33+
return buildState(tg), nil
3934
}

internal/pkg/plugin/trellogithub/delete.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
package trellogithub
22

3-
import (
4-
"github.com/merico-dev/stream/pkg/util/log"
5-
)
6-
73
// Delete remove trello-github-integ workflows.
84
func Delete(options map[string]interface{}) (bool, error) {
95
tg, err := NewTrelloGithub(options)
106
if err != nil {
117
return false, err
128
}
139

14-
api := tg.GetApi()
15-
log.Infof("API is %s.", api.Name)
16-
ws := defaultWorkflows.GetWorkflowByNameVersionTypeString(api.Name)
17-
18-
for _, w := range ws {
19-
err := tg.client.DeleteWorkflow(w, tg.options.Branch)
20-
if err != nil {
21-
return false, err
22-
}
10+
err = tg.client.DeleteWorkflow(trelloWorkflow, tg.options.Branch)
11+
if err != nil {
12+
return false, err
2313
}
2414

2515
return true, nil
Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,23 @@
11
package trellogithub
22

33
import (
4-
"bytes"
54
"fmt"
6-
"text/template"
7-
8-
"github.com/mitchellh/mapstructure"
9-
10-
"github.com/merico-dev/stream/pkg/util/github"
11-
"github.com/merico-dev/stream/pkg/util/trello"
125
)
136

14-
// renderTemplate render the github actions template with config.yaml
15-
func (tg *TrelloGithub) renderTemplate(workflow *github.Workflow) error {
16-
var jobs Jobs
17-
err := mapstructure.Decode(tg.options.Jobs, &jobs)
18-
if err != nil {
19-
return err
20-
}
21-
//if use default {{.}}, it will confict (github actions vars also use them)
22-
t, err := template.New("trellogithub").Delims("[[", "]]").Parse(workflow.WorkflowContent)
23-
if err != nil {
24-
return err
25-
}
26-
27-
var buff bytes.Buffer
28-
err = t.Execute(&buff, jobs)
29-
if err != nil {
30-
return err
31-
}
32-
workflow.WorkflowContent = buff.String()
33-
return nil
34-
}
35-
36-
func buildState(tg *TrelloGithub, ti *TrelloItemId) map[string]interface{} {
7+
func buildState(tg *TrelloGithub) map[string]interface{} {
378
res := make(map[string]interface{})
389
res["workflowDir"] = fmt.Sprintf("/repos/%s/%s/contents/.github/workflows", tg.options.Owner, tg.options.Repo)
39-
res["boardId"] = ti.boardId
40-
res["todoListId"] = ti.todoListId
41-
res["doingListId"] = ti.doingListId
42-
res["doneListId"] = ti.doneListId
4310
return res
4411
}
4512

46-
func (tg *TrelloGithub) buildReadState(api *Api) (map[string]interface{}, error) {
47-
c, err := trello.NewClient()
48-
if err != nil {
49-
return nil, err
50-
}
51-
listIds, err := c.GetBoardIdAndListId(tg.options.Owner, tg.options.Repo, api.KanbanBoardName)
52-
if err != nil {
53-
return nil, err
54-
}
13+
func (tg *TrelloGithub) buildReadState() (map[string]interface{}, error) {
14+
listIds := make(map[string]interface{})
5515

5616
path, err := tg.client.GetWorkflowPath()
5717
if err != nil {
5818
return nil, err
5919
}
6020
listIds["workflowDir"] = path
21+
6122
return listIds, nil
6223
}

internal/pkg/plugin/trellogithub/read.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package trellogithub
22

33
import (
4+
"github.com/merico-dev/stream/pkg/util/github"
45
"github.com/merico-dev/stream/pkg/util/log"
56
)
67

@@ -10,10 +11,7 @@ func Read(options map[string]interface{}) (map[string]interface{}, error) {
1011
return nil, err
1112
}
1213

13-
api := tg.GetApi()
14-
log.Infof("API is: %s.", api.Name)
15-
16-
ws := defaultWorkflows.GetWorkflowByNameVersionTypeString(api.Name)
14+
var ws = []*github.Workflow{trelloWorkflow}
1715
retMap, err := tg.VerifyWorkflows(ws)
1816
if err != nil {
1917
return nil, err
@@ -31,5 +29,5 @@ func Read(options map[string]interface{}) (map[string]interface{}, error) {
3129
return nil, nil
3230
}
3331

34-
return tg.buildReadState(api)
32+
return tg.buildReadState()
3533
}

0 commit comments

Comments
 (0)