Skip to content

Commit

Permalink
fix: implement parsing for locally referenced actions in 'uses'
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Rose <[email protected]>
  • Loading branch information
matty-rose committed Dec 4, 2021
1 parent e4bb0a9 commit 08d4ffd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
43 changes: 37 additions & 6 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package parser

import (
"io/ioutil"
"path/filepath"
"regexp"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -107,6 +108,37 @@ func parseOutputs(action *types.CompositeAction, data map[interface{}]interface{
return nil
}

func tryMatchRemoteUses(text string) ([][]string, bool) {
regex := *regexp.MustCompile(`(.+)\/(.+)@(.+)`)

res := regex.FindAllStringSubmatch(text, -1)
if res != nil {
return res, true
}

return res, false
}

func parseUses(ext *types.ExternalAction, uses string) error {
if remote, ok := tryMatchRemoteUses(uses); ok {
ext.Creator = remote[0][1]
ext.Name = remote[0][2]
ext.Version = remote[0][3]
ext.Local = false

return nil
}

// If remote regex doesn't match, assume its a local reference to an action
logrus.Debug("Matching local action reference")

ext.Local = true
ext.LocalPath = &uses
ext.Name = filepath.Base(uses)

return nil
}

func parseExternalActions(action *types.CompositeAction, data map[interface{}]interface{}) error {
runs, ok := data["runs"].(map[string]interface{})
if !ok {
Expand Down Expand Up @@ -139,13 +171,12 @@ func parseExternalActions(action *types.CompositeAction, data map[interface{}]in
ext.StepID = stepID
}

regex := *regexp.MustCompile(`(.+)\/(.+)@(.+)`)
res := regex.FindAllStringSubmatch(step["uses"].(string), -1)
err := parseUses(&ext, step["uses"].(string))
if err != nil {
return errors.Wrap(err, "couldn't parse the value in the 'uses' field")
}

// There should only be one match
ext.Creator = res[0][1]
ext.Name = res[0][2]
ext.Version = res[0][3]
logrus.Debug(ext)

action.AddExternalAction(ext)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestParseUses(t *testing.T) {
t.Fatal(err)
}

assert.Len(t, action.Uses, 3)
assert.Len(t, action.Uses, 4)
}

func TestInvalidFiles(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/parser/testdata/uses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ runs:
uses: actions/[email protected]
with:
path: ./
- name: Local
id: cache-test
uses: ./.github/actions/test-action-dir
21 changes: 15 additions & 6 deletions pkg/types/external_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ import "fmt"

// ExternalAction represents a single external action that is used by the composite action.
type ExternalAction struct {
Creator string
Name string
Version string
StepName string
StepID string
Creator string
Name string
Version string
StepName string
StepID string
Local bool
LocalPath *string
}

func (e ExternalAction) GetLink() string {
return fmt.Sprintf("https://github.com/%s/%s/tree/%s", e.Creator, e.Name, e.Version)
switch e.Local {
case true:
return *e.LocalPath
case false:
return fmt.Sprintf("https://github.com/%s/%s/tree/%s", e.Creator, e.Name, e.Version)
default:
return ""
}
}

0 comments on commit 08d4ffd

Please sign in to comment.