import os import csv import re from dotenv import load_dotenv load_dotenv() FOREGEJO_API_TOKEN = os.getenv("FORGEJO_API_TOKEN") forgejo_issue_api_string = "/repos/{owner}/{repo}/issues/{index}" # {'harvest_project': ('owner', 'repo')} projects = { 'MASS Continuous Improvement': ('mass', 'mass'), } def test_parse_notes_section(): if (parse_notes_section('Resolved Issue #4, #5 and Issue 6') == ['Issue #4', '#5', 'Issue 6']): print("test parse_notes_section passed") else: print("Test parse_notes_section 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 parse_harvest_csv(file=None): if file is None: file = input('Enter harvest report: ') print(file) if os.path.exists(file): print('Beginning parsing for issues') with open(file, 'r') as f: csv_reader = csv.DictReader(f) rows = list(csv_reader) original_fieldnames = csv_reader.fieldnames issue_fields = [ "First Issue Title", "First Issue URL", "Second Issue Title", "Second Issue URL", "Third Issue Title", "Third Issue URL" ] modified_fieldnames = original_fieldnames + issue_fields with open('modified_csv.csv', 'w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames = modified_fieldnames) writer.writeheader() row_count = 0 for row in rows: issues = parse_notes_section(row['Notes']) issues_dict = dict(zip(issue_fields, issues)) row.update(issues_dict) writer.writerow(row) def test_program(): parse_harvest_csv("louis_harvest_time_report_from2023-10-01to2023-10-07.csv") test_parse_notes_section() test_program()