Skip to content

Commit

Permalink
Bump version to v0.2.0 for release
Browse files Browse the repository at this point in the history
  • Loading branch information
richardkoehler committed Feb 21, 2024
1 parent 7d18dc6 commit 6782e27
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## Unreleased

## [v0.3.0](https://github.com/richardkoehler/pte-stats/releases/tag/v0.3.0) - 2024-02-21

### Added

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()
19 changes: 19 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

set -e

TAG=$(python -c 'from pte_stats import __version__; print("v" + __version__)')

read -p "Creating new release for $TAG. Do you want to continue? [Y/n] " prompt

if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
python scripts/prepare_changelog.py
git add -A
git commit -m "Bump version to $TAG for release" || true && git push
echo "Creating new git tag $TAG"
git tag "$TAG" -m "$TAG"
git push --tags
else
echo "Cancelled"
exit 1
fi
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 6782e27

Please sign in to comment.