-
Notifications
You must be signed in to change notification settings - Fork 67
Fix bin update on prerelease-only github repos #87
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
@@ -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 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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
vanduare empty?While reviewing this code, it was not helpful, that the two variables are named
vandu, because this does not tell me anything what I can expect to be in these variables. Also the signature ofGetLatestVersiondoes 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.