-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathprocess_llvm_commits.py
More file actions
325 lines (276 loc) · 9.49 KB
/
process_llvm_commits.py
File metadata and controls
325 lines (276 loc) · 9.49 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import dataclasses
import datetime
import logging
import math
import os
import re
import git
from google.cloud import bigquery
import requests
import retry
GITHUB_GRAPHQL_API_URL = "https://api.github.com/graphql"
REPOSITORY_URL = "https://github.com/llvm/llvm-project.git"
# BigQuery dataset and tables to write metrics to.
OPERATIONAL_METRICS_DATASET = "operational_metrics"
LLVM_COMMITS_TABLE = "llvm_commits"
# How many commits to query the GitHub GraphQL API for at a time.
# Querying too many commits at once often leads to the call failing.
GITHUB_API_BATCH_SIZE = 35
# Number of days to look back for new commits
# We allow some buffer time between when a commit is made and when it is queried
# for reviews. This is to allow time for any new GitHub events to propogate.
LOOKBACK_DAYS = 2
# Template GraphQL subquery to check if a commit has an associated pull request
# and whether that pull request has been reviewed and approved.
COMMIT_GRAPHQL_SUBQUERY_TEMPLATE = """
commit_{commit_sha}:
object(oid:"{commit_sha}") {{
... on Commit {{
author {{
user {{
login
}}
}}
associatedPullRequests(first: 1) {{
totalCount
pullRequest: nodes {{
number
reviewDecision
reviews(first: 25) {{
nodes {{
state
reviewer: author {{
login
}}
}}
}}
}}
}}
}}
}}
"""
@dataclasses.dataclass
class LLVMCommitInfo:
commit_sha: str
commit_timestamp_seconds: int
diff: list[dict[str, int | str]]
commit_author: str = "" # GitHub username of author is unknown until API call
has_pull_request: bool = False
pull_request_number: int = 0
is_reviewed: bool = False
is_approved: bool = False
reviewers: set[str] = dataclasses.field(default_factory=set)
is_revert: bool = False
pull_request_reverted: int | None = None
commit_reverted: str | None = None
class GitHubAPIError(Exception):
"""Raised when a GitHub GraphQL API call fails."""
def scrape_new_commits_by_date(
target_datetime: datetime.datetime,
) -> list[git.Commit]:
"""Scrape new commits from a given dates.
Args:
target_datetime: The date to scrape for new commits.
Returns:
List of new commits made on the given date.
"""
# Clone repository to current working directory
repo = git.Repo.clone_from(
url=REPOSITORY_URL,
to_path="./llvm-project",
)
# Scrape for new commits
# iter_commits() yields commits in reverse chronological order
new_commits = []
for commit in repo.iter_commits():
# Skip commits that don't match the target date
committed_datetime = commit.committed_datetime.astimezone(
datetime.timezone.utc
)
if committed_datetime.date() != target_datetime.date():
continue
new_commits.append(commit)
logging.info("Found %d new commits", len(new_commits))
return new_commits
@retry.retry(
exceptions=(GitHubAPIError, requests.exceptions.ChunkedEncodingError),
tries=5,
delay=1,
backoff=2,
)
def query_github_graphql_api(
query: str,
github_token: str,
) -> requests.Response:
"""Query GitHub GraphQL API, retrying on failure.
Args:
query: The GraphQL query to send to the GitHub API.
github_token: The access token to use with the GitHub GraphQL API.
Returns:
The response from the GitHub GraphQL API.
"""
response = requests.post(
url=GITHUB_GRAPHQL_API_URL,
headers={
"Authorization": f"bearer {github_token}",
},
json={"query": query},
timeout=30,
)
# Exit if API call fails
# A failed API call means a large batch of data is missing and will not be
# reflected in the dashboard. The dashboard will silently misrepresent
# commit data if we continue execution, so it's better to fail loudly.
if response.status_code < 200 or response.status_code >= 300:
raise GitHubAPIError(
"[%d] Failed to query GitHub GraphQL API: %s"
% (response.status_code, response.text)
)
return response
def query_for_reviews(
new_commits: list[git.Commit], github_token: str
) -> list[LLVMCommitInfo]:
"""Query GitHub GraphQL API for reviews of new commits.
Args:
new_commits: List of new commits to query for reviews.
github_token: The access token to use with the GitHub GraphQL API.
Returns:
List of LLVMCommitInfo objects for each commit's review information.
"""
# Create a map of commit sha to info
new_commits_info = {}
for commit in new_commits:
# Check if this commit is a revert
is_revert = (
re.match(
r"^Revert \".*\"( \(#\d+\))?", commit.message, flags=re.IGNORECASE
)
is not None
)
# Check which pull request or commit is being reverted (if any)
pull_request_match = re.search(
r"Reverts? (?:llvm\/llvm-project)?#(\d+)",
commit.message,
flags=re.IGNORECASE,
)
commit_match = re.search(
r"This reverts commit (\w+)", commit.message, flags=re.IGNORECASE
)
pull_request_reverted = (
int(pull_request_match.group(1)) if pull_request_match else None
)
commit_reverted = commit_match.group(1) if commit_match else None
# Add entry
new_commits_info[commit.hexsha] = LLVMCommitInfo(
commit_sha=commit.hexsha,
commit_timestamp_seconds=commit.committed_date,
diff=[
{
"file": file,
"additions": line_stats["insertions"],
"deletions": line_stats["deletions"],
"total": line_stats["lines"],
}
for file, line_stats in commit.stats.files.items()
],
is_revert=is_revert,
pull_request_reverted=pull_request_reverted,
commit_reverted=commit_reverted,
)
# Create GraphQL subqueries for each commit
commit_subqueries = []
for commit_sha in new_commits_info:
commit_subqueries.append(
COMMIT_GRAPHQL_SUBQUERY_TEMPLATE.format(commit_sha=commit_sha)
)
api_commit_data = {}
query_template = """
query {
repository(owner:"llvm", name:"llvm-project"){
%s
}
}
"""
num_batches = math.ceil(len(commit_subqueries) / GITHUB_API_BATCH_SIZE)
logging.info("Querying GitHub GraphQL API in %d batches", num_batches)
for i in range(num_batches):
subquery_batch = commit_subqueries[
i * GITHUB_API_BATCH_SIZE : (i + 1) * GITHUB_API_BATCH_SIZE
]
query = query_template % "".join(subquery_batch)
logging.info(
"Querying batch %d of %d (%d commits)",
i + 1,
num_batches,
len(subquery_batch),
)
response = query_github_graphql_api(query, github_token)
api_commit_data.update(response.json()["data"]["repository"])
# Amend commit information with GitHub data
for commit_sha, data in api_commit_data.items():
commit_sha = commit_sha.removeprefix("commit_")
commit_info = new_commits_info[commit_sha]
commit_info.commit_author = data["author"]["user"]["login"]
# If commit has no pull requests, skip it. No data to update.
if data["associatedPullRequests"]["totalCount"] == 0:
continue
pull_request = data["associatedPullRequests"]["pullRequest"][0]
commit_info.has_pull_request = True
commit_info.pull_request_number = pull_request["number"]
# Check the state of reviews to determine if this commit was reviewed and
# approved.
review_states = set([
review["state"].upper()
for review in pull_request["reviews"]["nodes"]
if review["reviewer"]["login"] != commit_info.commit_author
])
commit_info.is_reviewed = bool(review_states)
commit_info.is_approved = "APPROVED" in review_states
commit_info.reviewers = set([
review["reviewer"]["login"]
for review in pull_request["reviews"]["nodes"]
])
# There are cases where the commit author is counted as a reviewer. This is
# against what we want to measure, so remove them from the set of reviewers.
commit_info.reviewers.discard(commit_info.commit_author)
return list(new_commits_info.values())
def upload_daily_metrics_to_bigquery(
bq_client: bigquery.Client, new_commits: list[LLVMCommitInfo]
) -> None:
"""Upload processed commit metrics to a BigQuery dataset.
Args:
bq_client: The BigQuery client to use.
new_commits: List of commits to process & upload to BigQuery.
"""
table_ref = bq_client.dataset(OPERATIONAL_METRICS_DATASET).table(
LLVM_COMMITS_TABLE
)
table = bq_client.get_table(table_ref)
commit_records = [dataclasses.asdict(commit) for commit in new_commits]
errors = bq_client.insert_rows(table, commit_records)
if errors:
logging.error("Failed to upload commit info to BigQuery: %s", errors)
exit(1)
def main() -> None:
github_token = os.environ["GITHUB_TOKEN"]
# Scrape new commits
date_to_scrape = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(days=LOOKBACK_DAYS)
logging.info(
"Cloning and scraping llvm/llvm-project for new commits on %s",
date_to_scrape.strftime("%Y-%m-%d"),
)
new_commits = scrape_new_commits_by_date(date_to_scrape)
if not new_commits:
logging.info("No new commits found. Exiting.")
return
logging.info("Querying for reviews of new commits.")
new_commit_info = query_for_reviews(new_commits, github_token)
logging.info("Uploading metrics to BigQuery.")
bq_client = bigquery.Client()
upload_daily_metrics_to_bigquery(bq_client, new_commit_info)
bq_client.close()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()