Skip to content

Commit d932de2

Browse files
Merge pull request #786 from ibuildthecloud/main
chore: support looking up go binary releases by tag
2 parents 9696f8d + e1e2002 commit d932de2

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

Diff for: pkg/repos/runtimes/golang/golang.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/sha256"
88
_ "embed"
99
"encoding/hex"
10+
"encoding/json"
1011
"errors"
1112
"fmt"
1213
"io"
@@ -89,6 +90,13 @@ func (r release) srcBinName() string {
8990
runtime.GOARCH + suffix
9091
}
9192

93+
type tag struct {
94+
Name string `json:"name,omitempty"`
95+
Commit struct {
96+
Sha string `json:"sha,omitempty"`
97+
} `json:"commit"`
98+
}
99+
92100
func getLatestRelease(tool types.Tool) (*release, bool) {
93101
if tool.Source.Repo == nil || !strings.HasPrefix(tool.Source.Repo.Root, "https://github.com/") {
94102
return nil, false
@@ -105,7 +113,30 @@ func getLatestRelease(tool types.Tool) (*release, bool) {
105113
},
106114
}
107115

108-
resp, err := client.Get(fmt.Sprintf("https://github.com/%s/%s/releases/latest", parts[1], parts[2]))
116+
account, repo := parts[1], parts[2]
117+
118+
resp, err := client.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", account, repo))
119+
if err != nil || resp.StatusCode != http.StatusOK {
120+
// ignore error
121+
return nil, false
122+
}
123+
defer resp.Body.Close()
124+
125+
var tags []tag
126+
if err := json.NewDecoder(resp.Body).Decode(&tags); err != nil {
127+
return nil, false
128+
}
129+
for _, tag := range tags {
130+
if tag.Commit.Sha == tool.Source.Repo.Revision {
131+
return &release{
132+
account: account,
133+
repo: repo,
134+
label: tag.Name,
135+
}, true
136+
}
137+
}
138+
139+
resp, err = client.Get(fmt.Sprintf("https://github.com/%s/%s/releases/latest", account, repo))
109140
if err != nil || resp.StatusCode != http.StatusFound {
110141
// ignore error
111142
return nil, false
@@ -117,7 +148,6 @@ func getLatestRelease(tool types.Tool) (*release, bool) {
117148
return nil, false
118149
}
119150

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

0 commit comments

Comments
 (0)