Skip to content

Add option to set github auth token #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions pkg/loader/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"

Expand All @@ -20,19 +21,35 @@ const (
githubCommitURL = "https://api.github.com/repos/%s/%s/commits/%s"
)

var (
githubAuthToken = os.Getenv("GITHUB_AUTH_TOKEN")
)

func init() {
loader.AddVSC(Load)
}

func getCommit(account, repo, ref string) (string, error) {
url := fmt.Sprintf(githubCommitURL, account, repo, ref)
resp, err := http.Get(url)
client := &http.Client{}

req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return "", fmt.Errorf("failed to create request of %s/%s at %s: %w", account, repo, url, err)
}

if githubAuthToken != "" {
req.Header.Add("Authorization", "Bearer "+githubAuthToken)
}

resp, err := client.Do(req)

if err != nil {
return "", err
} else if resp.StatusCode != http.StatusOK {
c, _ := io.ReadAll(resp.Body)
resp.Body.Close()
return "", fmt.Errorf("failed to GitHub commit of %s/%s at %s: %s %s",
return "", fmt.Errorf("failed to get GitHub commit of %s/%s at %s: %s %s",
account, repo, ref, resp.Status, c)
}
defer resp.Body.Close()
Expand Down