5
5
"fmt"
6
6
"io"
7
7
"net/http"
8
+ "os"
8
9
"path/filepath"
9
10
"strings"
10
11
@@ -20,19 +21,35 @@ const (
20
21
githubCommitURL = "https://api.github.com/repos/%s/%s/commits/%s"
21
22
)
22
23
24
+ var (
25
+ githubAuthToken = os .Getenv ("GITHUB_AUTH_TOKEN" )
26
+ )
27
+
23
28
func init () {
24
29
loader .AddVSC (Load )
25
30
}
26
31
27
32
func getCommit (account , repo , ref string ) (string , error ) {
28
33
url := fmt .Sprintf (githubCommitURL , account , repo , ref )
29
- resp , err := http .Get (url )
34
+ client := & http.Client {}
35
+
36
+ req , err := http .NewRequest (http .MethodGet , url , nil )
37
+ if err != nil {
38
+ return "" , fmt .Errorf ("failed to create request of %s/%s at %s: %w" , account , repo , url , err )
39
+ }
40
+
41
+ if githubAuthToken != "" {
42
+ req .Header .Add ("Authorization" , "Bearer " + githubAuthToken )
43
+ }
44
+
45
+ resp , err := client .Do (req )
46
+
30
47
if err != nil {
31
48
return "" , err
32
49
} else if resp .StatusCode != http .StatusOK {
33
50
c , _ := io .ReadAll (resp .Body )
34
51
resp .Body .Close ()
35
- return "" , fmt .Errorf ("failed to GitHub commit of %s/%s at %s: %s %s" ,
52
+ return "" , fmt .Errorf ("failed to get GitHub commit of %s/%s at %s: %s %s" ,
36
53
account , repo , ref , resp .Status , c )
37
54
}
38
55
defer resp .Body .Close ()
0 commit comments