@@ -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.
4848func (* 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