-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauto_run.py
More file actions
executable file
·233 lines (189 loc) · 6.44 KB
/
Copy pathauto_run.py
File metadata and controls
executable file
·233 lines (189 loc) · 6.44 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
#!/usr/bin/env python3
"""
自动运行GitHub Bounty Agent,查找悬赏项目、分析需求、fork并提交PR
"""
import os
import sys
import requests
from pathlib import Path
import subprocess
# 添加项目路径
sys.path.insert(0, str(Path(__file__).parent))
from github_bounty_agent import GitHubBountyAgent
from bounty_finder import search_bounty_issues, display_results
print("=" * 70)
print("🤖 GitHub Bounty Agent - 自动化运行")
print("=" * 70)
# 获取配置
token = os.getenv("GITHUB_TOKEN")
username = os.getenv("GITHUB_USERNAME", "robellliu-dev")
if not token:
print("\n❌ 错误: 需要GitHub Token")
print("请设置环境变量: export GITHUB_TOKEN='your_token'")
sys.exit(1)
print(f"\n📋 配置:")
print(f" 用户名: {username}")
print(f" 工作目录: ./workspace")
# 创建工作目录
work_dir = Path("./workspace")
work_dir.mkdir(exist_ok=True)
# 创建Agent实例
agent = GitHubBountyAgent(token, username, str(work_dir))
# 搜索真实的GitHub issues
print("\n" + "-" * 70)
print("🔍 步骤1: 搜索GitHub上的赏金任务")
print("-" * 70)
# 搜索带有good first issue, help wanted, bounty标签的issues
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json"
}
queries = [
('label:"good first issue" is:issue is:open language:python', "Python Good First Issues"),
('label:"help wanted" is:issue is:open language:python', "Python Help Wanted"),
('label:"bounty" is:issue is:open', "Bounty Issues"),
]
all_issues = []
for query, label in queries:
print(f"\n搜索: {label}")
try:
response = requests.get(
"https://api.github.com/search/issues",
headers=headers,
params={
"q": query,
"per_page": 10,
"sort": "created",
"order": "desc"
}
)
if response.status_code == 200:
data = response.json()
items = data.get("items", [])
print(f" 找到 {len(items)} 个issues")
all_issues.extend(items)
else:
print(f" 搜索失败: {response.status_code}")
except Exception as e:
print(f" 错误: {e}")
# 去重
seen = set()
unique_issues = []
for issue in all_issues:
key = f"{issue['repository_url']}#{issue['number']}"
if key not in seen:
seen.add(key)
unique_issues.append(issue)
if not unique_issues:
print("\n❌ 未找到任何合适的任务")
sys.exit(1)
# 显示结果
print("\n" + "-" * 70)
print("✅ 步骤2: 找到的任务列表")
print("-" * 70)
for i, issue in enumerate(unique_issues[:5], 1):
repo_url = issue["repository_url"]
parts = repo_url.split("/")
owner = parts[-2]
repo_name = parts[-1]
print(f"\n{i}. {issue['title']}")
print(f" 仓库: {owner}/{repo_name}")
print(f" 链接: {issue['html_url']}")
print(f" 标签: {', '.join([l['name'] for l in issue.get('labels', [])])}")
print(f" 评论: {issue.get('comments', 0)}")
# 选择第一个任务进行自动处理
selected_issue = unique_issues[0]
# 转换为Agent需要的格式
repo_url = selected_issue["repository_url"]
parts = repo_url.split("/")
owner = parts[-2]
repo_name = parts[-1]
task = {
"title": selected_issue['title'],
"number": selected_issue['number'],
"url": selected_issue['html_url'],
"repo_full_name": f"{owner}/{repo_name}",
"labels": [l['name'] for l in selected_issue.get('labels', [])],
"bounty_amount": 0 # 默认值,实际可能没有悬赏金额
}
print("\n" + "-" * 70)
print(f"📌 步骤3: 自动处理任务 - {task['title']}")
print("-" * 70)
# Fork并克隆仓库
print("\n🍴 Fork并克隆仓库...")
fork_success, fork_info = agent.fork_and_clone(task)
if not fork_success:
print("❌ Fork或克隆失败")
sys.exit(1)
print(f"✅ Fork成功,克隆到: {fork_info['clone_path']}")
# 分析仓库
print("\n🔬 分析代码库...")
repo_info = agent.analyze_repository(fork_info['clone_path'])
print(f" 文件数: {len(repo_info['structure'])}")
print(f" 有测试: {repo_info['has_tests']}")
# 获取任务详情
print("\n📝 获取任务详情...")
task_details = agent.get_task_details(task)
print(f" Issue创建时间: {task_details.get('created_at')}")
print(f" Issue评论数: {task_details.get('comments', 0)}")
# 生成解决方案
print("\n💡 生成解决方案...")
solution = agent.generate_solution(task_details, repo_info)
print(f" PR标题: {solution['pr_title']}")
# 保存解决方案
import json
solution_file = work_dir / f"solution_plan_{task['number']}.json"
with open(solution_file, 'w', encoding='utf-8') as f:
json.dump(solution, f, indent=2, ensure_ascii=False)
print(f" 解决方案已保存: {solution_file}")
# 实现解决方案
print("\n⚙️ 实现解决方案...")
implementation_result = agent.implement_solution(solution, fork_info['clone_path'])
if not implementation_result:
print("❌ 实现失败")
sys.exit(1)
print("✅ 实现完成")
# 运行测试
print("\n🧪 运行测试...")
test_result = agent.run_tests(fork_info['clone_path'])
print(f" 测试结果: {'通过' if test_result else '跳过'}")
# 提交并推送代码
print("\n📤 提交并推送代码...")
commit_success = agent.commit_and_push(fork_info['clone_path'])
if not commit_success:
print("❌ 提交失败")
sys.exit(1)
print("✅ 代码已推送")
# 创建Pull Request
print("\n🎯 创建Pull Request...")
pr_url = agent.create_pull_request(
task['repo_full_name'],
task['number'],
solution['pr_title'],
solution['pr_description']
)
if pr_url:
print(f"\n✅ Pull Request创建成功!")
print(f"🔗 PR链接: {pr_url}")
# 保存PR信息
pr_info_file = work_dir / f"pr_info_{task['number']}.json"
pr_info = {
"pr_url": pr_url,
"task": task,
"solution": solution,
"timestamp": __import__('datetime').datetime.now().isoformat()
}
with open(pr_info_file, 'w', encoding='utf-8') as f:
json.dump(pr_info, f, indent=2, ensure_ascii=False)
print(f" PR信息已保存: {pr_info_file}")
else:
print("❌ PR创建失败")
sys.exit(1)
print("\n" + "=" * 70)
print("✅ 自动化流程完成!")
print("=" * 70)
print(f"\n📊 总结:")
print(f" - 搜索到 {len(unique_issues)} 个任务")
print(f" - 处理了任务: {task['title']}")
print(f" - Fork仓库: {task['repo_full_name']}")
print(f" - 创建PR: {pr_url}")