1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-10-18 22:09:07 +00:00
trezor-firmware/tools/linkify-changelogs.py

88 lines
2.4 KiB
Python
Raw Normal View History

2020-07-02 09:59:37 +00:00
#!/usr/bin/env python3
import os
from pathlib import Path
import re
import click
LINK_RE = re.compile(r"\[#(\d+)\]")
ISSUE_URL = "https://github.com/trezor/trezor-firmware/issues/{issue}"
ROOT = Path(__file__).parent.resolve().parent
DEFAULT_CHANGELOGS = ( # TODO replace with a wildcard?
ROOT / "core" / "CHANGELOG.md",
ROOT / "legacy" / "firmware" / "CHANGELOG.md",
ROOT / "legacy" / "bootloader" / "CHANGELOG.md",
2020-07-02 09:59:37 +00:00
ROOT / "python" / "CHANGELOG.md",
)
2020-07-02 13:36:38 +00:00
def process_changelog(changelog_file, only_check=False):
2020-07-02 09:59:37 +00:00
links = {}
2020-07-02 13:36:38 +00:00
orig_links = {}
2020-07-02 09:59:37 +00:00
result_lines = []
with open(changelog_file, "r+") as changelog:
for line in changelog:
m = LINK_RE.match(line)
if m: # line *starts with* issue identifier
# keep existing links as-is
2020-07-02 13:36:38 +00:00
orig_links[int(m[1])] = line.replace(m[0] + ": ", "").strip()
2020-07-02 09:59:37 +00:00
else:
for issue in LINK_RE.findall(line):
links[int(issue)] = ISSUE_URL.format(issue=issue)
result_lines.append(line)
2020-07-02 13:36:38 +00:00
if only_check:
missing_links = set(links.keys()) - set(orig_links.keys())
if missing_links:
click.echo(f"missing links: {missing_links}")
return False
else:
return True
links.update(orig_links)
2020-07-02 09:59:37 +00:00
changelog.seek(0)
changelog.truncate(0)
for line in result_lines:
changelog.write(line)
for marker, url in sorted(links.items()):
changelog.write(f"[#{marker}]: {url}\n")
2020-07-02 13:36:38 +00:00
return True
2020-07-02 09:59:37 +00:00
@click.command()
@click.argument(
"changelogs",
nargs=-1,
type=click.Path(exists=True, dir_okay=False, writable=True),
)
2020-07-02 13:36:38 +00:00
@click.option("--check", is_flag=True, help="Check for missing links, do not modify.")
def cli(changelogs, check):
2020-07-02 09:59:37 +00:00
"""Linkify changelog.
Find all occurences of "[#123]" in text, and add a Markdown link to the referenced
issue.
If no arguments are provided, runs on all known changelogs.
"""
if not changelogs:
changelogs = DEFAULT_CHANGELOGS
2020-07-02 13:36:38 +00:00
all_ok = True
2020-07-02 09:59:37 +00:00
for changelog in changelogs:
2020-07-02 13:36:38 +00:00
click.echo(changelog)
if not process_changelog(changelog, check):
all_ok = False
if not all_ok:
raise click.ClickException("Some links are missing. Run `make style` to fix.")
2020-07-02 09:59:37 +00:00
if __name__ == "__main__":
cli()