Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: add make changelog_style for automatic changelog formatting #4532

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ C_FILES = $(shell find . -type f -name '*.[ch]' | grep -f ./tools/style.c.inclu

style_check: pystyle_check ruststyle_check cstyle_check changelog_check yaml_check docs_summary_check editor_check ## run all style checks

style: pystyle ruststyle cstyle ## apply all code styles (C+Rust+Py)
style: pystyle ruststyle cstyle changelog_style ## apply all code styles (C+Rust+Py+Changelog)

pystyle_check: ## run code style check on application sources and tests
flake8 --version
Expand Down Expand Up @@ -52,15 +52,10 @@ pystyle: ## apply code style on application sources and tests
make -C python style

changelog_check: ## check changelog format
./tools/generate-changelog.py --check core
./tools/generate-changelog.py --check core/embed/projects/boardloader
./tools/generate-changelog.py --check core/embed/projects/bootloader
./tools/generate-changelog.py --check core/embed/projects/bootloader_ci
./tools/generate-changelog.py --check core/embed/projects/prodtest
./tools/generate-changelog.py --check legacy/bootloader
./tools/generate-changelog.py --check legacy/firmware
./tools/generate-changelog.py --check legacy/intermediate_fw
./tools/generate-changelog.py --check python
./tools/changelog.py check

changelog_style: ## fix changelog format
./tools/changelog.py style

yaml_check: ## check yaml formatting
yamllint .
Expand Down
4 changes: 2 additions & 2 deletions docs/misc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ message to exclude that commit from the check.
## Generating changelog at the time of release

When it's time to release new version of a repository component the formatted
changelog needs to be generated using the `tools/generate-changelog.py` script.
changelog needs to be generated using the `tools/changelog.py generate` command.
It accepts repo subdirectory and the version number as arguments and you can
specify the release date if it's different from today's date:

```
tools/generate-changelog.py --date "20th April 2021" legacy/firmware 1.10.0
tools/changelog.py generate --date "20th April 2021" legacy/firmware 1.10.0
```

## Cherry-picking changes to release branch
Expand Down
70 changes: 62 additions & 8 deletions tools/generate-changelog.py → tools/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import click

from typing import Iterator

LINK_RE = re.compile(r"\[#(\d+)\]")
ISSUE_URL = "https://github.com/trezor/trezor-firmware/pull/{issue}"

Expand All @@ -17,6 +19,25 @@
INTERNAL_MODELS = ("T2T1", "T2B1", "T3B1", "T3T1", "D001")
INTERNAL_MODELS_SKIP = ("D001",)

ROOT = Path(__file__).parent.parent

# Source of truth for all managed changelogs in this repository.
# Please extend it when adding a new project with a managed changelog.
KNOWN_PROJECTS = (
ROOT / "core",
ROOT / "core/embed/projects/boardloader",
ROOT / "core/embed/projects/bootloader",
ROOT / "core/embed/projects/bootloader_ci",
ROOT / "core/embed/projects/prodtest",
ROOT / "legacy/bootloader",
ROOT / "legacy/firmware",
ROOT / "legacy/intermediate_fw",
ROOT / "python",
)

for project in KNOWN_PROJECTS:
assert project.is_dir(), f"Project {project} does not exist"

IGNORED_FILES = ('.gitignore', '.keep')


Expand Down Expand Up @@ -118,21 +139,34 @@ def filter_line(line: str) -> str | None:
destination.write(res)


def check_style(project: Path):
success = True
def _iter_fragments(project: Path) -> Iterator[Path]:
fragements_dir = project / ".changelog.d"
for fragment in fragements_dir.iterdir():
if fragment.name in IGNORED_FILES:
continue
yield fragment


def check_fragments_style(project: Path):
success = True
for fragment in _iter_fragments(project):
fragment_text = fragment.read_text().rstrip()
if not fragment_text.endswith("."):
click.echo(f"Fragment '{fragment}' must end with a period.")
click.echo(f"Changelog '{fragment}' must end with a period.")
success = False

if not success:
raise click.ClickException("Fragment style check failed.")
raise click.ClickException(f"Changelog style error: {project}")
else:
click.echo("Fragment style check passed.")
click.echo(f"Changelog style OK: {project}")


def fix_fragments_style(project: Path):
for fragment in _iter_fragments(project):
fragment_text = fragment.read_text().rstrip()
if not fragment_text.endswith("."):
fragment.write_text(fragment_text + ".\n")
click.echo(f"Changelog '{fragment}' style fixed.")


def generate_filtered(project: Path, changelog: Path):
Expand All @@ -145,7 +179,26 @@ def generate_filtered(project: Path, changelog: Path):
filter_changelog(changelog, internal_name)


@click.command()
@click.group()
def cli():
pass


@cli.command()
def check():
"""Check the style of all changelog fragments."""
for project in KNOWN_PROJECTS:
check_fragments_style(project)


@cli.command()
def style():
"""Fix the style of all changelog fragments."""
for project in KNOWN_PROJECTS:
fix_fragments_style(project)


@cli.command()
@click.argument(
"project",
type=click.Path(exists=True, dir_okay=True, file_okay=False, resolve_path=True),
Expand All @@ -160,7 +213,7 @@ def generate_filtered(project: Path, changelog: Path):
"--check", is_flag=True, help="Dry run, do not actually create changelog."
)
@click.option("--only-models", is_flag=True, help="Only regenerate the model-changelogs from the main one.")
def cli(project, version, date, check, only_models):
def generate(project, version, date, check, only_models):
"""Generate changelog for given project (core, python, legacy/firmware,
legacy/bootloader).

Expand All @@ -172,7 +225,8 @@ def cli(project, version, date, check, only_models):
- Tell git to stage changed files.
"""
project = Path(project)
check_style(project)
if project not in KNOWN_PROJECTS:
raise click.ClickException(f"Please add '{project}' to `KNOWN_PROJECTS` to be part of our managed changelogs.")

changelog = project / "CHANGELOG.md"

Expand Down
Loading