Skip to content

Commit 87a577c

Browse files
authored
fix: authenticate GitHub API calls to avoid 60/hr unauthenticated rate limit (#411)
fix: authenticate github login for ratelimit with token
1 parent ef29aac commit 87a577c

4 files changed

Lines changed: 46 additions & 3 deletions

File tree

bin/install-latest.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ case "${ARCH}" in
99
aarch64) ARCH="arm64" ;;
1010
esac
1111

12+
# GitHub API token: GITHUB_TOKEN env, else gh auth token.
13+
GITHUB_TOKEN="${GITHUB_TOKEN:-}"
14+
if [ -z "${GITHUB_TOKEN}" ] && command -v gh >/dev/null 2>&1; then
15+
GITHUB_TOKEN="$(gh auth token 2>/dev/null || true)"
16+
fi
17+
1218
# Fetch release metadata from GitHub API
1319
API_RESPONSE="$(curl -sf ${GITHUB_TOKEN:+-H "Authorization: token ${GITHUB_TOKEN}"} https://api.github.com/repos/brevdev/brev-cli/releases/latest)" || {
1420
echo "Error: Failed to fetch release info from GitHub API." >&2

pkg/cmd/agentskill/agentskill.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
breverrors "github.com/brevdev/brev-cli/pkg/errors"
14+
"github.com/brevdev/brev-cli/pkg/store"
1415
"github.com/brevdev/brev-cli/pkg/terminal"
1516
"github.com/spf13/cobra"
1617
)
@@ -58,6 +59,9 @@ func resolveCommitSHA(client *http.Client, ref string) (string, error) {
5859
return "", breverrors.WrapAndTrace(err)
5960
}
6061
req.Header.Set("Accept", "application/vnd.github.v3+json")
62+
if token := store.GitHubAPIToken(); token != "" {
63+
req.Header.Set("Authorization", "token "+token)
64+
}
6165

6266
resp, err := client.Do(req) //nolint:bodyclose // closed below
6367
if err != nil {

pkg/store/release.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package store
22

33
import (
4+
"context"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
"time"
9+
410
breverrors "github.com/brevdev/brev-cli/pkg/errors"
511
"github.com/go-resty/resty/v2"
612
)
@@ -14,15 +20,36 @@ type GithubReleaseMetadata struct {
1420
}
1521

1622
const (
17-
cliReleaseURL = "https://api.github.com/repos/brevdev/brev-cli/releases/latest"
23+
cliReleaseURL = "https://api.github.com/repos/brevdev/brev-cli/releases/latest"
24+
ghAuthTokenTimeout = 60 * time.Second
1825
)
1926

27+
// GitHubAPIToken returns GITHUB_TOKEN from the environment, or "gh auth token" with a timeout.
28+
func GitHubAPIToken() string {
29+
if token := strings.TrimSpace(os.Getenv("GITHUB_TOKEN")); token != "" {
30+
return token
31+
}
32+
33+
ctx, cancel := context.WithTimeout(context.Background(), ghAuthTokenTimeout)
34+
defer cancel()
35+
36+
out, err := exec.CommandContext(ctx, "gh", "auth", "token").Output() //nolint:gosec // intentional gh probe
37+
if err != nil {
38+
return ""
39+
}
40+
return strings.TrimSpace(string(out))
41+
}
42+
2043
func (n NoAuthHTTPStore) GetLatestReleaseMetadata() (*GithubReleaseMetadata, error) {
2144
var result GithubReleaseMetadata
2245

2346
client := resty.New()
47+
req := client.R().SetResult(&result)
48+
if token := GitHubAPIToken(); token != "" {
49+
req.SetHeader("Authorization", "token "+token)
50+
}
2451

25-
res, err := client.R().SetResult(&result).Get(cliReleaseURL)
52+
res, err := req.Get(cliReleaseURL)
2653
if err != nil {
2754
return nil, breverrors.WrapAndTrace(err)
2855
}

scripts/install-agent-skill.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
set -e
1515

16+
# GitHub API token: GITHUB_TOKEN env, else gh auth token.
17+
GITHUB_TOKEN="${GITHUB_TOKEN:-}"
18+
if [ -z "${GITHUB_TOKEN}" ] && command -v gh >/dev/null 2>&1; then
19+
GITHUB_TOKEN="$(gh auth token 2>/dev/null || true)"
20+
fi
21+
1622
# Configuration
1723
REPO="brevdev/brev-cli"
1824
BRANCH="main"
@@ -116,7 +122,7 @@ done
116122
rm -rf "$TMPDIR"
117123

118124
# Resolve commit SHA and write .version file
119-
VERSION_RESPONSE=$(curl -fsSL "https://api.github.com/repos/$REPO/commits/$BRANCH" 2>&1) || true
125+
VERSION_RESPONSE=$(curl -fsSL ${GITHUB_TOKEN:+-H "Authorization: token ${GITHUB_TOKEN}"} "https://api.github.com/repos/$REPO/commits/$BRANCH" 2>&1) || true
120126
if echo "$VERSION_RESPONSE" | grep -q "API rate limit exceeded"; then
121127
echo -e " ${YELLOW}${NC} .version (skipped — GitHub API rate limit exceeded)"
122128
echo -e " ${YELLOW}If you are using a VPN, try turning it off and running this script again.${NC}"

0 commit comments

Comments
 (0)