Implement UnmarshalYAML for ActionRunsUsing (#223)

In #222 I added case normalisation to ReadAction() to ensure that Docker and docker are interpreted the same way.

I realised that this was being done at the wrong level and required multiple type conversions. By implementing `func (a ActionRunsUsing) UnmarshalYAML` we can lowercase the string as it's being unserialized

This has an added benefit that any time this type is hydrated the `runs.using` key will be lowercased, rather than relying on `ReadAction()`
This commit is contained in:
Michael Heap 2020-05-04 13:40:11 +01:00 committed by GitHub
parent 6196436f70
commit 6d6ea7ac04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View file

@ -1,6 +1,7 @@
package model package model
import ( import (
"fmt"
"io" "io"
"strings" "strings"
@ -10,6 +11,26 @@ import (
// ActionRunsUsing is the type of runner for the action // ActionRunsUsing is the type of runner for the action
type ActionRunsUsing string type ActionRunsUsing string
func (a *ActionRunsUsing) UnmarshalYAML(unmarshal func(interface{}) error) error {
var using string
if err := unmarshal(&using); err != nil {
return err
}
// Force input to lowercase for case insensitive comparison
format := ActionRunsUsing(strings.ToLower(using))
switch format {
case ActionRunsUsingNode12, ActionRunsUsingDocker:
*a = format
default:
return fmt.Errorf(fmt.Sprintf("The runs.using key in action.yml must be one of: %v, got %s", []string{
ActionRunsUsingDocker,
ActionRunsUsingNode12,
}, format))
}
return nil
}
const ( const (
// ActionRunsUsingNode12 for running with node12 // ActionRunsUsingNode12 for running with node12
ActionRunsUsingNode12 = "node12" ActionRunsUsingNode12 = "node12"
@ -54,9 +75,5 @@ type Output struct {
func ReadAction(in io.Reader) (*Action, error) { func ReadAction(in io.Reader) (*Action, error) {
a := new(Action) a := new(Action)
err := yaml.NewDecoder(in).Decode(a) err := yaml.NewDecoder(in).Decode(a)
// Normalise Runs.Using to lowercase so that Docker and docker are
// equivalent when evaluating a step context
a.Runs.Using = ActionRunsUsing(strings.ToLower(string(a.Runs.Using)))
return a, err return a, err
} }

View file

@ -307,7 +307,7 @@ func (sc *StepContext) runAction(actionDir string, actionPath string) common.Exe
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers), stepContainer.Remove().IfBool(!rc.Config.ReuseContainers),
)(ctx) )(ctx)
default: default:
return fmt.Errorf(fmt.Sprintf("The runs.using key in action.yml must be one of: %v, got %s", []string{ return fmt.Errorf(fmt.Sprintf("The runs.using key must be one of: %v, got %s", []string{
model.ActionRunsUsingDocker, model.ActionRunsUsingDocker,
model.ActionRunsUsingNode12, model.ActionRunsUsingNode12,
}, action.Runs.Using)) }, action.Runs.Using))