1
1
package github
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
6
7
"io"
@@ -9,7 +10,9 @@ import (
9
10
"path/filepath"
10
11
"strings"
11
12
13
+ "github.com/gptscript-ai/gptscript/pkg/cache"
12
14
"github.com/gptscript-ai/gptscript/pkg/loader"
15
+ "github.com/gptscript-ai/gptscript/pkg/repos/git"
13
16
"github.com/gptscript-ai/gptscript/pkg/system"
14
17
"github.com/gptscript-ai/gptscript/pkg/types"
15
18
)
@@ -29,11 +32,14 @@ func init() {
29
32
loader .AddVSC (Load )
30
33
}
31
34
32
- func getCommit (account , repo , ref string ) (string , error ) {
33
- url := fmt .Sprintf (githubCommitURL , account , repo , ref )
34
- client := & http.Client {}
35
+ func getCommitLsRemote (ctx context.Context , account , repo , ref string ) (string , error ) {
36
+ url := fmt .Sprintf (githubRepoURL , account , repo )
37
+ return git .LsRemote (ctx , url , ref )
38
+ }
35
39
36
- req , err := http .NewRequest (http .MethodGet , url , nil )
40
+ func getCommit (ctx context.Context , account , repo , ref string ) (string , error ) {
41
+ url := fmt .Sprintf (githubCommitURL , account , repo , ref )
42
+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
37
43
if err != nil {
38
44
return "" , fmt .Errorf ("failed to create request of %s/%s at %s: %w" , account , repo , url , err )
39
45
}
@@ -42,13 +48,15 @@ func getCommit(account, repo, ref string) (string, error) {
42
48
req .Header .Add ("Authorization" , "Bearer " + githubAuthToken )
43
49
}
44
50
45
- resp , err := client .Do (req )
46
-
51
+ resp , err := http .DefaultClient .Do (req )
47
52
if err != nil {
48
53
return "" , err
49
54
} else if resp .StatusCode != http .StatusOK {
50
55
c , _ := io .ReadAll (resp .Body )
51
56
resp .Body .Close ()
57
+ if commit , err := getCommitLsRemote (ctx , account , repo , ref ); err == nil {
58
+ return commit , nil
59
+ }
52
60
return "" , fmt .Errorf ("failed to get GitHub commit of %s/%s at %s: %s %s" ,
53
61
account , repo , ref , resp .Status , c )
54
62
}
@@ -68,7 +76,7 @@ func getCommit(account, repo, ref string) (string, error) {
68
76
return commit .SHA , nil
69
77
}
70
78
71
- func Load (urlName string ) (string , * types.Repo , bool , error ) {
79
+ func Load (ctx context. Context , _ * cache. Client , urlName string ) (string , * types.Repo , bool , error ) {
72
80
if ! strings .HasPrefix (urlName , GithubPrefix ) {
73
81
return "" , nil , false , nil
74
82
}
@@ -93,7 +101,7 @@ func Load(urlName string) (string, *types.Repo, bool, error) {
93
101
path += "/tool.gpt"
94
102
}
95
103
96
- ref , err := getCommit (account , repo , ref )
104
+ ref , err := getCommit (ctx , account , repo , ref )
97
105
if err != nil {
98
106
return "" , nil , false , err
99
107
}
0 commit comments