import os import re from pathlib import Path import click import gitea GITEA_URL = "https://git.cpi.imtek.uni-freiburg.de" GITEA_OWNER = "Safeguard" GITEA_REPO = "MBP-issues" DIR_MBP_WORKBOOKS = "/mnt/e/Safeguard MBP Workbooks" FILE_CHANGELOG = "/mnt/e/Safeguard-MBP-issues/CHANGELOG.txt" FILE_OUTPUT = "List of FRMs.txt" RE_ISSUE_CHANGES = re.compile("#(\d+)") RE_ISSUE_FILENAME = re.compile("Issue (\d+)", re.IGNORECASE) WORKBOOK_TYPES = ["hyb", "regen", "dry", "qc"] def connect_to_gitea(url=GITEA_URL): token = os.getenv("GITEA_SG_FRM_TOKEN") return gitea.Gitea(url, token) def get_gitea_issues(url=GITEA_URL, owner=GITEA_OWNER, repo=GITEA_REPO): client = connect_to_gitea(url) repo = gitea.Repository.request(client, owner, repo) return repo.get_issues_state(gitea.Issue.CLOSED) def get_issue_numbers_from_changelog(filepath=FILE_CHANGELOG): with Path(FILE_CHANGELOG).open("r") as fh: # remove starting lines for line in fh: if line.startswith("v3"): break # this are now the changes, until a blank line occures issues = set() for line in fh: if not line.strip(): break results = (r.group(1) for r in RE_ISSUE_CHANGES.finditer(line)) # hash_stripped = (issue[1:] for issue in results) issues.update(int(issue) for issue in results) return issues def _issue_and_type_from_file_path(file_path): raw_issue = RE_ISSUE_FILENAME.search(file_path.stem) issue = int(raw_issue.group(1)) rest, raw_kind = file_path.stem.rsplit(" ", 1) kind = raw_kind.lower() if kind == "reg": kind = "regen" if kind not in WORKBOOK_TYPES: raise ValueError(f"Unknown workbook type: {file_path.name}") return issue, kind def get_frms(issues, workbook_dir=DIR_MBP_WORKBOOKS): directory = Path(DIR_MBP_WORKBOOKS) search_result = directory.glob("**/*.docx") visible = (i for i in search_result if not i.stem.startswith(".")) file_paths = (i for i in visible if i.is_file()) result = {} for file_path in file_paths: if file_path.parent == directory: continue try: issue, kind = _issue_and_type_from_file_path(file_path) except ValueError(): continue if issue in issues: item = result.setdefault(issue, dict.fromkeys(WORKBOOK_TYPES, "")) item[kind] = file_path.name return result def process( url=GITEA_URL, owner=GITEA_OWNER, repo=GITEA_REPO, changes=FILE_CHANGELOG, frm_dir=DIR_MBP_WORKBOOKS, ): gitea_issues = get_gitea_issues(url, owner, repo) closed_issues = {i.number: i.title for i in gitea_issues} change_issues = get_issue_numbers_from_changelog(changes) frm_issues = get_frms(change_issues) for issue, info in frm_issues.items(): info["title"] = closed_issues.get(issue, "") return frm_issues def write(frms, output_path=FILE_OUTPUT): with Path(output_path).open("w") as fh: for issue in sorted(frms.keys()): frm = frms[issue] title = frm["title"] fh.write(f"Issue {issue}: {title}\n") for kind in WORKBOOK_TYPES: frm_file_name = frm[kind] if frm_file_name: fh.write(f" - {frm_file_name}\n") fh.write("\n") @click.command() @click.option("-o", "--output", default=FILE_OUTPUT) def cli(output=FILE_OUTPUT): frms = process() write(frms, output)