Skip to content

Commit 624f9b7

Browse files
Merge pull request #229 from nw0rn/github-auth-token
Add option to set github auth token
2 parents e224772 + bb33f6f commit 624f9b7

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

pkg/loader/github/github.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
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+
2328
func init() {
2429
loader.AddVSC(Load)
2530
}
2631

2732
func 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

Comments
 (0)