Skip to content

feat(github): support github app auth #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from

Conversation

wwcchh0123
Copy link
Contributor

No description provided.

wwcchh0123 and others added 5 commits August 12, 2025 11:41
- Add JWT generation and signing functionality for GitHub App authentication
- Support RS256 signing algorithm as required by GitHub Apps
- Implement private key management with multiple loading methods (file, env, bytes)
- Add comprehensive JWT claims construction and validation
- Include full test coverage for all components
- Support both PKCS#1 and PKCS#8 private key formats

Components added:
- internal/github/app/jwt.go: Core JWT generation logic
- internal/github/app/private_key.go: Private key management
- internal/github/app/claims.go: JWT claims construction
- internal/github/app/jwt_test.go: JWT module tests
- internal/github/app/private_key_test.go: Private key tests

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
…uto-refresh

- Add Installation Access Token management for GitHub Apps
- Implement in-memory token cache with TTL and expiration handling
- Support automatic token refresh with configurable intervals and thresholds
- Add concurrent token refresh with semaphore-based rate limiting
- Include comprehensive error handling and retry mechanisms
- Support token invalidation and cache cleanup operations
- Add permission-specific token creation for fine-grained access control

Components added:
- internal/github/app/installation.go: Installation token manager
- internal/github/app/cache.go: Token caching implementation
- internal/github/app/refresh.go: Automatic refresh logic
- internal/github/app/installation_test.go: Comprehensive test coverage

Features:
- Token expiration safety margin (5 minutes before actual expiration)
- Configurable refresh intervals and concurrency limits
- GitHub API integration with proper authentication headers
- Thread-safe operations with mutex protection
- Metrics and statistics tracking for monitoring

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
…pport

- Extend GitHubConfig to support GitHub App authentication alongside PAT
- Add GitHubAppConfig struct with App ID and private key configuration options
- Support multiple private key loading methods (file path, env var, direct content)
- Implement auth mode selection: "token", "app", or "auto" detection
- Add comprehensive configuration validation and default value handling
- Maintain full backward compatibility with existing PAT configurations
- Add environment variable mapping for all GitHub App settings

New configuration fields:
- github.app.app_id: GitHub App ID
- github.app.private_key_path: Path to private key file
- github.app.private_key_env: Environment variable containing private key
- github.app.private_key: Direct private key content (not recommended)
- github.auth_mode: Authentication mode selection

Environment variables:
- GITHUB_APP_ID: GitHub App ID
- GITHUB_APP_PRIVATE_KEY_PATH: Private key file path
- GITHUB_APP_PRIVATE_KEY_ENV: Private key environment variable name
- GITHUB_APP_PRIVATE_KEY: Direct private key content
- GITHUB_AUTH_MODE: Force specific authentication mode

Features:
- Automatic auth mode detection based on available configuration
- Comprehensive validation with helpful error messages
- Helper methods for checking configuration state
- Full test coverage including backward compatibility verification

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
…terfaces

- Create unified Authenticator interface supporting both PAT and GitHub App authentication
- Implement PATAuthenticator for Personal Access Token authentication
- Implement GitHubAppAuthenticator for GitHub App authentication with installation support
- Add DefaultClientFactory for streamlined GitHub client creation
- Build AuthenticatorBuilder for configuration-driven authenticator creation
- Support HybridAuthenticator with primary/fallback authentication strategies
- Provide comprehensive error handling and validation across all authenticators

Core interfaces:
- Authenticator: Unified authentication interface with client creation methods
- ClientFactory: Simplified client creation with authentication abstraction
- AuthInfo: Detailed authentication metadata and permissions

Authentication implementations:
- PATAuthenticator: OAuth2-based PAT authentication with user validation
- GitHubAppAuthenticator: JWT + Installation token authentication with app validation
- HybridAuthenticator: Multi-method authentication with graceful fallback

Key features:
- Automatic authentication method detection from configuration
- Installation-specific client creation for GitHub Apps
- Comprehensive validation and access testing
- Thread-safe operations with proper error propagation
- Full backward compatibility with existing PAT workflows
- Support for custom HTTP clients and base URLs for testing

Components added:
- internal/github/auth/interfaces.go: Core authentication interfaces
- internal/github/auth/pat_authenticator.go: PAT authentication implementation
- internal/github/auth/app_authenticator.go: GitHub App authentication implementation
- internal/github/auth/factory.go: Client factory and builder implementations
- internal/github/auth/auth_test.go: Comprehensive test coverage

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Implement automatic authentication detection and switching between PAT and GitHub App based on configuration.

## Key Features
- GitHubClientManager with automatic auth mode detection
- Context-based installation ID handling for GitHub App webhook events
- Backward compatibility with existing PAT authentication
- New Agent methods for accessing auto-switching GitHub clients
- Comprehensive unit tests for the new functionality

## Implementation Details
- Created GitHubClientManager in internal/github/manager.go
- Enhanced Agent with GetGitHubClient() and GetGitHubInstallationClient() methods
- Updated webhook handlers to extract installation ID from GitHub events
- Added installation ID context utilities for GitHub App support
- Maintained full backward compatibility with existing codebase

## Configuration Support
- Automatically detects GitHub App vs PAT configuration
- Falls back gracefully when GitHub App auth fails
- Logs authentication method being used for debugging

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link

codecov bot commented Aug 14, 2025

Codecov Report

❌ Patch coverage is 42.70833% with 385 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@e487a75). Learn more about missing BASE report.

Files with missing lines Patch % Lines
internal/github/client.go 0.00% 116 Missing ⚠️
internal/github/auth/factory.go 49.59% 52 Missing and 10 partials ⚠️
internal/github/auth/app_authenticator.go 42.68% 43 Missing and 4 partials ⚠️
internal/github/manager.go 48.10% 37 Missing and 4 partials ⚠️
internal/agent/enhanced_agent.go 22.22% 28 Missing ⚠️
internal/github/auth/pat_authenticator.go 56.86% 20 Missing and 2 partials ⚠️
internal/webhook/handler.go 29.03% 22 Missing ⚠️
internal/webhook/signature_validation.go 62.50% 12 Missing and 6 partials ⚠️
internal/config/config.go 86.20% 8 Missing and 4 partials ⚠️
cmd/server/main.go 0.00% 8 Missing ⚠️
... and 3 more
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #303   +/-   ##
=======================================
  Coverage        ?   16.71%           
=======================================
  Files           ?       43           
  Lines           ?     9033           
  Branches        ?        0           
=======================================
  Hits            ?     1510           
  Misses          ?     7431           
  Partials        ?       92           
Flag Coverage Δ
unittests 16.71% <42.70%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -171,6 +197,11 @@ func loadFromEnv() *Config {
CodeProvider: getEnvOrDefault("CODE_PROVIDER", "claude"),
UseDocker: getEnvBoolOrDefault("USE_DOCKER", true),
}

// Load additional environment variables including GitHub App config
config.loadFromEnv()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

循环引用了?

)

type Client struct {
client *github.Client
config *config.Config
manager *GitHubClientManager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为何不能一开始就确定好具体的client类型?

@CarlJi CarlJi closed this Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants