-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
main.py
308 lines (238 loc) · 10.2 KB
/
main.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
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
import os
from collections import defaultdict
from datetime import datetime, timedelta
from typing import Optional
from github import Github
from github.Issue import Issue
from github.Repository import Repository
from pytz import timezone
import typer
from typer import Typer
app: Typer = typer.Typer()
DATETIME_FORMAT: str = "%m/%d/%Y %I:%M %p"
CORE_LABELS: set[str] = {
"defect",
"design",
"documentation",
"duplicate",
"enhancement",
"panic / crash",
"support",
}
# A set of labels for adding in labels that we want present in the final
# report, but that we don't want being defined as a core label, since issues
# with without core labels are flagged as errors.
ADDITIONAL_LABELS: set[str] = {
"ai",
"linux",
"vim",
"windows",
}
IGNORED_LABEL_TEXT: str = "ignore top-ranking issues"
ISSUES_PER_LABEL: int = 20
class IssueData:
def __init__(self, issue: Issue) -> None:
self.url: str = issue.html_url
self.like_count: int = issue._rawData["reactions"]["+1"] # type: ignore [attr-defined]
self.creation_datetime: str = issue.created_at.strftime(DATETIME_FORMAT)
# TODO: Change script to support storing labels here, rather than directly in the script
self.labels: set[str] = {label["name"] for label in issue._rawData["labels"]} # type: ignore [attr-defined]
@app.command()
def main(
github_token: Optional[str] = None,
issue_reference_number: Optional[int] = None,
query_day_interval: Optional[int] = None,
) -> None:
start_time: datetime = datetime.now()
start_date: datetime | None = None
if query_day_interval:
tz = timezone("america/new_york")
current_time = datetime.now(tz).replace(
hour=0, minute=0, second=0, microsecond=0
)
start_date = current_time - timedelta(days=query_day_interval)
# GitHub Workflow will pass in the token as an environment variable,
# but we can place it in our env when running the script locally, for convenience
github_token = github_token or os.getenv("GITHUB_ACCESS_TOKEN")
github = Github(github_token)
remaining_requests_before: int = github.rate_limiting[0]
print(f"Remaining requests before: {remaining_requests_before}")
repo_name: str = "zed-industries/zed"
repository: Repository = github.get_repo(repo_name)
# There has to be a nice way of adding types to tuple unpacking
label_to_issue_data: dict[str, list[IssueData]]
error_message_to_erroneous_issue_data: dict[str, list[IssueData]]
(
label_to_issue_data,
error_message_to_erroneous_issue_data,
) = get_issue_maps(github, repository, start_date)
issue_text: str = get_issue_text(
label_to_issue_data,
error_message_to_erroneous_issue_data,
)
if issue_reference_number:
top_ranking_issues_issue: Issue = repository.get_issue(issue_reference_number)
top_ranking_issues_issue.edit(body=issue_text)
else:
print(issue_text)
remaining_requests_after: int = github.rate_limiting[0]
print(f"Remaining requests after: {remaining_requests_after}")
print(f"Requests used: {remaining_requests_before - remaining_requests_after}")
run_duration: timedelta = datetime.now() - start_time
print(run_duration)
def get_issue_maps(
github: Github,
repository: Repository,
start_date: datetime | None = None,
) -> tuple[dict[str, list[IssueData]], dict[str, list[IssueData]]]:
label_to_issues: defaultdict[str, list[Issue]] = get_label_to_issues(
github,
repository,
start_date,
)
label_to_issue_data: dict[str, list[IssueData]] = get_label_to_issue_data(
label_to_issues
)
error_message_to_erroneous_issues: defaultdict[str, list[Issue]] = (
get_error_message_to_erroneous_issues(github, repository)
)
error_message_to_erroneous_issue_data: dict[str, list[IssueData]] = (
get_error_message_to_erroneous_issue_data(error_message_to_erroneous_issues)
)
# Create a new dictionary with labels ordered by the summation the of likes on the associated issues
labels = list(label_to_issue_data.keys())
labels.sort(
key=lambda label: sum(
issue_data.like_count for issue_data in label_to_issue_data[label]
),
reverse=True,
)
label_to_issue_data = {label: label_to_issue_data[label] for label in labels}
return (
label_to_issue_data,
error_message_to_erroneous_issue_data,
)
def get_label_to_issues(
github: Github,
repository: Repository,
start_date: datetime | None = None,
) -> defaultdict[str, list[Issue]]:
label_to_issues: defaultdict[str, list[Issue]] = defaultdict(list)
labels: set[str] = CORE_LABELS | ADDITIONAL_LABELS
date_query: str = (
f"created:>={start_date.strftime('%Y-%m-%d')}" if start_date else ""
)
for label in labels:
query: str = f'repo:{repository.full_name} is:open is:issue {date_query} label:"{label}" -label:"{IGNORED_LABEL_TEXT}" sort:reactions-+1-desc'
issues = github.search_issues(query)
if issues.totalCount > 0:
for issue in issues[0:ISSUES_PER_LABEL]:
label_to_issues[label].append(issue)
return label_to_issues
def get_label_to_issue_data(
label_to_issues: defaultdict[str, list[Issue]],
) -> dict[str, list[IssueData]]:
label_to_issue_data: dict[str, list[IssueData]] = {}
for label in label_to_issues:
issues: list[Issue] = label_to_issues[label]
issue_data: list[IssueData] = [IssueData(issue) for issue in issues]
issue_data.sort(
key=lambda issue_data: (
-issue_data.like_count,
issue_data.creation_datetime,
)
)
if issue_data:
label_to_issue_data[label] = issue_data
return label_to_issue_data
def get_error_message_to_erroneous_issues(
github: Github, repository: Repository
) -> defaultdict[str, list[Issue]]:
error_message_to_erroneous_issues: defaultdict[str, list[Issue]] = defaultdict(list)
# Query for all open issues that don't have either a core or the ignored label and mark those as erroneous
filter_labels: set[str] = CORE_LABELS | {IGNORED_LABEL_TEXT}
filter_labels_text: str = " ".join([f'-label:"{label}"' for label in filter_labels])
query: str = f"repo:{repository.full_name} is:open is:issue {filter_labels_text}"
for issue in github.search_issues(query):
error_message_to_erroneous_issues["missing core label"].append(issue)
return error_message_to_erroneous_issues
def get_error_message_to_erroneous_issue_data(
error_message_to_erroneous_issues: defaultdict[str, list[Issue]],
) -> dict[str, list[IssueData]]:
error_message_to_erroneous_issue_data: dict[str, list[IssueData]] = {}
for label in error_message_to_erroneous_issues:
issues: list[Issue] = error_message_to_erroneous_issues[label]
issue_data: list[IssueData] = [IssueData(issue) for issue in issues]
error_message_to_erroneous_issue_data[label] = issue_data
return error_message_to_erroneous_issue_data
def get_issue_text(
label_to_issue_data: dict[str, list[IssueData]],
error_message_to_erroneous_issue_data: dict[str, list[IssueData]],
) -> str:
tz = timezone("america/new_york")
current_datetime: str = datetime.now(tz).strftime(f"{DATETIME_FORMAT} (%Z)")
highest_ranking_issues_lines: list[str] = get_highest_ranking_issues_lines(
label_to_issue_data
)
issue_text_lines: list[str] = [
f"*Updated on {current_datetime}*",
*highest_ranking_issues_lines,
"",
"---\n",
]
erroneous_issues_lines: list[str] = get_erroneous_issues_lines(
error_message_to_erroneous_issue_data
)
if erroneous_issues_lines:
core_labels_text: str = ", ".join(
f'"{core_label}"' for core_label in CORE_LABELS
)
issue_text_lines.extend(
[
"## errors with issues (this section only shows when there are errors with issues)\n",
f"This script expects every issue to have at least one of the following core labels: {core_labels_text}",
f"This script currently ignores issues that have the following label: {IGNORED_LABEL_TEXT}\n",
"### what to do?\n",
"- Adjust the core labels on an issue to put it into a correct state or add a currently-ignored label to the issue",
"- Adjust the core and ignored labels registered in this script",
*erroneous_issues_lines,
"",
"---\n",
]
)
issue_text_lines.extend(
[
"*For details on how this issue is generated, [see the script](https://github.com/zed-industries/zed/blob/main/script/update_top_ranking_issues/main.py)*",
]
)
return "\n".join(issue_text_lines)
def get_highest_ranking_issues_lines(
label_to_issue_data: dict[str, list[IssueData]],
) -> list[str]:
highest_ranking_issues_lines: list[str] = []
if label_to_issue_data:
for label, issue_data in label_to_issue_data.items():
highest_ranking_issues_lines.append(f"\n## {label}\n")
for i, issue_data in enumerate(issue_data):
markdown_bullet_point: str = (
f"{issue_data.url} ({issue_data.like_count} :thumbsup:)"
)
markdown_bullet_point = f"{i + 1}. {markdown_bullet_point}"
highest_ranking_issues_lines.append(markdown_bullet_point)
return highest_ranking_issues_lines
def get_erroneous_issues_lines(
error_message_to_erroneous_issue_data,
) -> list[str]:
erroneous_issues_lines: list[str] = []
if error_message_to_erroneous_issue_data:
for (
error_message,
erroneous_issue_data,
) in error_message_to_erroneous_issue_data.items():
erroneous_issues_lines.append(f"\n#### {error_message}\n")
for erroneous_issue_data in erroneous_issue_data:
erroneous_issues_lines.append(f"- {erroneous_issue_data.url}")
return erroneous_issues_lines
if __name__ == "__main__":
app()
# TODO: Sort label output into core and non core sections