From 4a31c1e4ca8779a8298e488235b5a8a5929d5493 Mon Sep 17 00:00:00 2001 From: dave vader <48764154+plyr4@users.noreply.github.com> Date: Thu, 16 May 2024 10:17:32 -0500 Subject: [PATCH] fix(settings): missing fields in api conversion and func renames (#1130) --- api/admin/settings.go | 15 ++++++++----- api/types/settings/platform.go | 35 ++++++++++++++++++++++------- api/types/settings/platform_test.go | 2 +- cmd/vela-server/server.go | 10 ++------- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/api/admin/settings.go b/api/admin/settings.go index 0b067372b..f567c4263 100644 --- a/api/admin/settings.go +++ b/api/admin/settings.go @@ -119,7 +119,7 @@ func UpdateSettings(c *gin.Context) { // duplicate settings to not alter the shared pointer _s := new(settings.Platform) - _s.Update(s) + _s.FromSettings(s) // ensure we update the singleton record _s.SetID(1) @@ -254,18 +254,21 @@ func RestoreSettings(c *gin.Context) { return } - s.SetUpdatedAt(time.Now().UTC().Unix()) - s.SetUpdatedBy(u.GetName()) + // initialize a new settings record + _s := settings.FromCLIContext(cliCtx) + + _s.SetUpdatedAt(time.Now().UTC().Unix()) + _s.SetUpdatedBy(u.GetName()) // read in defaults supplied from the cli runtime compilerSettings := compiler.GetSettings() - s.SetCompiler(compilerSettings) + _s.SetCompiler(compilerSettings) queueSettings := queue.GetSettings() - s.SetQueue(queueSettings) + _s.SetQueue(queueSettings) // send API call to update the settings - s, err = database.FromContext(c).UpdateSettings(ctx, s) + s, err = database.FromContext(c).UpdateSettings(ctx, _s) if err != nil { retErr := fmt.Errorf("unable to update (restore) settings: %w", err) diff --git a/api/types/settings/platform.go b/api/types/settings/platform.go index b494c8faa..ff7c58ef3 100644 --- a/api/types/settings/platform.go +++ b/api/types/settings/platform.go @@ -4,6 +4,8 @@ package settings import ( "fmt" + + "github.com/urfave/cli/v2" ) // Platform is the API representation of platform settingps. @@ -20,6 +22,19 @@ type Platform struct { UpdatedBy *string `json:"updated_by,omitempty" yaml:"updated_by,omitempty"` } +// FromCLIContext returns a new Platform record from a cli context. +func FromCLIContext(c *cli.Context) *Platform { + ps := new(Platform) + + // set repos permitted to be added + ps.SetRepoAllowlist(c.StringSlice("vela-repo-allowlist")) + + // set repos permitted to use schedules + ps.SetScheduleAllowlist(c.StringSlice("vela-schedule-allowlist")) + + return ps +} + // GetID returns the ID field. // // When the provided Platform type is nil, or the field within @@ -228,21 +243,25 @@ func (ps *Platform) SetUpdatedBy(v string) { ps.UpdatedBy = &v } -// Update takes another settings record and updates the internal fields, intended -// to be used when the refreshing settings record shared across the server. -func (ps *Platform) Update(newSettingps *Platform) { +// FromSettings takes another settings record and updates the internal fields, +// used when the updating settings and refreshing the record shared across the server. +func (ps *Platform) FromSettings(_ps *Platform) { if ps == nil { return } - if newSettingps == nil { + if _ps == nil { return } - ps.SetCompiler(newSettingps.GetCompiler()) - ps.SetQueue(newSettingps.GetQueue()) - ps.SetRepoAllowlist(newSettingps.GetRepoAllowlist()) - ps.SetScheduleAllowlist(newSettingps.GetScheduleAllowlist()) + ps.SetCompiler(_ps.GetCompiler()) + ps.SetQueue(_ps.GetQueue()) + ps.SetRepoAllowlist(_ps.GetRepoAllowlist()) + ps.SetScheduleAllowlist(_ps.GetScheduleAllowlist()) + + ps.SetCreatedAt(_ps.GetCreatedAt()) + ps.SetUpdatedAt(_ps.GetUpdatedAt()) + ps.SetUpdatedBy(_ps.GetUpdatedBy()) } // String implements the Stringer interface for the Platform type. diff --git a/api/types/settings/platform_test.go b/api/types/settings/platform_test.go index 56c9cec7b..059c7b574 100644 --- a/api/types/settings/platform_test.go +++ b/api/types/settings/platform_test.go @@ -121,7 +121,7 @@ func TestTypes_Platform_Update(t *testing.T) { // run tests for _, test := range tests { - test.platform.Update(test.want) + test.platform.FromSettings(test.want) if diff := cmp.Diff(test.want, test.platform); diff != "" { t.Errorf("(Update: -want +got):\n%s", diff) diff --git a/cmd/vela-server/server.go b/cmd/vela-server/server.go index bae9ec326..dd5b6d6c6 100644 --- a/cmd/vela-server/server.go +++ b/cmd/vela-server/server.go @@ -119,7 +119,7 @@ func server(c *cli.Context) error { logrus.Info("creating initial platform settings") // create initial settings record - ps = new(settings.Platform) + ps = settings.FromCLIContext(c) // singleton record ID should always be 1 ps.SetID(1) @@ -135,12 +135,6 @@ func server(c *cli.Context) error { queueSettings := queue.GetSettings() ps.SetQueue(queueSettings) - // set repos permitted to be added - ps.SetRepoAllowlist(c.StringSlice("vela-repo-allowlist")) - - // set repos permitted to use schedules - ps.SetScheduleAllowlist(c.StringSlice("vela-schedule-allowlist")) - // create the settings record in the database _, err = database.CreateSettings(context.Background(), ps) if err != nil { @@ -252,7 +246,7 @@ func server(c *cli.Context) error { } // update the internal fields for the shared settings record - ps.Update(newSettings) + ps.FromSettings(newSettings) } })