Skip to content

Commit

Permalink
Add command to produce CHANGELOG.md file
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Sep 3, 2024
1 parent 397b8c5 commit d2a6572
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ src/mk/_version.py
.coverage*
coverage.xml
CMakeFiles
CHANGELOG.md
74 changes: 58 additions & 16 deletions src/mk/tools/pre.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import datetime
import json
import logging
import os
import shutil
Expand All @@ -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

0 comments on commit d2a6572

Please sign in to comment.