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

fix: use first github release in the list as latest when latest is not available #178

Closed
Closed
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
40 changes: 32 additions & 8 deletions pkg/providers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package providers
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"net/http"
"net/url"
Expand All @@ -29,16 +30,17 @@ func (g *gitHub) Fetch(opts *FetchOpts) (*File, error) {

// If we have a tag, let's fetch from there
var err error
var resp *github.Response
if len(g.tag) > 0 {
log.Infof("Getting %s release for %s/%s", g.tag, g.owner, g.repo)
release, _, err = g.client.Repositories.GetReleaseByTag(context.TODO(), g.owner, g.repo, g.tag)
} else {
log.Infof("Getting latest release for %s/%s", g.owner, g.repo)
release, resp, err = g.client.Repositories.GetLatestRelease(context.TODO(), g.owner, g.repo)
if resp.StatusCode == http.StatusNotFound {
err = fmt.Errorf("repository %s/%s does not have releases", g.owner, g.repo)
var tag string
tag, _, err = g.GetLatestVersion()
if err != nil {
return nil, fmt.Errorf("repository %s/%s does not have releases", g.owner, g.repo)
}
release, _, err = g.client.Repositories.GetReleaseByTag(context.TODO(), g.owner, g.repo, tag)
}

if err != nil {
Expand Down Expand Up @@ -80,11 +82,33 @@ func (g *gitHub) Fetch(opts *FetchOpts) (*File, error) {
// returns the corresponding name and url to fetch the version
func (g *gitHub) GetLatestVersion() (string, string, error) {
log.Debugf("Getting latest release for %s/%s", g.owner, g.repo)
release, _, err := g.client.Repositories.GetLatestRelease(context.TODO(), g.owner, g.repo)
if err != nil {
return "", "", err
}

release, resp, err := g.client.Repositories.GetLatestRelease(context.TODO(), g.owner, g.repo)
if err == nil && resp.StatusCode == http.StatusOK {
return release.GetTagName(), release.GetHTMLURL(), nil
}
latestResp, latestErr := resp, err

// repo does not have a "latest" release, fallback to first release on the list
var releases []*github.RepositoryRelease
releases, resp, err = g.client.Repositories.ListReleases(context.TODO(), g.owner, g.repo, &github.ListOptions{
Page: 1,
PerPage: 1,
})
if err != nil || len(releases) == 0 {
if latestErr != nil {
latestErr = fmt.Errorf("error getting latest release: %w", latestErr)
} else {
latestErr = fmt.Errorf("error getting latest release. http status: %v", latestResp.StatusCode)
}
if err != nil {
err = fmt.Errorf("error listing releases: %w", err)
} else {
err = fmt.Errorf("error listing releases. http status: %v", resp.StatusCode)
}
return "", "", errors.Join(latestErr, err)
}
release = releases[0]
return release.GetTagName(), release.GetHTMLURL(), nil
}

Expand Down