55 "fmt"
66 "io"
77 "net/http"
8+ "os"
89 "path/filepath"
910 "strings"
1011
@@ -20,19 +21,35 @@ const (
2021 githubCommitURL = "https://api.github.com/repos/%s/%s/commits/%s"
2122)
2223
24+ var (
25+ githubAuthToken = os .Getenv ("GITHUB_AUTH_TOKEN" )
26+ )
27+
2328func init () {
2429 loader .AddVSC (Load )
2530}
2631
2732func getCommit (account , repo , ref string ) (string , error ) {
2833 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+
3047 if err != nil {
3148 return "" , err
3249 } else if resp .StatusCode != http .StatusOK {
3350 c , _ := io .ReadAll (resp .Body )
3451 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" ,
3653 account , repo , ref , resp .Status , c )
3754 }
3855 defer resp .Body .Close ()
0 commit comments