-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand_tool.py
252 lines (205 loc) · 11.2 KB
/
command_tool.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
import shlex, subprocess, json, argparse, re
# GitHub functionality
def get_issue_list():
# Get list of all issues in repository
get_issue_list = "gh issue list -s all -L 10000 --json title,number"
list_args = shlex.split(get_issue_list)
issue_list = json.loads(subprocess.check_output(list_args))
return issue_list
def get_issue_numbers_from_exceptions(issue_list, exceptions):
# Get remaining issues from list of exceptions
numbers = []
exceptions = exceptions.split(',')
if exceptions[0].isdigit():
exceptions_int = []
for exception in exceptions:
exceptions_int.insert(len(exceptions_int),int(exception))
for issue in issue_list:
if issue["number"] not in exceptions_int:
numbers.insert(len(numbers), issue["number"])
else:
for issue in issue_list:
if issue["title"].split(" |")[0] not in exceptions:
numbers.insert(len(numbers),issue["number"])
return numbers
def get_issue_numbers(issue_list, countries):
# Get issue numbers for desired countries
numbers = []
if countries == "all":
for issue in issue_list:
numbers.insert(len(numbers),issue["number"])
else:
for issue in issue_list:
if issue["title"].split(" |")[0] in countries:
numbers.insert(len(numbers),issue["number"])
return numbers
def add_message_to_top(numbers, addition):
for i in numbers:
try:
# Get current issue body
view_body = "gh issue view " + str(i)
print(view_body)
view_args = shlex.split(view_body)
body = subprocess.check_output(view_args, stderr=subprocess.STDOUT).decode('utf-8')
body = body.split("--")[1]
# Create new issue body
new_body = ">" + addition + "\n" + body
# Update issue body
update_code = "gh issue edit " + str(i) + " --body"
print(update_code)
split_update_code = shlex.split(update_code)
split_update_code.insert(len(split_update_code),new_body)
subprocess.run(split_update_code, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
if "GraphQL: Could not resolve to an issue or pull request with the number of" in e.output.decode('utf-8'):
e = "Issue could not be found"
print(f"Error in processing Issue #{i}: {e}")
except Exception as e:
print(f"Error in processing Issue #{i}: {e}")
def delete_message_from_top(numbers):
for i in numbers:
try:
# Get current issue body
view_body = "gh issue view " + str(i)
print(view_body)
view_args = shlex.split(view_body)
body = subprocess.check_output(view_args, stderr=subprocess.STDOUT).decode('utf-8')
body = body.split("--")[1]
# Create new issue body
new_body = body
if ">" in new_body[:2]:
new_body = body[body.index("\n", 1)+1:].lstrip()
# Update issue body
update_code = "gh issue edit " + str(i) + " --body"
print(update_code)
split_update_code = shlex.split(update_code)
split_update_code.insert(len(split_update_code),new_body)
subprocess.run(split_update_code, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
if "GraphQL: Could not resolve to an issue or pull request with the number of" in e.output.decode('utf-8'):
e = "Issue could not be found"
print(f"Error in processing Issue #{i}: {e}")
except Exception as e:
print(f"Error in processing Issue #{i}: {e}")
def find_and_replace(numbers, find, replace):
find = bytes(find, "utf-8").decode("unicode_escape")
replace = bytes(replace, "utf-8").decode("unicode_escape")
for i in numbers:
try:
# Get current issue body
view_body = "gh issue view " + str(i)
print(view_body)
view_args = shlex.split(view_body)
body = subprocess.check_output(view_args, stderr=subprocess.STDOUT).decode('utf-8')
body = body.split("--")[1]
# Find and replace phrase
result = body.replace(find, replace)
if result == body:
raise Exception("Phrase '" + find + "' could not be found.")
else:
new_body = result
# Update issue body
update_code = "gh issue edit " + str(i) + " --body"
print(update_code)
split_update_code = shlex.split(update_code)
split_update_code.insert(len(split_update_code),new_body)
subprocess.run(split_update_code, stderr=subprocess.STDOUT)
print("Replacement was made.")
except subprocess.CalledProcessError as e:
if "GraphQL: Could not resolve to an issue or pull request with the number of" in e.output.decode('utf-8'):
e = "Issue could not be found"
print(f"Error in processing Issue #{i}: {e}")
except Exception as e:
print(f"Error in processing Issue #{i}: {e}")
def load_issue_body(issue_number):
# Get current issue body
view_body = "gh issue view " + str(issue_number) + " --json body"
print(view_body)
view_args = shlex.split(view_body)
subprocess.run(view_args, stderr=subprocess.STDOUT)
# Command Line Functionality
def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(prog='command_testing',
description='Edits GitHub issues from command line')
parser.add_argument('-a', '--all', action='store_true',
help='Indicates that all issues should be edited.', required=False)
parser.add_argument('-c', '--countries',
help='Comma separated list of counties to edit. Names longer than 1 word must be in quotes.', required=False)
parser.add_argument('-d', '--delete', action='store_true',
help='Indicates that message at top should be deleted.', required=False)
parser.add_argument('-e', '--exceptions',
help='Comma separated list of issue numbers NOT to edit.', required=False)
parser.add_argument('-f', '--find',
help='Phrase to find.', required=False)
parser.add_argument('-m', '--message', type=str,
help='Message to add to top of issue description. Must be in quotes.', required=False)
parser.add_argument('-n', '--numbers',
help='Comma separated list of issue numbers to edit.', required=False)
parser.add_argument('-r', '--replace',
help='Replacement phrase to accompany find phrase.', required=False)
parser.add_argument('-g', '--getIssueBody',
help='Issue number to retrieve issue body.', required=False)
args = parser.parse_args()
if not args.getIssueBody is None:
for arg in vars(args):
if arg == "getIssueBody":
continue
if not getattr(args, arg) in [None, False]:
print(arg, getattr(args, arg))
raise Exception("Get Issue Body parameter cannot be combined with other parameters.")
return args
if (args.replace is None and not args.find is None) or (args.find is None and not args.replace is None):
raise Exception("If you wish to find and replace a phrase, please provide both a phrase to find (-f) and a phrase to use as replacement (-r). Otherwise please remove the -f or -r input.")
if (not args.replace is None and args.delete) or (not args.replace is None and not args.message is None) or (args.delete and not args.message is None):
raise Exception("You cannot delete the message at the top, find and replace a phrase, or add a message to the top simultaneously. Please either choose -d, provide a message using -m, or provide phrases to find and replace using -r.")
if not args.delete and args.message is None and args.replace is None:
raise Exception("Please indicate that the message at the top should be deleted, provide a message to be added, or provide phrases to find and replace.")
if (args.all and not args.countries is None) or (args.all and not args.numbers is None) or (not args.numbers is None and not args.countries is None) or (not args.exceptions is None and args.all) or (not args.exceptions is None and not args.countries is None) or (not args.exceptions is None and not args.numbers is None):
raise Exception("You cannot provide issue numbers, countries, exceptions, or all issues simultaneously. Please either indicate a list of issue numbers, a list of countries, a list of exceptions, or all issues.")
if args.countries is None and args.numbers is None and not args.all and args.exceptions is None:
raise Exception("Please indicate --all or provide a list of countries or a list of issue numbers.")
elif args.countries is None and not args.numbers is None:
args.numbers = args.numbers.split(',')
elif args.numbers is None and not args.countries is None:
args.countries = args.countries.split(',')
return args
def main():
args = parse_arguments()
if not args.getIssueBody is None:
load_issue_body(args.getIssueBody)
return
if not args.countries is None:
issue_list = get_issue_list()
number_list = get_issue_numbers(issue_list, args.countries)
if args.delete:
delete_message_from_top(number_list)
if not args.message is None:
add_message_to_top(number_list, args.message)
if not args.replace is None:
find_and_replace(number_list, args.find, args.replace)
if not args.numbers is None:
if args.delete:
delete_message_from_top(args.numbers)
if not args.message is None:
add_message_to_top(args.numbers, args.message)
if not args.replace is None:
find_and_replace(args.numbers, args.find, args.replace)
if args.all:
issue_list = get_issue_list()
number_list = get_issue_numbers(issue_list, "all")
if args.delete:
delete_message_from_top(number_list)
if not args.message is None:
add_message_to_top(number_list, args.message)
if not args.replace is None:
find_and_replace(number_list, args.find, args.replace)
if not args.exceptions is None:
issue_list = get_issue_list()
number_list = get_issue_numbers_from_exceptions(issue_list, args.exceptions)
if args.delete:
delete_message_from_top(number_list)
if not args.message is None:
add_message_to_top(number_list, args.message)
if not args.replace is None:
find_and_replace(number_list, args.find, args.replace)
main()