Skip to content

Commit

Permalink
update GHReleases func to support tags without v prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
spowelljr committed Sep 27, 2023
1 parent 9d31fed commit 9b61bda
Showing 1 changed file with 13 additions and 37 deletions.
50 changes: 13 additions & 37 deletions hack/update/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,80 +48,56 @@ func GHReleases(ctx context.Context, owner, repo string) (stable, latest, edge R
// walk through the paginated list of up to ghSearchLimit newest releases
opts := &github.ListOptions{PerPage: ghListPerPage}
for (opts.Page+1)*ghListPerPage <= ghSearchLimit {
rls, resp, err := ghc.Repositories.ListReleases(ctx, owner, repo, opts)
rls, resp, err := ghc.Repositories.ListTags(ctx, owner, repo, opts)
if err != nil {
return stable, latest, edge, err
}
for _, rl := range rls {
ver := rl.GetTagName()
ver := rl.GetName()
commit := rl.GetCommit().GetSHA()
if !semver.IsValid(ver) {
continue
ver = fmt.Sprintf("v%s", ver)
if !semver.IsValid(ver) {
continue
}
}
// check if ver version is release (ie, 'v1.19.2') or pre-release (ie, 'v1.19.3-rc.0' or 'v1.19.0-beta.2')
prerls := semver.Prerelease(ver)
if prerls == "" {
if semver.Compare(ver, stable.Tag) == 1 {
stable.Tag = ver
stable.Commit = commit
}
} else if strings.HasPrefix(prerls, "-rc") || strings.HasPrefix(prerls, "-beta") {
if semver.Compare(ver, latest.Tag) == 1 {
latest.Tag = ver
latest.Commit = commit
}
} else if strings.Contains(prerls, "-alpha") {
if semver.Compare(ver, edge.Tag) == 1 {
edge.Tag = ver
edge.Commit = commit
}
}

// make sure that latest >= stable
if semver.Compare(latest.Tag, stable.Tag) == -1 {
latest.Tag = stable.Tag
latest.Commit = stable.Commit
}
// make sure that edge >= latest
if semver.Compare(edge.Tag, latest.Tag) == -1 {
edge.Tag = latest.Tag
edge.Commit = latest.Commit
}
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
// create a map where the key is the tag and the values is an array of releases (stable, latest, edge) that match the tag
releasesWithoutCommits := map[string][]*Release{}
for _, rl := range []*Release{&stable, &latest, &edge} {
releasesWithoutCommits[rl.Tag] = append(releasesWithoutCommits[rl.Tag], rl)
}
// run though the releases to find ones that don't yet have a commit and assign it
opts = &github.ListOptions{PerPage: ghListPerPage}
for (opts.Page+1)*ghListPerPage <= ghSearchLimit {
tags, resp, err := ghc.Repositories.ListTags(ctx, owner, repo, opts)
if err != nil {
return stable, latest, edge, err
}
for _, tag := range tags {
rls, ok := releasesWithoutCommits[*tag.Name]
if !ok {
continue
}
for _, rl := range rls {
rl.Commit = *tag.Commit.SHA
}
delete(releasesWithoutCommits, *tag.Name)
if len(releasesWithoutCommits) == 0 {
return stable, latest, edge, nil
}
}
if len(releasesWithoutCommits) == 0 {
break
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}

return stable, latest, edge, fmt.Errorf("wasn't able to find commit for releases")
return stable, latest, edge, nil
}

func StableVersion(ctx context.Context, owner, repo string) (string, error) {
Expand Down

0 comments on commit 9b61bda

Please sign in to comment.