|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import sys |
| 4 | +from dataclasses import dataclass |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +PARSING_STATUSES = ["broken"] |
| 8 | + |
| 9 | + |
| 10 | +@dataclass |
| 11 | +class BrokenLink: |
| 12 | + location: str |
| 13 | + line_nr: str |
| 14 | + reasoning: str |
| 15 | + |
| 16 | + |
| 17 | +def parse_broken_links(log: str) -> list[BrokenLink]: |
| 18 | + broken_links: list[BrokenLink] = [] |
| 19 | + lines = log.strip().split("\n") |
| 20 | + |
| 21 | + for line in lines: |
| 22 | + parts = line.split(") ") |
| 23 | + if len(parts) < 2: |
| 24 | + continue |
| 25 | + |
| 26 | + location_part = parts[0].replace("(", "").strip() |
| 27 | + location = location_part.split(":")[0].strip() |
| 28 | + line_nr = location_part.split("line")[-1].strip() |
| 29 | + status_and_url_part = parts[1] |
| 30 | + |
| 31 | + if not any(status in status_and_url_part for status in PARSING_STATUSES): |
| 32 | + continue |
| 33 | + status_and_url = status_and_url_part.split(" - ") |
| 34 | + if len(status_and_url) < 2: |
| 35 | + continue |
| 36 | + reasoning = status_and_url[1].strip() |
| 37 | + |
| 38 | + broken_links.append( |
| 39 | + BrokenLink( |
| 40 | + location=location, |
| 41 | + line_nr=line_nr, |
| 42 | + reasoning=reasoning, |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + return broken_links |
| 47 | + |
| 48 | + |
| 49 | +def generate_markdown_table(broken_links: list[BrokenLink]) -> str: |
| 50 | + table = "| Location | Line Number | Reasoning |\n" |
| 51 | + table += "|----------|-------------|-----------|\n" |
| 52 | + |
| 53 | + for link in broken_links: |
| 54 | + table += ( |
| 55 | + f"| {link.location} | {link.line_nr} | {link.reasoning} |\n" |
| 56 | + ) |
| 57 | + |
| 58 | + return table |
| 59 | + |
| 60 | + |
| 61 | +def generate_issue_body(broken_links: list[BrokenLink]) -> str: |
| 62 | + markdown_table = generate_markdown_table(broken_links) |
| 63 | + return f""" |
| 64 | +# Broken Links Report. |
| 65 | +**Last updated: {datetime.now().strftime('%d-%m-%Y %H:%M')}** |
| 66 | +
|
| 67 | +The following broken links were detected in the documentation: |
| 68 | +{markdown_table} |
| 69 | +Please investigate and fix these issues to ensure all links are functional. |
| 70 | +Thank you! |
| 71 | +
|
| 72 | +--- |
| 73 | +This issue will be auto updated regularly if link issues are found. |
| 74 | +You may close it if you wish, though a new one will be created if link issues are still present. |
| 75 | +
|
| 76 | +""" |
| 77 | + |
| 78 | + |
| 79 | +def strip_ansi_codes(text: str) -> str: |
| 80 | + """Remove ANSI escape sequences from text""" |
| 81 | + ansi_escape = re.compile(r"\x1b\[[0-9;]*m") |
| 82 | + return ansi_escape.sub("", text) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + argparse = argparse.ArgumentParser( |
| 87 | + description="Parse broken links from Sphinx log and generate issue body." |
| 88 | + ) |
| 89 | + argparse.add_argument("logfile", type=str, help="Path to the Sphinx log file.") |
| 90 | + args = argparse.parse_args() |
| 91 | + with open(args.logfile) as f: |
| 92 | + log_content_raw = f.read() |
| 93 | + log_content = strip_ansi_codes(log_content_raw) |
| 94 | + broken_links = parse_broken_links(log_content) |
| 95 | + if not broken_links: |
| 96 | + # Nothing broken found, can exit early |
| 97 | + sys.exit(0) |
| 98 | + issue_body = generate_issue_body(broken_links) |
| 99 | + if broken_links: |
| 100 | + with open("issue_body.md", "w") as out: |
| 101 | + out.write(issue_body) |
0 commit comments