Skip to content

Commit c76466a

Browse files
authored
Merge pull request #12 from jhrozek/list
Use a set for the list to avoid duplicates
2 parents 68ab48b + 990310b commit c76466a

File tree

4 files changed

+55
-23
lines changed

4 files changed

+55
-23
lines changed

cmd/ghactions/list.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ package ghactions
1818
import (
1919
"encoding/json"
2020
"fmt"
21-
"path/filepath"
2221

23-
"github.com/go-git/go-billy/v5/osfs"
2422
"github.com/spf13/cobra"
25-
"gopkg.in/yaml.v3"
2623

2724
"github.com/stacklok/frizbee/pkg/ghactions"
2825
)
@@ -49,21 +46,9 @@ Example:
4946
func list(cmd *cobra.Command, _ []string) error {
5047
dir := cmd.Flag("dir").Value.String()
5148

52-
base := filepath.Base(dir)
53-
bfs := osfs.New(filepath.Dir(dir), osfs.WithBoundOS())
54-
actions := []ghactions.Action{}
55-
56-
err := ghactions.TraverseGitHubActionWorkflows(bfs, base, func(path string, wflow *yaml.Node) error {
57-
wfActions, err := ghactions.ListActionsInYAML(wflow)
58-
if err != nil {
59-
return fmt.Errorf("failed to get actions from YAML file %s: %w", path, err)
60-
}
61-
actions = append(actions, wfActions...)
62-
63-
return nil
64-
})
49+
actions, err := ghactions.ListActionsInDirectory(dir)
6550
if err != nil {
66-
return err
51+
return fmt.Errorf("failed to list actions: %w", err)
6752
}
6853

6954
jsonBytes, err := json.MarshalIndent(actions, "", " ")

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
require (
1414
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
1515
github.com/davecgh/go-spew v1.1.1 // indirect
16+
github.com/deckarep/golang-set/v2 v2.3.1 // indirect
1617
github.com/google/go-querystring v1.1.0 // indirect
1718
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1819
github.com/pmezard/go-difflib v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53E
33
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/deckarep/golang-set/v2 v2.3.1 h1:vjmkvJt/IV27WXPyYQpAh4bRyWJc5Y435D17XQ9QU5A=
7+
github.com/deckarep/golang-set/v2 v2.3.1/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
68
github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=
79
github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow=
810
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=

pkg/ghactions/ghactions.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ package ghactions
2020
import (
2121
"context"
2222
"fmt"
23+
"path/filepath"
2324
"strings"
2425

26+
mapset "github.com/deckarep/golang-set/v2"
27+
"github.com/go-git/go-billy/v5/osfs"
2528
"github.com/google/go-github/v56/github"
2629
"gopkg.in/yaml.v3"
2730

@@ -140,8 +143,8 @@ type Action struct {
140143
}
141144

142145
// ListActionsInYAML returns a list of actions referenced in the given YAML structure.
143-
func ListActionsInYAML(node *yaml.Node) ([]Action, error) {
144-
var uses []Action
146+
func setOfActions(node *yaml.Node) (mapset.Set[Action], error) {
147+
actions := mapset.NewSet[Action]()
145148
foundUses := false
146149

147150
for _, v := range node.Content {
@@ -157,19 +160,60 @@ func ListActionsInYAML(node *yaml.Node) ([]Action, error) {
157160
return nil, fmt.Errorf("failed to parse action reference '%s': %w", v.Value, err)
158161
}
159162

160-
uses = append(uses, *a)
163+
actions.Add(*a)
161164
continue
162165
}
163166

164167
// Otherwise recursively look more
165-
childUses, err := ListActionsInYAML(v)
168+
childUses, err := setOfActions(v)
166169
if err != nil {
167170
return nil, err
168171
}
169-
uses = append(uses, childUses...)
172+
actions = actions.Union(childUses)
173+
}
174+
175+
return actions, nil
176+
}
177+
178+
// ListActionsInYAML returns a list of actions referenced in the given YAML structure.
179+
func ListActionsInYAML(node *yaml.Node) ([]Action, error) {
180+
// just convert the set to a slice
181+
actions, err := setOfActions(node)
182+
if err != nil {
183+
return nil, err
184+
}
185+
186+
return setAsSlice[Action](actions), nil
187+
}
188+
189+
// ListActionsInDirectory returns a list of actions referenced in the given directory.
190+
func ListActionsInDirectory(dir string) ([]Action, error) {
191+
base := filepath.Base(dir)
192+
bfs := osfs.New(filepath.Dir(dir), osfs.WithBoundOS())
193+
actions := mapset.NewSet[Action]()
194+
195+
err := TraverseGitHubActionWorkflows(bfs, base, func(path string, wflow *yaml.Node) error {
196+
wfActions, err := setOfActions(wflow)
197+
if err != nil {
198+
return fmt.Errorf("failed to get actions from YAML file %s: %w", path, err)
199+
}
200+
201+
actions = actions.Union(wfActions)
202+
return nil
203+
})
204+
if err != nil {
205+
return nil, err
170206
}
171207

172-
return uses, nil
208+
return setAsSlice[Action](actions), nil
209+
}
210+
211+
func setAsSlice[T comparable](s mapset.Set[T]) []T {
212+
res := make([]T, 0, s.Cardinality())
213+
for item := range s.Iter() {
214+
res = append(res, item) // Type assertion to T
215+
}
216+
return res
173217
}
174218

175219
func parseValue(val string) (*Action, error) {

0 commit comments

Comments
 (0)