send_pull_request.py 26 KB

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