Skip to content

Commit

Permalink
fix: allow events yaml unmarshal (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 authored Apr 17, 2024
1 parent c5cba4e commit fb4a95c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
25 changes: 25 additions & 0 deletions library/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
package library

import (
"errors"
"fmt"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library/actions"
"github.com/go-vela/types/raw"
)

// Events is the library representation of the various events that generate a
Expand All @@ -19,6 +21,29 @@ type Events struct {
Schedule *actions.Schedule `json:"schedule"`
}

// UnmarshalYAML implements the Unmarshaler interface for the Events type.
func (e *Events) UnmarshalYAML(unmarshal func(interface{}) error) error {
// string slice we try unmarshalling to
stringSlice := new(raw.StringSlice)

// attempt to unmarshal as a string slice type
err := unmarshal(stringSlice)
if err == nil {
// create new events from string slice
evs, err := NewEventsFromSlice(*stringSlice)
if err != nil {
return err
}

// overwrite existing Events
*e = *evs

return nil
}

return errors.New("failed to unmarshal Events")
}

// NewEventsFromMask is an instatiation function for the Events type that
// takes in an event mask integer value and populates the nested Events struct.
func NewEventsFromMask(mask int64) *Events {
Expand Down
21 changes: 20 additions & 1 deletion library/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,31 @@ type Repo struct {
Private *bool `json:"private,omitempty"`
Trusted *bool `json:"trusted,omitempty"`
Active *bool `json:"active,omitempty"`
AllowEvents *Events `json:"allow_events,omitempty"`
AllowEvents *Events `json:"allow_events,omitempty" yaml:"allow_events"`
PipelineType *string `json:"pipeline_type,omitempty"`
PreviousName *string `json:"previous_name,omitempty"`
ApproveBuild *string `json:"approve_build,omitempty"`
}

// UnmarshalYAML implements the Unmarshaler interface for the Repo type.
// This allows custom fields in the Repo type to be read from a YAML file, like AllowEvents.
func (r *Repo) UnmarshalYAML(unmarshal func(interface{}) error) error {
// create an alias to perform a normal unmarshal and avoid an infinite loop
type jsonRepo Repo

tmp := &jsonRepo{}

err := unmarshal(tmp)
if err != nil {
return err
}

// overwrite existing Repo
*r = Repo(*tmp)

return nil
}

// Environment returns a list of environment variables
// provided from the fields of the Repo type.
func (r *Repo) Environment() map[string]string {
Expand Down
21 changes: 20 additions & 1 deletion library/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Secret struct {
Value *string `json:"value,omitempty"`
Type *string `json:"type,omitempty"`
Images *[]string `json:"images,omitempty"`
AllowEvents *Events `json:"allow_events,omitempty"`
AllowEvents *Events `json:"allow_events,omitempty" yaml:"allow_events"`
AllowCommand *bool `json:"allow_command,omitempty"`
AllowSubstitution *bool `json:"allow_substitution,omitempty"`
CreatedAt *int64 `json:"created_at,omitempty"`
Expand All @@ -31,6 +31,25 @@ type Secret struct {
UpdatedBy *string `json:"updated_by,omitempty"`
}

// UnmarshalYAML implements the Unmarshaler interface for the Secret type.
// This allows custom fields in the Secret type to be read from a YAML file, like AllowEvents.
func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
// create an alias to perform a normal unmarshal and avoid an infinite loop
type jsonSecret Secret

tmp := &jsonSecret{}

err := unmarshal(tmp)
if err != nil {
return err
}

// overwrite existing secret
*s = Secret(*tmp)

return nil
}

// Sanitize creates a duplicate of the Secret without the value.
func (s *Secret) Sanitize() *Secret {
// create a variable since constants can not be addressable
Expand Down

0 comments on commit fb4a95c

Please sign in to comment.