Skip to content

Commit

Permalink
Fixing as per @Armels comments
Browse files Browse the repository at this point in the history
Signed-off-by: Mohammed Zeeshan Ahmed <mohammed.zee1000@gmail.com>
  • Loading branch information
mohammedzee1000 committed May 5, 2022
1 parent 05a63ad commit 28d9787
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
6 changes: 5 additions & 1 deletion pkg/init/backend/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ func (o *FlagsBackend) Validate(flags map[string]string, fs filesystem.Filesyste
}
registries := o.preferenceClient.RegistryList()
for _, r := range *registries {
if r.Name == flags[FLAG_DEVFILE_REGISTRY] && util.IsGithubBasedRegistry(r.URL) {
isGithubRegistry, err := util.IsGithubBasedRegistry(r.URL)
if err != nil {
return err
}
if r.Name == flags[FLAG_DEVFILE_REGISTRY] && isGithubRegistry {
return util.ErrGithubRegistryNotSupported
}
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/odo/cli/preference/registry/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ func (o *AddOptions) Validate() (err error) {
if err != nil {
return err
}
if util2.IsGithubBasedRegistry(o.registryURL) {
isGithubRegistry, err := util2.IsGithubBasedRegistry(o.registryURL)
if err != nil {
return err
}
if isGithubRegistry {
return util2.ErrGithubRegistryNotSupported
}
return nil
Expand Down
6 changes: 5 additions & 1 deletion pkg/odo/cli/preference/registry/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ func (o *UpdateOptions) Validate() (err error) {
if err != nil {
return err
}
if registryUtil.IsGithubBasedRegistry(o.registryURL) {
isGithubBasedRegistry, err := registryUtil.IsGithubBasedRegistry(o.registryURL)
if err != nil {
return err
}
if isGithubBasedRegistry {
return registryUtil.ErrGithubRegistryNotSupported
}
return nil
Expand Down
10 changes: 8 additions & 2 deletions pkg/odo/cli/preference/registry/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
// odo packages

"errors"
"fmt"
"strings"

"github.com/redhat-developer/odo/pkg/preference"
url2 "net/url"
)

const (
Expand All @@ -30,6 +32,10 @@ func IsSecure(prefClient preference.Client, registryName string) bool {
return isSecure
}

func IsGithubBasedRegistry(url string) bool {
return strings.Contains(url, "github.com") || strings.Contains(url, "raw.githubusercontent.com")
func IsGithubBasedRegistry(url string) (bool, error) {
pu, err := url2.Parse(url)
if err != nil {
return false, fmt.Errorf("unable to parse registry url %w", err)
}
return strings.Contains(pu.Host, "github.com") || strings.Contains(url, "raw.githubusercontent.com"), nil
}
11 changes: 10 additions & 1 deletion pkg/odo/cli/preference/registry/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,20 @@ func TestIsGitBasedRegistry(t *testing.T) {
registryURL: "https://raw.githubusercontent.com/odo-devfiles/registry",
want: true,
},
{
name: "Case 4: Returns false if URL contains github.com in a non-host position",
registryURL: "https://my.registry.example.com/github.com",
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if actual := IsGithubBasedRegistry(tt.registryURL); actual != tt.want {
actual, err := IsGithubBasedRegistry(tt.registryURL)
if err != nil {
t.Errorf("unexpected error %s occoured", err.Error())
}
if actual != tt.want {
t.Errorf("failed checking if registry is git based, got %t want %t", actual, tt.want)
}
})
Expand Down
6 changes: 5 additions & 1 deletion pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ func (o RegistryClient) ListDevfileStacks(registryName string) (DevfileStackList

// getRegistryStacks retrieves the registry's index devfile stack entries
func getRegistryStacks(preferenceClient preference.Client, registry Registry) ([]DevfileStack, error) {
if registryUtil.IsGithubBasedRegistry(registry.URL) {
isGithubregistry, err := registryUtil.IsGithubBasedRegistry(registry.URL)
if err != nil {
return nil, err
}
if isGithubregistry {
return nil, registryUtil.ErrGithubRegistryNotSupported
}
// OCI-based registry
Expand Down

0 comments on commit 28d9787

Please sign in to comment.