harvest_issues/generate_issues.py

117 lines
3.3 KiB
Python
Raw Normal View History

2023-10-03 19:15:36 +00:00
import os
import csv
import re
from dotenv import load_dotenv
2023-10-03 22:16:42 +00:00
import requests
load_dotenv()
2023-10-03 22:16:42 +00:00
FORGEJO_API_TOKEN = os.getenv("FORGEJO_API_TOKEN")
2023-10-03 22:16:42 +00:00
forgejo_issue_api_string = "https://git.agaric.com/api/v1/repos/{owner}/{repo}/issues/{index}"
2023-10-03 19:15:36 +00:00
2023-10-03 22:16:42 +00:00
"""
2023-10-03 19:15:36 +00:00
projects = {
2023-10-03 22:16:42 +00:00
'harvest_project': ('owner', 'repo name')
}
"""
projects = {
"MASS Continuous Improvement": ('mass', 'mass'),
"Housing Works": ("housingworks", "app-housingworks-net"),
2023-10-03 19:15:36 +00:00
}
2023-10-03 22:16:42 +00:00
2023-10-03 19:15:36 +00:00
def test_parse_notes_section():
2023-10-03 22:16:42 +00:00
if (parse_notes_section('Resolved Issue #4, #5 and Issue 6') == ['4', '5', '6']):
print("TEST PARSE NOTES SECTION PASSED")
2023-10-03 19:15:36 +00:00
else:
2023-10-03 22:16:42 +00:00
print("TEST PARSE NOTES SECTION FAILED")
2023-10-03 19:15:36 +00:00
2023-10-03 22:16:42 +00:00
def test_get_issue_title_and_url():
solution = [
"Little slow downs are happening - these often signal that the server memory is getting overwhelmed?",
"https://git.agaric.com/housingworks/app-housingworks-net/issues/363"
]
if get_issue_title_and_url(363) == solution:
print("TEST GET ISSUE TITLE AND URL PASSED")
else:
print("TEST GET ISSUE TITLE AND URL FAILED")
2023-10-03 19:15:36 +00:00
def parse_notes_section(notes):
regex_pattern = r"[Ii]ssue\s*(?:#)?\d+|#\d+"
matches = re.findall(regex_pattern, notes)[:3]
issue_numbers = []
for match in matches:
match = re.search(r"\d+", match).group()
issue_numbers.append(match)
return issue_numbers
2023-10-03 19:15:36 +00:00
2023-10-03 22:16:42 +00:00
def get_issue_title_and_url(issue_number):
global issues_and_urls
issues_and_urls = []
owner = projects["Housing Works"][0]
repo = projects["Housing Works"][1]
issue_url = forgejo_issue_api_string.format(owner=owner, repo=repo, index=issue_number)
response = requests.get(issue_url, params={"access_token": FORGEJO_API_TOKEN})
json_response = response.json()
issue_title = json_response['title']
issue_url = json_response['html_url']
issues_and_urls += [issue_title, issue_url]
return [issue_title, issue_url]
def prompt_for_file(file):
file = input("Enter harvest report: ")
if not os.path.exists(file):
print("THAT FILE DOES NOT EXIST, EXITING PROGRAM")
quit()
print(file)
return file
2023-10-03 19:15:36 +00:00
def parse_harvest_csv(file=None):
2023-10-03 22:16:42 +00:00
global issues_and_urls
2023-10-03 19:15:36 +00:00
if file is None:
2023-10-03 22:16:42 +00:00
file = prompt_for_file(file)
print('Beginning parsing for issues')
issue_fields = [
"First Issue Title",
"First Issue URL",
"Second Issue Title",
"Second Issue URL",
"Third Issue Title",
"Third Issue URL"
]
with open(file, 'r') as f:
csv_reader = csv.DictReader(f)
rows = list(csv_reader)
original_fieldnames = csv_reader.fieldnames
2023-10-03 19:15:36 +00:00
modified_fieldnames = original_fieldnames + issue_fields
2023-10-03 22:16:42 +00:00
f.close()
with open('modified_report.csv', 'w', newline='') as csvfile:
issues_and_urls = []
writer = csv.DictWriter(csvfile, fieldnames = modified_fieldnames)
writer.writeheader()
row_count = 0
for row in rows:
issues = parse_notes_section(row['Notes'])
for issue in issues:
get_issue_title_and_url(issue)
issues_dict = dict(zip(issue_fields, issues_and_urls))
row.update(issues_dict)
issues_and_urls = []
writer.writerow(row)
2023-10-03 19:15:36 +00:00
2023-10-03 22:16:42 +00:00
def run_program():
parse_harvest_csv()
2023-10-03 19:15:36 +00:00
test_parse_notes_section()
2023-10-03 22:16:42 +00:00
test_get_issue_title_and_url()
run_program()