-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtidier.py
executable file
·290 lines (247 loc) · 8.25 KB
/
tidier.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
#!/usr/bin/env python3
import collections
from datetime import datetime, timezone
import functools
import os
import re
import sys
import textwrap
import dotenv
import github
import gql
from gql import gql as GraphQLQuery
from gql.transport.requests import RequestsHTTPTransport
import requests
dotenv.load_dotenv()
NotSet = object()
DEFAULT_COMMENT_FORMAT = """\
This thread is being closed automatically by \
[Tidier](https://github.com/radian-software/tidier) because it is labeled with \
"{label}" and has not seen any activity for {num_days} days. But don't \
worry—if you have any information that might advance the discussion, \
leave a comment and I will be happy to reopen the thread :)\
"""
def die(message):
"""
Print message to stderr and exit with failure.
"""
print("tidier: " + message, file=sys.stderr)
sys.exit(1)
def done():
"""
Print newline and "Done!" to stdout and exit with success.
"""
print()
print("Done!")
sys.exit(0)
def get_environ_var(name, default=NotSet) -> str:
"""
Get value of environment variable with given name. If not set,
return default. If default is not given, then die and ask the user
to set the environment variable.
"""
if name not in os.environ and default is NotSet:
die("you must set ${}".format(name))
return os.environ.get(name, default) # type: ignore
def get_issue_repo_name(issue):
match = re.fullmatch(
r"https://api.github.com/repos/([^/]+/[^/]+)/issues/[0-9]+", issue.url
)
if not match:
die("unexpected error response from GitHub API")
return match.group(1)
def normalize_boolean(value):
if value == "0" or "no".startswith(value.lower()):
return ""
return value
@functools.cache
def get_graphql_client(token: str):
return gql.Client(
transport=RequestsHTTPTransport(
url="https://api.github.com/graphql",
headers={"Authorization": f"Bearer {token}"},
),
)
@functools.cache
def get_graphql_issue_id(client: gql.Client, owner: str, repo: str, number: int):
return client.execute(
GraphQLQuery(
"""
query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
issueOrPullRequest(number: $number) {
... on Issue {
id
}
... on PullRequest {
id
}
}
}
}
"""
),
variable_values={
"owner": owner,
"repo": repo,
"number": number,
},
)["repository"]["issueOrPullRequest"]["id"]
def close_issue(client: gql.Client, graphql_issue_id: str):
"""
Close the issue with the given internal ID returned from
get_graphql_issue_id. Why use this method instead of the REST API?
Because only with the GraphQL API is it possible to close an issue
when having only triage permission on the repository. This makes
zero sense but is just a documented bug in the REST API.
"""
client.execute(
GraphQLQuery(
"""
mutation($id: ID!) {
closeIssue(input: {issueId: $id, stateReason: NOT_PLANNED}) {
clientMutationId
}
}
"""
),
variable_values={
"id": graphql_issue_id,
},
)
token = get_environ_var("TIDIER_ACCESS_TOKEN").strip()
label = get_environ_var("TIDIER_LABEL", default="waiting on response")
include = get_environ_var("TIDIER_INCLUDE_REPOS", default=".*")
exclude = get_environ_var("TIDIER_EXCLUDE_REPOS", default="(?!)")
num_days = get_environ_var("TIDIER_NUM_DAYS", default="90")
comment_format = get_environ_var(
"TIDIER_COMMENT_FORMAT", default=DEFAULT_COMMENT_FORMAT
)
for_real = get_environ_var("TIDIER_FOR_REAL", default="")
webhook = get_environ_var("TIDIER_WEBHOOK", default="")
try:
num_days = int(num_days)
except ValueError:
die("number of days not an integer: {}".format(num_days))
if num_days < 0:
die("number of days is negative: {}".format(num_days))
for_real = normalize_boolean(for_real)
webhook = normalize_boolean(webhook)
if not re.fullmatch(r'[^"]+', label):
die("unacceptable label name: {}".format(label))
try:
re.compile(include)
except re.error:
die("invalid include regex: {}".format(include))
try:
re.compile(exclude)
except re.error:
die("invalid exclude regex: {}".format(exclude))
comment_text = comment_format.format(label=label, num_days=num_days)
print("Configuration")
print(" label: {}".format(label))
print(" include regex: {}".format(include))
print(" exclude regex: {}".format(exclude))
print(" number of days before closing: {}".format(num_days))
print(" for real: {}".format("yes" if for_real else "no"))
print(
" webhook: {}{}".format(
webhook or "(none)", " (disabled)" if webhook and not for_real else ""
)
)
print(" comment text:")
print(textwrap.indent(textwrap.fill(comment_text), " " * 4))
print()
now = datetime.now(timezone.utc)
print("Timestamp")
print(" {}".format(now))
print()
graphql_client = get_graphql_client(token)
print('Search for issues with label "{}"'.format(label))
g = github.Github(token)
all_issues = list(g.search_issues('label:"{}"'.format(label)))
if not all_issues:
print(" No issues found")
done()
all_issues_by_repo = collections.defaultdict(list)
for issue in all_issues:
all_issues_by_repo[get_issue_repo_name(issue)].append(issue)
print("Retrieve list of repositories to which you have access")
you = g.get_user()
your_username = you.login
your_repo_names = {repo.full_name for repo in you.get_repos()}
if not your_repo_names:
print(" No repositories found")
done()
print()
print("Check your permissions on the repositories")
issues_by_repo = {}
for repo_name, issues in all_issues_by_repo.items():
if repo_name not in your_repo_names:
continue
print(" Repository {}".format(repo_name))
if not re.fullmatch(include, repo_name):
print(" Excluding: does not match include regex")
continue
if re.fullmatch(exclude, repo_name):
print(" Excluding: matches exclude regex")
continue
print("--> Including: all checks passed")
issues_by_repo[repo_name] = issues
print()
print("Process issues and pull requests")
for repo_name, issues in issues_by_repo.items():
print(" Repository {}".format(repo_name))
for issue in issues:
issue_type = "Pull request" if issue.pull_request else "Issue"
print(" {} #{}: {}".format(issue_type, issue.number, issue.title))
if issue.state == "open":
how_old = now - issue.updated_at
if how_old.days >= num_days:
if for_real:
print(
" Closing: {} days since last activity".format(
how_old.days
)
)
# Close the issue first because it's possible that we
# can comment but not close, if the code above doesn't
# do a good job of filtering out repositories where we
# aren't collaborators.
close_issue(
graphql_client,
get_graphql_issue_id(
graphql_client,
issue.repository.owner.login,
issue.repository.name,
issue.number,
),
)
issue.create_comment(comment_text)
issue.remove_from_labels(label)
else:
print(
"----> Would close: {} days since last activity".format(
how_old.days
)
)
else:
print(
" Not closing: {} days since last activity".format(
how_old.days
)
)
else:
if for_real:
print(" Removing label: already closed")
issue.remove_from_labels(label)
else:
print("----> Would remove label: already closed")
if webhook:
print()
print("Webhook {}".format(webhook))
if for_real:
print(" {}".format(requests.get(webhook)))
else:
print(" Skipping")
done()