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

Add command to produce CHANGELOG.md file #239

Merged
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
1 change: 1 addition & 0 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ concurrency:
cancel-in-progress: true

env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
FORCE_COLOR: 1 # tox, pytest, ansible-lint
PY_COLORS: 1

Expand Down
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
3 changes: 3 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ build:
tools:
python: "3.11"
commands:
- asdf --version
- asdf plugin add github-cli
- asdf install
- pip install --user tox
- python3 -m tox -e docs -- --strict --site-dir=_readthedocs/html/
python:
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github-cli 2.55.0
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ source = ["src", ".tox/*/site-packages"]
exclude_also = ["pragma: no cover", "if TYPE_CHECKING:"]
omit = ["test/*"]
# Increase it just so it would pass on any single-python run
fail_under = 47
fail_under = 46
skip_covered = true
skip_empty = true
# During development we might remove code (files) with coverage data, and we dont want to fail:
Expand Down
1 change: 1 addition & 0 deletions samples/integration/ansible-lint.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
alerts
build
changelog
docs
drafts
eco
Expand Down
1 change: 1 addition & 0 deletions samples/integration/cookiecutter.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
alerts
build
changelog
clean-build
clean-coverage
clean-docs-build
Expand Down
1 change: 1 addition & 0 deletions samples/integration/flask-babel.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
alerts
build
changelog
drafts
install
prs
Expand Down
1 change: 1 addition & 0 deletions samples/integration/nox.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
alerts
build
changelog
conda_tests
cover
docs
Expand Down
1 change: 1 addition & 0 deletions samples/integration/podman.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ alerts
binaries
binaries2
binaries23
changelog
clean
clean-binaries
docs
Expand Down
1 change: 1 addition & 0 deletions samples/integration/typeshed.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
alerts
changelog
create_baseline_stubs
drafts
generate_proto_stubs
Expand Down
79 changes: 63 additions & 16 deletions src/mk/tools/pre.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import annotations

import datetime
import json
import logging
import os
import re
import shutil
from pathlib import Path

Expand All @@ -15,32 +18,76 @@ 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 = """---\ntitle: Changelog\n---\n\n"""
for release in drafts_json:
logging.info("Processing release '%s'", release["tag_name"])
result += f"# {release['tag_name']}"
if release["draft"]:
result += " (unreleased)"
else:
created = datetime.datetime.fromisoformat(
release["created_at"][:10],
).replace(
tzinfo=datetime.timezone.utc,
)
result += f" ({created.strftime('%Y-%m-%d')})"

body = release["body"].strip()
result += f"\n\n{body}\n\n"

result = result.rstrip("\r\n") + "\n"
result = result.replace("\r\n", "\n")
result = re.sub(r"\n{3,}", "\n\n", result, re.MULTILINE)
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
7 changes: 6 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ commands =
passenv =
CURL_CA_BUNDLE # https proxies, https://github.com/tox-dev/tox/issues/1437
FORCE_COLOR
GH_*
GITHUB_*
HOME
LANG
LC_*
Expand Down Expand Up @@ -91,7 +93,7 @@ setenv =
commands =
pre-commit run --all-files --show-diff-on-failure --hook-stage manual deps

[testenv:docs]
[testenv:docs,py{310,311,312,313}-docs]
description = Builds docs
extras =
docs
Expand All @@ -103,7 +105,10 @@ setenv =
skip_install = false
usedevelop = true
commands =
sh -c "cd docs && mk changelog"
mkdocs build {posargs:}
white_list_externals =
sh

[testenv:lint]
description = Run linters
Expand Down
Loading