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]
Make github token fully optional (#189)
Kilian Lieretb084c9fecb0c
diff --git a/README.md b/README.mdindex 12de76b9..63edd81a 100644--- a/README.md+++ b/README.md@@ -114,16 +114,18 @@ If you're using docker, pass the key with the [`-e` option](https://stackoverflo<details><summary>🔎 More options for different keys (click to unfold)</summary>+All keys are optional.+```-GITHUB_TOKEN: 'GitHub Token Here (required)'-OPENAI_API_KEY: 'OpenAI API Key Here if using OpenAI Model (optional)'-ANTHROPIC_API_KEY: 'Anthropic API Key Here if using Anthropic Model (optional)'-TOGETHER_API_KEY: 'Together API Key Here if using Together Model (optional)'-AZURE_OPENAI_API_KEY: 'Azure OpenAI API Key Here if using Azure OpenAI Model (optional)'-AZURE_OPENAI_ENDPOINT: 'Azure OpenAI Endpoint Here if using Azure OpenAI Model (optional)'-AZURE_OPENAI_DEPLOYMENT: 'Azure OpenAI Deployment Here if using Azure OpenAI Model (optional)'-AZURE_OPENAI_API_VERSION: 'Azure OpenAI API Version Here if using Azure OpenAI Model (optional)'-OPENAI_API_BASE_URL: 'LM base URL here if using Local or alternative api Endpoint (optional)'+GITHUB_TOKEN: 'GitHub Token Here'+OPENAI_API_KEY: 'OpenAI API Key Here if using OpenAI Model'+ANTHROPIC_API_KEY: 'Anthropic API Key Here if using Anthropic Model'+TOGETHER_API_KEY: 'Together API Key Here if using Together Model'+AZURE_OPENAI_API_KEY: 'Azure OpenAI API Key Here if using Azure OpenAI Model'+AZURE_OPENAI_ENDPOINT: 'Azure OpenAI Endpoint Here if using Azure OpenAI Model'+AZURE_OPENAI_DEPLOYMENT: 'Azure OpenAI Deployment Here if using Azure OpenAI Model'+AZURE_OPENAI_API_VERSION: 'Azure OpenAI API Version Here if using Azure OpenAI Model'+OPENAI_API_BASE_URL: 'LM base URL here if using Local or alternative api Endpoint'```</details>diff --git a/run.py b/run.pyindex 1b3d3bed..a90d9983 100644--- a/run.py+++ b/run.py@@ -143,7 +143,7 @@ def main(args: ScriptArguments):)save_predictions(traj_dir, instance_id, info)save_patch(traj_dir, instance_id, info)- if args.actions.open_pr and should_open_pr(args, info, token=env.token):+ if args.actions.open_pr and should_open_pr(args, info, token=env._github_token):env.open_pr(trajectory=trajectory, push_gh_repo_url=args.actions.push_gh_repo_url)except KeyboardInterrupt:diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 9f30e59e..bb9b680d 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -86,7 +86,8 @@ class SWEEnv(gym.Env):if not self.args.verbose:self.logger.disabled = True- # Get commit hash+ #: The commit hash of the swe-agent repository+ self.commit_sha = Nonetry:repo = Repo(search_parent_directories=True)self.commit_sha = repo.head.object.hexsha@@ -94,19 +95,17 @@ class SWEEnv(gym.Env):raiseexcept:logger.warning("Failed to get commit hash for this repo")- self.commit_sha = None- # Set GitHub Token- self.token = os.environ.get("GITHUB_TOKEN", None)- if (self.token is None or self.token == "") and os.path.isfile(+ self._github_token = os.environ.get("GITHUB_TOKEN", None)+ if not self._github_token and os.path.isfile(os.path.join(os.getcwd(), "keys.cfg")):- self.cfg = config.Config(os.path.join(os.getcwd(), "keys.cfg"))- self.token = self.cfg.get("GITHUB_TOKEN", None)+ cfg = config.Config(os.path.join(os.getcwd(), "keys.cfg"))+ self._github_token = cfg.get("GITHUB_TOKEN", None)# Load Task Instancesself.data_path = self.args.data_path- self.data = get_instances(self.data_path, self.args.base_commit, self.args.split, token=self.token)+ self.data = get_instances(self.data_path, self.args.base_commit, self.args.split, token=self._github_token)self.logger.info(f"💽 Loaded dataset from {self.data_path}")# Establish connection with execution container@@ -152,17 +151,20 @@ class SWEEnv(gym.Env):folders = self.communicate(input="ls").split("\n")repo_name = self.record["repo"].replace("/", "__")if repo_name not in folders:+ token_prefix = ""+ if self._github_token:+ token_prefix = f"{self._github_token}@"if not self.args.no_mirror and not self.is_from_github_url:self.logger.info(f"{repo_name} not found in container, cloning...")self.communicate_with_handling(- input=f"git clone https://{self.token}@github.com/swe-bench/{repo_name}.git",+ input=f"git clone https://{token_prefix}github.com/swe-bench/{repo_name}.git",error_msg="Failed to clone repository from mirror",timeout_duration=LONG_TIMEOUT,)else:logger.info(f"Trying to clone from non-mirror...")self.communicate_with_handling(- input=f"git clone https://{self.token}@github.com/{self.record['repo']}.git {repo_name}",+ input=f"git clone https://{token_prefix}github.com/{self.record['repo']}.git {repo_name}",error_msg="Failed to clone repository from non-mirror",timeout_duration=LONG_TIMEOUT,)@@ -746,7 +748,7 @@ class SWEEnv(gym.Env):# todo: have better way of handling this# Adding random string suffix to avoid name conflicts if we had a previously failed runissue_url = self.args.data_path- issue = get_gh_issue_data(issue_url, token=self.token)+ issue = get_gh_issue_data(issue_url, token=self._github_token)branch_name = f"swe-agent-fix-#{issue.number}-" + str(random.random())[2:10]self.communicate_with_handling(@@ -778,7 +780,10 @@ class SWEEnv(gym.Env):else:owner, repo = parse_gh_repo_url(push_gh_repo_url)if push_gh_repo_url:- fork_url = f"https://{self.token}@github.com/{owner}/{repo}.git"+ token_prefix = ""+ if self._github_token:+ token_prefix = f"{self._github_token}@"+ fork_url = f"https://{token_prefix}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",@@ -801,7 +806,7 @@ class SWEEnv(gym.Env):f"to close [#{issue.number}]({issue_url}) ({issue.title}).\n\nCloses #{issue.number}.")body += "\n\n" + format_trajectory_markdown(trajectory)- api = GhApi(token=self.token)+ api = GhApi(token=self._github_token)if not _dry_run:pr_info = api.pulls.create(owner=owner,