forked from gitbugactions/gitbugactions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_repos.py
210 lines (175 loc) · 7.96 KB
/
collect_repos.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import tempfile
import os, logging, sys, traceback
import json
import uuid
import fire
import datetime
from github import Repository
from pathlib import Path
from gitbugactions.util import delete_repo_clone, clone_repo
from gitbugactions.crawler import RepoStrategy, RepoCrawler
from gitbugactions.actions.actions import (
GitHubActions,
ActCacheDirManager,
ActCheckCodeFailureStrategy,
)
from gitbugactions.infra.infra_checkers import is_infra_file
class CollectReposStrategy(RepoStrategy):
def __init__(self, data_path: str):
self.data_path = data_path
self.uuid = str(uuid.uuid1())
def save_data(self, data: dict, repo):
"""
Saves the data json to a file with the name of the repository
"""
repo_name = repo.full_name.replace("/", "-")
data_path = os.path.join(self.data_path, repo_name + ".json")
with open(data_path, "w") as f:
json.dump(data, f, indent=4)
def handle_repo(self, repo: Repository):
logging.info(f"Cloning {repo.full_name} - {repo.clone_url}")
repo_path = os.path.join(
tempfile.gettempdir(), self.uuid, repo.full_name.replace("/", "-")
)
data = {
"repository": repo.full_name,
"stars": repo.stargazers_count,
"language": repo.language.strip().lower(),
"size": repo.size,
"clone_url": repo.clone_url,
"timestamp": datetime.datetime.now(datetime.UTC).isoformat() + "Z",
"clone_success": False,
"number_of_actions": 0,
"number_of_test_actions": 0,
"actions_successful": False,
}
repo_clone = clone_repo(repo.clone_url, repo_path)
try:
data["clone_success"] = True
actions = GitHubActions(repo_path, repo.language)
data["number_of_actions"] = len(actions.workflows)
data["actions_build_tools"] = [
x.get_build_tool() for x in actions.workflows
]
data["number_of_test_actions"] = len(actions.test_workflows)
data["actions_test_build_tools"] = [
x.get_build_tool() for x in actions.test_workflows
]
actions.save_workflows()
if len(actions.test_workflows) == 1:
logging.info(f"Running actions for {repo.full_name}")
# Act creates names for the containers by hashing the content of the workflows
# To avoid conflicts between threads, we randomize the name
actions.test_workflows[0].doc["name"] = str(uuid.uuid4())
actions.save_workflows()
act_cache_dir = ActCacheDirManager.acquire_act_cache_dir()
try:
act_run = actions.run_workflow(
actions.test_workflows[0], act_cache_dir=act_cache_dir
)
finally:
ActCacheDirManager.return_act_cache_dir(act_cache_dir)
data["actions_successful"] = not act_run.failed
data["actions_run"] = act_run.asdict()
delete_repo_clone(repo_clone)
self.save_data(data, repo)
except Exception as e:
logging.error(
f"Error while processing {repo.full_name}: {traceback.format_exc()}"
)
delete_repo_clone(repo_clone)
self.save_data(data, repo)
class CollectInfraReposStrategy(CollectReposStrategy):
def __init__(self, data_path: str):
super().__init__(data_path)
def test_actions(self, data: dict, repo: Repository, repo_path: str):
actions = GitHubActions(repo_path, repo.language)
data["number_of_actions"] = len(actions.workflows)
data["actions_build_tools"] = [x.get_build_tool() for x in actions.workflows]
data["number_of_test_actions"] = len(actions.test_workflows)
data["actions_test_build_tools"] = [
x.get_build_tool() for x in actions.test_workflows
]
data["actions_run"] = []
actions.save_workflows()
if len(actions.workflows) >= 1:
logging.info(f"Running actions for {repo.full_name}")
for workflow in actions.workflows:
# Act creates names for the containers by hashing the content of the workflows
# To avoid conflicts between threads, we randomize the name
workflow.doc["name"] = str(uuid.uuid4())
actions.save_workflows()
act_cache_dir = ActCacheDirManager.acquire_act_cache_dir()
try:
act_run = actions.run_workflow(
workflow,
act_cache_dir=act_cache_dir,
act_fail_strategy=ActCheckCodeFailureStrategy(),
)
finally:
ActCacheDirManager.return_act_cache_dir(act_cache_dir)
data["actions_run"].append(act_run.asdict())
if not act_run.failed:
data["actions_successful"] = True
break
else:
data["actions_successful"] = False
def handle_repo(self, repo: Repository):
logging.info(f"Cloning {repo.full_name} - {repo.clone_url}")
repo_path = os.path.join(
tempfile.gettempdir(), self.uuid, repo.full_name.replace("/", "-")
)
data = {
"repository": repo.full_name,
"stars": repo.stargazers_count,
"language": (
repo.language.strip().lower() if repo.language is not None else ""
),
"size": repo.size,
"clone_url": repo.clone_url,
"timestamp": datetime.datetime.now(datetime.UTC).isoformat() + "Z",
"clone_success": False,
"number_of_actions": -1,
"number_of_test_actions": -1,
"actions_successful": None,
}
repo_clone = clone_repo(repo.clone_url, repo_path)
data["clone_success"] = True
infra_files = 0
for root, _, files in os.walk(repo_path):
for f in files:
if is_infra_file(Path(os.path.join(root, f))):
infra_files += 1
data["infra_files"] = infra_files
if infra_files == 0:
delete_repo_clone(repo_clone)
self.save_data(data, repo)
return
try:
self.test_actions(data, repo, repo_path)
delete_repo_clone(repo_clone)
self.save_data(data, repo)
except Exception as e:
logging.error(
f"Error while processing {repo.full_name}: {traceback.format_exc()}"
)
delete_repo_clone(repo_clone)
self.save_data(data, repo)
def collect_repos(
query: str, pagination_freq: str = "M", n_workers: int = 1, out_path: str = "./out/"
):
"""Collect the repositories from GitHub that match the query and have executable
GitHub Actions workflows with parsable tests.
Args:
query (str): Query with the Github searching format (https://docs.github.com/en/search-github/searching-on-github/searching-for-repositories).
pagination_freq (str, optional): Useful if the number of repos to collect is superior to 1000 results (GitHub limit). The possible values are listed here: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases.
For instance, if the value is 'D', each request will be limited to the repos created in a single day, until all the days are obtained.
n_workers (int, optional): Number of parallel workers. Defaults to 1.
out_path (str, optional): Folder on which the results will be saved. Defaults to "./out/".
"""
crawler = RepoCrawler(query, pagination_freq=pagination_freq, n_workers=n_workers)
crawler.get_repos(CollectReposStrategy(out_path))
def main():
fire.Fire(collect_repos)
if __name__ == "__main__":
sys.exit(main())