Skip to content

Commit

Permalink
Update release workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
richardkoehler committed Feb 15, 2024
1 parent 0166957 commit 36b86c5
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ dmypy.json

# Ruff litner
.ruff_cache/

# Archive
.archive/
29 changes: 18 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
build-backend = "hatchling.build"
requires = ["hatchling"]


[project]
authors = [
{name = "Richard M. Köhler"},
Expand Down Expand Up @@ -30,13 +29,13 @@ dependencies = [
"numpy",
"scikit-image",
"statsmodels",
"pandas-stubs",
]

[project.optional-dependencies]
dev = [
"black",
"mypy",
"packaging",
"pandas-stubs",
"pre-commit",
"pytest",
Expand All @@ -45,28 +44,36 @@ dev = [
"tox",
]

[tool.flit.module]
name = "pte_stats"

[tool.hatch.version]
path = "src/pte_stats/__init__.py"

[project.urls]
bugtracker = "https://github.com/richardkoehler/pte-stats/issues"
changelog = "https://github.com/richardkoehler/pte-stats/blob/main/doc/CHANGELOG.md"
changelog = "https://github.com/richardkoehler/pte-stats/blob/main/docs/CHANGELOG.md"
repository = "https://github.com/richardkoehler/pte-stats"

[tool.black]
exclude = '''
/(
__pycache__
)/
'''
line-length = 79

[tool.ruff]
line-length = 79

[tool.ruff.lint]
select = [
# pycodestyle
"E",
# Pyflakes
"F",
# pyupgrade
"UP",
# flake8-bugbear
"B",
# flake8-simplify
"SIM",
# isort
"I",
]

[tool.ruff.per-file-ignores]
"__init__.py" = ["F401", "F403"]

Expand Down
41 changes: 41 additions & 0 deletions scripts/prepare_changelog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from datetime import datetime
from pathlib import Path

from pte_stats import __version__


def main():
changelog = Path(r"docs/CHANGELOG.md")

with changelog.open() as f:
lines = f.readlines()

insert_index: int = -1
for i in range(len(lines)):
line = lines[i]
if line.startswith("## Unreleased"):
insert_index = i + 1
elif line.startswith(f"## [v{__version__}]"):
print("CHANGELOG already up-to-date")
return
elif line.startswith("## [v"):
break

if insert_index < 0:
raise RuntimeError("Couldn't find 'Unreleased' section")

lines.insert(insert_index, "\n")
lines.insert(
insert_index + 1,
f"## [v{__version__}]"
"(https://github.com/richardkoehler/pte-stats/releases"
f"/tag/v{__version__}) - "
f"{datetime.now().strftime('%Y-%m-%d')}\n",
)

with changelog.open("w") as f:
f.writelines(lines)


if __name__ == "__main__":
main()
83 changes: 83 additions & 0 deletions scripts/release_notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Prepares markdown release notes for GitHub releases.
"""

import os

import packaging.version

TAG = os.environ["TAG"]

ADDED_HEADER = "### Added 🎉"
CHANGED_HEADER = "### Changed ⚠️"
FIXED_HEADER = "### Fixed ✅"
REMOVED_HEADER = "### Removed 👋"


def get_change_log_notes() -> str:
in_current_section = False
current_section_notes: list[str] = []
with open(r"docs/CHANGELOG.md") as changelog:
for line in changelog:
if line.startswith("## "):
if line.startswith("## Unreleased"):
continue
if line.startswith(f"## [{TAG}]"):
in_current_section = True
continue
break
if in_current_section:
if line.startswith("### Added"):
line = ADDED_HEADER + "\n"
elif line.startswith("### Changed"):
line = CHANGED_HEADER + "\n"
elif line.startswith("### Fixed"):
line = FIXED_HEADER + "\n"
elif line.startswith("### Removed"):
line = REMOVED_HEADER + "\n"
current_section_notes.append(line)
assert current_section_notes
return "## What's new\n\n" + "".join(current_section_notes).strip() + "\n"


def get_commit_history() -> str:
new_version = packaging.version.parse(TAG)

# Pull all tags.
os.popen("git fetch --tags")

# Get all tags sorted by version, latest first.
all_tags = (
os.popen("git tag -l --sort=-version:refname 'v*'").read().split("\n")
)

# Out of `all_tags`, find the latest previous version so that we can
# collect all commits between that version and the new version we're about
# to publish. Note that we ignore pre-releases unless the new version is
# also a pre-release.
last_tag: str | None = None
for tag in all_tags:
if not tag.strip(): # could be blank line
continue
version = packaging.version.parse(tag)
if new_version.pre is None and version.pre is not None:
continue
if version < new_version:
last_tag = tag
break
if last_tag is not None:
commits = os.popen(
f"git log {last_tag}..{TAG} --oneline --first-parent"
).read()
else:
commits = os.popen("git log --oneline --first-parent").read()
return "## Commits\n\n" + commits


def main():
print(get_change_log_notes())
print(get_commit_history())


if __name__ == "__main__":
main()

0 comments on commit 36b86c5

Please sign in to comment.