From c5acda20848f5d265e644c4a0140c97b83db3c5d Mon Sep 17 00:00:00 2001 From: danielm-codefresh Date: Mon, 20 Feb 2023 11:54:13 +0200 Subject: [PATCH 1/6] make sure repo name is not empty --- go.mod | 3 ++- go.sum | 5 ++++- pkg/git/repository.go | 6 ++++++ pkg/git/repository_test.go | 11 +++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 4864e078..d4d11bd8 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( code.gitea.io/sdk/gitea v0.15.1 github.com/argoproj/argo-cd/v2 v2.5.9 github.com/argoproj/gitops-engine v0.7.1-0.20221004132320-98ccd3d43fd9 + github.com/beaconsoftwarellc/gadget v1.3.8 github.com/briandowns/spinner v1.18.1 github.com/ghodss/yaml v1.0.0 github.com/go-git/go-billy/v5 v5.3.1 @@ -96,7 +97,7 @@ require ( github.com/go-openapi/validate v0.19.8 // indirect github.com/go-redis/cache/v8 v8.4.2 // indirect github.com/go-redis/redis/v8 v8.11.3 // indirect - github.com/go-stack/stack v1.8.0 // indirect + github.com/go-stack/stack v1.8.1 // indirect github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0 // indirect github.com/gobwas/glob v0.2.3 // indirect github.com/gogits/go-gogs-client v0.0.0-20190616193657-5a05380e4bc2 // indirect diff --git a/go.sum b/go.sum index 3f14773a..5b87894d 100644 --- a/go.sum +++ b/go.sum @@ -182,6 +182,8 @@ github.com/aws/aws-sdk-go v1.33.16/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZve github.com/aws/aws-sdk-go v1.35.24/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k= github.com/aws/aws-sdk-go v1.38.49/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= +github.com/beaconsoftwarellc/gadget v1.3.8 h1:B9E4VyUeZhg6WwDhEkDOF07dQ9ds1J7oke/0o2kwbBo= +github.com/beaconsoftwarellc/gadget v1.3.8/go.mod h1:VjsazzjXTA2Aibxhh9b4oouC4/Qk0MQHyq89gO13/6s= github.com/beevik/ntp v0.2.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg= github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= @@ -473,8 +475,9 @@ github.com/go-redis/redis/v8 v8.11.3 h1:GCjoYp8c+yQTJfc0n69iwSiHjvuAdruxl7elnZCx github.com/go-redis/redis/v8 v8.11.3/go.mod h1:xNJ9xDG09FsIPwh3bWdk+0oDWHbtF9rPN0F/oD9XeKc= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= +github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0 h1:Mr3JcvBjQEhCN9wld6OHKHuHxWaoXTaQfYKmj7QwP18= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= diff --git a/pkg/git/repository.go b/pkg/git/repository.go index 0d4cecfe..f7c571af 100644 --- a/pkg/git/repository.go +++ b/pkg/git/repository.go @@ -15,6 +15,7 @@ import ( "github.com/argoproj-labs/argocd-autopilot/pkg/log" "github.com/argoproj-labs/argocd-autopilot/pkg/store" "github.com/argoproj-labs/argocd-autopilot/pkg/util" + "github.com/beaconsoftwarellc/gadget/stringutil" billy "github.com/go-git/go-billy/v5" gg "github.com/go-git/go-git/v5" @@ -505,6 +506,11 @@ var createRepo = func(ctx context.Context, opts *CloneOptions) (defaultBranch st } _, orgRepo, _, _, _, _, _ := util.ParseGitUrl(opts.Repo) + // It depends on the provider, but org repo strucure should at least contain org and repo name + if len(stringutil.CleanWhiteSpace(strings.Split(orgRepo, "/"))) < 2 { + return "", errors.New("repo name can't be empty") + } + return provider.CreateRepository(ctx, orgRepo) } diff --git a/pkg/git/repository_test.go b/pkg/git/repository_test.go index 76662618..29332180 100644 --- a/pkg/git/repository_test.go +++ b/pkg/git/repository_test.go @@ -1340,6 +1340,17 @@ func Test_createRepo(t *testing.T) { }, want: "main", }, + "Should fail if repo name is empty": { + opts: &CloneOptions{ + Repo: "https://github.com/owner/", + Provider: "github", + Auth: Auth{ + Username: "username", + Password: "password", + }, + }, + wantErr: "repo name can't be empty", + }, } orgGetProvider := getProvider From 156090cbb33fa4c1b2c0a112893b081901d3ad27 Mon Sep 17 00:00:00 2001 From: danielm-codefresh Date: Mon, 20 Feb 2023 12:33:10 +0200 Subject: [PATCH 2/6] create a util function to clean white spaces from slice --- go.mod | 1 - go.sum | 2 -- pkg/git/repository.go | 5 +++-- pkg/util/util.go | 11 +++++++++++ pkg/util/util_test.go | 29 +++++++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 pkg/util/util_test.go diff --git a/go.mod b/go.mod index d4d11bd8..1cda4a30 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( code.gitea.io/sdk/gitea v0.15.1 github.com/argoproj/argo-cd/v2 v2.5.9 github.com/argoproj/gitops-engine v0.7.1-0.20221004132320-98ccd3d43fd9 - github.com/beaconsoftwarellc/gadget v1.3.8 github.com/briandowns/spinner v1.18.1 github.com/ghodss/yaml v1.0.0 github.com/go-git/go-billy/v5 v5.3.1 diff --git a/go.sum b/go.sum index 5b87894d..ccb7c471 100644 --- a/go.sum +++ b/go.sum @@ -182,8 +182,6 @@ github.com/aws/aws-sdk-go v1.33.16/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZve github.com/aws/aws-sdk-go v1.35.24/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k= github.com/aws/aws-sdk-go v1.38.49/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= -github.com/beaconsoftwarellc/gadget v1.3.8 h1:B9E4VyUeZhg6WwDhEkDOF07dQ9ds1J7oke/0o2kwbBo= -github.com/beaconsoftwarellc/gadget v1.3.8/go.mod h1:VjsazzjXTA2Aibxhh9b4oouC4/Qk0MQHyq89gO13/6s= github.com/beevik/ntp v0.2.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg= github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= diff --git a/pkg/git/repository.go b/pkg/git/repository.go index f7c571af..e8a1cbb7 100644 --- a/pkg/git/repository.go +++ b/pkg/git/repository.go @@ -15,7 +15,6 @@ import ( "github.com/argoproj-labs/argocd-autopilot/pkg/log" "github.com/argoproj-labs/argocd-autopilot/pkg/store" "github.com/argoproj-labs/argocd-autopilot/pkg/util" - "github.com/beaconsoftwarellc/gadget/stringutil" billy "github.com/go-git/go-billy/v5" gg "github.com/go-git/go-git/v5" @@ -506,8 +505,10 @@ var createRepo = func(ctx context.Context, opts *CloneOptions) (defaultBranch st } _, orgRepo, _, _, _, _, _ := util.ParseGitUrl(opts.Repo) + // It depends on the provider, but org repo strucure should at least contain org and repo name - if len(stringutil.CleanWhiteSpace(strings.Split(orgRepo, "/"))) < 2 { + slc := util.CleanSliceWhiteSpaces(strings.Split(orgRepo, "/")) + if len(slc) < 2 { return "", errors.New("repo name can't be empty") } diff --git a/pkg/util/util.go b/pkg/util/util.go index 259f7cf5..d6db38f9 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -170,3 +170,14 @@ func StealFlags(cmd *cobra.Command, exceptFor []string) (*pflag.FlagSet, error) return fs, nil } + +func CleanSliceWhiteSpaces(slc []string) []string { + var res []string + for i := range slc { + if slc[i] != "" { + res = append(res, slc[i]) + } + } + + return res +} diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go new file mode 100644 index 00000000..a176132f --- /dev/null +++ b/pkg/util/util_test.go @@ -0,0 +1,29 @@ +package util + +import ( + "reflect" + "testing" +) + +func TestCleanSliceWhiteSpaces(t *testing.T) { + testcases := []struct { + input []string + expected []string + }{ + { + input: []string{"org", "repo"}, + expected: []string{"org", "repo"}, + }, + { + input: []string{"org", ""}, + expected: []string{"org"}, + }, + } + + for _, testcase := range testcases { + res := CleanSliceWhiteSpaces(testcase.input) + if !reflect.DeepEqual(res, testcase.expected) { + t.Errorf("slice expected to be %v, but got %v", testcase.expected, res) + } + } +} From 85cb74c463ee09250d1bed6153fef104624b6e28 Mon Sep 17 00:00:00 2001 From: danielm-codefresh Date: Mon, 20 Feb 2023 14:37:08 +0200 Subject: [PATCH 3/6] install libssl-dev --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a99b63ca..a91be661 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ RUN groupadd -g 999 autopilot && \ chown autopilot:0 /home/autopilot && \ chmod g=u /home/autopilot && \ apt-get update -y && \ - apt-get install -y git git-lfs tini gpg tzdata && \ + apt-get install -y libssl-dev git git-lfs tini gpg tzdata && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* From e4c705baa765aced37fdac1b765c1e6d636c8a6f Mon Sep 17 00:00:00 2001 From: danielm-codefresh Date: Mon, 20 Feb 2023 14:44:41 +0200 Subject: [PATCH 4/6] From 7774a6598653299d80530ba15ba99a01e0f46653 Mon Sep 17 00:00:00 2001 From: danielm-codefresh Date: Mon, 20 Feb 2023 15:11:38 +0200 Subject: [PATCH 5/6] fix --- pkg/util/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/util/util.go b/pkg/util/util.go index d6db38f9..99080da6 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -174,7 +174,7 @@ func StealFlags(cmd *cobra.Command, exceptFor []string) (*pflag.FlagSet, error) func CleanSliceWhiteSpaces(slc []string) []string { var res []string for i := range slc { - if slc[i] != "" { + if strings.TrimSpace(slc[i]) != "" { res = append(res, slc[i]) } } From 5f68955e2719e93f88d23d2c88f325c975a461ed Mon Sep 17 00:00:00 2001 From: danielm-codefresh Date: Mon, 20 Feb 2023 15:15:13 +0200 Subject: [PATCH 6/6] fix --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a91be661..2bfc11e7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,8 @@ RUN groupadd -g 999 autopilot && \ chown autopilot:0 /home/autopilot && \ chmod g=u /home/autopilot && \ apt-get update -y && \ - apt-get install -y libssl-dev git git-lfs tini gpg tzdata && \ + apt-get upgrade -y && \ + apt-get install -y git git-lfs tini gpg tzdata && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*