@@ -2,6 +2,7 @@ package github
22
33import (
44 "context"
5+ "crypto/tls"
56 "encoding/json"
67 "fmt"
78 "io"
@@ -18,52 +19,65 @@ import (
1819 "github.com/gptscript-ai/gptscript/pkg/types"
1920)
2021
21- const (
22- GithubPrefix = "github.com/"
23- githubRepoURL = "https://github.com/%s/%s.git"
24- githubDownloadURL = "https://raw.githubusercontent.com/%s/%s/%s/%s"
25- githubCommitURL = "https://api.github.com/repos/%s/%s/commits/%s"
26- )
22+ type GithubConfig struct {
23+ Prefix string
24+ RepoURL string
25+ DownloadURL string
26+ CommitURL string
27+ AuthToken string
28+ Enterprise bool
29+ }
2730
2831var (
29- githubAuthToken = os .Getenv ("GITHUB_AUTH_TOKEN" )
30- log = mvl .Package ()
32+ log = mvl .Package ()
33+ DEFAULT_GITHUB_CONFIG = & GithubConfig {
34+ Prefix : "github.com/" ,
35+ RepoURL : "https://github.com/%s/%s.git" ,
36+ DownloadURL : "https://raw.githubusercontent.com/%s/%s/%s/%s" ,
37+ CommitURL : "https://api.github.com/repos/%s/%s/commits/%s" ,
38+ AuthToken : os .Getenv ("GITHUB_AUTH_TOKEN" ),
39+ Enterprise : false ,
40+ }
3141)
3242
3343func init () {
3444 loader .AddVSC (Load )
3545}
3646
37- func getCommitLsRemote (ctx context.Context , account , repo , ref string ) (string , error ) {
38- url := fmt .Sprintf (githubRepoURL , account , repo )
47+ func getCommitLsRemote (ctx context.Context , account , repo , ref string , config * GithubConfig ) (string , error ) {
48+ url := fmt .Sprintf (config . RepoURL , account , repo )
3949 return git .LsRemote (ctx , url , ref )
4050}
4151
4252// regexp to match a git commit id
4353var commitRegexp = regexp .MustCompile ("^[a-f0-9]{40}$" )
4454
45- func getCommit (ctx context.Context , account , repo , ref string ) (string , error ) {
55+ func getCommit (ctx context.Context , account , repo , ref string , config * GithubConfig ) (string , error ) {
4656 if commitRegexp .MatchString (ref ) {
4757 return ref , nil
4858 }
4959
50- url := fmt .Sprintf (githubCommitURL , account , repo , ref )
60+ url := fmt .Sprintf (config . CommitURL , account , repo , ref )
5161 req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
5262 if err != nil {
5363 return "" , fmt .Errorf ("failed to create request of %s/%s at %s: %w" , account , repo , url , err )
5464 }
5565
56- if githubAuthToken != "" {
57- req .Header .Add ("Authorization" , "Bearer " + githubAuthToken )
66+ if config . AuthToken != "" {
67+ req .Header .Add ("Authorization" , "Bearer " + config . AuthToken )
5868 }
5969
60- resp , err := http .DefaultClient .Do (req )
70+ client := http .DefaultClient
71+ if req .Host == config .Prefix && strings .ToLower (os .Getenv ("GH_ENTERPRISE_SKIP_VERIFY" )) == "true" {
72+ client = & http.Client {Transport : & http.Transport {TLSClientConfig : & tls.Config {InsecureSkipVerify : true }}}
73+ }
74+ resp , err := client .Do (req )
6175 if err != nil {
6276 return "" , err
6377 } else if resp .StatusCode != http .StatusOK {
6478 c , _ := io .ReadAll (resp .Body )
6579 resp .Body .Close ()
66- commit , fallBackErr := getCommitLsRemote (ctx , account , repo , ref )
80+ commit , fallBackErr := getCommitLsRemote (ctx , account , repo , ref , config )
6781 if fallBackErr == nil {
6882 return commit , nil
6983 }
@@ -88,8 +102,29 @@ func getCommit(ctx context.Context, account, repo, ref string) (string, error) {
88102 return commit .SHA , nil
89103}
90104
91- func Load (ctx context.Context , _ * cache.Client , urlName string ) (string , * types.Repo , bool , error ) {
92- if ! strings .HasPrefix (urlName , GithubPrefix ) {
105+ func LoaderForPrefix (prefix string ) func (context.Context , * cache.Client , string ) (string , * types.Repo , bool , error ) {
106+ return func (ctx context.Context , c * cache.Client , urlName string ) (string , * types.Repo , bool , error ) {
107+ return LoadWithConfig (ctx , c , urlName , NewGithubEnterpriseConfig (prefix ))
108+ }
109+ }
110+
111+ func Load (ctx context.Context , c * cache.Client , urlName string ) (string , * types.Repo , bool , error ) {
112+ return LoadWithConfig (ctx , c , urlName , DEFAULT_GITHUB_CONFIG )
113+ }
114+
115+ func NewGithubEnterpriseConfig (prefix string ) * GithubConfig {
116+ return & GithubConfig {
117+ Prefix : prefix ,
118+ RepoURL : fmt .Sprintf ("https://%s/%%s/%%s.git" , prefix ),
119+ DownloadURL : fmt .Sprintf ("https://raw.%s/%%s/%%s/%%s/%%s" , prefix ),
120+ CommitURL : fmt .Sprintf ("https://%s/api/v3/repos/%%s/%%s/commits/%%s" , prefix ),
121+ AuthToken : os .Getenv ("GH_ENTERPRISE_TOKEN" ),
122+ Enterprise : true ,
123+ }
124+ }
125+
126+ func LoadWithConfig (ctx context.Context , _ * cache.Client , urlName string , config * GithubConfig ) (string , * types.Repo , bool , error ) {
127+ if ! strings .HasPrefix (urlName , config .Prefix ) {
93128 return "" , nil , false , nil
94129 }
95130
@@ -107,12 +142,12 @@ func Load(ctx context.Context, _ *cache.Client, urlName string) (string, *types.
107142 account , repo := parts [1 ], parts [2 ]
108143 path := strings .Join (parts [3 :], "/" )
109144
110- ref , err := getCommit (ctx , account , repo , ref )
145+ ref , err := getCommit (ctx , account , repo , ref , config )
111146 if err != nil {
112147 return "" , nil , false , err
113148 }
114149
115- downloadURL := fmt .Sprintf (githubDownloadURL , account , repo , ref , path )
150+ downloadURL := fmt .Sprintf (config . DownloadURL , account , repo , ref , path )
116151 if path == "" || path == "/" || ! strings .Contains (parts [len (parts )- 1 ], "." ) {
117152 var (
118153 testPath string
@@ -124,7 +159,7 @@ func Load(ctx context.Context, _ *cache.Client, urlName string) (string, *types.
124159 } else {
125160 testPath = path + "/" + ext
126161 }
127- testURL = fmt .Sprintf (githubDownloadURL , account , repo , ref , testPath )
162+ testURL = fmt .Sprintf (config . DownloadURL , account , repo , ref , testPath )
128163 if i == len (types .DefaultFiles )- 1 {
129164 // no reason to test the last one, we are just going to use it. Being that the default list is only
130165 // two elements this loop could have been one check, but hey over-engineered code ftw.
@@ -141,11 +176,17 @@ func Load(ctx context.Context, _ *cache.Client, urlName string) (string, *types.
141176 path = testPath
142177 }
143178
144- return downloadURL , & types.Repo {
179+ repoConfig := & types.Repo {
145180 VCS : "git" ,
146- Root : fmt .Sprintf (githubRepoURL , account , repo ),
181+ Root : fmt .Sprintf (config . RepoURL , account , repo ),
147182 Path : gpath .Dir (path ),
148183 Name : gpath .Base (path ),
149184 Revision : ref ,
150- }, true , nil
185+ }
186+ if config .Enterprise {
187+ repoConfig .Headers = map [string ]string {
188+ "Authorization" : fmt .Sprintf ("bearer %s" , config .AuthToken ),
189+ }
190+ }
191+ return downloadURL , repoConfig , true , nil
151192}
0 commit comments