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]
Allow to specify separate URL to push to a fork
Kilian Lieret1973d7859f4a
diff --git a/run.py b/run.pyindex 7e717c2b..7ddf876e 100644--- a/run.py+++ b/run.py@@ -42,6 +42,9 @@ class ActionsArguments(FlattenedAccess, FrozenSerializable):# set this to False if you are sure the commits are not fixes or if this is your# own repository!skip_if_commits_reference_issue: bool = True+ # For PRs: If you want to push the branch to a fork (e.g., because you lack+ # permissions to push to the main repo), set this to the URL of the fork.+ push_gh_repo_url: str = ""@dataclass(frozen=True)@@ -129,7 +132,7 @@ def main(args: ScriptArguments):)save_predictions(traj_dir, instance_id, info)if args.actions.open_pr and should_open_pr(args, info, token=env.token):- env.open_pr()+ env.open_pr(args.actions)except KeyboardInterrupt:logger.info("Exiting InterCode environment...")@@ -166,10 +169,17 @@ def should_open_pr(args, info: Dict[str, Any], *, token: str="") -> bool:return Falseorg, repo, issue_number = parse_gh_issue_url(args.environment.data_path)associated_commits = get_associated_commit_urls(org, repo, issue_number, token=token)- if args.actions.skip_if_commits_reference_issue and associated_commits:+ if associated_commits:commit_url_strs = ", ".join(associated_commits)- logger.info(f"Issue already has associated commits (see {commit_url_strs}). Skipping PR creation.")- return False+ if args.actions.skip_if_commits_reference_issue:+ logger.info(f"Issue already has associated commits (see {commit_url_strs}). Skipping PR creation.")+ return False+ else:+ logger.warning(+ f"Proceeding with PR creation even though there are already commits "+ "({commit_url_strs}) associated with the issue. Please only do this for your own repositories "+ "or after verifying that the existing commits do not fix the issue."+ )return Truediff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 7596782a..47e88d17 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -23,6 +23,7 @@ from sweagent.environment.utils import (get_instances,is_from_github_url,parse_gh_issue_url,+ parse_gh_repo_url,read_with_timeout,LOGGER_NAME,)@@ -673,7 +674,7 @@ class SWEEnv(gym.Env):raise RuntimeError("Failed to interrupt container")- def open_pr(self):+ def open_pr(self, action_config: "ActionsArguments"):"""Create PR to repository"""logger.info("Opening PR")# todo: have better way of handling this@@ -702,9 +703,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+ remote = "origin"+ if not action_config.push_gh_repo_url:+ owner, repo, _ = parse_gh_issue_url(issue_url)+ else:+ owner, repo = parse_gh_repo_url(action_config.push_gh_repo_url)+ if action_config.push_gh_repo_url:+ fork_url = f"https://{self.token}@github.com/{owner}/{repo}.git"+ 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"self.communicate_with_handling(- input=f"git push origin {branch_name}",- error_msg="Failed to push branch to origin",+ input=f"git push {remote} {branch_name}",+ error_msg=(+ "Failed to push branch to remote. Please check your token and permissions. "+ "You might want to push to a fork with the push_gh_repo_url option."+ ),timeout_duration=10,)@@ -714,7 +733,6 @@ class SWEEnv(gym.Env):f"to close [#{issue.number}]({issue_url}) ({issue.title}).\n\nCloses #{issue.number}.")api = GhApi(token=self.token)- owner, repo, _ = parse_gh_issue_url(issue_url)pr_info = api.pulls.create(owner=owner,repo=repo,@@ -724,4 +742,8 @@ class SWEEnv(gym.Env):body=body,draft=True,)- logger.info(f"PR created as a draft at {pr_info.html_url}")\ No newline at end of file+ logger.info(+ 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 filediff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.pyindex 6588ea01..ff5ad42b 100644--- a/sweagent/environment/utils.py+++ b/sweagent/environment/utils.py@@ -295,6 +295,15 @@ def parse_gh_issue_url(issue_url: str) -> Tuple[str, str, str]:return tuple(res) # type: ignore+def parse_gh_repo_url(repo_url: str) -> Tuple[str, str]:+ """Return owner, repo from repo url"""+ if not repo_url.startswith('http://') and not repo_url.startswith('https://'):+ repo_url = 'https://' + repo_url+ parts = repo_url.split('/')+ owner = parts[3]+ repo = parts[4]+ return owner, repo+def get_gh_issue_data(issue_url: str, *, token: str = ""):"""Returns github issue data in the form of a dictionary.