-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_generate_issue_p.py
198 lines (154 loc) · 5.33 KB
/
auto_generate_issue_p.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
import requests
import json
import ast
from PyPDF2 import PdfReader
from pprint import pprint
from pathlib import Path
from countdown import countdown
# from itertools import cycle
# from os import listdir
# from os.path import isfile, join
# import re
def read_pdf_template(pdf_template):
"""
Read attached pdf using PyPDF2, define variables for importing
"""
reader = ''
reader = PdfReader(pdf_template)
global dict
dict = reader.get_form_text_fields()
def set_repo():
global repo
repo = str(dict['github_handle'])
repo_qstn = True
while repo_qstn:
repo_qstn = input(f'The working repo is set to: {repo}. Switch? ')
repo = str(dict['github_handle']) if repo_qstn.lower() == 'n' else 'hackforla'
print(f'Proceeding with working repo: {repo}')
break
def evaluate_template():
"""
Evaluate imported pdf, generate unique templates
"""
global saved_templates
saved_templates = []
all_files = ast.literal_eval(str(dict["file_info"]))
pprint(all_files)
for k, v in all_files.items():
saved_templates.append(create_json_template(k, v))
def create_json_template(FILE_NAME, temp_value):
"""
Create json_template using imported values
"""
BEFORE = temp_value[0]
AFTER = temp_value[1]
"""
Create body of template
"""
DEPENDENCY = f"### Dependency \n{dict['dependency']}" if dict['dependency'] else ""
DETAILS = f"### Details \n{dict['details']}" if dict['details'] else ""
ACTIONS = (dict['action_items']).replace('{FILE_NAME}', FILE_NAME).replace('{BEFORE}', BEFORE).replace('{AFTER}', AFTER)
RESOURCES = (dict['resources'])
body = f"""
### Prerequisites
1. You must be a member of Hack for LA to work on an issue. If you have not joined yet, please follow the steps on our [Getting Started](https://www.hackforla.org/getting-started) page.
2. Please make sure you have read our Hack for LA [Contributing Guide](https://github.com/hackforla/website/blob/gh-pages/CONTRIBUTING.md) before you claim/start working on an issue.
{DEPENDENCY}
{DETAILS}
### Overview
{dict['overview']}
### Action Items
{ACTIONS}
### Resources/Instructions
{RESOURCES}
"""
body_lines = ''
for line in body.splitlines():
body_lines += line + '\\n'
"""
Create head of template
"""
TITLE = '"' + dict['title'].replace('{FILE_NAME}', FILE_NAME) + '"'
LABELS = (dict['labels'])
json_template = f'''
"title": {TITLE},
"labels": [
{LABELS}
],
"body":
'''
json_template += '"'+body_lines+'"'
return json_template
def generate_issue(num):
"""
Generate issue
"""
token = dict['secret']
headers = {"Authorization": "token {}".format(token)}
data = ast.literal_eval('{'+saved_templates[int(num)]+'}')
countdown(5)
print(f'Generating issue {num} of total {len(saved_templates)}:', '\r')
# Script to create issue
url = f"https://api.github.com/repos/{repo}/website/issues"
response = requests.post(url, data=json.dumps(data), headers=headers)
if response.status_code == 201:
print(f"Success! Created issue {num} of {len(saved_templates)}")
print(response.content)
def main():
"""
Program that reads input from a specific PDF form, prepares data in markdown format,
then sends to GitHub for creation of a new issue in user-specified repo
"""
print('Issue Generator')
pdf_template = "issue_template_ex_4777.pdf"
"""
--> TO DO (optionally) Code to allow change to pdf template file, search, etc.
while True:
print(f'Ready to read PDF Template: \"{pdf_template}\"')
pdf_qstn = input('Enter any key to proceed, or \"c\" to change: ')
if pdf_qstn.lower() == 'c':
pdf_ans = input("Enter new name of PDF template: ")
if Path(pdf_ans).suffix == '.pdf' and Path(pdf_ans).is_file():
pdf_template = pdf_ans
else:
print('Entry not valid. Using default')
break
"""
read_pdf_template(pdf_template)
set_repo()
evaluate_template()
"""
Prepare the issues
"""
response = True
while response:
which_one = input(f"\n\nPrepare which number (0 to {len(saved_templates)-1}, [A]ll, or e[x]it)? ")
try:
if which_one == 'x':
break
which = int(which_one)
if 0 <= which < len(saved_templates):
print(f'Displaying template {which_one}\n')
elif which_one == 'A':
proceed = input('Generating All- are you sure? ')
if proceed.lower() == 'y':
for num in range(len(saved_templates)):
generate_issue(num)
print('Finished!')
break
else:
raise ValueError
except ValueError:
print('Retry or e[x]it: ')
pprint(saved_templates[which])
rvw_temp = input(f'\nThis is the template that will be generated in {repo}\nContinue to generate issue? ')
if rvw_temp.lower() == 'y':
generate_issue(which)
else:
qstn = input(f'Enter [y]es to start over or e[x]it: ')
if qstn.lower() == 'y':
continue
else:
break
if __name__ == "__main__":
main()