From fb4a95c30338777b10416ff3c6f1ed249eb8e9b7 Mon Sep 17 00:00:00 2001 From: dave vader <48764154+plyr4@users.noreply.github.com> Date: Wed, 17 Apr 2024 08:50:26 -0500 Subject: [PATCH] fix: allow events yaml unmarshal (#370) --- library/events.go | 25 +++++++++++++++++++++++++ library/repo.go | 21 ++++++++++++++++++++- library/secret.go | 21 ++++++++++++++++++++- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/library/events.go b/library/events.go index 655b1b49..48351517 100644 --- a/library/events.go +++ b/library/events.go @@ -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 @@ -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 { diff --git a/library/repo.go b/library/repo.go index 8755d524..46c87df7 100644 --- a/library/repo.go +++ b/library/repo.go @@ -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 { diff --git a/library/secret.go b/library/secret.go index 57aa3e84..099efb55 100644 --- a/library/secret.go +++ b/library/secret.go @@ -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"` @@ -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