fix: add github.com as default for owner/repo parameter (#872)

* fix: add github.com as default for owner/repo parameter #780

* fix: use const to fix build error

* fix: nitpick fix and golangci-lint issue
This commit is contained in:
Nanik 2021-08-20 10:07:30 +10:00 committed by GitHub
parent c54d77b0d7
commit c73b28f13c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 4 deletions

View File

@ -56,12 +56,28 @@ func (r *RepoURL) String() string {
// Set parses a URL string into RepoURL struct.
func (r *RepoURL) Set(s string) error {
// Allow skipping scheme for ease-of-use, default to https.
if !strings.Contains(s, "://") {
s = "https://" + s
var t string
const two = 2
const three = 3
c := strings.Split(s, "/")
switch l := len(c); {
// This will takes care for repo/owner format.
// By default it will use github.com
case l == two:
t = "github.com/" + c[0] + "/" + c[1]
case l >= three:
t = s
}
u, e := url.Parse(s)
// Allow skipping scheme for ease-of-use, default to https.
if !strings.Contains(t, "://") {
t = "https://" + t
}
u, e := url.Parse(t)
if e != nil {
//nolint:wrapcheck
return sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("url.Parse: %v", e))

View File

@ -64,6 +64,26 @@ func TestRepoURL_ValidGitHubUrl(t *testing.T) {
args: args{s: "https://gitlab.com/foo/kubeflow"},
wantErr: true,
},
{
name: "github repository",
fields: fields{
Host: "github.com",
Owner: "foo",
Repo: "kubeflow",
},
args: args{s: "foo/kubeflow"},
wantErr: false,
},
{
name: "github repository",
fields: fields{
Host: "github.com",
Owner: "foo",
Repo: "kubeflow",
},
args: args{s: "https://github.com/foo/kubeflow"},
wantErr: false,
},
}
for _, tt := range tests {
tt := tt // Re-initializing variable so it is not changed while executing the closure below