diff --git a/flytectl/cmd/register/examples.go b/flytectl/cmd/register/examples.go index c27ca674091..d19641fd852 100644 --- a/flytectl/cmd/register/examples.go +++ b/flytectl/cmd/register/examples.go @@ -6,7 +6,7 @@ import ( "github.com/flyteorg/flytestdlib/logger" - "github.com/google/go-github/github" + "github.com/google/go-github/v37/github" rconfig "github.com/flyteorg/flytectl/cmd/config/subcommand/register" cmdCore "github.com/flyteorg/flytectl/cmd/core" @@ -31,24 +31,23 @@ Usage ) var ( - githubOrg = "flyteorg" - flytesnacksRepository = "flytesnacks" + flytesnacks = "flytesnacks" ) func registerExamplesFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error { - var examples []github.ReleaseAsset + var examples []*github.ReleaseAsset // Deprecated checks for --k8Service deprecatedCheck(ctx, &rconfig.DefaultFilesConfig.K8sServiceAccount, rconfig.DefaultFilesConfig.K8ServiceAccount) - examples, tag, err := getAllFlytesnacksExample(githubOrg, flytesnacksRepository, rconfig.DefaultFilesConfig.Version) + examples, tag, err := getAllExample(flytesnacks, rconfig.DefaultFilesConfig.Version) if err != nil { return err } - logger.Infof(ctx, "Register started for %s %s release https://github.com/%s/%s/releases/tag/%s", flytesnacksRepository, tag, githubOrg, flytesnacksRepository, tag) + logger.Infof(ctx, "Register started for %s %s release https://github.com/flyteorg/%s/releases/tag/%s", flytesnacks, tag, flytesnacks, tag) rconfig.DefaultFilesConfig.Archive = true - rconfig.DefaultFilesConfig.Version = tag + rconfig.DefaultFilesConfig.Version = *tag.TagName for _, v := range examples { args := []string{ *v.BrowserDownloadURL, diff --git a/flytectl/cmd/register/examples_test.go b/flytectl/cmd/register/examples_test.go index 5b38de8196e..f405d44284c 100644 --- a/flytectl/cmd/register/examples_test.go +++ b/flytectl/cmd/register/examples_test.go @@ -16,11 +16,11 @@ func TestRegisterExamplesFunc(t *testing.T) { func TestRegisterExamplesFuncErr(t *testing.T) { setup() registerFilesSetup() - flytesnacksRepository = "testingsnacks" + flytesnacks = "testingsnacks" args = []string{""} err := registerExamplesFunc(ctx, args, cmdCtx) // TODO (Yuvraj) make test to success after fixing flytesnacks bug assert.NotNil(t, err) - flytesnacksRepository = "flytesnacks" + flytesnacks = "flytesnacks" } diff --git a/flytectl/cmd/register/register_util.go b/flytectl/cmd/register/register_util.go index d8b11c8d085..30f2ae8350c 100644 --- a/flytectl/cmd/register/register_util.go +++ b/flytectl/cmd/register/register_util.go @@ -15,13 +15,13 @@ import ( "sort" "strings" + "github.com/flyteorg/flytectl/pkg/util/githubutil" + "github.com/flyteorg/flytestdlib/contextutils" "github.com/flyteorg/flytestdlib/promutils" "github.com/flyteorg/flytestdlib/promutils/labeled" "github.com/flyteorg/flytestdlib/utils" - "github.com/google/go-github/github" - "github.com/flyteorg/flytectl/cmd/config" rconfig "github.com/flyteorg/flytectl/cmd/config/subcommand/register" cmdCore "github.com/flyteorg/flytectl/cmd/core" @@ -30,6 +30,7 @@ import ( "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core" "github.com/flyteorg/flytestdlib/logger" "github.com/flyteorg/flytestdlib/storage" + "github.com/google/go-github/v37/github" "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/proto" @@ -541,8 +542,8 @@ func getJSONSpec(message proto.Message) string { return jsonSpec } -func filterExampleFromRelease(releases github.RepositoryRelease) []github.ReleaseAsset { - var assets []github.ReleaseAsset +func filterExampleFromRelease(releases *github.RepositoryRelease) []*github.ReleaseAsset { + var assets []*github.ReleaseAsset for _, v := range releases.Assets { isValid, _ := checkSupportedExtensionForCompress(*v.Name) if isValid { @@ -552,24 +553,27 @@ func filterExampleFromRelease(releases github.RepositoryRelease) []github.Releas return assets } -func getAllFlytesnacksExample(org, repository, version string) ([]github.ReleaseAsset, string, error) { - c := github.NewClient(nil) - opt := &github.ListOptions{Page: 1, PerPage: 1} +func getAllExample(repository, version string) ([]*github.ReleaseAsset, *github.RepositoryRelease, error) { if len(version) > 0 { - releases, _, err := c.Repositories.GetReleaseByTag(context.Background(), org, repository, version) + release, err := githubutil.CheckVersionExist(version, repository) if err != nil { - return nil, "", err + return nil, nil, err } - return filterExampleFromRelease(*releases), version, nil + return filterExampleFromRelease(release), release, nil } - releases, _, err := c.Repositories.ListReleases(context.Background(), org, repository, opt) + releases, err := githubutil.GetListRelease(repository) if err != nil { - return nil, "", err + return nil, nil, err } if len(releases) == 0 { - return nil, "", fmt.Errorf("repository doesn't have any release") + return nil, nil, fmt.Errorf("repository doesn't have any release") + } + for _, v := range releases { + if !*v.Prerelease { + return filterExampleFromRelease(v), v, nil + } } - return filterExampleFromRelease(*releases[0]), *releases[0].TagName, nil + return nil, nil, nil } diff --git a/flytectl/cmd/register/register_util_test.go b/flytectl/cmd/register/register_util_test.go index f3af72c5ff7..a7114952939 100644 --- a/flytectl/cmd/register/register_util_test.go +++ b/flytectl/cmd/register/register_util_test.go @@ -456,19 +456,17 @@ func TestGetStorageClient(t *testing.T) { func TestGetAllFlytesnacksExample(t *testing.T) { t.Run("Failed to get manifest with wrong name", func(t *testing.T) { - _, tag, err := getAllFlytesnacksExample("no////ne", "no////ne", "") + _, _, err := getAllExample("no////ne", "") assert.NotNil(t, err) - assert.Equal(t, len(tag), 0) }) t.Run("Failed to get release", func(t *testing.T) { - _, tag, err := getAllFlytesnacksExample("flyteorg", "homebrew-tap", "") + _, _, err := getAllExample("homebrew-tap", "") assert.NotNil(t, err) - assert.Equal(t, len(tag), 0) }) t.Run("Successfully get examples", func(t *testing.T) { - assets, tag, err := getAllFlytesnacksExample("flyteorg", "flytesnacks", "v0.2.175") + assets, r, err := getAllExample("flytesnacks", "v0.2.175") assert.Nil(t, err) - assert.Greater(t, len(tag), 0) + assert.Greater(t, len(*r.TagName), 0) assert.Greater(t, len(assets), 0) }) } diff --git a/flytectl/pkg/util/githubutil/githubutil.go b/flytectl/pkg/util/githubutil/githubutil.go index ba51c160db9..109b6daec1d 100644 --- a/flytectl/pkg/util/githubutil/githubutil.go +++ b/flytectl/pkg/util/githubutil/githubutil.go @@ -62,6 +62,18 @@ func GetLatestVersion(repository string) (*github.RepositoryRelease, error) { return release, err } +// GetListRelease returns the list of release of provided repository +func GetListRelease(repository string) ([]*github.RepositoryRelease, error) { + client := GetGHClient() + releases, _, err := client.Repositories.ListReleases(context.Background(), owner, repository, &github.ListOptions{ + PerPage: 100, + }) + if err != nil { + return nil, err + } + return releases, err +} + func getFlytectlAssetName() string { if arch == platformutil.ArchAmd64 { arch = platformutil.ArchX86