From 5cfdca318e5a980e2f1b1d05cbf51da64a256ae3 Mon Sep 17 00:00:00 2001 From: Evan Anderson Date: Wed, 29 Nov 2023 11:28:32 -0800 Subject: [PATCH 1/2] Remove custom setAsSlice for ToSlice, switch to ThreadUnsafeSets --- pkg/ghactions/ghactions.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/pkg/ghactions/ghactions.go b/pkg/ghactions/ghactions.go index 571f21d..7b22ad5 100644 --- a/pkg/ghactions/ghactions.go +++ b/pkg/ghactions/ghactions.go @@ -13,8 +13,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package ghactions provides functions to replace tags for checksums -// in GitHub Actions workflows. +// Package ghactions provides functions to process action references and +// replace tags for checksums in GitHub Actions workflows. The functions +// in this package are NOT thread-safe; for a given FS, only one thread +// should update/scan the workflow configurations at a time. package ghactions import ( @@ -144,7 +146,7 @@ type Action struct { // ListActionsInYAML returns a list of actions referenced in the given YAML structure. func setOfActions(node *yaml.Node) (mapset.Set[Action], error) { - actions := mapset.NewSet[Action]() + actions := mapset.NewThreadUnsafeSet[Action]() foundUses := false for _, v := range node.Content { @@ -183,14 +185,14 @@ func ListActionsInYAML(node *yaml.Node) ([]Action, error) { return nil, err } - return setAsSlice[Action](actions), nil + return actions.ToSlice(), nil } // ListActionsInDirectory returns a list of actions referenced in the given directory. func ListActionsInDirectory(dir string) ([]Action, error) { base := filepath.Base(dir) bfs := osfs.New(filepath.Dir(dir), osfs.WithBoundOS()) - actions := mapset.NewSet[Action]() + actions := mapset.NewThreadUnsafeSet[Action]() err := TraverseGitHubActionWorkflows(bfs, base, func(path string, wflow *yaml.Node) error { wfActions, err := setOfActions(wflow) @@ -205,15 +207,7 @@ func ListActionsInDirectory(dir string) ([]Action, error) { return nil, err } - return setAsSlice[Action](actions), nil -} - -func setAsSlice[T comparable](s mapset.Set[T]) []T { - res := make([]T, 0, s.Cardinality()) - for item := range s.Iter() { - res = append(res, item) // Type assertion to T - } - return res + return actions.ToSlice(), nil } func parseValue(val string) (*Action, error) { From 7a050669200e158462df27cc716f98bcc7ef7769 Mon Sep 17 00:00:00 2001 From: Evan Anderson Date: Wed, 29 Nov 2023 11:55:34 -0800 Subject: [PATCH 2/2] Update comment -- the methods should still be thread-safe --- pkg/ghactions/ghactions.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pkg/ghactions/ghactions.go b/pkg/ghactions/ghactions.go index 7b22ad5..35e83f4 100644 --- a/pkg/ghactions/ghactions.go +++ b/pkg/ghactions/ghactions.go @@ -13,10 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package ghactions provides functions to process action references and -// replace tags for checksums in GitHub Actions workflows. The functions -// in this package are NOT thread-safe; for a given FS, only one thread -// should update/scan the workflow configurations at a time. +// Package ghactions provides functions to locate action references and +// replace tags for checksums in GitHub Actions workflows. package ghactions import ( @@ -179,7 +177,6 @@ func setOfActions(node *yaml.Node) (mapset.Set[Action], error) { // ListActionsInYAML returns a list of actions referenced in the given YAML structure. func ListActionsInYAML(node *yaml.Node) ([]Action, error) { - // just convert the set to a slice actions, err := setOfActions(node) if err != nil { return nil, err