From d2a657253d8edfd503a69c2ddc76b9ab5641c2ee Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Tue, 3 Sep 2024 14:45:59 +0100 Subject: [PATCH] Add command to produce CHANGELOG.md file --- .gitignore | 1 + src/mk/tools/pre.py | 74 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index c22da3f..07e6f2d 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ src/mk/_version.py .coverage* coverage.xml CMakeFiles +CHANGELOG.md diff --git a/src/mk/tools/pre.py b/src/mk/tools/pre.py index 5bddc4a..5ecd380 100644 --- a/src/mk/tools/pre.py +++ b/src/mk/tools/pre.py @@ -1,5 +1,7 @@ from __future__ import annotations +import datetime +import json import logging import os import shutil @@ -15,32 +17,72 @@ class PreTool(Tool): def run(self, action: Action | None = None): if action: + if action.name in ["changelog"]: + self.changelog() + return run_or_fail(["pre", action.name], tee=True) def is_present(self, path: Path) -> bool: - if not os.path.exists(os.path.expanduser(CFG_FILE)): - msg = f"Multi-repo feature was disabled because {CFG_FILE} was not found." - logging.debug(msg) - return False if not shutil.which("gh"): logging.warning("Unable to find gh tool. See https://cli.github.com/") return False return True + def changelog(self) -> None: + """Pre helps you generate changelog.md from github releases.""" + releases_json = run_or_fail( + "gh api repos/{owner}/{repo}/releases", + ) + drafts_json = json.loads(releases_json.stdout) + result = "" + for release in drafts_json: + result += f"# {release['tag_name']}" + if release["draft"]: + result += " (unreleased)" + else: + created = datetime.datetime.fromisoformat( + release["created_at"], + ).replace( + tzinfo=datetime.timezone.utc, + ) + result += f" ({created.strftime('%Y-%m-%d')})" + + result += f"\n\n{release['body']}\n" + + result += result.rstrip("\r\n") + "\n" + with open("CHANGELOG.md", "w", encoding="utf-8") as f: + f.write(result) + logging.info("Wrote CHANGELOG.md") + def actions(self) -> list[Action]: - # for command in app.registered_commands: - # print(command.name, command.short_help, command.short_help, command.epilog, command.short_help) - # breakpoint() - return [ - Action(name="prs", description="[dim]Show open PRs[/dim]", tool=self), + actions = [ Action( - name="drafts", - description="[dim]Show draft releases[/dim]", - tool=self, - ), - Action( - name="alerts", - description="[dim]Show dependabot security alerts[/dim]", + name="changelog", + description="[dim]Generate a changelog.md based on github releases.[/dim]", tool=self, ), ] + if not os.path.exists(os.path.expanduser(CFG_FILE)): + msg = f"Multi-repo feature was disabled because {CFG_FILE} was not found." + logging.debug(msg) + else: + actions.extend( + [ + Action( + name="prs", + description="[dim]Show open PRs[/dim]", + tool=self, + ), + Action( + name="drafts", + description="[dim]Show draft releases[/dim]", + tool=self, + ), + Action( + name="alerts", + description="[dim]Show dependabot security alerts[/dim]", + tool=self, + ), + ], + ) + return actions