Skip to content

Commit

Permalink
Migrated github methods to githubutil (flyteorg#268)
Browse files Browse the repository at this point in the history
Signed-off-by: Yuvraj <[email protected]>
  • Loading branch information
yindia authored Jan 23, 2022
1 parent 1477c4d commit efee92d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
13 changes: 6 additions & 7 deletions cmd/register/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions cmd/register/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
32 changes: 18 additions & 14 deletions cmd/register/register_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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

}

Expand Down
10 changes: 4 additions & 6 deletions cmd/register/register_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/githubutil/githubutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit efee92d

Please sign in to comment.