Skip to content

Commit b69bd17

Browse files
wwcchh0123claude
andcommitted
feat: implement PR-5 GitHub Client auto authentication switching
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>
1 parent 3b3d047 commit b69bd17

File tree

5 files changed

+456
-244
lines changed

5 files changed

+456
-244
lines changed

docs/app-issue.md

Lines changed: 44 additions & 243 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
### GitHub App 认证需求拆分为 PR 计划
1+
### GitHub App 基础认证功能实现计划
22

33
#### 任务清单
44
- [x] 分析之前的需求文档结构
55
- [x] 将需求按逻辑模块拆分为独立 PR
66
- [x] 为每个 PR 生成详细的架构文档
77
- [x] 定义 PR 之间的依赖关系和实施顺序
8-
- [x] 完成最终的 PR 拆分计划
8+
- [x] 完成基础功能的 PR 拆分计划
99

1010
---
1111

1212
## 📋 PR 拆分方案概览
1313

14-
基于之前的需求分析,我将把 GitHub App 认证功能拆分为 **9 个独立的 PR**按照依赖关系和逻辑模块划分
14+
基于需求分析,将 GitHub App 基础认证功能拆分为 **4 个独立的 PR**实现核心认证功能
1515

1616
### 🏗️ 核心基础设施层 (PR 1-4)
1717
- **PR-1**: JWT 生成和签名模块
1818
- **PR-2**: Installation Token 管理
1919
- **PR-3**: 配置系统扩展
2020
- **PR-4**: 认证抽象层重构
2121

22-
### 🏢 多租户支持层 (PR 5-6)
23-
- **PR-5**: Installation 管理和发现
24-
- **PR-6**: 多租户工作空间隔离
25-
26-
### 🔄 兼容性和迁移层 (PR 7-8)
27-
- **PR-7**: 向后兼容性实现
28-
- **PR-8**: 配置迁移工具
29-
30-
### 🚀 增强功能层 (PR 9)
31-
- **PR-9**: 监控、权限验证和文档
22+
### 🎯 核心目标
23+
实现 GitHub App 认证功能,当配置了 GitHub App 时,GitHub Client 自动使用 App 认证方式,保持与现有 PAT 认证的兼容性。
3224

3325
---
3426

@@ -242,230 +234,59 @@ func (f *ClientFactory) CreateClient(ctx context.Context, installationID int64)
242234

243235
#### 🔗 **依赖关系**
244236
- **前置依赖**: PR-1, PR-2, PR-3
245-
- **后续依赖**: PR-5 (Installation 管理)
246-
247-
---
248-
249-
### PR-5: Installation 管理和发现
250-
251-
#### 🎯 **功能范围**
252-
- GitHub App Installation 发现和枚举
253-
- Installation 元数据管理
254-
- Webhook 到 Installation 的映射
255-
- Installation 权限验证
256-
257-
#### 🏛️ **架构设计**
258-
```
259-
internal/installation/
260-
├── manager.go # Installation 管理器
261-
├── discovery.go # Installation 发现
262-
├── metadata.go # 元数据管理
263-
└── webhook_mapper.go # Webhook 映射
264-
```
265-
266-
#### 🔧 **核心组件**
267-
268-
**Installation Manager**
269-
```go
270-
type InstallationManager struct {
271-
authenticator github.Authenticator
272-
cache InstallationCache
273-
}
274-
275-
type Installation struct {
276-
ID int64
277-
AccountType string // "Organization" | "User"
278-
AccountLogin string
279-
Permissions map[string]string
280-
Events []string
281-
}
282-
```
283-
284-
#### ⚙️ **实现原理**
285-
1. **发现机制**: 定期调用 GitHub API 发现新的 Installation
286-
2. **Webhook 映射**: 基于 Repository Owner 或 Installation ID 确定目标 Installation
287-
3. **权限检查**: 验证 Installation 是否具有必要的权限
288-
4. **缓存策略**: 缓存 Installation 信息减少 API 调用
289-
290-
#### 🔗 **依赖关系**
291-
- **前置依赖**: PR-4 (认证抽象层)
292-
- **后续依赖**: PR-6 (工作空间隔离)
293-
294-
---
237+
- **后续依赖**: PR-5 (Client自动切换)
295238

296-
### PR-6: 多租户工作空间隔离
239+
### PR-5: GitHub Client 自动认证切换
297240

298241
#### 🎯 **功能范围**
299-
- 扩展 Workspace 模型支持 Installation ID
300-
- 基于 Installation 的数据隔离
301-
- 工作空间清理和管理
302-
- 多租户安全检查
242+
- 在配置了 GitHub App 时,GitHub Client 自动使用 App 认证
243+
- 保持与现有 PAT 认证的完全兼容性
244+
- 运行时认证方式自动检测和切换
245+
- 简化的客户端获取接口
303246

304247
#### 🏛️ **架构设计**
305248
```go
306-
// 扩展现有 Workspace 结构
307-
type Workspace struct {
308-
ID string
309-
InstallationID int64 // 新增字段
310-
RepoOwner string
311-
RepoName string
312-
Branch string
313-
WorkDir string
314-
CreatedAt time.Time
249+
// 在现有 GitHub Client 基础上添加认证方式检测
250+
type GitHubClientManager struct {
251+
config *Config
252+
authenticator Authenticator // PR-4 中定义的接口
315253
}
316254

317-
type MultiTenantWorkspaceManager struct {
318-
baseManager *workspace.Manager
319-
installations *installation.Manager
320-
}
321-
```
322-
323-
#### ⚙️ **实现原理**
324-
1. **隔离策略**: 基于 Installation ID 的文件夹隔离
325-
2. **安全检查**: 确保工作空间只能访问对应 Installation 的仓库
326-
3. **资源管理**: 按 Installation 统计和限制资源使用
327-
4. **清理机制**: 基于 Installation 状态清理无效工作空间
328-
329-
#### 🔗 **依赖关系**
330-
- **前置依赖**: PR-5 (Installation 管理)
331-
- **后续依赖**: PR-7 (向后兼容实现)
332-
333-
---
334-
335-
### PR-7: 向后兼容性实现
336-
337-
#### 🎯 **功能范围**
338-
- 同时支持 PAT 和 GitHub App 两种认证方式
339-
- 平滑的认证方式切换
340-
- 现有 API 接口保持不变
341-
- 运行时认证方式检测
342-
343-
#### 🏛️ **架构设计**
344-
```go
345-
type HybridAuthenticator struct {
346-
patAuth *PATAuthenticator
347-
appAuth *GitHubAppAuthenticator
348-
authMode AuthMode
349-
}
350-
351-
func (h *HybridAuthenticator) GetClient(ctx context.Context) (*github.Client, error) {
352-
switch h.authMode {
353-
case AuthModePAT:
354-
return h.patAuth.GetClient(ctx)
355-
case AuthModeApp:
356-
// App 模式需要 Installation ID,从上下文获取
357-
return h.getAppClient(ctx)
358-
default:
359-
return h.autoDetectAndGetClient(ctx)
255+
func (m *GitHubClientManager) GetClient(ctx context.Context) (*github.Client, error) {
256+
// 自动检测配置的认证方式
257+
if m.config.GitHub.App.AppID != 0 {
258+
// 使用 GitHub App 认证
259+
return m.authenticator.GetInstallationClient(ctx, m.getInstallationID(ctx))
360260
}
261+
// 回退到 PAT 认证
262+
return m.authenticator.GetClient(ctx)
361263
}
362264
```
363265

364-
#### ⚙️ **实现原理**
365-
1. **适配器模式**: 包装现有认证实现,提供统一接口
366-
2. **自动检测**: 基于配置和上下文自动选择合适的认证方式
367-
3. **优雅降级**: GitHub App 不可用时自动回退到 PAT
368-
4. **兼容性保证**: 现有代码无需修改即可工作
369-
370-
#### 🔗 **依赖关系**
371-
- **前置依赖**: PR-6 (多租户工作空间隔离)
372-
- **后续依赖**: PR-8 (配置迁移工具)
373-
374-
---
375-
376-
### PR-8: 配置迁移工具
377-
378-
#### 🎯 **功能范围**
379-
- PAT 到 GitHub App 配置转换工具
380-
- 配置验证和测试工具
381-
- 迁移指南和文档
382-
- 配置备份和回滚机制
383-
384-
#### 🏛️ **架构设计**
385-
```
386-
cmd/migrate/
387-
├── main.go # 迁移工具入口
388-
├── pat_to_app.go # PAT 转 App 逻辑
389-
├── validate.go # 配置验证
390-
└── backup.go # 备份和回滚
391-
```
392-
393266
#### 🔧 **核心组件**
394267

395-
**Migration Tool**
268+
**认证方式检测器**
396269
```go
397-
type MigrationTool struct {
398-
configPath string
399-
backupPath string
400-
dryRun bool
270+
type AuthModeDetector struct {
271+
config *Config
401272
}
402273

403-
func (m *MigrationTool) MigratePATToApp(appConfig GitHubAppConfig) error
404-
func (m *MigrationTool) ValidateConfiguration() error
405-
func (m *MigrationTool) TestConnectivity() error
406-
```
407-
408-
#### ⚙️ **实现原理**
409-
1. **安全第一**: 迁移前自动备份现有配置
410-
2. **验证机制**: 迁移后测试新配置的连接性和权限
411-
3. **Dry Run**: 支持预览模式,不实际修改配置
412-
4. **详细日志**: 记录迁移过程,便于问题诊断
413-
414-
#### 🔗 **依赖关系**
415-
- **前置依赖**: PR-7 (向后兼容实现)
416-
- **后续依赖**: PR-9 (监控和文档)
417-
418-
---
419-
420-
### PR-9: 监控、权限验证和文档
421-
422-
#### 🎯 **功能范围**
423-
- 认证相关的监控指标
424-
- 细粒度权限验证中间件
425-
- 完整的部署和使用文档
426-
- 故障排查手册
427-
428-
#### 🏛️ **架构设计**
429-
```
430-
internal/middleware/
431-
├── auth_middleware.go # 认证中间件
432-
├── permission_check.go # 权限检查
433-
└── metrics.go # 监控指标
434-
435-
docs/
436-
├── github-app-setup.md # GitHub App 创建指南
437-
├── deployment.md # 部署文档
438-
└── troubleshooting.md # 故障排查
439-
```
440-
441-
#### 🔧 **核心组件**
442-
443-
**权限验证中间件**
444-
```go
445-
type PermissionMiddleware struct {
446-
requiredPermissions []string
447-
}
448-
449-
func (m *PermissionMiddleware) CheckPermissions(ctx context.Context, installationID int64) error
450-
```
451-
452-
**监控指标**
453-
```go
454-
type AuthMetrics struct {
455-
TokenRefreshCount prometheus.Counter
456-
AuthenticationFailures prometheus.Counter
457-
InstallationCount prometheus.Gauge
274+
func (d *AuthModeDetector) DetectAuthMode() AuthMode {
275+
if d.config.GitHub.App.AppID != 0 && d.config.GitHub.App.PrivateKeyPath != "" {
276+
return AuthModeApp
277+
}
278+
return AuthModePAT
458279
}
459280
```
460281

461282
#### ⚙️ **实现原理**
462-
1. **可观测性**: Prometheus 指标 + 结构化日志
463-
2. **权限最小化**: 运行时验证实际权限需求
464-
3. **用户体验**: 友好的错误消息和解决建议
465-
4. **文档驱动**: 完整的操作手册和最佳实践
283+
1. **配置驱动**: 基于配置文件自动选择认证方式
284+
2. **优雅降级**: App 认证失败时自动回退到 PAT
285+
3. **透明切换**: 业务代码无需修改,保持原有接口
286+
4. **Installation ID 获取**: 从 Webhook 上下文中提取 Installation ID
466287

467288
#### 🔗 **依赖关系**
468-
- **前置依赖**: PR-8 (配置迁移工具)
289+
- **前置依赖**: PR-4 (认证抽象层)
469290
- **后续依赖**: 无
470291

471292
---
@@ -483,19 +304,7 @@ PR-3 (配置扩展) ──────────────────┐
483304
PR-4 (认证抽象层) ◄──────────────────┘
484305
485306
486-
PR-5 (Installation管理)
487-
488-
489-
PR-6 (工作空间隔离)
490-
491-
492-
PR-7 (向后兼容)
493-
494-
495-
PR-8 (迁移工具)
496-
497-
498-
PR-9 (监控文档)
307+
PR-5 (Client自动切换)
499308
```
500309

501310
### 建议实施顺序
@@ -510,19 +319,9 @@ PR-9 (监控文档)
510319
- PR-4: 认证抽象层重构
511320
- 顺序开发,PR-2 完成后开始 PR-4
512321

513-
#### 🏃‍♂️ **Sprint 3 (Week 5-6): 多租户支持**
514-
- PR-5: Installation 管理和发现
515-
- PR-6: 多租户工作空间隔离
516-
- 顺序开发
517-
518-
#### 🏃‍♂️ **Sprint 4 (Week 7-8): 兼容性和迁移**
519-
- PR-7: 向后兼容性实现
520-
- PR-8: 配置迁移工具
521-
- 顺序开发,可部分并行
522-
523-
#### 🏃‍♂️ **Sprint 5 (Week 9): 完善和文档**
524-
- PR-9: 监控、权限验证和文档
525-
- 最终集成测试和优化
322+
#### 🏃‍♂️ **Sprint 3 (Week 5): 自动切换**
323+
- PR-5: GitHub Client 自动认证切换
324+
- 集成测试和验证
526325

527326
---
528327

@@ -531,7 +330,7 @@ PR-9 (监控文档)
531330
### 🔄 **开发策略**
532331
1. **测试驱动**: 每个 PR 都包含完整的单元测试和集成测试
533332
2. **渐进式发布**: 每个 PR 合并后进行部分功能验证
534-
3. **文档先行**: 核心 PR (1-6) 都包含详细的 API 文档
333+
3. **文档先行**: 核心 PR (1-5) 都包含详细的 API 文档
535334
4. **向后兼容**: 确保每个 PR 不破坏现有功能
536335

537336
### 🎯 **质量保证**
@@ -550,7 +349,7 @@ PR-9 (监控文档)
550349

551350
## 📊 **总结**
552351

553-
本计划将 GitHub App 认证功能合理拆分为 **9 个独立且有序的 PR**,每个 PR 都有:
352+
本计划将 GitHub App 基础认证功能合理拆分为 **5 个独立且有序的 PR**,每个 PR 都有:
554353
- ✅ 明确的功能边界和职责范围
555354
- ✅ 详细的架构设计和实现原理
556355
- ✅ 清晰的依赖关系和实施顺序
@@ -562,7 +361,9 @@ PR-9 (监控文档)
562361
- 👥 **团队协作**: 多人可并行开发不同模块
563362
- 📈 **质量保证**: 每个组件都有充分的测试和文档
564363

565-
预计总体实施周期为 **9 周**,可根据团队资源和优先级适当调整。
364+
**核心目标**: 实现 GitHub App 基础认证功能,当配置了 GitHub App 时,GitHub Client 自动使用 App 认证,保持与现有 PAT 认证的完全兼容性。
365+
366+
预计总体实施周期为 **5 周**,可根据团队资源和优先级适当调整。
566367

567368
---
568369

0 commit comments

Comments
 (0)