Skip to content

Commit

Permalink
feat: add external action parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
matty-rose committed Oct 24, 2021
1 parent 20cab33 commit e75ab78
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 17 deletions.
46 changes: 46 additions & 0 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"
"regexp"

"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
Expand Down Expand Up @@ -56,6 +57,10 @@ func Parse(filename string) (*types.CompositeAction, error) {
return nil, err
}

if err := parseExternalActions(&action, data); err != nil {
return nil, err
}

return &action, nil
}

Expand Down Expand Up @@ -101,3 +106,44 @@ func parseOutputs(action *types.CompositeAction, data map[interface{}]interface{

return nil
}

func parseExternalActions(action *types.CompositeAction, data map[interface{}]interface{}) error {
runs, ok := data["runs"].(map[string]interface{})
if !ok {
logrus.Debug("no runs found")
}

steps, ok := runs["steps"].([]interface{})
if !ok {
logrus.Debug("no steps found")
}

for _, s := range steps {
step, ok := s.(map[string]interface{})
if !ok {
return errors.New("step does not have a valid structure")
}

var ext types.ExternalAction

if stepName, ok := step["name"].(string); ok {
ext.StepName = stepName
}

if stepID, ok := step["id"].(string); ok {
ext.StepID = stepID
}

regex := *regexp.MustCompile(`(.+)\/(.+)@(.+)`)
res := regex.FindAllStringSubmatch(step["uses"].(string), -1)

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

action.AddExternalAction(ext)
}

return nil
}
25 changes: 18 additions & 7 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,46 @@ import (
)

func TestParseNameDescription(t *testing.T) {
assertion := assert.New(t)
t.Parallel()

action, err := parser.Parse("./testdata/name_description.yaml")
if err != nil {
t.Fatal(err)
}

assertion.Equal("test", action.Name)
assertion.Equal("test", action.Description)
assert.Equal(t, "test", action.Name)
assert.Equal(t, "test", action.Description)
}

func TestParseInputs(t *testing.T) {
assertion := assert.New(t)
t.Parallel()

action, err := parser.Parse("./testdata/inputs.yaml")
if err != nil {
t.Fatal(err)
}

assertion.Len(action.Inputs, 2)
assert.Len(t, action.Inputs, 2)
}

func TestParseOutputs(t *testing.T) {
assertion := assert.New(t)
t.Parallel()

action, err := parser.Parse("./testdata/outputs.yaml")
if err != nil {
t.Fatal(err)
}

assertion.Len(action.Outputs, 1)
assert.Len(t, action.Outputs, 1)
}

func TestParseUses(t *testing.T) {
t.Parallel()

action, err := parser.Parse("./testdata/uses.yaml")
if err != nil {
t.Fatal(err)
}

assert.Len(t, action.Uses, 3)
}
18 changes: 18 additions & 0 deletions pkg/parser/testdata/uses.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "test"
description: "test"

runs:
using: "composite"
steps:
- name: Set up python
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Set up pip cache
uses: actions/cache@v2
with:
path: ./
- name: Cache test
uses: actions/[email protected]
with:
path: ./
14 changes: 9 additions & 5 deletions pkg/types/composite_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ package types

// CompositeAction represents a single composite action.
type CompositeAction struct {
Name string
Description string
Inputs []Input
Outputs []Output
ThirdPartyActions []ThirdPartyAction
Name string
Description string
Inputs []Input
Outputs []Output
Uses []ExternalAction
}

func (c *CompositeAction) SetName(name string) {
Expand All @@ -45,3 +45,7 @@ func (c *CompositeAction) AddInput(input Input) {
func (c *CompositeAction) AddOutput(output Output) {
c.Outputs = append(c.Outputs, output)
}

func (c *CompositeAction) AddExternalAction(e ExternalAction) {
c.Uses = append(c.Uses, e)
}
12 changes: 7 additions & 5 deletions pkg/types/third_party_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ THE SOFTWARE.
*/
package types

// ThirdPartyAction represents a single third party action that is used within the composite action.
type ThirdPartyAction struct {
Creator string
Name string
Version string
// ExternalAction represents a single external action that is used by the composite action.
type ExternalAction struct {
StepName string
StepID string
Creator string
Name string
Version string
}

0 comments on commit e75ab78

Please sign in to comment.