import os import csv import re from dotenv import load_dotenv import requests load_dotenv() FORGEJO_API_TOKEN = os.getenv("FORGEJO_API_TOKEN") forgejo_issue_api_string = "https://git.agaric.com/api/v1/repos/{owner}/{repo}/issues/{index}" """ projects = { 'harvest_project': ('owner', 'repo name') } """ projects = { "MASS Continuous Improvement": ('mass', 'mass'), "Housing Works": ("housingworks", "app-housingworks-net"), } def test_parse_notes_section(): if (parse_notes_section('Resolved Issue #4, #5 and Issue 6') == ['4', '5', '6']): print("TEST PARSE NOTES SECTION PASSED") else: print("TEST PARSE NOTES SECTION FAILED") 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") 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 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 def parse_harvest_csv(file=None): global issues_and_urls if file is None: 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 modified_fieldnames = original_fieldnames + issue_fields 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) def run_program(): parse_harvest_csv() test_parse_notes_section() test_get_issue_title_and_url() run_program()