Skip to content

Commit

Permalink
Add timezone as an input
Browse files Browse the repository at this point in the history
  • Loading branch information
nwiltsie committed Jul 30, 2024
1 parent 718967b commit 29c4319
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/create-release-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ on:
description: Exact version number to target. Only used if bump_type is set to `exact`.
required: false
default: ""
timezone:
type: string
description: IANA timezone to use when computing the current date
default: "America/Los_Angeles"
required: false

jobs:
prepare-release:
Expand All @@ -28,6 +33,7 @@ jobs:
env:
BUMP_TYPE: ${{ inputs.bump_type }}
EXACT_VERSION: ${{ inputs.exact_version }}
CHANGELOG_TIMEZONE: ${{ inputs.timezone }}

steps:
# Get the version of _this_ repository that is in use so that we can use
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
fail-fast: false
matrix:
python-version:
- "3.8"
- "3.9"
- "3.12"

steps:
Expand Down
29 changes: 23 additions & 6 deletions bumpchanges/bump.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
"Work with CHANGELOG.md files."

import argparse
import datetime
import logging
import os
import tempfile
import zoneinfo

from logging import getLogger

from pathlib import Path

from .changelog import Changelog, ChangelogError
from .logging import setup_logging


def update_changelog(changelog_file: Path, repo_url: str, version: str):
def update_changelog(changelog_file: Path, repo_url: str, version: str, date: datetime.date):
"Rewrite a CHANGELOG file for a new release."

try:
Expand All @@ -21,7 +23,7 @@ def update_changelog(changelog_file: Path, repo_url: str, version: str):
getLogger(__name__).exception("Could not parse changelog")
raise

changelog.update_version(version)
changelog.update_version(version, date)

changelog_file.write_text(changelog.render(), encoding="utf-8")

Expand Down Expand Up @@ -57,7 +59,7 @@ def write_commit_details(version: str):
bodyfile.write(f"""\
Update CHANGELOG in preparation for release **{version}**.
Merging this PR will trigger another workflow to create the release tag **v{version}**."
Merging this PR will trigger another workflow to create the release tag **v{version}**.
| Input | Value |
| ----- | ----- |
Expand All @@ -68,7 +70,7 @@ def write_commit_details(version: str):

outputs["pr_bodyfile"] = bodyfile.name

outputs["pr_title"] = f"Prepare for version **`{version}`**"
outputs["pr_title"] = f"Prepare for version `{version}`"
outputs["commit_message"] = f"Update CHANGELOG for version `{version}`"

Path(os.environ["GITHUB_OUTPUT"]).write_text(
Expand All @@ -87,5 +89,20 @@ def entrypoint():
args = parser.parse_args()
setup_logging()

update_changelog(args.changelog, args.repo_url, args.version)
try:
input_timezone = os.environ["CHANGELOG_TIMEZONE"]
try:
tzinfo = zoneinfo.ZoneInfo(input_timezone)
except zoneinfo.ZoneInfoNotFoundError:
logging.getLogger(__name__).warning(
"Time zone `%s` not found! Defaulting to UTC", input_timezone
)
tzinfo = datetime.timezone.utc
except KeyError:
logging.getLogger(__name__).notice("No time zone provided, defaulting to UTC")
tzinfo = datetime.timezone.utc

now_date = datetime.datetime.now(tzinfo).date()

update_changelog(args.changelog, args.repo_url, args.version, now_date)
write_commit_details(args.version)
8 changes: 4 additions & 4 deletions bumpchanges/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime
import itertools
import logging
import os
import re

from dataclasses import dataclass, field
Expand Down Expand Up @@ -304,7 +305,7 @@ def __init__(self, changelog_file: Path, repo_url: str):
if not self.versions:
raise ChangelogError("No versions!")

def update_version(self, next_version: str):
def update_version(self, next_version: str, date: datetime.date):
"Move all unreleased changes under the new version."
if not self.versions or self.versions[0].version != "Unreleased":
logging.getLogger(__name__).warning(
Expand All @@ -315,9 +316,8 @@ def update_version(self, next_version: str):
# Change the version and date of the unreleased section. For now
# explicitly assume UTC, but that should probably be an input.
self.versions[0].version = next_version
self.versions[0].date = (
datetime.datetime.now(datetime.timezone.utc).date().isoformat()
)
self.versions[0].date = date.isoformat()


def render(self) -> str:
"Render the CHANGELOG to markdown."
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dynamic = ["version"]

keywords = ["changelog", "ci"]

requires-python = ">=3.8"
requires-python = ">=3.9"

dependencies = [
"linkify-it-py>=2.0.3",
Expand Down Expand Up @@ -38,7 +38,7 @@ version-file = "bumpchanges/_version.py"
legacy_tox_ini = """
[tox]
env_list =
py3.8
py3.9
py3.12
[testenv]
Expand Down

0 comments on commit 29c4319

Please sign in to comment.