Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Fix opening PR from fork

Kilian Lieret58aa046a9b5d
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 401369ac..da750e89 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -25,6 +25,7 @@ from sweagent.environment.utils import (
get_container,
get_gh_issue_data,
get_instances,
+ parse_gh_issue_url,
read_with_timeout,
LOGGER_NAME,
)
@@ -802,11 +803,27 @@ class SWEEnv(gym.Env):
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}",
@@ -816,8 +833,6 @@ class SWEEnv(gym.Env):
),
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}."
@@ -829,7 +844,7 @@ class SWEEnv(gym.Env):
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,
@@ -838,4 +853,4 @@ class SWEEnv(gym.Env):
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."
- )
\ No newline at end of file
+ )