Skip to content

Commit

Permalink
fix: ensure branch has a default value set (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Apr 22, 2024
1 parent 9b68258 commit 2f1564f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
47 changes: 47 additions & 0 deletions cmd/wp-git-clone/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/thegeeklab/wp-git-clone/plugin"
wp "github.com/thegeeklab/wp-plugin-go/plugin"
)

func Test_pluginOptions(t *testing.T) {
tests := []struct {
name string
envs map[string]string
want string
}{
{
name: "ensure default branch is set",
envs: map[string]string{
"CI_COMMIT_BRANCH": "",
},
want: "main",
},
}

for _, tt := range tests {
for key, value := range tt.envs {
t.Setenv(key, value)
}

settings := &plugin.Settings{}
options := wp.Options{
Name: "wp-git-clone",
Flags: settingsFlags(settings, wp.FlagsPluginCategory),
Execute: func(_ context.Context) error { return nil },
}

got := plugin.New(options, settings)

_ = got.App.Run([]string{"wp-git-clone"})
_ = got.Validate()
_ = got.FlagsFromContext()

assert.EqualValues(t, tt.want, got.Settings.Repo.Branch)
}
}
1 change: 0 additions & 1 deletion cmd/wp-git-clone/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Name: "branch",
Usage: "change branch name",
EnvVars: []string{"PLUGIN_BRANCH", "CI_COMMIT_BRANCH", "CI_REPO_DEFAULT_BRANCH"},
Value: "main",
Destination: &settings.Repo.Branch,
Category: category,
},
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
github.com/cenkalti/backoff/v4 v4.3.0
github.com/rs/zerolog v1.32.0
github.com/stretchr/testify v1.9.0
github.com/thegeeklab/wp-plugin-go v1.7.1
github.com/urfave/cli/v2 v2.27.1
golang.org/x/sys v0.19.0
Expand All @@ -15,6 +16,7 @@ require (
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.11 // indirect
Expand All @@ -23,10 +25,12 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
Expand Down
6 changes: 6 additions & 0 deletions plugin/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func (p *Plugin) run(ctx context.Context) error {

// Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error {
// This default cannot be set in the cli flag, as the CI_* environment variables
// can be set empty, resulting in an empty default value.
if p.Settings.Repo.Branch == "" {
p.Settings.Repo.Branch = "main"
}

if p.Settings.WorkDir == "" {
var err error
if p.Settings.WorkDir, err = os.Getwd(); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ type Settings struct {
func New(options wp.Options, settings *Settings) *Plugin {
p := &Plugin{}

options.Execute = p.run
if options.Execute == nil {
options.Execute = p.run
}

p.Plugin = wp.New(options)
p.Settings = settings
Expand Down

0 comments on commit 2f1564f

Please sign in to comment.