Skip to content

chore: bubble up errors when downloading credential helper fails #845

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

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
5 changes: 5 additions & 0 deletions pkg/repos/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
}

tool := types.Tool{
ToolDef: types.ToolDef{
Parameters: types.Parameters{
Name: "gptscript-credential-helpers",
},
},
Source: types.ToolSource{
Repo: &types.Repo{
Root: runtimeEnv.VarOrDefault("GPTSCRIPT_CRED_HELPERS_ROOT", "https://github.com/gptscript-ai/gptscript-credential-helpers.git"),
Expand Down
37 changes: 21 additions & 16 deletions pkg/repos/runtimes/golang/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,26 @@ type tag struct {
}

func GetLatestTag(tool types.Tool) (string, error) {
r, ok := getLatestRelease(tool)
r, ok, err := getLatestRelease(tool)
if err != nil {
return "", err
}

if !ok {
return "", fmt.Errorf("failed to get latest release for %s", tool.Name)
}

return r.label, nil
}

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

parts := strings.Split(strings.TrimPrefix(strings.TrimSuffix(tool.Source.Repo.Root, ".git"), "https://"), "/")
if len(parts) != 3 {
return nil, false
return nil, false, fmt.Errorf("invalid GitHub URL: %s", tool.Source.Repo.Root)
}

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

resp, err := client.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", account, repo))
if err != nil {
// ignore error
return nil, false
return nil, false, fmt.Errorf("failed to get tags: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, false
return nil, false, fmt.Errorf("unexpected status when getting tags: %s", resp.Status)
}

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

resp, err = client.Get(fmt.Sprintf("https://github.com/%s/%s/releases/latest", account, repo))
if err != nil {
// ignore error
return nil, false
return nil, false, fmt.Errorf("failed to get latest release: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusFound {
return nil, false
return nil, false, fmt.Errorf("unexpected status when getting latest release: %s", resp.Status)
}

target := resp.Header.Get("Location")
if target == "" {
return nil, false
return nil, false, nil
}

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

func get(ctx context.Context, url string) (*http.Response, error) {
Expand Down Expand Up @@ -249,7 +252,8 @@ func (r *Runtime) Binary(ctx context.Context, tool types.Tool, _, toolSource str
return false, nil, nil
}

rel, ok := getLatestRelease(tool)
// ignore the error
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason we need to ignore the error here, while not on the getLatestRelease method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The getLatestRelease is best-effort because we don't know if the tool actually has a release. So errors don't really matter....

Except for the credential helpers. We know those have releases and we expect them to be used.

rel, ok, _ := getLatestRelease(tool)
if !ok {
return false, nil, nil
}
Expand Down Expand Up @@ -286,7 +290,8 @@ func (r *Runtime) DownloadCredentialHelper(ctx context.Context, tool types.Tool,
return nil
}

rel, ok := getLatestRelease(tool)
// ignore the error
rel, ok, _ := getLatestRelease(tool)
if !ok {
return fmt.Errorf("failed to find %s release", r.ID())
}
Expand Down