send_pull_request.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. pr_title: str | None = None,
  204. ) -> str:
  205. """Send a pull request to a GitHub repository.
  206. Args:
  207. github_issue: The issue to send the pull request for
  208. github_token: The GitHub token to use for authentication
  209. github_username: The GitHub username, if provided
  210. patch_dir: The directory containing the patches to apply
  211. pr_type: The type: branch (no PR created), draft or ready (regular PR created)
  212. fork_owner: The owner of the fork to push changes to (if different from the original repo owner)
  213. additional_message: The additional messages to post as a comment on the PR in json list format
  214. target_branch: The target branch to create the pull request against (defaults to repository default branch)
  215. reviewer: The GitHub username of the reviewer to assign
  216. pr_title: Custom title for the pull request (optional)
  217. """
  218. if pr_type not in ['branch', 'draft', 'ready']:
  219. raise ValueError(f'Invalid pr_type: {pr_type}')
  220. # Set up headers and base URL for GitHub API
  221. headers = {
  222. 'Authorization': f'token {github_token}',
  223. 'Accept': 'application/vnd.github.v3+json',
  224. }
  225. base_url = f'https://api.github.com/repos/{github_issue.owner}/{github_issue.repo}'
  226. # Create a new branch with a unique name
  227. base_branch_name = f'openhands-fix-issue-{github_issue.number}'
  228. branch_name = base_branch_name
  229. attempt = 1
  230. # Find a unique branch name
  231. print('Checking if branch exists...')
  232. while branch_exists(base_url, branch_name, headers):
  233. attempt += 1
  234. branch_name = f'{base_branch_name}-try{attempt}'
  235. # Get the default branch or use specified target branch
  236. print('Getting base branch...')
  237. if target_branch:
  238. base_branch = target_branch
  239. # Verify the target branch exists
  240. response = requests.get(f'{base_url}/branches/{target_branch}', headers=headers)
  241. if response.status_code != 200:
  242. raise ValueError(f'Target branch {target_branch} does not exist')
  243. else:
  244. response = requests.get(f'{base_url}', headers=headers)
  245. response.raise_for_status()
  246. base_branch = response.json()['default_branch']
  247. print(f'Base branch: {base_branch}')
  248. # Create and checkout the new branch
  249. print('Creating new branch...')
  250. result = subprocess.run(
  251. ['git', '-C', patch_dir, 'checkout', '-b', branch_name],
  252. capture_output=True,
  253. text=True,
  254. )
  255. if result.returncode != 0:
  256. print(f'Error creating new branch: {result.stderr}')
  257. raise RuntimeError(
  258. f'Failed to create a new branch {branch_name} in {patch_dir}:'
  259. )
  260. # Determine the repository to push to (original or fork)
  261. push_owner = fork_owner if fork_owner else github_issue.owner
  262. push_repo = github_issue.repo
  263. print('Pushing changes...')
  264. username_and_token = (
  265. f'{github_username}:{github_token}'
  266. if github_username
  267. else f'x-auth-token:{github_token}'
  268. )
  269. push_url = f'https://{username_and_token}@github.com/{push_owner}/{push_repo}.git'
  270. result = subprocess.run(
  271. ['git', '-C', patch_dir, 'push', push_url, branch_name],
  272. capture_output=True,
  273. text=True,
  274. )
  275. if result.returncode != 0:
  276. print(f'Error pushing changes: {result.stderr}')
  277. raise RuntimeError('Failed to push changes to the remote repository')
  278. # Prepare the PR data: title and body
  279. final_pr_title = (
  280. pr_title
  281. if pr_title
  282. else f'Fix issue #{github_issue.number}: {github_issue.title}'
  283. )
  284. pr_body = f'This pull request fixes #{github_issue.number}.'
  285. if additional_message:
  286. pr_body += f'\n\n{additional_message}'
  287. pr_body += '\n\nAutomatic fix generated by [OpenHands](https://github.com/All-Hands-AI/OpenHands/) 🙌'
  288. # If we are not sending a PR, we can finish early and return the
  289. # URL for the user to open a PR manually
  290. if pr_type == 'branch':
  291. url = f'https://github.com/{push_owner}/{github_issue.repo}/compare/{branch_name}?expand=1'
  292. else:
  293. # Prepare the PR for the GitHub API
  294. data = {
  295. 'title': final_pr_title, # No need to escape title for GitHub API
  296. 'body': pr_body,
  297. 'head': branch_name,
  298. 'base': base_branch,
  299. 'draft': pr_type == 'draft',
  300. }
  301. # Send the PR and get its URL to tell the user
  302. response = requests.post(f'{base_url}/pulls', headers=headers, json=data)
  303. if response.status_code == 403:
  304. raise RuntimeError(
  305. 'Failed to create pull request due to missing permissions. '
  306. 'Make sure that the provided token has push permissions for the repository.'
  307. )
  308. response.raise_for_status()
  309. pr_data = response.json()
  310. # Request review if a reviewer was specified
  311. if reviewer and pr_type != 'branch':
  312. review_data = {'reviewers': [reviewer]}
  313. review_response = requests.post(
  314. f'{base_url}/pulls/{pr_data["number"]}/requested_reviewers',
  315. headers=headers,
  316. json=review_data,
  317. )
  318. if review_response.status_code != 201:
  319. print(
  320. f'Warning: Failed to request review from {reviewer}: {review_response.text}'
  321. )
  322. url = pr_data['html_url']
  323. print(
  324. f'{pr_type} created: {url}\n\n--- Title: {final_pr_title}\n\n--- Body:\n{pr_body}'
  325. )
  326. return url
  327. def reply_to_comment(github_token: str, comment_id: str, reply: str):
  328. """Reply to a comment on a GitHub issue or pull request.
  329. Args:
  330. github_token: The GitHub token to use for authentication
  331. comment_id: The ID of the comment to reply to
  332. reply: The reply message to post
  333. """
  334. # Opting for graphql as REST API doesn't allow reply to replies in comment threads
  335. query = """
  336. mutation($body: String!, $pullRequestReviewThreadId: ID!) {
  337. addPullRequestReviewThreadReply(input: { body: $body, pullRequestReviewThreadId: $pullRequestReviewThreadId }) {
  338. comment {
  339. id
  340. body
  341. createdAt
  342. }
  343. }
  344. }
  345. """
  346. # Prepare the reply to the comment
  347. comment_reply = f'Openhands fix success summary\n\n\n{reply}'
  348. variables = {'body': comment_reply, 'pullRequestReviewThreadId': comment_id}
  349. url = 'https://api.github.com/graphql'
  350. headers = {
  351. 'Authorization': f'Bearer {github_token}',
  352. 'Content-Type': 'application/json',
  353. }
  354. # Send the reply to the comment
  355. response = requests.post(
  356. url, json={'query': query, 'variables': variables}, headers=headers
  357. )
  358. response.raise_for_status()
  359. def send_comment_msg(base_url: str, issue_number: int, github_token: str, msg: str):
  360. """Send a comment message to a GitHub issue or pull request.
  361. Args:
  362. base_url: The base URL of the GitHub repository API
  363. issue_number: The issue or pull request number
  364. github_token: The GitHub token to use for authentication
  365. msg: The message content to post as a comment
  366. """
  367. # Set up headers for GitHub API
  368. headers = {
  369. 'Authorization': f'token {github_token}',
  370. 'Accept': 'application/vnd.github.v3+json',
  371. }
  372. # Post a comment on the PR
  373. comment_url = f'{base_url}/issues/{issue_number}/comments'
  374. comment_data = {'body': msg}
  375. comment_response = requests.post(comment_url, headers=headers, json=comment_data)
  376. if comment_response.status_code != 201:
  377. print(
  378. f'Failed to post comment: {comment_response.status_code} {comment_response.text}'
  379. )
  380. else:
  381. print(f'Comment added to the PR: {msg}')
  382. def update_existing_pull_request(
  383. github_issue: GithubIssue,
  384. github_token: str,
  385. github_username: str | None,
  386. patch_dir: str,
  387. llm_config: LLMConfig,
  388. comment_message: str | None = None,
  389. additional_message: str | None = None,
  390. ) -> str:
  391. """Update an existing pull request with the new patches.
  392. Args:
  393. github_issue: The issue to update.
  394. github_token: The GitHub token to use for authentication.
  395. github_username: The GitHub username to use for authentication.
  396. patch_dir: The directory containing the patches to apply.
  397. llm_config: The LLM configuration to use for summarizing changes.
  398. comment_message: The main message to post as a comment on the PR.
  399. additional_message: The additional messages to post as a comment on the PR in json list format.
  400. """
  401. # Set up base URL for GitHub API
  402. base_url = f'https://api.github.com/repos/{github_issue.owner}/{github_issue.repo}'
  403. branch_name = github_issue.head_branch
  404. # Prepare the push command
  405. push_command = (
  406. f'git -C {patch_dir} push '
  407. f'https://{github_username}:{github_token}@github.com/'
  408. f'{github_issue.owner}/{github_issue.repo}.git {branch_name}'
  409. )
  410. # Push the changes to the existing branch
  411. result = subprocess.run(push_command, shell=True, capture_output=True, text=True)
  412. if result.returncode != 0:
  413. print(f'Error pushing changes: {result.stderr}')
  414. raise RuntimeError('Failed to push changes to the remote repository')
  415. pr_url = f'https://github.com/{github_issue.owner}/{github_issue.repo}/pull/{github_issue.number}'
  416. print(f'Updated pull request {pr_url} with new patches.')
  417. # Generate a summary of all comment success indicators for PR message
  418. if not comment_message and additional_message:
  419. try:
  420. explanations = json.loads(additional_message)
  421. if explanations:
  422. comment_message = (
  423. 'OpenHands made the following changes to resolve the issues:\n\n'
  424. )
  425. for explanation in explanations:
  426. comment_message += f'- {explanation}\n'
  427. # Summarize with LLM if provided
  428. if llm_config is not None:
  429. llm = LLM(llm_config)
  430. with open(
  431. os.path.join(
  432. os.path.dirname(__file__),
  433. 'prompts/resolve/pr-changes-summary.jinja',
  434. ),
  435. 'r',
  436. ) as f:
  437. template = jinja2.Template(f.read())
  438. prompt = template.render(comment_message=comment_message)
  439. response = llm.completion(
  440. messages=[{'role': 'user', 'content': prompt}],
  441. )
  442. comment_message = response.choices[0].message.content.strip()
  443. except (json.JSONDecodeError, TypeError):
  444. comment_message = f'A new OpenHands update is available, but failed to parse or summarize the changes:\n{additional_message}'
  445. # Post a comment on the PR
  446. if comment_message:
  447. send_comment_msg(base_url, github_issue.number, github_token, comment_message)
  448. # Reply to each unresolved comment thread
  449. if additional_message and github_issue.thread_ids:
  450. try:
  451. explanations = json.loads(additional_message)
  452. for count, reply_comment in enumerate(explanations):
  453. comment_id = github_issue.thread_ids[count]
  454. reply_to_comment(github_token, comment_id, reply_comment)
  455. except (json.JSONDecodeError, TypeError):
  456. msg = f'Error occured when replying to threads; success explanations {additional_message}'
  457. send_comment_msg(base_url, github_issue.number, github_token, msg)
  458. return pr_url
  459. def process_single_issue(
  460. output_dir: str,
  461. resolver_output: ResolverOutput,
  462. github_token: str,
  463. github_username: str,
  464. pr_type: str,
  465. llm_config: LLMConfig,
  466. fork_owner: str | None,
  467. send_on_failure: bool,
  468. target_branch: str | None = None,
  469. reviewer: str | None = None,
  470. pr_title: str | None = None,
  471. ) -> None:
  472. if not resolver_output.success and not send_on_failure:
  473. print(
  474. f'Issue {resolver_output.issue.number} was not successfully resolved. Skipping PR creation.'
  475. )
  476. return
  477. issue_type = resolver_output.issue_type
  478. if issue_type == 'issue':
  479. patched_repo_dir = initialize_repo(
  480. output_dir,
  481. resolver_output.issue.number,
  482. issue_type,
  483. resolver_output.base_commit,
  484. )
  485. elif issue_type == 'pr':
  486. patched_repo_dir = initialize_repo(
  487. output_dir,
  488. resolver_output.issue.number,
  489. issue_type,
  490. resolver_output.issue.head_branch,
  491. )
  492. else:
  493. raise ValueError(f'Invalid issue type: {issue_type}')
  494. apply_patch(patched_repo_dir, resolver_output.git_patch)
  495. make_commit(patched_repo_dir, resolver_output.issue, issue_type)
  496. if issue_type == 'pr':
  497. update_existing_pull_request(
  498. github_issue=resolver_output.issue,
  499. github_token=github_token,
  500. github_username=github_username,
  501. patch_dir=patched_repo_dir,
  502. additional_message=resolver_output.result_explanation,
  503. llm_config=llm_config,
  504. )
  505. else:
  506. send_pull_request(
  507. github_issue=resolver_output.issue,
  508. github_token=github_token,
  509. github_username=github_username,
  510. patch_dir=patched_repo_dir,
  511. pr_type=pr_type,
  512. fork_owner=fork_owner,
  513. additional_message=resolver_output.result_explanation,
  514. target_branch=target_branch,
  515. reviewer=reviewer,
  516. pr_title=pr_title,
  517. )
  518. def process_all_successful_issues(
  519. output_dir: str,
  520. github_token: str,
  521. github_username: str,
  522. pr_type: str,
  523. llm_config: LLMConfig,
  524. fork_owner: str | None,
  525. ) -> None:
  526. output_path = os.path.join(output_dir, 'output.jsonl')
  527. for resolver_output in load_all_resolver_outputs(output_path):
  528. if resolver_output.success:
  529. print(f'Processing issue {resolver_output.issue.number}')
  530. process_single_issue(
  531. output_dir,
  532. resolver_output,
  533. github_token,
  534. github_username,
  535. pr_type,
  536. llm_config,
  537. fork_owner,
  538. False,
  539. None,
  540. )
  541. def main():
  542. parser = argparse.ArgumentParser(description='Send a pull request to Github.')
  543. parser.add_argument(
  544. '--github-token',
  545. type=str,
  546. default=None,
  547. help='Github token to access the repository.',
  548. )
  549. parser.add_argument(
  550. '--github-username',
  551. type=str,
  552. default=None,
  553. help='Github username to access the repository.',
  554. )
  555. parser.add_argument(
  556. '--output-dir',
  557. type=str,
  558. default='output',
  559. help='Output directory to write the results.',
  560. )
  561. parser.add_argument(
  562. '--pr-type',
  563. type=str,
  564. default='draft',
  565. choices=['branch', 'draft', 'ready'],
  566. help='Type of the pull request to send [branch, draft, ready]',
  567. )
  568. parser.add_argument(
  569. '--issue-number',
  570. type=str,
  571. required=True,
  572. help="Issue number to send the pull request for, or 'all_successful' to process all successful issues.",
  573. )
  574. parser.add_argument(
  575. '--fork-owner',
  576. type=str,
  577. default=None,
  578. help='Owner of the fork to push changes to (if different from the original repo owner).',
  579. )
  580. parser.add_argument(
  581. '--send-on-failure',
  582. action='store_true',
  583. help='Send a pull request even if the issue was not successfully resolved.',
  584. )
  585. parser.add_argument(
  586. '--llm-model',
  587. type=str,
  588. default=None,
  589. help='LLM model to use for summarizing changes.',
  590. )
  591. parser.add_argument(
  592. '--llm-api-key',
  593. type=str,
  594. default=None,
  595. help='API key for the LLM model.',
  596. )
  597. parser.add_argument(
  598. '--llm-base-url',
  599. type=str,
  600. default=None,
  601. help='Base URL for the LLM model.',
  602. )
  603. parser.add_argument(
  604. '--target-branch',
  605. type=str,
  606. default=None,
  607. help='Target branch to create the pull request against (defaults to repository default branch)',
  608. )
  609. parser.add_argument(
  610. '--reviewer',
  611. type=str,
  612. help='GitHub username of the person to request review from',
  613. default=None,
  614. )
  615. parser.add_argument(
  616. '--pr-title',
  617. type=str,
  618. help='Custom title for the pull request',
  619. default=None,
  620. )
  621. my_args = parser.parse_args()
  622. github_token = (
  623. my_args.github_token if my_args.github_token else os.getenv('GITHUB_TOKEN')
  624. )
  625. if not github_token:
  626. raise ValueError(
  627. 'Github token is not set, set via --github-token or GITHUB_TOKEN environment variable.'
  628. )
  629. github_username = (
  630. my_args.github_username
  631. if my_args.github_username
  632. else os.getenv('GITHUB_USERNAME')
  633. )
  634. llm_config = LLMConfig(
  635. model=my_args.llm_model or os.environ['LLM_MODEL'],
  636. api_key=my_args.llm_api_key or os.environ['LLM_API_KEY'],
  637. base_url=my_args.llm_base_url or os.environ.get('LLM_BASE_URL', None),
  638. )
  639. if not os.path.exists(my_args.output_dir):
  640. raise ValueError(f'Output directory {my_args.output_dir} does not exist.')
  641. if my_args.issue_number == 'all_successful':
  642. if not github_username:
  643. raise ValueError('Github username is required.')
  644. process_all_successful_issues(
  645. my_args.output_dir,
  646. github_token,
  647. github_username,
  648. my_args.pr_type,
  649. llm_config,
  650. my_args.fork_owner,
  651. )
  652. else:
  653. if not my_args.issue_number.isdigit():
  654. raise ValueError(f'Issue number {my_args.issue_number} is not a number.')
  655. issue_number = int(my_args.issue_number)
  656. output_path = os.path.join(my_args.output_dir, 'output.jsonl')
  657. resolver_output = load_single_resolver_output(output_path, issue_number)
  658. if not github_username:
  659. raise ValueError('Github username is required.')
  660. process_single_issue(
  661. my_args.output_dir,
  662. resolver_output,
  663. github_token,
  664. github_username,
  665. my_args.pr_type,
  666. llm_config,
  667. my_args.fork_owner,
  668. my_args.send_on_failure,
  669. my_args.target_branch,
  670. my_args.reviewer,
  671. my_args.pr_title,
  672. )
  673. if __name__ == '__main__':
  674. main()