Skip to content

Commit

Permalink
Fix opening PR from fork
Browse files Browse the repository at this point in the history
  • Loading branch information
klieret committed Apr 15, 2024
1 parent d54d5ac commit 58aa046
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions sweagent/environment/swe_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
get_container,
get_gh_issue_data,
get_instances,
parse_gh_issue_url,
read_with_timeout,
LOGGER_NAME,
)
Expand Down Expand Up @@ -802,11 +803,27 @@ def open_pr(self, *, trajectory, _dry_run: bool=False):
error_msg="Failed to commit changes",
timeout_duration=10,
)
# If users want to push to a fork, we add a new remote and push to that
# otherwise, we push to 'origin' which has already been set up

owner, repo, _ = parse_gh_issue_url(issue_url)
# If `--repo_path` was specified with a different github URL, then the record will contain
# the forking user
assert self.record is not None
forker, _ = self.record["repo"].split("/")
head = branch_name
remote = "origin"
assert self.record is not None # mypy
owner, repo = self.record["repo"].split("/")
if forker != owner:
head = f"{forker}:{branch_name}"
token_prefix = ""
if self._github_token:
token_prefix = f"{self._github_token}@"
fork_url = f"https://{token_prefix}github.com/{forker}/{repo}.git"
logger.debug(f"Using fork: {fork_url}")
self.communicate_with_handling(
input=f"git remote add fork {fork_url}",
error_msg="Failed to create new git remote",
timeout_duration=10,
)
remote = "fork"
dry_run_prefix = "echo " if _dry_run else ""
self.communicate_with_handling(
input=f"{dry_run_prefix} git push {remote} {branch_name}",
Expand All @@ -816,8 +833,6 @@ def open_pr(self, *, trajectory, _dry_run: bool=False):
),
timeout_duration=10,
)

# todo: add representation of trajectory to PR body
body = (
f"This is a PR opened by AI tool [SWE Agent](https://github.com/princeton-nlp/SWE-agent/) "
f"to close [#{issue.number}]({issue_url}) ({issue.title}).\n\nCloses #{issue.number}."
Expand All @@ -829,7 +844,7 @@ def open_pr(self, *, trajectory, _dry_run: bool=False):
owner=owner,
repo=repo,
title=f"SWE-agent[bot] PR to fix: {issue.title}",
head=branch_name,
head=head,
base="main",
body=body,
draft=True,
Expand All @@ -838,4 +853,4 @@ def open_pr(self, *, trajectory, _dry_run: bool=False):
f"🎉 PR created as a draft at {pr_info.html_url}. Please review it carefully, push "
"any required changes onto the branch and then click "
"'Ready for Review' to bring it to the attention of the maintainers."
)
)

0 comments on commit 58aa046

Please sign in to comment.