-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathbuild_dataset.py
executable file
·190 lines (166 loc) · 6.62 KB
/
build_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
import argparse
import json
import logging
import os
from typing import Optional
from swebench.collect.utils import (
extract_patches,
extract_problem_statement_and_hints,
Repo,
)
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def create_instance(repo: Repo, pull: dict) -> dict:
"""
Create a single task instance from a pull request, where task instance is:
{
repo (str): owner/repo this task instance is from,
pull_number (int): number of PR this task instance is from,
base_commit (str): SHA of the base commit PR is based on,
patch (str): reference solution as .patch (apply to base commit),
test_patch (str): test suite as .patch (apply to base commit),
}
"""
patch, test_patch = extract_patches(pull, repo)
problem_statement, hints = extract_problem_statement_and_hints(pull, repo)
return {
"repo": repo.repo.full_name,
"pull_number": pull["number"],
"instance_id": (repo.repo.full_name + "-" + str(pull["number"])).replace(
"/", "__"
),
"issue_numbers": pull["resolved_issues"],
"base_commit": pull["base"]["sha"],
"patch": patch,
"test_patch": test_patch,
"problem_statement": problem_statement,
"hints_text": hints,
"created_at": pull["created_at"],
}
def is_valid_pull(pull: dict) -> bool:
"""
Check whether PR has an associated issue and is merged
Args:
pull (dict): pull request object
Returns:
bool: whether PR is valid
"""
if pull["merged_at"] is None:
return False
if "resolved_issues" not in pull or len(pull["resolved_issues"]) < 1:
return False
return True
def is_valid_instance(instance: dict) -> bool:
"""
Check whether task instance has all required fields for task instance creation
Args:
instance (dict): task instance object
Returns:
bool: whether task instance is valid
"""
if instance["patch"] is None or instance["patch"] == "":
return False
if instance["problem_statement"] is None or instance["problem_statement"] == "":
return False
return True
def has_test_patch(instance: dict) -> bool:
"""
Check whether task instance has a test suite
Args:
instance (dict): task instance object
Returns:
bool: whether task instance has a test suite
"""
if instance["test_patch"] is None or instance["test_patch"].strip() == "":
return False
return True
def main(pr_file: str, output: str, token: Optional[str] = None):
"""
Main thread for creating task instances from pull requests
Args:
pr_file (str): path to pull request JSONL file
output (str): output file name
token (str): GitHub token
"""
if token is None:
# Get GitHub token from environment variable if not provided
token = os.environ.get("GITHUB_TOKEN")
def load_repo(repo_name):
# Return repo object for a given repo name
owner, repo = repo_name.split("/")
return Repo(owner, repo, token=token)
repos = dict()
completed = 0
with_tests = 0
total_instances = 0
all_output = output + ".all"
seen_prs = set()
# Continue where we left off if output file already exists
if os.path.exists(all_output):
with open(all_output) as f:
for line in f:
pr = json.loads(line)
if "instance_id" not in pr:
pr["instance_id"] = (
pr["repo"] + "-" + str(pr["pull_number"])
).replace("/", "__")
instance_id = pr["instance_id"]
seen_prs.add(instance_id)
if is_valid_instance(pr):
completed += 1
if has_test_patch(pr):
with_tests += 1
logger.info(f"Will skip {len(seen_prs)} pull requests that have already been inspected")
# Write to .all file for all PRs
write_mode_all = "w" if not os.path.exists(all_output) else "a"
with open(all_output, write_mode_all) as all_output:
# Write to output file for PRs with test suites
write_mode = "w" if not os.path.exists(output) else "a"
with open(output, write_mode) as output:
for ix, line in enumerate(open(pr_file)):
total_instances += 1
pull = json.loads(line)
if ix % 100 == 0:
logger.info(
f"[{pull['base']['repo']['full_name']}] (Up to {ix} checked) "
f"{completed} valid, {with_tests} with tests."
)
# Construct instance fields
instance_id = (
pull["base"]["repo"]["full_name"] + "-" + str(pull["number"])
)
instance_id = instance_id.replace("/", "__")
if instance_id in seen_prs:
seen_prs -= {instance_id}
continue
if not is_valid_pull(pull):
# Throw out invalid PRs
continue
# Create task instance
repo_name = pull["base"]["repo"]["full_name"]
if repo_name not in repos:
repos[repo_name] = load_repo(repo_name)
repo = repos[repo_name]
instance = create_instance(repo, pull)
if is_valid_instance(instance):
# If valid, write to .all output file
print(
json.dumps(instance), end="\n", flush=True, file=all_output
) # write all instances to a separate file
completed += 1
if has_test_patch(instance):
# If has test suite, write to output file
print(json.dumps(instance), end="\n", flush=True, file=output)
with_tests += 1
logger.info(f"[{', '.join(repos.keys())}] Total instances: {total_instances}, completed: {completed}, with tests: {with_tests}")
logger.info(f"[{', '.join(repos.keys())}] Skipped {len(seen_prs)} pull requests that have already been inspected")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("pr_file", type=str, help="Path to pull request JSONL file")
parser.add_argument("output", type=str, help="Output file name")
parser.add_argument("--token", type=str, help="GitHub token")
args = parser.parse_args()
main(**vars(args))