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

chore: support looking up go binary releases by tag #786

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
34 changes: 32 additions & 2 deletions pkg/repos/runtimes/golang/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/sha256"
_ "embed"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -89,6 +90,13 @@ func (r release) srcBinName() string {
runtime.GOARCH + suffix
}

type tag struct {
Name string `json:"name,omitempty"`
Commit struct {
Sha string `json:"sha,omitempty"`
} `json:"commit"`
}

func getLatestRelease(tool types.Tool) (*release, bool) {
if tool.Source.Repo == nil || !strings.HasPrefix(tool.Source.Repo.Root, "https://github.com/") {
return nil, false
Expand All @@ -105,7 +113,30 @@ func getLatestRelease(tool types.Tool) (*release, bool) {
},
}

resp, err := client.Get(fmt.Sprintf("https://github.com/%s/%s/releases/latest", parts[1], parts[2]))
account, repo := parts[1], parts[2]

resp, err := client.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", account, repo))
if err != nil || resp.StatusCode != http.StatusOK {
// ignore error
return nil, false
}
defer resp.Body.Close()

var tags []tag
if err := json.NewDecoder(resp.Body).Decode(&tags); err != nil {
return nil, false
}
for _, tag := range tags {
if tag.Commit.Sha == tool.Source.Repo.Revision {
return &release{
account: account,
repo: repo,
label: tag.Name,
}, true
}
}

resp, err = client.Get(fmt.Sprintf("https://github.com/%s/%s/releases/latest", account, repo))
if err != nil || resp.StatusCode != http.StatusFound {
// ignore error
return nil, false
Expand All @@ -117,7 +148,6 @@ func getLatestRelease(tool types.Tool) (*release, bool) {
return nil, false
}

account, repo := parts[1], parts[2]
parts = strings.Split(target, "/")
label := parts[len(parts)-1]

Expand Down