send_pull_request.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. import argparse
  2. import json
  3. import os
  4. import shutil
  5. import subprocess
  6. import jinja2
  7. import litellm
  8. import requests
  9. from openhands.core.config import LLMConfig
  10. from openhands.core.logger import openhands_logger as logger
  11. from openhands.resolver.github_issue import GithubIssue
  12. from openhands.resolver.io_utils import (
  13. load_all_resolver_outputs,
  14. load_single_resolver_output,
  15. )
  16. from openhands.resolver.patching import apply_diff, parse_patch
  17. from openhands.resolver.resolver_output import ResolverOutput
  18. def apply_patch(repo_dir: str, patch: str) -> None:
  19. diffs = parse_patch(patch)
  20. for diff in diffs:
  21. if not diff.header.new_path:
  22. print('Warning: Could not determine file to patch')
  23. continue
  24. # Remove both "a/" and "b/" prefixes from paths
  25. old_path = (
  26. os.path.join(
  27. repo_dir, diff.header.old_path.removeprefix('a/').removeprefix('b/')
  28. )
  29. if diff.header.old_path and diff.header.old_path != '/dev/null'
  30. else None
  31. )
  32. new_path = os.path.join(
  33. repo_dir, diff.header.new_path.removeprefix('a/').removeprefix('b/')
  34. )
  35. # Check if the file is being deleted
  36. if diff.header.new_path == '/dev/null':
  37. assert old_path is not None
  38. if os.path.exists(old_path):
  39. os.remove(old_path)
  40. print(f'Deleted file: {old_path}')
  41. continue
  42. # Handle file rename
  43. if old_path and new_path and 'rename from' in patch:
  44. # Create parent directory of new path
  45. os.makedirs(os.path.dirname(new_path), exist_ok=True)
  46. try:
  47. # Try to move the file directly
  48. shutil.move(old_path, new_path)
  49. except shutil.SameFileError:
  50. # If it's the same file (can happen with directory renames), copy first then remove
  51. shutil.copy2(old_path, new_path)
  52. os.remove(old_path)
  53. # Try to remove empty parent directories
  54. old_dir = os.path.dirname(old_path)
  55. while old_dir and old_dir.startswith(repo_dir):
  56. try:
  57. os.rmdir(old_dir)
  58. old_dir = os.path.dirname(old_dir)
  59. except OSError:
  60. # Directory not empty or other error, stop trying to remove parents
  61. break
  62. continue
  63. if old_path:
  64. # Open the file in binary mode to detect line endings
  65. with open(old_path, 'rb') as f:
  66. original_content = f.read()
  67. # Detect line endings
  68. if b'\r\n' in original_content:
  69. newline = '\r\n'
  70. elif b'\n' in original_content:
  71. newline = '\n'
  72. else:
  73. newline = None # Let Python decide
  74. try:
  75. with open(old_path, 'r', newline=newline) as f:
  76. split_content = [x.strip(newline) for x in f.readlines()]
  77. except UnicodeDecodeError as e:
  78. logger.error(f'Error reading file {old_path}: {e}')
  79. split_content = []
  80. else:
  81. newline = '\n'
  82. split_content = []
  83. if diff.changes is None:
  84. print(f'Warning: No changes to apply for {old_path}')
  85. continue
  86. new_content = apply_diff(diff, split_content)
  87. # Ensure the directory exists before writing the file
  88. os.makedirs(os.path.dirname(new_path), exist_ok=True)
  89. # Write the new content using the detected line endings
  90. with open(new_path, 'w', newline=newline) as f:
  91. for line in new_content:
  92. print(line, file=f)
  93. print('Patch applied successfully')
  94. def initialize_repo(
  95. output_dir: str, issue_number: int, issue_type: str, base_commit: str | None = None
  96. ) -> str:
  97. src_dir = os.path.join(output_dir, 'repo')
  98. dest_dir = os.path.join(output_dir, 'patches', f'{issue_type}_{issue_number}')
  99. if not os.path.exists(src_dir):
  100. raise ValueError(f'Source directory {src_dir} does not exist.')
  101. if os.path.exists(dest_dir):
  102. shutil.rmtree(dest_dir)
  103. shutil.copytree(src_dir, dest_dir)
  104. print(f'Copied repository to {dest_dir}')
  105. if base_commit:
  106. result = subprocess.run(
  107. f'git -C {dest_dir} checkout {base_commit}',
  108. shell=True,
  109. capture_output=True,
  110. text=True,
  111. )
  112. if result.returncode != 0:
  113. print(f'Error checking out commit: {result.stderr}')
  114. raise RuntimeError('Failed to check out commit')
  115. return dest_dir
  116. def make_commit(repo_dir: str, issue: GithubIssue, issue_type: str) -> None:
  117. # Check if git username is set
  118. result = subprocess.run(
  119. f'git -C {repo_dir} config user.name',
  120. shell=True,
  121. capture_output=True,
  122. text=True,
  123. )
  124. if not result.stdout.strip():
  125. # If username is not set, configure git
  126. subprocess.run(
  127. f'git -C {repo_dir} config user.name "openhands" && '
  128. f'git -C {repo_dir} config user.email "openhands@all-hands.dev" && '
  129. f'git -C {repo_dir} config alias.git "git --no-pager"',
  130. shell=True,
  131. check=True,
  132. )
  133. print('Git user configured as openhands')
  134. result = subprocess.run(
  135. f'git -C {repo_dir} add .', shell=True, capture_output=True, text=True
  136. )
  137. if result.returncode != 0:
  138. print(f'Error adding files: {result.stderr}')
  139. raise RuntimeError('Failed to add files to git')
  140. status_result = subprocess.run(
  141. f'git -C {repo_dir} status --porcelain',
  142. shell=True,
  143. capture_output=True,
  144. text=True,
  145. )
  146. if not status_result.stdout.strip():
  147. print(f'No changes to commit for issue #{issue.number}. Skipping commit.')
  148. raise RuntimeError('ERROR: Openhands failed to make code changes.')
  149. commit_message = f'Fix {issue_type} #{issue.number}: {issue.title}'
  150. result = subprocess.run(
  151. ['git', '-C', repo_dir, 'commit', '-m', commit_message],
  152. capture_output=True,
  153. text=True,
  154. )
  155. if result.returncode != 0:
  156. raise RuntimeError(f'Failed to commit changes: {result}')
  157. def branch_exists(base_url: str, branch_name: str, headers: dict) -> bool:
  158. print(f'Checking if branch {branch_name} exists...')
  159. response = requests.get(f'{base_url}/branches/{branch_name}', headers=headers)
  160. exists = response.status_code == 200
  161. print(f'Branch {branch_name} exists: {exists}')
  162. return exists
  163. def send_pull_request(
  164. github_issue: GithubIssue,
  165. github_token: str,
  166. github_username: str | None,
  167. patch_dir: str,
  168. llm_config: LLMConfig,
  169. pr_type: str,
  170. fork_owner: str | None = None,
  171. additional_message: str | None = None,
  172. target_branch: str | None = None,
  173. ) -> str:
  174. if pr_type not in ['branch', 'draft', 'ready']:
  175. raise ValueError(f'Invalid pr_type: {pr_type}')
  176. # Set up headers and base URL for GitHub API
  177. headers = {
  178. 'Authorization': f'token {github_token}',
  179. 'Accept': 'application/vnd.github.v3+json',
  180. }
  181. base_url = f'https://api.github.com/repos/{github_issue.owner}/{github_issue.repo}'
  182. # Create a new branch with a unique name
  183. base_branch_name = f'openhands-fix-issue-{github_issue.number}'
  184. branch_name = base_branch_name
  185. attempt = 1
  186. print('Checking if branch exists...')
  187. while branch_exists(base_url, branch_name, headers):
  188. attempt += 1
  189. branch_name = f'{base_branch_name}-try{attempt}'
  190. # Get the default branch or use specified target branch
  191. print('Getting base branch...')
  192. if target_branch:
  193. base_branch = target_branch
  194. # Verify the target branch exists
  195. response = requests.get(f'{base_url}/branches/{target_branch}', headers=headers)
  196. if response.status_code != 200:
  197. raise ValueError(f'Target branch {target_branch} does not exist')
  198. else:
  199. response = requests.get(f'{base_url}', headers=headers)
  200. response.raise_for_status()
  201. base_branch = response.json()['default_branch']
  202. print(f'Base branch: {base_branch}')
  203. # Create and checkout the new branch
  204. print('Creating new branch...')
  205. result = subprocess.run(
  206. ['git', '-C', patch_dir, 'checkout', '-b', branch_name],
  207. capture_output=True,
  208. text=True,
  209. )
  210. if result.returncode != 0:
  211. print(f'Error creating new branch: {result.stderr}')
  212. raise RuntimeError(
  213. f'Failed to create a new branch {branch_name} in {patch_dir}:'
  214. )
  215. # Determine the repository to push to (original or fork)
  216. push_owner = fork_owner if fork_owner else github_issue.owner
  217. push_repo = github_issue.repo
  218. print('Pushing changes...')
  219. username_and_token = (
  220. f'{github_username}:{github_token}'
  221. if github_username
  222. else f'x-auth-token:{github_token}'
  223. )
  224. push_url = f'https://{username_and_token}@github.com/{push_owner}/{push_repo}.git'
  225. result = subprocess.run(
  226. ['git', '-C', patch_dir, 'push', push_url, branch_name],
  227. capture_output=True,
  228. text=True,
  229. )
  230. if result.returncode != 0:
  231. print(f'Error pushing changes: {result.stderr}')
  232. raise RuntimeError('Failed to push changes to the remote repository')
  233. pr_title = f'Fix issue #{github_issue.number}: {github_issue.title}'
  234. pr_body = f'This pull request fixes #{github_issue.number}.'
  235. if additional_message:
  236. pr_body += f'\n\n{additional_message}'
  237. pr_body += '\n\nAutomatic fix generated by [OpenHands](https://github.com/All-Hands-AI/OpenHands/) 🙌'
  238. # If we are not sending a PR, we can finish early and return the
  239. # URL for the user to open a PR manually
  240. if pr_type == 'branch':
  241. url = f'https://github.com/{push_owner}/{github_issue.repo}/compare/{branch_name}?expand=1'
  242. else:
  243. data = {
  244. 'title': pr_title, # No need to escape title for GitHub API
  245. 'body': pr_body,
  246. 'head': branch_name,
  247. 'base': base_branch,
  248. 'draft': pr_type == 'draft',
  249. }
  250. response = requests.post(f'{base_url}/pulls', headers=headers, json=data)
  251. if response.status_code == 403:
  252. raise RuntimeError(
  253. 'Failed to create pull request due to missing permissions. '
  254. 'Make sure that the provided token has push permissions for the repository.'
  255. )
  256. response.raise_for_status()
  257. pr_data = response.json()
  258. url = pr_data['html_url']
  259. print(f'{pr_type} created: {url}\n\n--- Title: {pr_title}\n\n--- Body:\n{pr_body}')
  260. return url
  261. def reply_to_comment(github_token: str, comment_id: str, reply: str):
  262. # Opting for graphql as REST API doesn't allow reply to replies in comment threads
  263. query = """
  264. mutation($body: String!, $pullRequestReviewThreadId: ID!) {
  265. addPullRequestReviewThreadReply(input: { body: $body, pullRequestReviewThreadId: $pullRequestReviewThreadId }) {
  266. comment {
  267. id
  268. body
  269. createdAt
  270. }
  271. }
  272. }
  273. """
  274. comment_reply = f'Openhands fix success summary\n\n\n{reply}'
  275. variables = {'body': comment_reply, 'pullRequestReviewThreadId': comment_id}
  276. url = 'https://api.github.com/graphql'
  277. headers = {
  278. 'Authorization': f'Bearer {github_token}',
  279. 'Content-Type': 'application/json',
  280. }
  281. response = requests.post(
  282. url, json={'query': query, 'variables': variables}, headers=headers
  283. )
  284. response.raise_for_status()
  285. def update_existing_pull_request(
  286. github_issue: GithubIssue,
  287. github_token: str,
  288. github_username: str | None,
  289. patch_dir: str,
  290. llm_config: LLMConfig,
  291. comment_message: str | None = None,
  292. additional_message: str | None = None,
  293. ) -> str:
  294. """Update an existing pull request with the new patches.
  295. Args:
  296. github_issue: The issue to update.
  297. github_token: The GitHub token to use for authentication.
  298. github_username: The GitHub username to use for authentication.
  299. patch_dir: The directory containing the patches to apply.
  300. llm_config: The LLM configuration to use for summarizing changes.
  301. comment_message: The main message to post as a comment on the PR.
  302. additional_message: The additional messages to post as a comment on the PR in json list format.
  303. """
  304. # Set up headers and base URL for GitHub API
  305. headers = {
  306. 'Authorization': f'token {github_token}',
  307. 'Accept': 'application/vnd.github.v3+json',
  308. }
  309. base_url = f'https://api.github.com/repos/{github_issue.owner}/{github_issue.repo}'
  310. branch_name = github_issue.head_branch
  311. # Push the changes to the existing branch
  312. push_command = (
  313. f'git -C {patch_dir} push '
  314. f'https://{github_username}:{github_token}@github.com/'
  315. f'{github_issue.owner}/{github_issue.repo}.git {branch_name}'
  316. )
  317. result = subprocess.run(push_command, shell=True, capture_output=True, text=True)
  318. if result.returncode != 0:
  319. print(f'Error pushing changes: {result.stderr}')
  320. raise RuntimeError('Failed to push changes to the remote repository')
  321. pr_url = f'https://github.com/{github_issue.owner}/{github_issue.repo}/pull/{github_issue.number}'
  322. print(f'Updated pull request {pr_url} with new patches.')
  323. # Generate a summary of all comment success indicators for PR message
  324. if not comment_message and additional_message:
  325. try:
  326. explanations = json.loads(additional_message)
  327. if explanations:
  328. comment_message = (
  329. 'OpenHands made the following changes to resolve the issues:\n\n'
  330. )
  331. for explanation in explanations:
  332. comment_message += f'- {explanation}\n'
  333. # Summarize with LLM if provided
  334. if llm_config is not None:
  335. with open(
  336. os.path.join(
  337. os.path.dirname(__file__),
  338. 'prompts/resolve/pr-changes-summary.jinja',
  339. ),
  340. 'r',
  341. ) as f:
  342. template = jinja2.Template(f.read())
  343. prompt = template.render(comment_message=comment_message)
  344. response = litellm.completion(
  345. model=llm_config.model,
  346. messages=[{'role': 'user', 'content': prompt}],
  347. api_key=llm_config.api_key,
  348. base_url=llm_config.base_url,
  349. )
  350. comment_message = response.choices[0].message.content.strip()
  351. except (json.JSONDecodeError, TypeError):
  352. comment_message = 'New OpenHands update'
  353. # Post a comment on the PR
  354. if comment_message:
  355. comment_url = f'{base_url}/issues/{github_issue.number}/comments'
  356. comment_data = {'body': comment_message}
  357. comment_response = requests.post(
  358. comment_url, headers=headers, json=comment_data
  359. )
  360. if comment_response.status_code != 201:
  361. print(
  362. f'Failed to post comment: {comment_response.status_code} {comment_response.text}'
  363. )
  364. else:
  365. print(f'Comment added to the PR: {comment_message}')
  366. # Reply to each unresolved comment thread
  367. if additional_message and github_issue.thread_ids:
  368. explanations = json.loads(additional_message)
  369. for count, reply_comment in enumerate(explanations):
  370. comment_id = github_issue.thread_ids[count]
  371. reply_to_comment(github_token, comment_id, reply_comment)
  372. return pr_url
  373. def process_single_issue(
  374. output_dir: str,
  375. resolver_output: ResolverOutput,
  376. github_token: str,
  377. github_username: str,
  378. pr_type: str,
  379. llm_config: LLMConfig,
  380. fork_owner: str | None,
  381. send_on_failure: bool,
  382. target_branch: str | None = None,
  383. ) -> None:
  384. if not resolver_output.success and not send_on_failure:
  385. print(
  386. f'Issue {resolver_output.issue.number} was not successfully resolved. Skipping PR creation.'
  387. )
  388. return
  389. issue_type = resolver_output.issue_type
  390. if issue_type == 'issue':
  391. patched_repo_dir = initialize_repo(
  392. output_dir,
  393. resolver_output.issue.number,
  394. issue_type,
  395. resolver_output.base_commit,
  396. )
  397. elif issue_type == 'pr':
  398. patched_repo_dir = initialize_repo(
  399. output_dir,
  400. resolver_output.issue.number,
  401. issue_type,
  402. resolver_output.issue.head_branch,
  403. )
  404. else:
  405. raise ValueError(f'Invalid issue type: {issue_type}')
  406. apply_patch(patched_repo_dir, resolver_output.git_patch)
  407. make_commit(patched_repo_dir, resolver_output.issue, issue_type)
  408. if issue_type == 'pr':
  409. update_existing_pull_request(
  410. github_issue=resolver_output.issue,
  411. github_token=github_token,
  412. github_username=github_username,
  413. patch_dir=patched_repo_dir,
  414. additional_message=resolver_output.success_explanation,
  415. llm_config=llm_config,
  416. )
  417. else:
  418. send_pull_request(
  419. github_issue=resolver_output.issue,
  420. github_token=github_token,
  421. github_username=github_username,
  422. patch_dir=patched_repo_dir,
  423. pr_type=pr_type,
  424. llm_config=llm_config,
  425. fork_owner=fork_owner,
  426. additional_message=resolver_output.success_explanation,
  427. target_branch=target_branch,
  428. )
  429. def process_all_successful_issues(
  430. output_dir: str,
  431. github_token: str,
  432. github_username: str,
  433. pr_type: str,
  434. llm_config: LLMConfig,
  435. fork_owner: str | None,
  436. ) -> None:
  437. output_path = os.path.join(output_dir, 'output.jsonl')
  438. for resolver_output in load_all_resolver_outputs(output_path):
  439. if resolver_output.success:
  440. print(f'Processing issue {resolver_output.issue.number}')
  441. process_single_issue(
  442. output_dir,
  443. resolver_output,
  444. github_token,
  445. github_username,
  446. pr_type,
  447. llm_config,
  448. fork_owner,
  449. False,
  450. None,
  451. )
  452. def main():
  453. parser = argparse.ArgumentParser(description='Send a pull request to Github.')
  454. parser.add_argument(
  455. '--github-token',
  456. type=str,
  457. default=None,
  458. help='Github token to access the repository.',
  459. )
  460. parser.add_argument(
  461. '--github-username',
  462. type=str,
  463. default=None,
  464. help='Github username to access the repository.',
  465. )
  466. parser.add_argument(
  467. '--output-dir',
  468. type=str,
  469. default='output',
  470. help='Output directory to write the results.',
  471. )
  472. parser.add_argument(
  473. '--pr-type',
  474. type=str,
  475. default='draft',
  476. choices=['branch', 'draft', 'ready'],
  477. help='Type of the pull request to send [branch, draft, ready]',
  478. )
  479. parser.add_argument(
  480. '--issue-number',
  481. type=str,
  482. required=True,
  483. help="Issue number to send the pull request for, or 'all_successful' to process all successful issues.",
  484. )
  485. parser.add_argument(
  486. '--fork-owner',
  487. type=str,
  488. default=None,
  489. help='Owner of the fork to push changes to (if different from the original repo owner).',
  490. )
  491. parser.add_argument(
  492. '--send-on-failure',
  493. action='store_true',
  494. help='Send a pull request even if the issue was not successfully resolved.',
  495. )
  496. parser.add_argument(
  497. '--llm-model',
  498. type=str,
  499. default=None,
  500. help='LLM model to use for summarizing changes.',
  501. )
  502. parser.add_argument(
  503. '--llm-api-key',
  504. type=str,
  505. default=None,
  506. help='API key for the LLM model.',
  507. )
  508. parser.add_argument(
  509. '--llm-base-url',
  510. type=str,
  511. default=None,
  512. help='Base URL for the LLM model.',
  513. )
  514. parser.add_argument(
  515. '--target-branch',
  516. type=str,
  517. default=None,
  518. help='Target branch to create the pull request against (defaults to repository default branch)',
  519. )
  520. my_args = parser.parse_args()
  521. github_token = (
  522. my_args.github_token if my_args.github_token else os.getenv('GITHUB_TOKEN')
  523. )
  524. if not github_token:
  525. raise ValueError(
  526. 'Github token is not set, set via --github-token or GITHUB_TOKEN environment variable.'
  527. )
  528. github_username = (
  529. my_args.github_username
  530. if my_args.github_username
  531. else os.getenv('GITHUB_USERNAME')
  532. )
  533. llm_config = LLMConfig(
  534. model=my_args.llm_model or os.environ['LLM_MODEL'],
  535. api_key=my_args.llm_api_key or os.environ['LLM_API_KEY'],
  536. base_url=my_args.llm_base_url or os.environ.get('LLM_BASE_URL', None),
  537. )
  538. if not os.path.exists(my_args.output_dir):
  539. raise ValueError(f'Output directory {my_args.output_dir} does not exist.')
  540. if my_args.issue_number == 'all_successful':
  541. if not github_username:
  542. raise ValueError('Github username is required.')
  543. process_all_successful_issues(
  544. my_args.output_dir,
  545. github_token,
  546. github_username,
  547. my_args.pr_type,
  548. llm_config,
  549. my_args.fork_owner,
  550. )
  551. else:
  552. if not my_args.issue_number.isdigit():
  553. raise ValueError(f'Issue number {my_args.issue_number} is not a number.')
  554. issue_number = int(my_args.issue_number)
  555. output_path = os.path.join(my_args.output_dir, 'output.jsonl')
  556. resolver_output = load_single_resolver_output(output_path, issue_number)
  557. if not github_username:
  558. raise ValueError('Github username is required.')
  559. process_single_issue(
  560. my_args.output_dir,
  561. resolver_output,
  562. github_token,
  563. github_username,
  564. my_args.pr_type,
  565. llm_config,
  566. my_args.fork_owner,
  567. my_args.send_on_failure,
  568. my_args.target_branch,
  569. )
  570. if __name__ == '__main__':
  571. main()