-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_process_github_multi_repos.py
More file actions
288 lines (222 loc) · 9.97 KB
/
auto_process_github_multi_repos.py
File metadata and controls
288 lines (222 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
"""
GitHub 多仓库自动处理脚本
支持在一次运行中处理多个 GitHub 仓库的 issues
"""
import os
import sys
import json
import logging
from datetime import datetime
# 添加项目路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from core.github import GitHubClient
from providers.claude import ClaudeProvider
# 设置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('logs/github_multi_repo.log'),
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger(__name__)
# 状态文件
STATE_FILE = 'logs/github_multi_repo_state.json'
def load_processed_issues():
"""加载已处理的 issues"""
if os.path.exists(STATE_FILE):
try:
with open(STATE_FILE, 'r') as f:
return json.load(f)
except:
return {}
return {}
def save_processed_issues(processed):
"""保存已处理的 issues"""
os.makedirs('logs', exist_ok=True)
with open(STATE_FILE, 'w') as f:
json.dump(processed, f, indent=2)
def process_repository(github_client, ai_provider, repo_owner, repo_name, processed):
"""处理单个仓库的 issues"""
logger.info(f"\n{'='*60}")
logger.info(f"Processing repository: {repo_owner}/{repo_name}")
logger.info(f"{'='*60}")
try:
# 获取带 'bot' 标签的 open issues
issues = github_client.get_repository_issues(
owner=repo_owner,
repo=repo_name,
labels=['bot'],
state='open'
)
logger.info(f"Found {len(issues)} open issues with 'bot' label")
processed_count = 0
for issue in issues:
issue_number = issue['number']
issue_key = f"{repo_owner}/{repo_name}#{issue_number}"
# 获取当前标签
current_labels = [label['name'] for label in issue.get('labels', [])]
# 🔍 关键:如果有状态标签,跳过(用户需手动移除才会重新处理)
skip_labels = ['needs-info', 'in-progress', 'cannot-fix', 'analyzing']
if any(label in current_labels for label in skip_labels):
logger.debug(f"Issue #{issue_number} has status label, skipping")
continue
# 获取评论
comments = github_client.get_comments(issue_number, repo_owner, repo_name)
# 生成指纹(包含标签状态)
fingerprint = f"{issue['title']}_{issue['body']}_{len(comments)}_{','.join(sorted(current_labels))}"
# 检查是否已处理
if issue_key in processed and processed[issue_key] == fingerprint:
logger.debug(f"Issue #{issue_number} already processed, skipping")
continue
logger.info(f"Processing issue #{issue_number}: {issue['title']}")
try:
# 发布开始处理评论
start_comment = """🤖 **AI Agent 已开始处理此 issue,请稍等...**
正在分析 issue 内容,很快会给出反馈。
⏳ *Processing...*
"""
github_client.add_comment(issue_number, start_comment, repo_owner, repo_name)
# 添加 analyzing 标签
current_labels = [label['name'] for label in issue.get('labels', [])]
if 'analyzing' not in current_labels:
github_client.add_labels(issue_number, ['analyzing'], repo_owner, repo_name)
# 构建仓库信息
repo_info = {
'name': repo_name,
'path_with_namespace': f"{repo_owner}/{repo_name}",
'default_branch': 'main',
'description': f"GitHub repository: {repo_owner}/{repo_name}"
}
# 转换为统一格式
unified_issue = {
'iid': issue['number'],
'title': issue['title'],
'description': issue['body'] or '',
'author': {
'username': issue['user']['login']
},
'labels': [label['name'] for label in issue.get('labels', [])]
}
# 过滤用户评论
user_comments = []
for comment in comments:
author = comment['user']['login']
body = comment['body']
if '🤖' not in body and 'AI Agent' not in body:
user_comments.append({
'author': author,
'body': body,
'created_at': comment['created_at']
})
# AI 分析
analysis_result = ai_provider.analyze_issue(unified_issue, repo_info, user_comments)
action = analysis_result.get('action', 'skip')
logger.info(f"AI Analysis: {action}")
# 根据结果采取行动
if action == "need_info":
questions = analysis_result.get('questions', [])
questions_text = "\n".join(f"{i+1}. {q}" for i, q in enumerate(questions))
comment_body = f"""👋 Hi @{issue['user']['login']}!
I've analyzed your issue and need some more information:
{questions_text}
**Reason:** {analysis_result.get('reason', 'Need clarification')}
**📌 After you reply:** Please remove the `needs-info` label so I can process your response.
🤖 *Powered by [GitIssue AI Agent](https://github.com/{repo_owner}/{repo_name})*
"""
github_client.add_comment(issue_number, comment_body, repo_owner, repo_name)
new_labels = [l for l in current_labels if l != 'analyzing']
new_labels.append('needs-info')
github_client.update_issue_labels(issue_number, new_labels, repo_owner, repo_name)
elif action == "can_handle":
plan = analysis_result.get('plan', 'Will work on this issue')
comment_body = f"""✅ Great! I can help with this issue.
**Analysis:**
{analysis_result.get('reason', 'This issue can be automated')}
**Plan:**
{plan}
I'll start working on this shortly!
🤖 *Powered by [GitIssue AI Agent](https://github.com/{repo_owner}/{repo_name})*
"""
github_client.add_comment(issue_number, comment_body, repo_owner, repo_name)
new_labels = [l for l in current_labels if l != 'analyzing']
new_labels.append('in-progress')
github_client.update_issue_labels(issue_number, new_labels, repo_owner, repo_name)
else: # skip
comment_body = f"""ℹ️ I've analyzed this issue, but it requires human expertise.
**Reason:**
{analysis_result.get('reason', 'This task requires human review')}
🤖 *Powered by [GitIssue AI Agent](https://github.com/{repo_owner}/{repo_name})*
"""
github_client.add_comment(issue_number, comment_body, repo_owner, repo_name)
new_labels = [l for l in current_labels if l != 'analyzing']
new_labels.append('cannot-fix')
github_client.update_issue_labels(issue_number, new_labels, repo_owner, repo_name)
# 记录已处理
processed[issue_key] = fingerprint
processed_count += 1
logger.info(f"✅ Successfully processed issue #{issue_number}")
except Exception as e:
logger.error(f"Error processing issue #{issue_number}: {e}")
return processed_count
except Exception as e:
logger.error(f"Error fetching issues from {repo_owner}/{repo_name}: {e}")
return 0
def main():
"""主函数"""
logger.info("=" * 60)
logger.info("Starting GitHub multi-repository processing")
logger.info("=" * 60)
# 从环境变量获取配置
github_token = os.getenv('GITHUB_TOKEN')
repositories_str = os.getenv('GITHUB_REPOS', '') # 格式: owner1/repo1,owner2/repo2
use_local_proxy = os.getenv('USE_LOCAL_PROXY', '1')
anthropic_api_key = os.getenv('ANTHROPIC_API_KEY', 'any_value')
if not github_token:
logger.error("GITHUB_TOKEN environment variable not set")
sys.exit(1)
if not repositories_str:
logger.error("GITHUB_REPOS environment variable not set")
logger.info("Format: GITHUB_REPOS='owner1/repo1,owner2/repo2'")
sys.exit(1)
# 解析仓库列表
repositories = []
for repo_str in repositories_str.split(','):
repo_str = repo_str.strip()
if '/' not in repo_str:
logger.warning(f"Invalid repository format: {repo_str} (expected: owner/repo)")
continue
owner, repo = repo_str.split('/', 1)
repositories.append((owner, repo))
if not repositories:
logger.error("No valid repositories found")
sys.exit(1)
logger.info(f"Will process {len(repositories)} repositories:")
for owner, repo in repositories:
logger.info(f" - {owner}/{repo}")
# 初始化客户端
github_client = GitHubClient(token=github_token)
# 初始化 AI Provider
api_base = "http://localhost:8082" if use_local_proxy == '1' else None
ai_provider = ClaudeProvider(
api_key=anthropic_api_key,
model="claude-sonnet-4-5-20250929",
api_base=api_base
)
# 加载已处理记录
processed = load_processed_issues()
# 处理每个仓库
total_processed = 0
for repo_owner, repo_name in repositories:
count = process_repository(github_client, ai_provider, repo_owner, repo_name, processed)
total_processed += count
# 保存状态
save_processed_issues(processed)
logger.info("\n" + "=" * 60)
logger.info(f"Finished processing {len(repositories)} repositories")
logger.info(f"Total issues processed: {total_processed}")
logger.info("=" * 60)
if __name__ == "__main__":
main()