@@ -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,63 @@ 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
+ }
27
29
28
30
var (
29
- githubAuthToken = os .Getenv ("GITHUB_AUTH_TOKEN" )
30
- log = mvl .Package ()
31
+ log = mvl .Package ()
32
+ DEFAULT_GITHUB_CONFIG = & GithubConfig {
33
+ Prefix : "github.com/" ,
34
+ RepoURL : "https://github.com/%s/%s.git" ,
35
+ DownloadURL : "https://raw.githubusercontent.com/%s/%s/%s/%s" ,
36
+ CommitURL : "https://api.github.com/repos/%s/%s/commits/%s" ,
37
+ AuthToken : os .Getenv ("GITHUB_AUTH_TOKEN" ),
38
+ }
31
39
)
32
40
33
41
func init () {
34
42
loader .AddVSC (Load )
35
43
}
36
44
37
- func getCommitLsRemote (ctx context.Context , account , repo , ref string ) (string , error ) {
38
- url := fmt .Sprintf (githubRepoURL , account , repo )
45
+ func getCommitLsRemote (ctx context.Context , account , repo , ref string , config * GithubConfig ) (string , error ) {
46
+ url := fmt .Sprintf (config . RepoURL , account , repo )
39
47
return git .LsRemote (ctx , url , ref )
40
48
}
41
49
42
50
// regexp to match a git commit id
43
51
var commitRegexp = regexp .MustCompile ("^[a-f0-9]{40}$" )
44
52
45
- func getCommit (ctx context.Context , account , repo , ref string ) (string , error ) {
53
+ func getCommit (ctx context.Context , account , repo , ref string , config * GithubConfig ) (string , error ) {
46
54
if commitRegexp .MatchString (ref ) {
47
55
return ref , nil
48
56
}
49
57
50
- url := fmt .Sprintf (githubCommitURL , account , repo , ref )
58
+ url := fmt .Sprintf (config . CommitURL , account , repo , ref )
51
59
req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
52
60
if err != nil {
53
61
return "" , fmt .Errorf ("failed to create request of %s/%s at %s: %w" , account , repo , url , err )
54
62
}
55
63
56
- if githubAuthToken != "" {
57
- req .Header .Add ("Authorization" , "Bearer " + githubAuthToken )
64
+ if config . AuthToken != "" {
65
+ req .Header .Add ("Authorization" , "Bearer " + config . AuthToken )
58
66
}
59
67
60
- resp , err := http .DefaultClient .Do (req )
68
+ client := http .DefaultClient
69
+ if req .Host == config .Prefix && strings .ToLower (os .Getenv ("GH_ENTERPRISE_SKIP_VERIFY" )) == "true" {
70
+ client = & http.Client {Transport : & http.Transport {TLSClientConfig : & tls.Config {InsecureSkipVerify : true }}}
71
+ }
72
+ resp , err := client .Do (req )
61
73
if err != nil {
62
74
return "" , err
63
75
} else if resp .StatusCode != http .StatusOK {
64
76
c , _ := io .ReadAll (resp .Body )
65
77
resp .Body .Close ()
66
- commit , fallBackErr := getCommitLsRemote (ctx , account , repo , ref )
78
+ commit , fallBackErr := getCommitLsRemote (ctx , account , repo , ref , config )
67
79
if fallBackErr == nil {
68
80
return commit , nil
69
81
}
@@ -88,8 +100,28 @@ func getCommit(ctx context.Context, account, repo, ref string) (string, error) {
88
100
return commit .SHA , nil
89
101
}
90
102
91
- func Load (ctx context.Context , _ * cache.Client , urlName string ) (string , string , * types.Repo , bool , error ) {
92
- if ! strings .HasPrefix (urlName , GithubPrefix ) {
103
+ func LoaderForPrefix (prefix string ) func (context.Context , * cache.Client , string ) (string , string , * types.Repo , bool , error ) {
104
+ return func (ctx context.Context , c * cache.Client , urlName string ) (string , string , * types.Repo , bool , error ) {
105
+ return LoadWithConfig (ctx , c , urlName , NewGithubEnterpriseConfig (prefix ))
106
+ }
107
+ }
108
+
109
+ func Load (ctx context.Context , c * cache.Client , urlName string ) (string , string , * types.Repo , bool , error ) {
110
+ return LoadWithConfig (ctx , c , urlName , DEFAULT_GITHUB_CONFIG )
111
+ }
112
+
113
+ func NewGithubEnterpriseConfig (prefix string ) * GithubConfig {
114
+ return & GithubConfig {
115
+ Prefix : prefix ,
116
+ RepoURL : fmt .Sprintf ("https://%s/%%s/%%s.git" , prefix ),
117
+ DownloadURL : fmt .Sprintf ("https://raw.%s/%%s/%%s/%%s/%%s" , prefix ),
118
+ CommitURL : fmt .Sprintf ("https://%s/api/v3/repos/%%s/%%s/commits/%%s" , prefix ),
119
+ AuthToken : os .Getenv ("GH_ENTERPRISE_TOKEN" ),
120
+ }
121
+ }
122
+
123
+ func LoadWithConfig (ctx context.Context , _ * cache.Client , urlName string , config * GithubConfig ) (string , string , * types.Repo , bool , error ) {
124
+ if ! strings .HasPrefix (urlName , config .Prefix ) {
93
125
return "" , "" , nil , false , nil
94
126
}
95
127
@@ -107,12 +139,12 @@ func Load(ctx context.Context, _ *cache.Client, urlName string) (string, string,
107
139
account , repo := parts [1 ], parts [2 ]
108
140
path := strings .Join (parts [3 :], "/" )
109
141
110
- ref , err := getCommit (ctx , account , repo , ref )
142
+ ref , err := getCommit (ctx , account , repo , ref , config )
111
143
if err != nil {
112
144
return "" , "" , nil , false , err
113
145
}
114
146
115
- downloadURL := fmt .Sprintf (githubDownloadURL , account , repo , ref , path )
147
+ downloadURL := fmt .Sprintf (config . DownloadURL , account , repo , ref , path )
116
148
if path == "" || path == "/" || ! strings .Contains (parts [len (parts )- 1 ], "." ) {
117
149
var (
118
150
testPath string
@@ -124,13 +156,20 @@ func Load(ctx context.Context, _ *cache.Client, urlName string) (string, string,
124
156
} else {
125
157
testPath = path + "/" + ext
126
158
}
127
- testURL = fmt .Sprintf (githubDownloadURL , account , repo , ref , testPath )
159
+ testURL = fmt .Sprintf (config . DownloadURL , account , repo , ref , testPath )
128
160
if i == len (types .DefaultFiles )- 1 {
129
161
// no reason to test the last one, we are just going to use it. Being that the default list is only
130
162
// two elements this loop could have been one check, but hey over-engineered code ftw.
131
163
break
132
164
}
133
- if resp , err := http .Head (testURL ); err == nil {
165
+ headReq , err := http .NewRequest ("HEAD" , testURL , nil )
166
+ if err != nil {
167
+ break
168
+ }
169
+ if config .AuthToken != "" {
170
+ headReq .Header .Add ("Authorization" , "Bearer " + config .AuthToken )
171
+ }
172
+ if resp , err := http .DefaultClient .Do (headReq ); err == nil {
134
173
_ = resp .Body .Close ()
135
174
if resp .StatusCode == 200 {
136
175
break
@@ -141,9 +180,9 @@ func Load(ctx context.Context, _ *cache.Client, urlName string) (string, string,
141
180
path = testPath
142
181
}
143
182
144
- return downloadURL , githubAuthToken , & types.Repo {
183
+ return downloadURL , config . AuthToken , & types.Repo {
145
184
VCS : "git" ,
146
- Root : fmt .Sprintf (githubRepoURL , account , repo ),
185
+ Root : fmt .Sprintf (config . RepoURL , account , repo ),
147
186
Path : gpath .Dir (path ),
148
187
Name : gpath .Base (path ),
149
188
Revision : ref ,
0 commit comments