Skip to content
Open
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
3 changes: 3 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ func getLatestVersion(b *config.Binary, p providers.Provider) (*updateInfo, erro
if err != nil {
return nil, fmt.Errorf("Error checking updates for %s, %w", b.Path, err)
}
if v == "" && u == "" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case is it possible, that we do not have an error but still v and u are empty?

While reviewing this code, it was not helpful, that the two variables are named v and u, because this does not tell me anything what I can expect to be in these variables. Also the signature of GetLatestVersion does not tell me more (just that I get 2 strings and an error as return value). Therefore I propose to use some more speaking variable names.

return nil, nil
}

if b.Version == v {
return nil, nil
Expand Down
29 changes: 23 additions & 6 deletions pkg/providers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ 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)
}
release, err = g.getAnyLatestRelease(context.TODO())
}

if err != nil {
Expand Down Expand Up @@ -80,14 +76,35 @@ 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)
release, err := g.getAnyLatestRelease(context.TODO())
if err != nil {
return "", "", err
}

return release.GetTagName(), release.GetHTMLURL(), nil
}

func (g *gitHub) getAnyLatestRelease(ctx context.Context) (*github.RepositoryRelease, error) {
release, _, err := g.client.Repositories.GetLatestRelease(ctx, g.owner, g.repo)
if err != nil {
// If the error is that no latest release was found, it could be that there are only pre-releases
if ghErrResp, ok := err.(*github.ErrorResponse); ok && ghErrResp.Response.StatusCode == http.StatusNotFound {
// Get the first release returned by ListReleases
releases, _, listErr := g.client.Repositories.ListReleases(ctx, g.owner, g.repo, &github.ListOptions{PerPage: 1})
if listErr != nil {
return nil, listErr
}
if len(releases) > 0 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, is it possible to not get an error and an empty list of releases? I would expect the API to return an 404 here as well, if there are no releases at all.

return releases[0], nil
}
}

// Return original 404/StatusNotFound error from GetLatestRelease

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is misleading, because there error could be 404 but it could also be an other error.

return nil, err
}
return release, err

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would explicitly return nil for the error to make it obvious for the reader of the code, that this is the happy path.

return release, nil

}

func (g *gitHub) GetID() string {
return "github"
}
Expand Down