Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate that repo name is not empty #426

Merged
merged 6 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,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=
Expand Down
7 changes: 7 additions & 0 deletions pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,13 @@ 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
slc := util.CleanSliceWhiteSpaces(strings.Split(orgRepo, "/"))
if len(slc) < 2 {
return "", errors.New("repo name can't be empty")
}

return provider.CreateRepository(ctx, orgRepo)
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] != "" {
danielm-codefresh marked this conversation as resolved.
Show resolved Hide resolved
res = append(res, slc[i])
}
}

return res
}
29 changes: 29 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}