Skip to content

Commit

Permalink
fix(settings): missing fields in api conversion and func renames (#1130)
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 authored May 16, 2024
1 parent 927dc71 commit 4a31c1e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
15 changes: 9 additions & 6 deletions api/admin/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
35 changes: 27 additions & 8 deletions api/types/settings/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package settings

import (
"fmt"

"github.com/urfave/cli/v2"
)

// Platform is the API representation of platform settingps.
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion api/types/settings/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions cmd/vela-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
})

Expand Down

0 comments on commit 4a31c1e

Please sign in to comment.