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

Support to list tags and releases #99

Merged
merged 1 commit into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 46 additions & 7 deletions github/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/google/go-github/v29/github"
)

// ReleaseClient is the client of jcli github
// ReleaseClient is the client of GitHub
type ReleaseClient struct {
Client *github.Client
Org string
Expand All @@ -18,14 +18,20 @@ type ReleaseAsset struct {
Body string
}

// Init init the GitHub client
func (g *ReleaseClient) Init() {
g.Client = github.NewClient(nil)
// Release represents a GitHub release
type Release struct {
TagName string
ID int64
}

// GetLatestJCLIAsset returns the latest jcli asset
func (g *ReleaseClient) GetLatestJCLIAsset() (*ReleaseAsset, error) {
return g.GetLatestReleaseAsset(g.Org, g.Repo)
// Tag represents a tag of a git repository
type Tag struct {
Name string
}

// Init inits the GitHub client
func (g *ReleaseClient) Init() {
g.Client = github.NewClient(nil)
}

// GetLatestReleaseAsset returns the latest release asset
Expand All @@ -42,6 +48,39 @@ func (g *ReleaseClient) GetLatestReleaseAsset(owner, repo string) (ra *ReleaseAs
return
}

// GetReleaseList returns a list of release
func (g *ReleaseClient) GetReleaseList(owner, repo string, count int) (list []Release, err error) {
ctx := context.Background()

var releaseList []*github.RepositoryRelease
if releaseList, _, err = g.Client.Repositories.ListReleases(ctx, owner, repo, &github.ListOptions{PerPage: count}); err == nil {
for i := range releaseList {
release := releaseList[i]
list = append(list, Release{
TagName: *release.Name,
ID: *release.ID,
})
}
}
return
}

// GetTagList returns a list of tag
func (g *ReleaseClient) GetTagList(owner, repo string, count int) (list []Tag, err error) {
ctx := context.Background()

var tagList []*github.RepositoryTag
if tagList, _, err = g.Client.Repositories.ListTags(ctx, owner, repo, &github.ListOptions{PerPage: count}); err == nil {
for i := range tagList {
tag := tagList[i]
list = append(list, Tag{
Name: *tag.Name,
})
}
}
return
}

// GetJCLIAsset returns the asset from a tag name
func (g *ReleaseClient) GetJCLIAsset(tagName string) (*ReleaseAsset, error) {
return g.GetReleaseAssetByTagName(g.Org, g.Repo, tagName)
Expand Down
30 changes: 0 additions & 30 deletions github/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,6 @@ func TestGetLatestReleaseAsset(t *testing.T) {
assert.Equal(t, "body", asset.Body)
}

func TestGetLatestJCLIAsset(t *testing.T) {
client, teardown := jClient.PrepareForGetLatestJCLIAsset() //setup()
defer teardown()

ghClient := jClient.ReleaseClient{
Client: client,
}
asset, err := ghClient.GetLatestJCLIAsset()

assert.Nil(t, err)
assert.NotNil(t, asset)
assert.Equal(t, "tagName", asset.TagName)
assert.Equal(t, "body", asset.Body)
}

func TestGetJCLIAsset(t *testing.T) {
client, teardown := jClient.PrepareForGetJCLIAsset("tagName") //setup()
defer teardown()

ghClient := jClient.ReleaseClient{
Client: client,
}
asset, err := ghClient.GetJCLIAsset("tagName")

assert.Nil(t, err)
assert.NotNil(t, asset)
assert.Equal(t, "tagName", asset.TagName)
assert.Equal(t, "body", asset.Body)
}

func TestGetReleaseAssetByTagName(t *testing.T) {
client, teardown := jClient.PrepareForGetReleaseAssetByTagName() //setup()
defer teardown()
Expand Down