@@ -49,9 +49,8 @@ func (*App) githubToken(ctx context.Context) (string, error) {
4949 // First check for GITHUB_TOKEN environment variable
5050 if token := os .Getenv ("GITHUB_TOKEN" ); token != "" {
5151 token = strings .TrimSpace (token )
52- const minTokenLength = 20
53- if len (token ) < minTokenLength {
54- return "" , fmt .Errorf ("invalid GITHUB_TOKEN length: %d" , len (token ))
52+ if err := validateGitHubToken (token ); err != nil {
53+ return "" , fmt .Errorf ("invalid GITHUB_TOKEN: %w" , err )
5554 }
5655 log .Println ("Using GitHub token from GITHUB_TOKEN environment variable" )
5756 return token , nil
@@ -97,8 +96,11 @@ func (*App) githubToken(ctx context.Context) (string, error) {
9796 const executableMask = 0o111
9897 if info .Mode ().IsRegular () && info .Mode ()& executableMask != 0 {
9998 // Verify it's actually the gh binary by running version command
100- cmd := exec .Command (path , "version" ) //nolint:noctx // Quick version check doesn't need context
99+ // Use timeout to prevent hanging
100+ versionCtx , cancel := context .WithTimeout (ctx , 2 * time .Second )
101+ cmd := exec .CommandContext (versionCtx , path , "version" )
101102 output , err := cmd .Output ()
103+ cancel () // Call cancel immediately after command execution
102104 if err == nil && strings .Contains (string (output ), "gh version" ) {
103105 log .Printf ("Found and verified gh at: %s" , path )
104106 ghPath = path
@@ -116,16 +118,12 @@ func (*App) githubToken(ctx context.Context) (string, error) {
116118 cmd := exec .CommandContext (ctx , ghPath , "auth" , "token" )
117119 output , err := cmd .CombinedOutput ()
118120 if err != nil {
119- log .Printf ("gh command failed with output : %s " , string ( output ) )
120- return "" , fmt .Errorf ("exec 'gh auth token': %w (output: %s) " , err , string ( output ) )
121+ log .Printf ("gh command failed: %v " , err )
122+ return "" , fmt .Errorf ("exec 'gh auth token': %w" , err )
121123 }
122124 token := strings .TrimSpace (string (output ))
123- if token == "" {
124- return "" , errors .New ("empty github token" )
125- }
126- const minTokenLength = 20
127- if len (token ) < minTokenLength {
128- return "" , fmt .Errorf ("invalid github token length: %d" , len (token ))
125+ if err := validateGitHubToken (token ); err != nil {
126+ return "" , fmt .Errorf ("invalid token from gh CLI: %w" , err )
129127 }
130128 log .Println ("Successfully obtained GitHub token from gh CLI" )
131129 return token , nil
0 commit comments