-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
281 lines (207 loc) · 7.46 KB
/
app.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
# Github-Issues-Classifier
# Copyright(C) 2020 Georgios (Giorgos) Karantonis
#
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
A CLI tool for the classifier.
'''
import os
os.environ["WANDB_SILENT"] = "true"
import json
import sys
import click
import pandas as pd
from github import Github
from label_bot import models
def init_models(ctx, param, value):
'''
Initializes the trained model.
'''
global BOT
if not value or ctx.resilient_parsing:
BOT = models.Bot(use_head=False)
else:
BOT = models.Bot(use_head=True)
def set_token(ctx, param, value):
'''
Pass the token as a CLI argument.
'''
global token
if not value or ctx.resilient_parsing:
token = get_token()
if not token:
print("Exiting, no token found...")
return
token = value
def get_token(file="token.json"):
'''
Finds and the returns the personal access token
from the ./token.json file.
'''
with open(file) as f:
token = json.load(f)["token"]
return token
def predict(title, body):
'''
Returns the prediction scores.
args:
title : the titles of the issues.
body : the bodies of the issues.
'''
return BOT.predict(title, body)[0]
@click.group()
@click.option("--token", "-t", callback=set_token, expose_value=False)
@click.option("--use-head", "-h", is_flag=True, callback=init_models, expose_value=False)
@click.option("--threshold", "-th", default=.5, type=float)
@click.option("--apply-labels", "-l", is_flag=True)
def cli(threshold, apply_labels):
'''
The CLI tool for the classifier.
args:
threshold : the decision threshold.
apply_labels : whether or not to set the labels on all the visited issues.
'''
global THRESHOLD, APPLY_LABELS
THRESHOLD = threshold
APPLY_LABELS = apply_labels
pass
@cli.command("crawl-org")
@click.option("--organization", "-o")
def run_on_org(organization):
'''
Runs the classifier on all the issues
opened on all the repos of a certain organization.
args:
organization : the organization.
'''
results = pd.DataFrame(columns=["repo", "issue", "bug", "question", "enhancement"])
token = get_token()
g = Github(token)
root = g.get_organization(organization)
for repo in root.get_repos():
for issue in repo.get_issues():
b_score, q_score, e_score = predict(issue.title, issue.body)
if APPLY_LABELS:
for l, s in zip(("bug", "question", "enhancement"), (b_score, q_score, e_score)):
if s >= THRESHOLD:
issue.set_labels(l)
results = results.append({"repo" : repo.name,
"issue" : issue.number,
"bug" : b_score,
"question" : q_score,
"enhancement" : e_score
}, ignore_index=True)
return results
@cli.command("crawl-user")
@click.option("--user", "-u")
def run_on_user(user):
'''
Runs the classifier on all the issues
opened on all the repos of a certain user.
args:
user : the user.
'''
results = pd.DataFrame(columns=["repo", "issue", "bug", "question", "enhancement"])
token = get_token()
g = Github(token)
root = g.get_user(user)
for repo in root.get_repos():
for issue in repo.get_issues():
b_score, q_score, e_score = predict(issue.title, issue.body)
if APPLY_LABELS:
for l, s in zip(("bug", "question", "enhancement"), (b_score, q_score, e_score)):
if s >= THRESHOLD:
issue.set_labels(l)
results = results.append({"repo" : repo.name,
"issue" : issue.number,
"bug" : b_score,
"question" : q_score,
"enhancement" : e_score
}, ignore_index=True)
return results
@cli.command("crawl-repo")
@click.option("--repo", "-r")
def run_on_repo(repo):
'''
Runs the classifier on all the issues of a specific repo.
args:
repo : the repo name.
'''
results = pd.DataFrame(columns=["repo", "issue", "bug", "question", "enhancement"])
token = get_token()
g = Github(token)
repo = g.get_repo(repo)
for issue in repo.get_issues():
b_score, q_score, e_score = predict(issue.title, issue.body)
if APPLY_LABELS:
for l, s in zip(("bug", "question", "enhancement"), (b_score, q_score, e_score)):
if s >= THRESHOLD:
issue.set_labels(l)
results = results.append({"repo" : repo.name,
"issue" : issue.number,
"bug" : b_score,
"question" : q_score,
"enhancement" : e_score
}, ignore_index=True)
return results
@cli.command("crawl-issue")
@click.option("--repo", "-r")
@click.option("--issue", "-i")
def run_on_issue(repo, issue):
'''
Runs the classifier on a specific issue.
args:
repo : the repo where the issue is opened.
issue : the issue number.
'''
results = pd.DataFrame(columns=["repo", "issue", "bug", "question", "enhancement"])
token = get_token()
g = Github(token)
repo = g.get_repo(repo)
issue = repo.get_issue(number=issue)
b_score, q_score, e_score = predict(issue.title, issue.body)
if APPLY_LABELS:
for l, s in zip(("bug", "question", "enhancement"), (b_score, q_score, e_score)):
if s >= THRESHOLD:
issue.set_labels(l)
results = results.append({"repo" : repo.name,
"issue" : issue.number,
"bug" : b_score,
"question" : q_score,
"enhancement" : e_score
}, ignore_index=True)
return results
def demo():
'''
A simple demo function.
'''
title = input("Title: ")
body = input("Body: ")
scores = predict(title, body)
for kind, score in zip(("Bug", "Question", "Enhancement"), scores):
print(f"{kind}: {score}")
print()
keep_going = input("Try another one? [y/n] ")
while keep_going not in ("y", "n"):
print("Type 'y' for YES or 'n' for NO")
keep_going = input("Try another one? [y/n] ")
if keep_going == "y":
demo()
else:
sys.exit()
@cli.command("demo")
def start_demo():
demo()
if __name__ == "__main__":
cli()