Skip to content

Commit 3564d89

Browse files
committed
fix: surface the error message when we fail to find a release on GitHub
Signed-off-by: Grant Linville <[email protected]>
1 parent c516f7b commit 3564d89

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

pkg/repos/runtimes/golang/golang.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,23 @@ type tag struct {
9797
}
9898

9999
func GetLatestTag(tool types.Tool) (string, error) {
100-
r, ok := getLatestRelease(tool)
101-
if !ok {
100+
r, ok, err := getLatestRelease(tool)
101+
if err != nil {
102+
return "", fmt.Errorf("failed to get latest release for %s - %w", tool.Name, err)
103+
} else if !ok {
102104
return "", fmt.Errorf("failed to get latest release for %s", tool.Name)
103105
}
104106
return r.label, nil
105107
}
106108

107-
func getLatestRelease(tool types.Tool) (*release, bool) {
109+
func getLatestRelease(tool types.Tool) (*release, bool, error) {
108110
if tool.Source.Repo == nil || !strings.HasPrefix(tool.Source.Repo.Root, "https://github.com/") {
109-
return nil, false
111+
return nil, false, nil
110112
}
111113

112114
parts := strings.Split(strings.TrimPrefix(strings.TrimSuffix(tool.Source.Repo.Root, ".git"), "https://"), "/")
113115
if len(parts) != 3 {
114-
return nil, false
116+
return nil, false, nil
115117
}
116118

117119
client := http.Client{
@@ -124,41 +126,39 @@ func getLatestRelease(tool types.Tool) (*release, bool) {
124126

125127
resp, err := client.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", account, repo))
126128
if err != nil {
127-
// ignore error
128-
return nil, false
129+
return nil, false, err
129130
}
130131
defer resp.Body.Close()
131132
if resp.StatusCode != http.StatusOK {
132-
return nil, false
133+
return nil, false, fmt.Errorf("got status code %d from GitHub", resp.StatusCode)
133134
}
134135

135136
var tags []tag
136137
if err := json.NewDecoder(resp.Body).Decode(&tags); err != nil {
137-
return nil, false
138+
return nil, false, nil
138139
}
139140
for _, tag := range tags {
140141
if tag.Commit.Sha == tool.Source.Repo.Revision {
141142
return &release{
142143
account: account,
143144
repo: repo,
144145
label: tag.Name,
145-
}, true
146+
}, true, nil
146147
}
147148
}
148149

149150
resp, err = client.Get(fmt.Sprintf("https://github.com/%s/%s/releases/latest", account, repo))
150151
if err != nil {
151-
// ignore error
152-
return nil, false
152+
return nil, false, err
153153
}
154154
defer resp.Body.Close()
155155
if resp.StatusCode != http.StatusFound {
156-
return nil, false
156+
return nil, false, fmt.Errorf("got status code %d from GitHub", resp.StatusCode)
157157
}
158158

159159
target := resp.Header.Get("Location")
160160
if target == "" {
161-
return nil, false
161+
return nil, false, nil
162162
}
163163

164164
parts = strings.Split(target, "/")
@@ -168,7 +168,7 @@ func getLatestRelease(tool types.Tool) (*release, bool) {
168168
account: account,
169169
repo: repo,
170170
label: label,
171-
}, true
171+
}, true, nil
172172
}
173173

174174
func get(ctx context.Context, url string) (*http.Response, error) {
@@ -249,9 +249,9 @@ func (r *Runtime) Binary(ctx context.Context, tool types.Tool, _, toolSource str
249249
return false, nil, nil
250250
}
251251

252-
rel, ok := getLatestRelease(tool)
252+
rel, ok, err := getLatestRelease(tool)
253253
if !ok {
254-
return false, nil, nil
254+
return false, nil, err
255255
}
256256

257257
checksum := getChecksum(ctx, rel, rel.srcBinName())
@@ -286,8 +286,10 @@ func (r *Runtime) DownloadCredentialHelper(ctx context.Context, tool types.Tool,
286286
return nil
287287
}
288288

289-
rel, ok := getLatestRelease(tool)
290-
if !ok {
289+
rel, ok, err := getLatestRelease(tool)
290+
if err != nil {
291+
return fmt.Errorf("failed to get %s release: %w", r.ID(), err)
292+
} else if !ok {
291293
return fmt.Errorf("failed to find %s release", r.ID())
292294
}
293295
binaryName := "gptscript-credential-" + helperName

0 commit comments

Comments
 (0)