@@ -2,6 +2,7 @@ package github
2
2
3
3
import (
4
4
"context"
5
+ "crypto/tls"
5
6
"encoding/json"
6
7
"fmt"
7
8
"io"
@@ -18,52 +19,65 @@ import (
18
19
"github.com/gptscript-ai/gptscript/pkg/types"
19
20
)
20
21
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
+ }
27
30
28
31
var (
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
+ }
31
41
)
32
42
33
43
func init () {
34
44
loader .AddVSC (Load )
35
45
}
36
46
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 )
39
49
return git .LsRemote (ctx , url , ref )
40
50
}
41
51
42
52
// regexp to match a git commit id
43
53
var commitRegexp = regexp .MustCompile ("^[a-f0-9]{40}$" )
44
54
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 ) {
46
56
if commitRegexp .MatchString (ref ) {
47
57
return ref , nil
48
58
}
49
59
50
- url := fmt .Sprintf (githubCommitURL , account , repo , ref )
60
+ url := fmt .Sprintf (config . CommitURL , account , repo , ref )
51
61
req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
52
62
if err != nil {
53
63
return "" , fmt .Errorf ("failed to create request of %s/%s at %s: %w" , account , repo , url , err )
54
64
}
55
65
56
- if githubAuthToken != "" {
57
- req .Header .Add ("Authorization" , "Bearer " + githubAuthToken )
66
+ if config . AuthToken != "" {
67
+ req .Header .Add ("Authorization" , "Bearer " + config . AuthToken )
58
68
}
59
69
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 )
61
75
if err != nil {
62
76
return "" , err
63
77
} else if resp .StatusCode != http .StatusOK {
64
78
c , _ := io .ReadAll (resp .Body )
65
79
resp .Body .Close ()
66
- commit , fallBackErr := getCommitLsRemote (ctx , account , repo , ref )
80
+ commit , fallBackErr := getCommitLsRemote (ctx , account , repo , ref , config )
67
81
if fallBackErr == nil {
68
82
return commit , nil
69
83
}
@@ -88,8 +102,29 @@ func getCommit(ctx context.Context, account, repo, ref string) (string, error) {
88
102
return commit .SHA , nil
89
103
}
90
104
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 ) {
93
128
return "" , nil , false , nil
94
129
}
95
130
@@ -107,12 +142,12 @@ func Load(ctx context.Context, _ *cache.Client, urlName string) (string, *types.
107
142
account , repo := parts [1 ], parts [2 ]
108
143
path := strings .Join (parts [3 :], "/" )
109
144
110
- ref , err := getCommit (ctx , account , repo , ref )
145
+ ref , err := getCommit (ctx , account , repo , ref , config )
111
146
if err != nil {
112
147
return "" , nil , false , err
113
148
}
114
149
115
- downloadURL := fmt .Sprintf (githubDownloadURL , account , repo , ref , path )
150
+ downloadURL := fmt .Sprintf (config . DownloadURL , account , repo , ref , path )
116
151
if path == "" || path == "/" || ! strings .Contains (parts [len (parts )- 1 ], "." ) {
117
152
var (
118
153
testPath string
@@ -124,7 +159,7 @@ func Load(ctx context.Context, _ *cache.Client, urlName string) (string, *types.
124
159
} else {
125
160
testPath = path + "/" + ext
126
161
}
127
- testURL = fmt .Sprintf (githubDownloadURL , account , repo , ref , testPath )
162
+ testURL = fmt .Sprintf (config . DownloadURL , account , repo , ref , testPath )
128
163
if i == len (types .DefaultFiles )- 1 {
129
164
// no reason to test the last one, we are just going to use it. Being that the default list is only
130
165
// 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.
141
176
path = testPath
142
177
}
143
178
144
- return downloadURL , & types.Repo {
179
+ repoConfig := & types.Repo {
145
180
VCS : "git" ,
146
- Root : fmt .Sprintf (githubRepoURL , account , repo ),
181
+ Root : fmt .Sprintf (config . RepoURL , account , repo ),
147
182
Path : gpath .Dir (path ),
148
183
Name : gpath .Base (path ),
149
184
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
151
192
}
0 commit comments