| 1 | import json |
| 2 | import os |
| 3 | import shutil |
| 4 | from abc import ABC, abstractmethod |
| 5 | from os.path import join as pjoin |
| 6 | from pathlib import Path |
| 7 | |
| 8 | import requests |
| 9 | |
| 10 | from app import utils as app_utils |
| 11 | from app.log import log_and_print |
| 12 | from app.task import PlainTask, SweTask, Task |
| 13 | |
| 14 | |
| 15 | class RawTask(ABC): |
| 16 | @property |
| 17 | @abstractmethod |
| 18 | def task_id(self) -> str: |
| 19 | raise NotImplementedError("abstract base class") |
| 20 | |
| 21 | @abstractmethod |
| 22 | def to_task(self) -> Task: |
| 23 | raise NotImplementedError("abstract base class") |
| 24 | |
| 25 | @abstractmethod |
| 26 | def dump_meta_data(self, output_dir: str) -> None: |
| 27 | raise NotImplementedError("abstract base class") |
| 28 | |
| 29 | |
| 30 | class RawSweTask(RawTask): |
| 31 | """ |
| 32 | Encapsulate everything required to run one task. |
| 33 | """ |
| 34 | |
| 35 | def __init__(self, task_id: str, setup_info: dict, task_info: dict): |
| 36 | # a counter str, format "1/150", which means first task out of 150 |
| 37 | # id from the benchmark |
| 38 | self._task_id = task_id |
| 39 | # setup_info (Dict): keys: ['repo_path', 'env_name', 'pre_install', 'install','test_cmd'] |
| 40 | self.setup_info = setup_info |
| 41 | # task_info (Dict): keys: ['base_commit', 'hints_text', 'created_at', |
| 42 | # 'test_patch', 'repo', 'problem_statement', 'version', 'instance_id', |
| 43 | # 'FAIL_TO_PASS', 'PASS_TO_PASS', 'environment_setup_commit'] |
| 44 | self.task_info = task_info |
| 45 | |
| 46 | @property |
| 47 | def task_id(self) -> str: |
| 48 | return self._task_id |
| 49 | |
| 50 | def to_task(self) -> SweTask: |
| 51 | task_id = self.task_id |
| 52 | setup_info = self.setup_info |
| 53 | task_info = self.task_info |
| 54 | return SweTask( |
| 55 | task_id=task_id, |
| 56 | problem_statement=task_info["problem_statement"], |
| 57 | repo_path=setup_info["repo_path"], |
| 58 | env_name=setup_info["env_name"], |
| 59 | pre_install_cmds=setup_info["pre_install"], |
| 60 | install_cmd=setup_info["install"], |
| 61 | # command to run the relevant tests, |
| 62 | test_cmd=setup_info["test_cmd"], |
| 63 | commit=task_info["base_commit"], |
| 64 | repo_name=task_info["repo"], |
| 65 | # modifications to the test suite for this task instance, |
| 66 | test_patch=task_info["test_patch"], |
| 67 | testcases_passing=task_info["PASS_TO_PASS"], |
| 68 | testcases_failing=task_info["FAIL_TO_PASS"], |
| 69 | ) |
| 70 | |
| 71 | def dump_meta_data(self, output_dir: str): |
| 72 | meta = { |
| 73 | "task_id": self.task_id, |
| 74 | "setup_info": self.setup_info, |
| 75 | "task_info": self.task_info, |
| 76 | } |
| 77 | with open(pjoin(output_dir, "meta.json"), "w") as f: |
| 78 | json.dump(meta, f, indent=4) |
| 79 | with open(pjoin(output_dir, "problem_statement.txt"), "w") as f: |
| 80 | f.write(self.task_info["problem_statement"]) |
| 81 | with open(pjoin(output_dir, "developer_patch.diff"), "w") as f: |
| 82 | f.write(self.task_info["patch"]) |
| 83 | |
| 84 | |
| 85 | class RawGithubTask(RawTask): |
| 86 | """ |
| 87 | Encapsulate everything required to run ACR on a fresh issue from the internet. |
| 88 | """ |
| 89 | |
| 90 | def __init__( |
| 91 | self, |
| 92 | task_id: str, |
| 93 | clone_link: str, |
| 94 | commit_hash: str, |
| 95 | issue_link: str, |
| 96 | setup_dir: str, |
| 97 | ): |
| 98 | self._task_id = task_id |
| 99 | self.clone_link = clone_link |
| 100 | self.commit_hash = commit_hash |
| 101 | self.issue_link = issue_link |
| 102 | self.setup_dir = setup_dir |
| 103 | self.clone_path = pjoin(self.setup_dir, self.task_id) |
| 104 | self.problem_statement, self.created_at = self.fetch_issue() |
| 105 | self.clone_repo() |
| 106 | |
| 107 | @property |
| 108 | def task_id(self) -> str: |
| 109 | return self._task_id |
| 110 | |
| 111 | def clone_repo(self): |
| 112 | clone_path = Path(self.clone_path) |
| 113 | if os.path.exists(clone_path): |
| 114 | log_and_print( |
| 115 | f"Path {clone_path} already exists. Removing it to get a fresh clone." |
| 116 | ) |
| 117 | shutil.rmtree(clone_path) |
| 118 | app_utils.clone_repo(self.clone_link, str(clone_path.parent), clone_path.name) |
| 119 | log_and_print(f"Cloned source code to {clone_path}.") |
| 120 | |
| 121 | def dump_meta_data(self, output_dir: str): |
| 122 | meta = { |
| 123 | "task_info": { |
| 124 | "base_commit": self.commit_hash, |
| 125 | "created_at": self.created_at, |
| 126 | "problem_statement": self.problem_statement, |
| 127 | "instance_id": self.task_id, |
| 128 | }, |
| 129 | "setup_info": { |
| 130 | "repo_path": self.clone_path, |
| 131 | }, |
| 132 | } |
| 133 | |
| 134 | meta_file = pjoin(output_dir, "meta.json") |
| 135 | |
| 136 | with open(meta_file, "w") as f: |
| 137 | json.dump(meta, f, indent=4) |
| 138 | |
| 139 | def fetch_issue(self): |
| 140 | if "github.com" not in self.issue_link: |
| 141 | raise NotImplementedError("Only GitHub issues are supported for now.") |
| 142 | |
| 143 | retrieved_issue = self.fetch_github_issue(self.issue_link) |
| 144 | |
| 145 | if retrieved_issue is None: |
| 146 | raise RuntimeError( |
| 147 | f"Failed to retrieve issue information from {self.issue_link}" |
| 148 | ) |
| 149 | |
| 150 | title, body, created_at = retrieved_issue |
| 151 | |
| 152 | problem_statement = f"{title}\n{body}" |
| 153 | |
| 154 | return problem_statement, created_at |
| 155 | |
| 156 | @classmethod |
| 157 | def fetch_github_issue(cls, issue_url: str) -> tuple[str, str, str]: |
| 158 | """Extract owner, repo, and issue number from the URL""" |
| 159 | |
| 160 | # Example issue URL: https://github.com/owner/repo/issues/123 |
| 161 | |
| 162 | _, owner, repo, _, issue_number = issue_url.rsplit("/", 4) |
| 163 | |
| 164 | api_url = f"https://api.github.com/repos/{owner}/{repo}/issues/{issue_number}" |
| 165 | response = requests.get(api_url) |
| 166 | |
| 167 | if response.status_code != 200: |
| 168 | raise RuntimeError( |
| 169 | f"Failed to fetch issue information: {response.status_code}" |
| 170 | ) |
| 171 | |
| 172 | issue_info = response.json() |
| 173 | |
| 174 | title = issue_info["title"] |
| 175 | body = issue_info["body"] |
| 176 | created_at = issue_info["created_at"] |
| 177 | |
| 178 | return title, body, created_at |
| 179 | |
| 180 | def to_task(self) -> PlainTask: |
| 181 | return PlainTask( |
| 182 | commit_hash=self.commit_hash, |
| 183 | local_path=self.clone_path, |
| 184 | problem_statement=self.problem_statement, |
| 185 | ) |
| 186 | |
| 187 | |
| 188 | class RawLocalTask(RawTask): |
| 189 | """ |
| 190 | Encapsulate everything required to run ACR on a local issue on the disk. |
| 191 | """ |
| 192 | |
| 193 | def __init__(self, task_id: str, local_repo: str, issue_file: str): |
| 194 | self._task_id = task_id |
| 195 | self.local_repo = local_repo |
| 196 | self.issue_file = issue_file |
| 197 | self.commit_hash = self.init_local_repo() |
| 198 | self.problem_statement = self.read_issue_from_file() |
| 199 | |
| 200 | @property |
| 201 | def task_id(self) -> str: |
| 202 | return self._task_id |
| 203 | |
| 204 | def init_local_repo(self): |
| 205 | with app_utils.cd(self.local_repo): |
| 206 | if not app_utils.is_git_repo(): |
| 207 | # non git repo - let's make it a git repo first |
| 208 | app_utils.initialize_git_repo_and_commit() |
| 209 | commit = app_utils.get_current_commit_hash() |
| 210 | return commit |
| 211 | |
| 212 | def read_issue_from_file(self) -> str: |
| 213 | # ignore encoding errors so at least we can have some issue content |
| 214 | issue = Path(self.issue_file).read_text(errors="ignore") |
| 215 | return issue |
| 216 | |
| 217 | def dump_meta_data(self, output_dir: str): |
| 218 | meta = { |
| 219 | "task_info": { |
| 220 | "base_commit": self.commit_hash, |
| 221 | "problem_statement": self.problem_statement, |
| 222 | "instance_id": self.task_id, |
| 223 | }, |
| 224 | "setup_info": { |
| 225 | "repo_path": self.local_repo, |
| 226 | }, |
| 227 | } |
| 228 | |
| 229 | meta_file = pjoin(output_dir, "meta.json") |
| 230 | |
| 231 | with open(meta_file, "w") as f: |
| 232 | json.dump(meta, f, indent=4) |
| 233 | |
| 234 | def to_task(self) -> PlainTask: |
| 235 | return PlainTask( |
| 236 | commit_hash=self.commit_hash, |
| 237 | local_path=self.local_repo, |
| 238 | problem_statement=self.problem_statement, |
| 239 | ) |