Skip to content

fix: use GH token for VCS lookups #578

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 7 commits into from
Jul 1, 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
3 changes: 2 additions & 1 deletion .github/workflows/smoke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ jobs:
go-version: "1.21"
- env:
OPENAI_API_KEY: ${{ secrets.SMOKE_OPENAI_API_KEY }}
GPTSCRIPT_DEFAULT_MODEL: claude-3-opus-20240229 from github.com/gptscript-ai/claude3-anthropic-provider@tool-beta
GPTSCRIPT_DEFAULT_MODEL: claude-3-opus-20240229 from github.com/gptscript-ai/claude3-anthropic-provider
ANTHROPIC_API_KEY: ${{ secrets.SMOKE_ANTHROPIC_API_KEY }}
GPTSCRIPT_CREDENTIAL_OVERRIDE: "github.com/gptscript-ai/claude3-anthropic-provider/credential:ANTHROPIC_API_KEY"
name: Run smoke test for claude-3-opus-20240229
run: |
echo "Running smoke test for model claude-3-opus-20240229"
Expand Down
10 changes: 5 additions & 5 deletions pkg/loader/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func getCommit(ctx context.Context, account, repo, ref string) (string, error) {
return commit.SHA, nil
}

func Load(ctx context.Context, _ *cache.Client, urlName string) (string, *types.Repo, bool, error) {
func Load(ctx context.Context, _ *cache.Client, urlName string) (string, string, *types.Repo, bool, error) {
if !strings.HasPrefix(urlName, GithubPrefix) {
return "", nil, false, nil
return "", "", nil, false, nil
}

url, ref, _ := strings.Cut(urlName, "@")
Expand All @@ -101,15 +101,15 @@ func Load(ctx context.Context, _ *cache.Client, urlName string) (string, *types.
parts := strings.Split(url, "/")
// Must be at least 3 parts github.com/ACCOUNT/REPO[/FILE]
if len(parts) < 3 {
return "", nil, false, nil
return "", "", nil, false, nil
}

account, repo := parts[1], parts[2]
path := strings.Join(parts[3:], "/")

ref, err := getCommit(ctx, account, repo, ref)
if err != nil {
return "", nil, false, err
return "", "", nil, false, err
}

downloadURL := fmt.Sprintf(githubDownloadURL, account, repo, ref, path)
Expand Down Expand Up @@ -141,7 +141,7 @@ func Load(ctx context.Context, _ *cache.Client, urlName string) (string, *types.
path = testPath
}

return downloadURL, &types.Repo{
return downloadURL, githubAuthToken, &types.Repo{
VCS: "git",
Root: fmt.Sprintf(githubRepoURL, account, repo),
Path: gpath.Dir(path),
Expand Down
6 changes: 3 additions & 3 deletions pkg/loader/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestLoad(t *testing.T) {
url, repo, ok, err := Load(context.Background(), nil, "github.com/gptscript-ai/gptscript/pkg/loader/testdata/tool@172dfb0")
url, _, repo, ok, err := Load(context.Background(), nil, "github.com/gptscript-ai/gptscript/pkg/loader/testdata/tool@172dfb0")
require.NoError(t, err)
assert.True(t, ok)
autogold.Expect("https://raw.githubusercontent.com/gptscript-ai/gptscript/172dfb00b48c6adbbaa7e99270933f95887d1b91/pkg/loader/testdata/tool/tool.gpt").Equal(t, url)
Expand All @@ -22,7 +22,7 @@ func TestLoad(t *testing.T) {
Revision: "172dfb00b48c6adbbaa7e99270933f95887d1b91",
}).Equal(t, repo)

url, repo, ok, err = Load(context.Background(), nil, "github.com/gptscript-ai/gptscript/pkg/loader/testdata/agent@172dfb0")
url, _, repo, ok, err = Load(context.Background(), nil, "github.com/gptscript-ai/gptscript/pkg/loader/testdata/agent@172dfb0")
require.NoError(t, err)
assert.True(t, ok)
autogold.Expect("https://raw.githubusercontent.com/gptscript-ai/gptscript/172dfb00b48c6adbbaa7e99270933f95887d1b91/pkg/loader/testdata/agent/agent.gpt").Equal(t, url)
Expand All @@ -33,7 +33,7 @@ func TestLoad(t *testing.T) {
Revision: "172dfb00b48c6adbbaa7e99270933f95887d1b91",
}).Equal(t, repo)

url, repo, ok, err = Load(context.Background(), nil, "github.com/gptscript-ai/gptscript/pkg/loader/testdata/bothtoolagent@172dfb0")
url, _, repo, ok, err = Load(context.Background(), nil, "github.com/gptscript-ai/gptscript/pkg/loader/testdata/bothtoolagent@172dfb0")
require.NoError(t, err)
assert.True(t, ok)
autogold.Expect("https://raw.githubusercontent.com/gptscript-ai/gptscript/172dfb00b48c6adbbaa7e99270933f95887d1b91/pkg/loader/testdata/bothtoolagent/agent.gpt").Equal(t, url)
Expand Down
18 changes: 12 additions & 6 deletions pkg/loader/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/gptscript-ai/gptscript/pkg/types"
)

type VCSLookup func(context.Context, *cache.Client, string) (string, *types.Repo, bool, error)
type VCSLookup func(context.Context, *cache.Client, string) (string, string, *types.Repo, bool, error)

var vcsLookups []VCSLookup

Expand All @@ -35,10 +35,11 @@ type cacheValue struct {

func loadURL(ctx context.Context, cache *cache.Client, base *source, name string) (*source, bool, error) {
var (
repo *types.Repo
url = name
relative = strings.HasPrefix(name, ".") || !strings.Contains(name, "/")
cachedKey = cacheKey{
repo *types.Repo
url = name
bearerToken = ""
relative = strings.HasPrefix(name, ".") || !strings.Contains(name, "/")
cachedKey = cacheKey{
Name: name,
Path: base.Path,
Repo: base.Repo,
Expand Down Expand Up @@ -67,12 +68,13 @@ func loadURL(ctx context.Context, cache *cache.Client, base *source, name string

if repo == nil || !relative {
for _, vcs := range vcsLookups {
newURL, newRepo, ok, err := vcs(ctx, cache, name)
newURL, newBearer, newRepo, ok, err := vcs(ctx, cache, name)
if err != nil {
return nil, false, err
} else if ok {
repo = newRepo
url = newURL
bearerToken = newBearer
break
}
}
Expand Down Expand Up @@ -105,6 +107,10 @@ func loadURL(ctx context.Context, cache *cache.Client, base *source, name string
return nil, false, err
}

if bearerToken != "" {
req.Header.Set("Authorization", "Bearer "+bearerToken)
}

data, err := getWithDefaults(req)
if err != nil {
return nil, false, fmt.Errorf("error loading %s: %v", url, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/repos/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
}

// Load the credential helpers repo information.
_, repo, _, err := github.Load(ctx, nil, credentialHelpersRepo)
_, _, repo, _, err := github.Load(ctx, nil, credentialHelpersRepo)
if err != nil {
return err
}
Expand Down
Loading