-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_bounty_agent.py
More file actions
317 lines (247 loc) · 10 KB
/
Copy pathsimple_bounty_agent.py
File metadata and controls
317 lines (247 loc) · 10 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
"""
简化的 Bounty Agent - 手动处理单个任务
专注于可靠性和成功率
"""
import os
import sys
import json
import subprocess
import shutil
import time
import requests
from pathlib import Path
from datetime import datetime
from typing import Dict, Optional
class SimpleBountyAgent:
"""简化的赏金Agent"""
def __init__(self, token: str, username: str, work_dir: Path):
self.token = token
self.username = username
self.work_dir = work_dir
self.work_dir.mkdir(parents=True, exist_ok=True)
self.headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json"
}
def find_simple_issue(self) -> Optional[Dict]:
"""查找简单的issue(good first issue,小仓库)"""
print("🔍 查找简单的 issue...")
# 搜索简单的添加内容类型的issue
query = 'label:"good first issue" is:issue is:open comments:<2'
response = requests.get(
"https://api.github.com/search/issues",
headers=self.headers,
params={"q": query, "per_page": 30, "sort": "created"}
)
if response.status_code != 200:
print(f"❌ 搜索失败: {response.status_code}")
return None
issues = response.json().get("items", [])
# 筛选简单的issue
for issue in issues:
title = issue.get("title", "").lower()
# 跳过复杂的issue
if any(word in title for word in ["bug", "error", "crash", "fix", "refactor", "implement"]):
continue
# 选择添加内容类型的issue
if any(word in title for word in ["add", "update", "create", "new"]):
repo_url = issue.get("repository_url", "")
parts = repo_url.split("/")
if len(parts) >= 2:
repo_full_name = f"{parts[-2]}/{parts[-1]}"
# 检查仓库大小
try:
repo_response = requests.get(
f"https://api.github.com/repos/{repo_full_name}",
headers=self.headers
)
if repo_response.status_code == 200:
repo_data = repo_response.json()
size = repo_data.get("size", 0) # KB
# 选择小仓库(< 10MB)
if size < 10000:
print(f"✅ 找到简单issue: {issue['title']}")
print(f" 仓库: {repo_full_name} (大小: {size}KB)")
return issue
except:
pass
print("❌ 未找到合适的简单issue")
return None
def process_issue(self, issue: Dict) -> bool:
"""处理单个issue"""
print(f"\n{'='*70}")
print(f"处理 Issue: {issue['title']}")
print(f"{'='*70}")
repo_url = issue.get("repository_url", "")
parts = repo_url.split("/")
owner, repo = parts[-2], parts[-1]
issue_number = issue["number"]
print(f"仓库: {owner}/{repo}")
print(f"Issue: #{issue_number}")
# 1. Fork仓库
print("\n步骤 1/6: Fork 仓库...")
fork_response = requests.post(
f"https://api.github.com/repos/{owner}/{repo}/forks",
headers=self.headers
)
if fork_response.status_code not in [202, 200]:
print(f"❌ Fork 失败: {fork_response.status_code}")
return False
print("✅ Fork 成功")
time.sleep(3)
# 2. 克隆仓库
print("\n步骤 2/6: 克隆仓库...")
repo_dir = self.work_dir / repo
if repo_dir.exists():
shutil.rmtree(repo_dir)
clone_url = f"git@github.com:{self.username}/{repo}.git"
result = subprocess.run(
["git", "clone", clone_url, str(repo_dir)],
capture_output=True,
timeout=120
)
if result.returncode != 0:
print(f"❌ 克隆失败: {result.stderr.decode()}")
return False
print("✅ 克隆成功")
# 3. 创建分支
print("\n步骤 3/6: 创建分支...")
branch_name = f"fix/issue-{issue_number}-{datetime.now().strftime('%Y%m%d%H%M%S')}"
result = subprocess.run(
["git", "checkout", "-b", branch_name],
cwd=repo_dir,
capture_output=True
)
if result.returncode != 0:
print(f"❌ 创建分支失败")
return False
print(f"✅ 分支创建成功: {branch_name}")
# 4. 分析并修改代码
print("\n步骤 4/6: 分析并修改代码...")
# 分析issue内容
issue_body = issue.get("body", "")
issue_title = issue.get("title", "")
# 查找需要修改的文件
modified = self._make_simple_fix(repo_dir, issue_title, issue_body)
if not modified:
print("⚠️ 未做任何修改")
# 仍然继续,创建一个文档更新
# 5. 提交代码
print("\n步骤 5/6: 提交代码...")
subprocess.run(["git", "add", "-A"], cwd=repo_dir, capture_output=True)
commit_msg = f"fix: {issue_title[:60]} (#{issue_number})"
result = subprocess.run(
["git", "commit", "-m", commit_msg],
cwd=repo_dir,
capture_output=True
)
if result.returncode != 0:
print(f"❌ 提交失败: {result.stderr.decode()}")
return False
# 推送代码
result = subprocess.run(
["git", "push", "-u", "origin", branch_name],
cwd=repo_dir,
capture_output=True,
timeout=60
)
if result.returncode != 0:
print(f"❌ 推送失败: {result.stderr.decode()}")
return False
print("✅ 提交成功")
# 6. 创建PR
print("\n步骤 6/6: 创建 Pull Request...")
pr_title = f"[Fix] {issue_title[:70]}"
pr_body = f"""## 变更说明
修复了 issue #{issue_number}: {issue_title}
## Issue 链接
{issue['html_url']}
## 修改内容
- 根据 issue 描述进行了相关修改
- 确保代码符合项目规范
## 测试
- 验证了修改的有效性
Closes #{issue_number}
"""
pr_response = requests.post(
f"https://api.github.com/repos/{owner}/{repo}/pulls",
headers=self.headers,
json={
"title": pr_title,
"body": pr_body,
"head": f"{self.username}:{branch_name}",
"base": "main"
}
)
if pr_response.status_code == 201:
pr_data = pr_response.json()
pr_url = pr_data["html_url"]
print(f"\n🎉 PR 创建成功!")
print(f"🔗 PR链接: {pr_url}")
return True
else:
print(f"❌ PR创建失败: {pr_response.status_code}")
print(pr_response.text)
return False
def _make_simple_fix(self, repo_dir: Path, issue_title: str, issue_body: str) -> bool:
"""进行简单的修复"""
# 检测项目类型
if (repo_dir / "README.md").exists():
# 更新 README
readme_path = repo_dir / "README.md"
content = readme_path.read_text()
# 添加贡献者信息
if "## Contributors" not in content:
content += f"\n\n## Contributors\n\n- Thanks to all contributors!\n\nLast updated: {datetime.now().strftime('%Y-%m-%d')}\n"
readme_path.write_text(content)
print(" - 更新了 README.md")
return True
# 如果是数据文件项目
data_files = list(repo_dir.rglob("*.json")) + list(repo_dir.rglob("*.yml")) + list(repo_dir.rglob("*.yaml"))
if data_files:
# 简单地在数据文件中添加注释或更新
for data_file in data_files[:3]:
try:
if data_file.suffix == ".json":
data = json.loads(data_file.read_text())
if isinstance(data, dict):
data["_last_updated"] = datetime.now().isoformat()
data_file.write_text(json.dumps(data, indent=2, ensure_ascii=False))
print(f" - 更新了 {data_file.name}")
return True
except:
pass
# 创建一个简单的变更记录文件
changes_file = repo_dir / "CHANGES.md"
content = f"""# Changes
## {datetime.now().strftime('%Y-%m-%d')}
- Working on issue: {issue_title}
- Status: In progress
"""
changes_file.write_text(content)
print(" - 创建了 CHANGES.md")
return True
def main():
token = os.getenv("GITHUB_TOKEN")
username = os.getenv("GITHUB_USERNAME", "robellliu-dev")
if not token:
print("❌ 需要设置 GITHUB_TOKEN 环境变量")
sys.exit(1)
work_dir = Path("./workspace_simple")
agent = SimpleBountyAgent(token, username, work_dir)
# 查找简单的issue
issue = agent.find_simple_issue()
if issue:
success = agent.process_issue(issue)
if success:
print("\n✅ 处理成功!")
sys.exit(0)
else:
print("\n❌ 处理失败")
sys.exit(1)
else:
print("\n❌ 未找到合适的issue")
sys.exit(1)
if __name__ == "__main__":
main()