Skip to content

Commit

Permalink
Retrieve all tags instead of releases
Browse files Browse the repository at this point in the history
some tags have releases
  • Loading branch information
dtan4 committed Jan 2, 2019
1 parent a32615c commit ee0c62c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 41 deletions.
61 changes: 38 additions & 23 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ type Client struct {

type Release struct {
Name string
Tag string
CreatedAt time.Time
}

type Tag struct {
Name string
Release *Release
}

// NewClient creates new Client object
func NewClient(httpClient *http.Client) *Client {
return &Client{
Expand Down Expand Up @@ -69,26 +73,26 @@ func (c *Client) GetTagCommit(owner, repo, tag string) (*github.RepositoryCommit
return commit, nil
}

// ListReleasesNew retrieves all tags and releases of the given repository
func (c *Client) ListReleasesNew(owner, repo string) ([]*Release, error) {
releases, err := c.ListReleases(owner, repo)
// ListTagsNew retrieves all tags and releases of the given repository
func (c *Client) ListTagsNew(owner, repo string) ([]*Tag, error) {
tags, err := c.ListTags(owner, repo)
if err != nil {
return []*Release{}, err
return []*Tag{}, err
}

releasesMap := MakeReleasesMap(releases)

tags, err := c.ListTags(owner, repo)
releases, err := c.ListReleases(owner, repo)
if err != nil {
return []*Release{}, err
return []*Tag{}, err
}

rs := []*Release{}
releasesMap := MakeReleasesMap(releases)

for _, tag := range tags {
var release *Release
ts := []*Tag{}

if r, ok := releasesMap[*tag.Name]; ok {
for _, t := range tags {
var tag *Tag

if r, ok := releasesMap[*t.Name]; ok {
var name string

if r.Name == nil {
Expand All @@ -97,23 +101,34 @@ func (c *Client) ListReleasesNew(owner, repo string) ([]*Release, error) {
name = *r.Name
}

t := *r.CreatedAt

release = &Release{
Name: name,
Tag: *tag.Name,
CreatedAt: time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location()),
createdAt := *r.CreatedAt

tag = &Tag{
Name: *t.Name,
Release: &Release{
Name: name,
CreatedAt: time.Date(
createdAt.Year(),
createdAt.Month(),
createdAt.Day(),
createdAt.Hour(),
createdAt.Minute(),
createdAt.Second(),
createdAt.Nanosecond(),
createdAt.Location(),
),
},
}
} else {
release = &Release{
Tag: *tag.Name,
tag = &Tag{
Name: *t.Name,
}
}

rs = append(rs, release)
ts = append(ts, tag)
}

return rs, nil
return ts, nil
}

// ListReleases lists all releases of the given repository
Expand Down
40 changes: 22 additions & 18 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,43 +83,47 @@ func TestGetTagCommit(t *testing.T) {
}
}

func TestListReleasesNew(t *testing.T) {
func TestListTagsNew(t *testing.T) {
c := &Client{
repositories: fakeRepositoriesService{},
}

owner := "owner"
repo := "repo"

want := []*Release{
&Release{
Name: "",
Tag: "v1.13.2-beta.1",
want := []*Tag{
&Tag{
Name: "v1.13.2-beta.1",
Release: nil,
},
&Release{
Name: "",
Tag: "v1.13.2-beta.0",
CreatedAt: time.Date(2018, 12, 14, 0, 30, 24, 0, time.UTC),
&Tag{
Name: "v1.13.2-beta.0",
Release: &Release{
Name: "",
CreatedAt: time.Date(2018, 12, 14, 0, 30, 24, 0, time.UTC),
},
},
&Release{
Name: "v1.13.1",
Tag: "v1.13.1",
CreatedAt: time.Date(2018, 12, 13, 0, 30, 24, 0, time.UTC),
&Tag{
Name: "v1.13.1",
Release: &Release{
Name: "v1.13.1",
CreatedAt: time.Date(2018, 12, 13, 0, 30, 24, 0, time.UTC),
},
},
}

got, err := c.ListReleasesNew(owner, repo)
got, err := c.ListTagsNew(owner, repo)
if err != nil {
t.Errorf("want no error, got %#v", err)
t.Errorf("want no error, got: %#v", err)
}

if len(got) != len(want) {
t.Errorf("want %d items, got %d items", len(want), len(got))
t.Errorf("want: %d items, got: %d items", len(want), len(got))
}

for i, g := range got {
if !reflect.DeepEqual(g, want[i]) {
t.Errorf("want %#v, got %#v", *want[i], *g)
if !reflect.DeepEqual(*g, *want[i]) {
t.Errorf("want: %#v, got: %#v", *want[i], *g)
}
}
}
Expand Down

0 comments on commit ee0c62c

Please sign in to comment.