Skip to content

Commit

Permalink
Merge 9b5e304 into d609367
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW authored Feb 13, 2023
2 parents d609367 + 9b5e304 commit f2e9065
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ jobs:
- name: Run setup as a action (1/2)
uses: ./
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repo: k1LoW/tbls
force: true
gh-setup-version: v0.6.0
Expand Down
49 changes: 40 additions & 9 deletions gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"io"
"io/fs"
"os"
"net/http"
"regexp"
"runtime"
"sort"
Expand Down Expand Up @@ -218,17 +218,18 @@ func makeFS(owner, repo string, a *github.ReleaseAsset) (fs.FS, error) {
}

func downloadAsset(owner, repo string, a *github.ReleaseAsset) ([]byte, error) {
client, err := gh.HTTPClient(&api.ClientOptions{
Headers: map[string]string{
"Accept": "application/octet-stream",
},
})
client, err := httpClient()
if err != nil {
return nil, err
}
_, v3ep, _, _ := factory.GetTokenAndEndpoints()
u := fmt.Sprintf("%s/repos/%s/%s/releases/assets/%d", v3ep, owner, repo, a.GetID())
resp, err := client.Get(u)
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/octet-stream")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -252,8 +253,7 @@ func contains(s []string, e string) bool {
func client(ctx context.Context) (*github.Client, error) {
token, _, _, _ := factory.GetTokenAndEndpoints()
if token == "" {
// Successful unauthenticated API call with one failed authentication instead of skipping authentication immediately.
os.Setenv("GITHUB_TOKEN", "gh-setup-fake-token")
return factory.NewGithubClient(factory.SkipAuth(true))
}
c, err := factory.NewGithubClient()
if err != nil {
Expand All @@ -264,3 +264,34 @@ func client(ctx context.Context) (*github.Client, error) {
}
return c, nil
}

func httpClient() (*http.Client, error) {
token, v3ep, _, _ := factory.GetTokenAndEndpoints()
if token == "" {
return &http.Client{
Timeout: 30 * time.Second,
Transport: http.DefaultTransport.(*http.Transport).Clone(),
}, nil
}
client, err := gh.HTTPClient(&api.ClientOptions{})
if err != nil {
return nil, err
}
u := fmt.Sprintf("%s/user", v3ep)
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
client = &http.Client{
Timeout: 30 * time.Second,
Transport: http.DefaultTransport.(*http.Transport).Clone(),
}
}
return client, nil
}
2 changes: 2 additions & 0 deletions scripts/run-gh-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ token=${GH_SETUP_GITHUB_TOKEN}
if [ -z "${token}" ]; then
token=${GITHUB_TOKEN}
fi
export GITHUB_TOKEN=${token}

repo=${GH_SETUP_REPO}
bindir=${GH_SETUP_BIN_DIR}
version=${GH_SETUP_VERSION}
Expand Down

0 comments on commit f2e9065

Please sign in to comment.