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]
Skip PR creation if there are associated commits
Kilian Lieret58c9476945af
diff --git a/run.py b/run.pyindex ce7b1f48..9697a0cb 100644--- a/run.py+++ b/run.py@@ -23,7 +23,7 @@ from sweagent import (from swebench import KEY_INSTANCE_ID, KEY_MODEL, KEY_PREDICTIONfrom unidiff import PatchSet-from sweagent.environment.utils import InvalidGithubURL, get_gh_issue_data+from sweagent.environment.utils import InvalidGithubURL, get_associated_commit_urls, get_gh_issue_data, parse_gh_issue_urlhandler = RichHandler(show_time=False, show_path=False)handler.setLevel(logging.DEBUG)@@ -154,15 +154,12 @@ def should_open_pr(args, info: Dict[str, Any], *, token: str="") -> bool: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.+ org, repo, issue_number = parse_gh_issue_url(args.environment.data_path)+ associated_commits = get_associated_commit_urls(org, repo, issue_number, token=token)+ 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 Falsereturn Truediff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 5a9f1654..c1dbe80d 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -678,8 +678,8 @@ class SWEEnv(gym.Env):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 = get_gh_issue_data(issue_url, token=self.token)+ branch_name = f"swe-agent-fix-#{issue.number}-" + str(random.random())[2:10]issue_url = self.args.data_pathself.communicate_with_handling(@@ -697,7 +697,6 @@ class SWEEnv(gym.Env):error_msg="Failed to add commits",timeout_duration=10,)- issue = get_gh_issue_data(issue_url, token=self.token)self.communicate_with_handling(input=f"git commit -m 'Fix: {issue.title}' -m 'Closes #{issue.number}' ",error_msg="Failed to commit changes",diff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.pyindex f3b9a553..6588ea01 100644--- a/sweagent/environment/utils.py+++ b/sweagent/environment/utils.py@@ -357,5 +357,20 @@ def get_instances(file_path: str, base_commit: str = None, split: str = None, to)--+def get_associated_commit_urls(org: str, repo: str, issue_number: str, *, token: str = "") -> list[str]:+ """Return the URLs of commits that would close an issue."""+ api = GhApi(token=token)+ # Strangely the "pull_request" field of api.issues.get is often not set+ # so we have to go through the events to check if there's a commit+ events = api.issues.list_events(org, repo, issue_number)+ commit_urls = []+ for event in events:+ if not event.event == "referenced":+ continue+ if not event.commit_id:+ continue+ commit = api.repos.get_commit(org, repo, event.commit_id)+ message = commit.commit.message+ if f"fixes #{issue_number}" in message.lower() or f"closes #{issue_number}" in message.lower():+ commit_urls.append(commit.html_url)+ return commit_urls\ No newline at end of file