Skip to content

Commit f9f3496

Browse files
committed
Add support for GITHUB_TOKEN environment variable
1 parent 6989430 commit f9f3496

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ You can also visit the web-based equivalent at https://dash.ready-to-review.dev/
2424

2525
## macOS Quick Start ⚡ (How to Get Honked At)
2626

27+
### Option 1: Using GitHub CLI (Default)
28+
2729
Install dependencies: the [GitHub CLI, aka "gh"](https://cli.github.com/) and [Go](https://go.dev/):
2830

2931
```bash
@@ -38,6 +40,21 @@ git clone https://github.com/ready-to-review/goose.git
3840
cd goose && make run
3941
```
4042

43+
### Option 2: Using a GitHub Token (More Control)
44+
45+
If you want more control over which repositories the goose can access, you can use a GitHub personal access token instead:
46+
47+
1. Create a [GitHub personal access token](https://github.com/settings/tokens) with `repo` scope
48+
2. Set the `GITHUB_TOKEN` environment variable:
49+
50+
```bash
51+
export GITHUB_TOKEN=your_token_here
52+
git clone https://github.com/ready-to-review/goose.git
53+
cd goose && make run
54+
```
55+
56+
When `GITHUB_TOKEN` is set, the goose will use it directly instead of the GitHub CLI, giving you precise control over repository access.
57+
4158
## Known Issues
4259

4360
- Blocking logic isn't 100% accurate (we're working on it)

github.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,20 @@ func (app *App) initClients(ctx context.Context) error {
4444
return nil
4545
}
4646

47-
// githubToken retrieves the GitHub token using gh CLI.
47+
// githubToken retrieves the GitHub token from GITHUB_TOKEN env var or gh CLI.
4848
func (*App) githubToken(ctx context.Context) (string, error) {
49+
// First check for GITHUB_TOKEN environment variable
50+
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
51+
token = strings.TrimSpace(token)
52+
const minTokenLength = 20
53+
if len(token) < minTokenLength {
54+
return "", fmt.Errorf("invalid GITHUB_TOKEN length: %d", len(token))
55+
}
56+
log.Println("Using GitHub token from GITHUB_TOKEN environment variable")
57+
return token, nil
58+
}
59+
60+
// Fall back to gh CLI if GITHUB_TOKEN is not set
4961
// Only check absolute paths for security - never use PATH
5062
var trustedPaths []string
5163
switch runtime.GOOS {
@@ -97,7 +109,7 @@ func (*App) githubToken(ctx context.Context) (string, error) {
97109
}
98110

99111
if ghPath == "" {
100-
return "", errors.New("gh cli not found in trusted locations")
112+
return "", errors.New("gh cli not found in trusted locations and GITHUB_TOKEN not set")
101113
}
102114

103115
log.Printf("Executing command: %s auth token", ghPath)
@@ -115,7 +127,7 @@ func (*App) githubToken(ctx context.Context) (string, error) {
115127
if len(token) < minTokenLength {
116128
return "", fmt.Errorf("invalid github token length: %d", len(token))
117129
}
118-
log.Println("Successfully obtained GitHub token")
130+
log.Println("Successfully obtained GitHub token from gh CLI")
119131
return token, nil
120132
}
121133

@@ -200,9 +212,9 @@ func (app *App) fetchPRsInternal(ctx context.Context, waitForTurn bool) (incomin
200212

201213
// Run both queries in parallel
202214
type queryResult struct {
215+
err error
203216
query string
204217
issues []*github.Issue
205-
err error
206218
}
207219

208220
queryResults := make(chan queryResult, 2)
@@ -236,11 +248,13 @@ func (app *App) fetchPRsInternal(ctx context.Context, waitForTurn bool) (incomin
236248
// Collect results from both queries
237249
var allIssues []*github.Issue
238250
seenURLs := make(map[string]bool)
251+
var queryErrors []error
239252

240253
for range 2 {
241254
result := <-queryResults
242255
if result.err != nil {
243256
log.Printf("[GITHUB] Query failed: %s - %v", result.query, result.err)
257+
queryErrors = append(queryErrors, result.err)
244258
// Continue processing other query results even if one fails
245259
continue
246260
}
@@ -257,6 +271,11 @@ func (app *App) fetchPRsInternal(ctx context.Context, waitForTurn bool) (incomin
257271
}
258272
log.Printf("[GITHUB] Both searches completed in %v, found %d unique PRs", time.Since(searchStart), len(allIssues))
259273

274+
// If both queries failed, return an error
275+
if len(queryErrors) == 2 {
276+
return nil, nil, fmt.Errorf("all GitHub queries failed: %v", queryErrors)
277+
}
278+
260279
// Limit PRs for performance
261280
if len(allIssues) > maxPRsToProcess {
262281
log.Printf("Limiting to %d PRs for performance (total: %d)", maxPRsToProcess, len(allIssues))

0 commit comments

Comments
 (0)