-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmine-issues.py
243 lines (192 loc) · 6.7 KB
/
mine-issues.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
import argparse
import re
from math import ceil
from urllib.parse import urlencode
from joblib import load
from utils.commentProcessor import processComment
from utils.filterResults import filterIssueWithQueryString
from utils.io import printJSON, writeResultToCSV
from utils.githubAPI import gitHubSearchQueryAPI, gitHubCommentAPI, loadAccessToken
from interface import InitializeSearchInterface
#
# 0)
# Setup argparser to figure out if user wants to use pyinquirer
# or to just pass in the args via argparser
#
def sort_by_arg_checker(arg_value):
valid_options = ["comments", "best-match"]
if arg_value not in valid_options:
raise argparse.ArgumentTypeError
return arg_value
parser = argparse.ArgumentParser()
# Interface option, if true we toggle the interface below.
# If false, we just grab the params from argparser result.
parser.add_argument('-i', '--interactive', action='store_true',
help="**toggle the interactive CLI**")
parser.add_argument('query')
parser.add_argument('-v', '--verbose', action='store_true',
help="print additional logs")
parser.add_argument('-m', '--max-results',
type=int,
default=1000,
help="(int) max results to query")
parser.add_argument('-s', '--sort-by',
type=sort_by_arg_checker,
default='comments',
help='sort by one of: [comments, best-match]')
parser.add_argument('-p', '--prefix-filename',
default="results_",
help='(string) file name prefix for result output files')
parser.add_argument('-f', '--filter',
action='append',
nargs='*',
help='(string) category to filter from results')
args = parser.parse_args()
#
# 1)
# Initialize CLI/Get params
#
params = {}
FILTERED_CATEGORIES = []
# If user entered the '-i' or '--interface' option, trigger the pyinquirer interactive interface.
if args.interactive:
params = InitializeSearchInterface(args.query)
FILTERED_CATEGORIES = params['filter']
else:
params['q'] = args.query
params['max_results'] = args.max_results
params['sort_by'] = args.sort_by
params['print_logs'] = args.verbose
params['out_file_prefix'] = args.prefix_filename
if args.filter:
for f in args.filter:
FILTERED_CATEGORIES.append(" ".join(f))
PAGE = 1
SEARCH_QUERY = params['q']
max_results_param = int(params['max_results'])
pages_per_100 = ceil(max_results_param/100)
MAX_PAGES_TO_QUERY = pages_per_100 if pages_per_100 > 1 else 1
RESULTS_PER_PAGE = 100 if max_results_param > 100 else max_results_param
SORT_BY = params['sort_by']
PRINT_LOGS = params['print_logs']
OUTPUT_FILE_PREFIX = params['out_file_prefix']
# Load GitHub personal access token into memory.
loadAccessToken()
#
# 2)
# Search for issues using GitHub's search query API
#
searchResults = []
GITHUB_API_SEARCH_ISSUES_URL = "https://api.github.com/search/issues"
PAGES_LEFT_TO_QUERY = True
while PAGES_LEFT_TO_QUERY:
encodedQueryString = urlencode({
'q': SEARCH_QUERY,
'per_page': RESULTS_PER_PAGE,
'sort': SORT_BY,
'order': 'desc',
'page': PAGE
})
searchUrl = GITHUB_API_SEARCH_ISSUES_URL + "?" + encodedQueryString
print("[SEARCH QUERY GET]: " + searchUrl)
pageResults = gitHubSearchQueryAPI(searchUrl)
# If no more results, stop querying additional pages.
if len(pageResults['items']) == 0:
PAGES_LEFT_TO_QUERY = False
else:
# If new incoming page results will put us over user defined search limit
# Trim the results down to the user defined limit and stop query
searchResults += pageResults['items']
if len(searchResults) > max_results_param:
searchResults = searchResults[0:max_results_param]
PAGES_LEFT_TO_QUERY = False
PAGE += 1
if PAGE > MAX_PAGES_TO_QUERY:
PAGES_LEFT_TO_QUERY = False
'''
### DEV BLOCK START
This uses a search_sample.json file results for testing/dev purpose
'''
# Hard coded params for later use
# SEARCH_QUERY = ""
# PRINT_LOGS = True
# OUTPUT_FILE_PREFIX = ''
# with open("search_sample.json") as f:
# searchResults = json.load(f)
'''
### DEV BLOCK END
'''
#
# 3)
# Omit any search results that whose body/title does not contain our search query
# Checks for substring match
#
print(str(len(searchResults)) + " results retrieved.")
# Filter out search results that does not contain our query in the body/title
MATCHED_RESULTS, OMITTED_RESULTS = filterIssueWithQueryString(searchResults, SEARCH_QUERY)
print(str(len(MATCHED_RESULTS)) + " results title/body closely matched with query.")
print(str(len(OMITTED_RESULTS)) + " results omitted due to lack of match with query.")
OMITTED_ISSUES = []
for result in OMITTED_RESULTS:
OMITTED_ISSUES.append({
"issueID": result['id'],
"issueURL": result['html_url'],
"title": result['title'],
"body": result['body']
})
if PRINT_LOGS:
print('\n OMITTED ISSUES \n')
printJSON(OMITTED_ISSUES)
'''
### DEV BLOCK START
This slices the first 10 matches so we don't overload GitHub's API limit
'''
# Dev code to limit results to be processed to not further overload the API
# MATCHED_RESULTS = MATCHED_RESULTS[0:10]
# Test comment API URL:
# https://api.github.com/repos/tensorflow/tensorflow/issues/27880/comments
'''
### DEV BLOCK END
'''
#
# 4)
# For each of the results (MATCHED_RESULTS) that matches our query
# Fetch their comments and clean/tokenize them.
#
# Hold all comment lines, and tag each individual lines with the comment/issue related data
# i.e URLs, IDs, links etc...
CORPUS = []
# Hold all issues with their comments API url to query
issues_api_urls = []
for r in MATCHED_RESULTS:
# For each issue, append to issues_api_urls the comments_url API endpoint to query the comments
issues_api_urls.append({
"issueID": r['id'],
"comments_url": r['comments_url'],
"issueURL_HTML": r['html_url']
})
print("\n")
# For each of the comment_url in the list to query, query for all the comments
# and add them to the corpus.
CORPUS += gitHubCommentAPI(issues_api_urls)
#
# 5)
# Once we have fetch all the comments, and split them up into lines and tokenized them
# Load the prediction model from the serialized file and predict the comments.
#
### Load Model/Vector - Use the serialized model/count vector files included
model = load("models/GitHub_comments_logisticRegression.model")
vectorizer = load("models/GitHub_comments_logisticRegression.countVector")
for c in CORPUS:
test_vector = vectorizer.transform([c['commentLine']])
c['category'] = model.predict(test_vector)[0]
# Filter out any results that has category that is in the list of categories to be omitted from results.
CORPUS = [c for c in CORPUS if c['category'] not in FILTERED_CATEGORIES]
# Print the resulting corpus with the category predicted for each comment.
if PRINT_LOGS:
print('\n CLASSIFIED COMMENTS \n')
printJSON(CORPUS)
print("\n")
writeResultToCSV(OMITTED_ISSUES, OUTPUT_FILE_PREFIX + '_OMITTED_ISSUES')
writeResultToCSV(CORPUS, OUTPUT_FILE_PREFIX + '_CLASSIFIED_COMMENTS')
print("\n")