Skip to content

Commit 3df8079

Browse files
committed
fix:兼容性修复
1 parent b69bd17 commit 3df8079

File tree

7 files changed

+334
-106
lines changed

7 files changed

+334
-106
lines changed

cmd/server/config.yaml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,32 @@ server:
33
webhook_secret: "1234567890"
44

55
github:
6-
#token: "1234567890"
7-
webhook_url: "http://localhost:8888/hook"
6+
# 可选:保留 PAT 作为回退方案
7+
token: "ghp_your_personal_access_token"
8+
9+
# GitHub App 配置 / GitHub App Configuration
10+
app:
11+
app_id: 1032243
12+
13+
# 私钥配置(三种方式,按优先级顺序)/ Private key configuration (3 methods, in priority order):
14+
15+
# 方式1:文件路径(推荐,最高优先级)/ Method 1: File path (recommended, highest priority)
16+
private_key_path: "/Users/xxxxx.private-key.pem"
17+
18+
# 方式2:环境变量名(中等优先级)/ Method 2: Environment variable name (medium priority)
19+
# private_key_env: "GITHUB_APP_PRIVATE_KEY"
20+
21+
# 方式3:直接内容(最低优先级,生产环境不推荐)/ Method 3: Direct content (lowest priority, not recommended for production)
22+
# private_key: |
23+
# -----BEGIN RSA PRIVATE KEY-----
24+
# MIIEpAIBAAKCAQEA...
25+
# -----END RSA PRIVATE KEY-----
26+
27+
# 认证模式(可选)
28+
auth_mode: "token" # auto(默认)| app | token
829

930
workspace:
10-
base_dir: "/Users/jicarl/codeagent"
31+
base_dir: "/tmp/codeagent"
1132
cleanup_after: "24h"
1233

1334
claude:

internal/agent/agent.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func New(cfg *config.Config, workspaceManager *workspace.Manager) *Agent {
3535
return nil
3636
}
3737

38-
// Initialize legacy GitHub client for backward compatibility
38+
// Initialize GitHub client that uses the new manager internally
3939
githubClient, err := ghclient.NewClient(cfg)
4040
if err != nil {
4141
log.Errorf("Failed to create GitHub client: %v", err)
@@ -128,14 +128,14 @@ func (a *Agent) GetAuthInfo() interface{} {
128128
if a.githubManager == nil {
129129
return map[string]string{"status": "not_initialized"}
130130
}
131-
131+
132132
authInfo := a.githubManager.GetAuthInfo()
133133
return map[string]interface{}{
134-
"type": authInfo.Type,
135-
"user": authInfo.User,
136-
"permissions": authInfo.Permissions,
137-
"app_id": authInfo.AppID,
138-
"configured": a.githubManager.DetectAuthMode(),
134+
"type": authInfo.Type,
135+
"user": authInfo.User,
136+
"permissions": authInfo.Permissions,
137+
"app_id": authInfo.AppID,
138+
"configured": a.githubManager.DetectAuthMode(),
139139
}
140140
}
141141

@@ -171,7 +171,7 @@ func (a *Agent) ProcessIssueCommentWithAI(ctx context.Context, event *github.Iss
171171

172172
// 3. Create initial PR (before code generation)
173173
log.Infof("Creating initial PR")
174-
pr, err := a.github.CreatePullRequest(ws)
174+
pr, err := a.github.CreatePullRequestWithContext(ctx, ws)
175175
if err != nil {
176176
log.Errorf("Failed to create PR: %v", err)
177177
return err
@@ -276,7 +276,7 @@ Brief description of changes
276276
prBody += "<details><summary>Original Prompt</summary>\n\n" + codePrompt + "\n\n</details>"
277277

278278
log.Infof("Updating PR body")
279-
if err = a.github.UpdatePullRequest(pr, prBody); err != nil {
279+
if err = a.github.UpdatePullRequestWithContext(ctx, pr, prBody); err != nil {
280280
log.Errorf("Failed to update PR body with execution result: %v", err)
281281
return err
282282
}
@@ -409,7 +409,9 @@ func (a *Agent) processPRWithArgsAndAI(ctx context.Context, event *github.IssueC
409409

410410
// 3. 从 GitHub API 获取完整的 PR 信息
411411
log.Infof("Fetching PR information from GitHub API")
412-
pr, err := a.github.GetPullRequest(repoOwner, repoName, event.Issue.GetNumber())
412+
413+
// 使用GitHub client获取PR信息(内部自动使用新的认证系统)
414+
pr, err := a.github.GetPullRequestWithContext(ctx, repoOwner, repoName, event.Issue.GetNumber())
413415
if err != nil {
414416
log.Errorf("Failed to get PR #%d: %v", prNumber, err)
415417
return fmt.Errorf("failed to get PR information: %w", err)
@@ -456,7 +458,7 @@ func (a *Agent) processPRWithArgsAndAI(ctx context.Context, event *github.IssueC
456458

457459
// 7. 获取所有PR评论历史用于构建上下文
458460
log.Infof("Fetching all PR comments for historical context")
459-
allComments, err := a.github.GetAllPRComments(pr)
461+
allComments, err := a.github.GetAllPRCommentsWithContext(ctx, pr)
460462
if err != nil {
461463
log.Warnf("Failed to get PR comments for context: %v", err)
462464
// 不返回错误,使用简单的prompt
@@ -516,7 +518,9 @@ func (a *Agent) processPRWithArgsAndAI(ctx context.Context, event *github.IssueC
516518
// 11. 评论到 PR
517519
commentBody := string(output)
518520
log.Infof("Creating PR comment")
519-
if err = a.github.CreatePullRequestComment(pr, commentBody); err != nil {
521+
522+
// 使用GitHub client创建评论(内部自动使用新的认证系统)
523+
if err := a.github.CreatePullRequestCommentWithContext(ctx, pr, commentBody); err != nil {
520524
log.Errorf("Failed to create PR comment: %v", err)
521525
return fmt.Errorf("failed to create PR comment: %w", err)
522526
}
@@ -746,7 +750,7 @@ func (a *Agent) ContinuePRFromReviewCommentWithAI(ctx context.Context, event *gi
746750

747751
// 6. 回复原始评论
748752
commentBody := string(output)
749-
if err = a.github.ReplyToReviewComment(pr, event.Comment.GetID(), commentBody); err != nil {
753+
if err = a.github.ReplyToReviewCommentWithContext(ctx, pr, event.Comment.GetID(), commentBody); err != nil {
750754
log.Errorf("failed to reply to review comment for continue: %v", err)
751755
return err
752756
}
@@ -853,7 +857,7 @@ func (a *Agent) FixPRFromReviewCommentWithAI(ctx context.Context, event *github.
853857

854858
// 6. 回复原始评论
855859
commentBody := string(output)
856-
if err = a.github.ReplyToReviewComment(pr, event.Comment.GetID(), commentBody); err != nil {
860+
if err = a.github.ReplyToReviewCommentWithContext(ctx, pr, event.Comment.GetID(), commentBody); err != nil {
857861
log.Errorf("failed to reply to review comment for fix: %v", err)
858862
return err
859863
}
@@ -890,7 +894,7 @@ func (a *Agent) ProcessPRFromReviewWithTriggerUserAndAI(ctx context.Context, eve
890894
}
891895

892896
// 3. 获取指定 review 的所有 comments
893-
reviewComments, err := a.github.GetReviewComments(pr, reviewID)
897+
reviewComments, err := a.github.GetReviewCommentsWithContext(ctx, pr, reviewID)
894898
if err != nil {
895899
log.Errorf("Failed to get review comments: %v", err)
896900
return err
@@ -1004,7 +1008,8 @@ func (a *Agent) ProcessPRFromReviewWithTriggerUserAndAI(ctx context.Context, eve
10041008
}
10051009
}
10061010

1007-
if err = a.github.CreatePullRequestComment(pr, responseBody); err != nil {
1011+
// 使用GitHub client创建评论(内部自动使用新的认证系统)
1012+
if err := a.github.CreatePullRequestCommentWithContext(ctx, pr, responseBody); err != nil {
10081013
log.Errorf("failed to create PR comment for batch processing result: %v", err)
10091014
return err
10101015
}

internal/agent/enhanced_agent.go

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package agent
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"time"
78

@@ -40,7 +41,7 @@ func NewEnhancedAgent(cfg *config.Config, workspaceManager *workspace.Manager) (
4041
xl := xlog.New("")
4142
xl.Infof("NewEnhancedAgent: %+v", cfg)
4243

43-
// 1. 初始化GitHub客户端
44+
// 1. 初始化GitHub客户端(内部使用新的认证系统)
4445
githubClient, err := ghclient.NewClient(cfg)
4546
if err != nil {
4647
return nil, fmt.Errorf("failed to create GitHub client: %w", err)
@@ -52,7 +53,7 @@ func NewEnhancedAgent(cfg *config.Config, workspaceManager *workspace.Manager) (
5253
// 3. 初始化MCP管理器和服务器
5354
mcpManager := mcp.NewManager()
5455

55-
// 注册内置MCP服务器
56+
// 注册内置MCP服务器(直接使用重构后的客户端)
5657
githubFiles := servers.NewGitHubFilesServer(githubClient)
5758
githubComments := servers.NewGitHubCommentsServer(githubClient)
5859

@@ -73,7 +74,7 @@ func NewEnhancedAgent(cfg *config.Config, workspaceManager *workspace.Manager) (
7374
// 6. 初始化模式管理器
7475
modeManager := modes.NewManager()
7576

76-
// 注册处理器(按优先级顺序)
77+
// 注册处理器(按优先级顺序,直接使用重构后的客户端
7778
tagHandler := modes.NewTagHandler(cfg.CodeProvider, githubClient, workspaceManager, mcpClient, sessionManager)
7879
agentHandler := modes.NewAgentHandler(githubClient, workspaceManager, mcpClient)
7980
reviewHandler := modes.NewReviewHandler(githubClient, workspaceManager, mcpClient, sessionManager)
@@ -110,14 +111,22 @@ func (a *EnhancedAgent) ProcessGitHubWebhookEvent(ctx context.Context, eventType
110111
startTime := time.Now()
111112
xl.Infof("Processing GitHub webhook event: %s, delivery_id: %s", eventType, deliveryID)
112113

113-
// 1. 解析GitHub事件为类型安全的上下文
114-
githubCtx, err := a.eventParser.ParseWebhookEvent(ctx, eventType, deliveryID, payload)
114+
// 1. 提取 installation ID 从 payload 并添加到 context
115+
ctxWithInstallation, err := a.extractInstallationIDFromPayload(ctx, payload)
116+
if err != nil {
117+
xl.Warnf("Failed to extract installation ID: %v", err)
118+
// 不返回错误,继续处理(可能是PAT模式)
119+
ctxWithInstallation = ctx
120+
}
121+
122+
// 2. 解析GitHub事件为类型安全的上下文
123+
githubCtx, err := a.eventParser.ParseWebhookEvent(ctxWithInstallation, eventType, deliveryID, payload)
115124
if err != nil {
116125
xl.Warnf("Failed to parse GitHub webhook event: %v", err)
117126
return fmt.Errorf("failed to parse webhook event: %w", err)
118127
}
119128

120-
return a.processGitHubContext(ctx, githubCtx, startTime)
129+
return a.processGitHubContext(ctxWithInstallation, githubCtx, startTime)
121130
}
122131

123132
// processGitHubContext 处理已解析的GitHub上下文
@@ -173,3 +182,43 @@ func (a *EnhancedAgent) Shutdown(ctx context.Context) error {
173182
xl.Infof("Enhanced Agent shutdown completed")
174183
return nil
175184
}
185+
186+
// extractInstallationIDFromPayload 从 webhook payload 中提取 installation ID
187+
func (a *EnhancedAgent) extractInstallationIDFromPayload(ctx context.Context, payload []byte) (context.Context, error) {
188+
xl := xlog.NewWith(ctx)
189+
190+
// 解析payload为通用map,提取installation信息
191+
var data map[string]interface{}
192+
if err := json.Unmarshal(payload, &data); err != nil {
193+
return ctx, fmt.Errorf("failed to parse webhook payload: %w", err)
194+
}
195+
196+
// 检查是否存在installation字段
197+
if installation, ok := data["installation"]; ok {
198+
if installationMap, ok := installation.(map[string]interface{}); ok {
199+
if idInterface, ok := installationMap["id"]; ok {
200+
var installationID int64
201+
switch v := idInterface.(type) {
202+
case float64:
203+
installationID = int64(v)
204+
case int64:
205+
installationID = v
206+
case int:
207+
installationID = int64(v)
208+
default:
209+
return ctx, fmt.Errorf("invalid installation ID type: %T", v)
210+
}
211+
212+
xl.Infof("Extracted installation ID from webhook payload: %d", installationID)
213+
214+
// 将 installation ID 添加到 context
215+
ctxWithInstallation := context.WithValue(ctx, "installation_id", installationID)
216+
return ctxWithInstallation, nil
217+
}
218+
}
219+
}
220+
221+
// 如果没有找到installation字段,返回原context(可能是PAT模式)
222+
xl.Debugf("No installation ID found in webhook payload, continuing with PAT mode")
223+
return ctx, fmt.Errorf("no installation found in payload")
224+
}

0 commit comments

Comments
 (0)