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]
Refactor: Move open PR code to env
Kilian Lieretce10810257f0
diff --git a/run.py b/run.pyindex 9ab562f8..ce7b1f48 100644--- a/run.py+++ b/run.py@@ -1,11 +1,9 @@import jsonimport loggingimport os-import randomimport reimport traceback-from typing import Any, Dict, Optional, Tuple-import requests+from typing import Any, Dictimport yamlfrom dataclasses import dataclass@@ -25,7 +23,7 @@ from sweagent import (from swebench import KEY_INSTANCE_ID, KEY_MODEL, KEY_PREDICTIONfrom unidiff import PatchSet-from sweagent.environment.utils import is_from_github_url+from sweagent.environment.utils import InvalidGithubURL, get_gh_issue_datahandler = RichHandler(show_time=False, show_path=False)handler.setLevel(logging.DEBUG)@@ -121,7 +119,7 @@ def main(args: ScriptArguments):)save_predictions(traj_dir, instance_id, info)if should_open_pr(args, info, token=env.token):- open_pr(env)+ env.open_pr()except KeyboardInterrupt:logger.info("Exiting InterCode environment...")@@ -142,121 +140,31 @@ def should_open_pr(args, info: Dict[str, Any], *, token: str="") -> bool:if info["exit_status"] != "submitted":logger.info("Not openening PR because exit status was %s and not submitted.", info["exit_status"])return False- if not is_from_github_url(args.environment.data_path):+ try:+ issue = get_gh_issue_data(args.environment.data_path, token=token)+ except InvalidGithubURL:logger.info("Currently only github is supported to open githubs to. Skipping PR creation.")return False- issue_info = IssueInfo.from_issue_url(args.environment.data_path, token=token)- if issue_info.is_closed:- logger.info("Issue is already closed. Skipping PR creation.")+ if issue.state != "open":+ logger.info(f"Issue is not open (state={issue.state}. Skipping PR creation.")return False- if issue_info.is_assigned:+ if issue.assignee:logger.info("Issue is already assigned. Skipping PR creation. Be nice :)")return False+ if issue.locked:+ logger.info("Issue is locked. Skipping PR creation.")+ return False+ # todo: Ideally we would want to check if there is already a PR open for this issue+ # but that doesn't seem to be possible at the moment: https://stackoverflow.com/questions/36419105/+ # The docs say that there should be a pull_request field in the issue info,+ # but there isn't.+ # Similarly, there isn't an issue field in the PR data, so we can't loop+ # over PRs either :|+ # The only possibility is to go through associated events by+ # curling https://api.github.com/repos/{owner}/{repo}/issues/{issue_number}+ # but it's also not implemented in ghapi yet.return True-class GitHubApiException(RuntimeError):- pass---@dataclass-class IssueInfo:- title: str- number: int- is_closed: bool- is_assigned: bool- org: str- repo: str-- @classmethod- def from_issue_url(cls, issue_url: str, *, token: str = "") -> "IssueInfo":- parse_url = issue_url.removeprefix("https://")- assert parse_url.startswith("github.com/")- org, repo = parse_url.split("/")[1:3]- issue_number = issue_url.split("/")[-1]- issue_data = retrieve_issue_data(org, repo, issue_number, token=token)- if issue_data is None:- raise GitHubApiException(f"Failed to retrieve issue data from {issue_url}")- return cls(- title=issue_data["title"],- number=issue_data["number"],- is_closed=bool(issue_data["closed_at"]),- is_assigned=bool(issue_data["assignee"]) or bool(issue_data["assignees"]),- org=org,- repo=repo,- )--def retrieve_issue_data(org: str, repo: str, issue_num: str, *, token: str="") -> Optional[Dict[str, Any]]:- # fixme: Authentication- api_url = f"https://api.github.com/repos/{org}/{repo}/issues/{issue_num}"- response = requests.get(api_url)- if not response.ok:- logger.warning(f"Failed to retrieve issue data from {api_url}: {response.text}")- return None- return response.json()---def open_pr(env: SWEEnv) -> None:- """Create a PR with the patch."""- logger.info("Opening PR")- # todo: have better way of handling this- # Adding random string suffix to avoid name conflicts if we had a previously failed run- branch_name = "swe-agent-patch-branch-" + str(random.random())[2:10]- issue_url = env.args.data_path- issue_info = IssueInfo.from_issue_url(issue_url, token=env.token)- logger.debug(env.communicate_with_handling(- input=f"rm model.patch",- error_msg="Failed to remove model patch",- timeout_duration=10,- ))- logger.debug(env.communicate_with_handling(- input=f"git checkout -b {branch_name}",- error_msg="Failed to switch to new branch",- timeout_duration=10,- ))- logger.debug(env.communicate_with_handling(- input=f"git add .",- error_msg="Failed to add commits",- timeout_duration=10,- ))- logger.debug(env.communicate_with_handling(- input=f"git commit -m 'Fix: {issue_info.title}' -m 'Closes #{issue_info.number}' ",- error_msg="Failed to commit changes",- timeout_duration=10,- ))- logger.debug(env.communicate_with_handling(- input=f"git push origin {branch_name}",- error_msg="Failed to clone repository from mirror",- timeout_duration=10,- ))- api_url = f"https://api.github.com/repos/{issue_info.org}/{issue_info.repo}/pulls"-- headers = {- 'Accept': 'application/vnd.github+json',- 'Authorization': f'Bearer {env.token}',- 'X-GitHub-Api-Version': '2022-11-28'- }- # todo: add representation of trajectory to PR body- data = {- 'title': f'AI tool PR to fix: {issue_info.title}',- 'body': f'This is a PR opened by AI tool [SWE Agent](https://github.com/princeton-nlp/SWE-agent/) to close [#{issue_info.number}]({issue_url}) ({issue_info.title}).\n\nCloses #{issue_info.number}.',- 'head': branch_name,- 'base': 'main'- }- print("Request data:")- print(data)- print(headers)- print(api_url)- response = requests.post(api_url, headers=headers, json=data)-- if not response.ok:- error_message = f"Error {response.status_code}: {response.text}"- if response.status_code == 403:- raise GitHubApiException("Forbidden: " + error_message)- else:- raise GitHubApiException(error_message)-- print("Success:", response.json())-def save_arguments(traj_dir, args):"""Save the arguments to a yaml file to the run's trajectory directory."""diff --git a/run_replay.py b/run_replay.pyindex b4d02f90..652d90f9 100644--- a/run_replay.py+++ b/run_replay.py@@ -108,8 +108,8 @@ def process_single_traj(traj_path: str, config_file: str, data_path: str, suffixcommand.extend(["--suffix", suffix])subprocess.run(command)- os.remove(replay_action_trajs_path)- os.remove(replay_task_instances_path)+ # os.remove(replay_action_trajs_path)+ # os.remove(replay_task_instances_path)def main(diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex af63b27d..cffbda96 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -1,3 +1,4 @@+import randomimport configimport datetimeimport docker@@ -10,6 +11,7 @@ import subprocessimport tracebackimport time+from ghapi.all import GhApifrom dataclasses import dataclassfrom git import Repofrom rich.logging import RichHandler@@ -17,8 +19,10 @@ from simple_parsing.helpers import FrozenSerializablefrom sweagent.environment.utils import (copy_file_to_container,get_container,+ get_gh_issue_data,get_instances,is_from_github_url,+ parse_gh_issue_url,read_with_timeout,LOGGER_NAME,)@@ -667,3 +671,56 @@ class SWEEnv(gym.Env):assert output.strip().endswith("interrupted"), "container health check failed"except TimeoutError:raise RuntimeError("Failed to interrupt container")+++ def open_pr(self):+ """Create PR to repository"""+ logger.info("Opening PR")+ # todo: have better way of handling this+ # Adding random string suffix to avoid name conflicts if we had a previously failed run+ # todo: add issue number to branch name+ branch_name = "swe-agent-patch-branch-" + str(random.random())[2:10]+ issue_url = self.args.data_path++ logger.debug(self.communicate_with_handling(+ input=f"rm model.patch",+ error_msg="Failed to remove model patch",+ timeout_duration=10,+ ))+ logger.debug(self.communicate_with_handling(+ input=f"git checkout -b {branch_name}",+ error_msg="Failed to switch to new branch",+ timeout_duration=10,+ ))+ logger.debug(self.communicate_with_handling(+ input=f"git add .",+ error_msg="Failed to add commits",+ timeout_duration=10,+ ))+ issue = get_gh_issue_data(issue_url, token=self.token)+ logger.debug(self.communicate_with_handling(+ input=f"git commit -m 'Fix: {issue.title}' -m 'Closes #{issue.number}' ",+ error_msg="Failed to commit changes",+ timeout_duration=10,+ ))+ logger.debug(self.communicate_with_handling(+ input=f"git push origin {branch_name}",+ error_msg="Failed to clone repository from mirror",+ 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}."+ )+ api = GhApi(token=self.token)+ owner, repo, _ = parse_gh_issue_url(issue_url)+ api.pulls.create(+ owner=owner,+ repo=repo,+ title=f"SWE-agent[bot] PR to fix: {issue.title}",+ head=branch_name,+ base="main",+ body=body,+ )\ No newline at end of filediff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.pyindex 8d922893..f3b9a553 100644--- a/sweagent/environment/utils.py+++ b/sweagent/environment/utils.py@@ -1,3 +1,5 @@+from ast import Dict, parse+from dataclasses import dataclassimport dockerimport jsonimport logging@@ -16,7 +18,7 @@ from ghapi.all import GhApifrom io import BytesIOfrom pathlib import Pathfrom subprocess import PIPE, STDOUT-from typing import Tuple+from typing import Any, TupleLOGGER_NAME = "intercode"START_UP_DELAY = 5@@ -279,6 +281,31 @@ def get_commit(api: GhApi, owner: str, repo: str, base_commit: str = None):+class InvalidGithubURL(ValueError):+ ...+++def parse_gh_issue_url(issue_url: str) -> Tuple[str, str, str]:+ """Return owner, repo, issue number from issue url"""+ match = GITHUB_ISSUE_URL_PATTERN.search(issue_url)+ if not match:+ raise InvalidGithubURL(f"Invalid GitHub issue URL: {issue_url}")+ res = match.groups()+ assert len(res) == 3+ return tuple(res) # type: ignore++++def get_gh_issue_data(issue_url: str, *, token: str = ""):+ """Returns github issue data in the form of a dictionary.+ See https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#get-an-issue+ for return format+ """+ owner, repo, issue_number = parse_gh_issue_url(issue_url)+ api = GhApi(token=token)+ return api.issues.get(owner, repo, issue_number)++def get_instances(file_path: str, base_commit: str = None, split: str = None, token: str = None):"""Getter function for handling json, jsonl files@@ -294,11 +321,13 @@ def get_instances(file_path: str, base_commit: str = None, split: str = None, to# If file_path is a github issue url, fetch the issue and return a single instanceif is_from_github_url(file_path):- match = GITHUB_ISSUE_URL_PATTERN.search(file_path)- api = GhApi(token=token)- if match:- owner, repo, issue_number = match.groups()+ try:+ owner, repo, issue_number = parse_gh_issue_url(file_path)+ except InvalidGithubURL:+ pass+ else:record = dict()+ api = GhApi(token=token)issue = api.issues.get(owner, repo, issue_number)title = issue.title if issue.title else ""body = issue.body if issue.body else ""@@ -326,3 +355,7 @@ def get_instances(file_path: str, base_commit: str = None, split: str = None, tof"Could not load instances from {file_path}. ""Please ensure --data_path is a GitHub URL, a SWE-bench HuggingFace dataset, or a JSON/JSONL file.")++++