Skip to content

Commit

Permalink
Log more things
Browse files Browse the repository at this point in the history
  • Loading branch information
nwiltsie committed Jul 30, 2024
1 parent 8d7e43e commit 21178b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
14 changes: 12 additions & 2 deletions bumpchanges/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
"Work with CHANGELOG.md files."

import argparse
from logging import getLogger

from pathlib import Path

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


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

try:
changelog = Changelog(changelog_file, repo_url)
except ChangelogError:
getLogger(__name__).exception("Could not parse changelog")
raise

changelog.update_version(version)

changelog_file.write_text(changelog.render(), encoding="utf-8")
Expand All @@ -24,6 +32,8 @@ def main():
parser.add_argument("version", type=str)

args = parser.parse_args()
setup_logging()

update_changelog(args.changelog, args.repo_url, args.version)


Expand Down
12 changes: 10 additions & 2 deletions bumpchanges/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
import itertools
import logging
import re

from pathlib import Path
Expand Down Expand Up @@ -132,6 +133,8 @@ def from_tokens(cls, tokens):
else:
raise ChangelogError(f"Invalid section heading: {tokens[1].content}")

logging.getLogger(__name__).info("Parsed new version %s", kwargs.get("version"))

# The rest of the tokens should be the lists. Strip any rulers now.
tokens = [token for token in tokens[3:] if token.type != "hr"]

Expand Down Expand Up @@ -176,7 +179,6 @@ def from_tokens(cls, tokens):
kwargs.setdefault("changed", []).extend(items)

else:
print(tokens)
raise ChangelogError("Don't know how to handle these tokens")

assert not tokens
Expand Down Expand Up @@ -251,6 +253,8 @@ def __init__(self, changelog_file: Path, repo_url: str):
self.changelog_file = changelog_file
self.repo_url = repo_url

logger = logging.getLogger(__name__)

groups = [[]]

all_tokens = MarkdownIt("gfm-like").parse(
Expand All @@ -275,6 +279,7 @@ def __init__(self, changelog_file: Path, repo_url: str):
assert nexttoken is not None
if re.match(r"^\[\d", nexttoken.content):
token.tag = "h2"
logger.notice("Changing `%s` from h1 to h2", nexttoken.content)

if token.tag == "h2":
# A lot of our repositories have an issue where "Added",
Expand All @@ -285,6 +290,7 @@ def __init__(self, changelog_file: Path, repo_url: str):
r"Add|Fix|Change|Remove", nexttoken.content, flags=re.IGNORECASE
):
token.tag = "h3"
logger.notice("Changing `%s` from h2 to h3", nexttoken.content)
else:
# Split split these tokens off into a new Version
groups.append([])
Expand All @@ -301,7 +307,9 @@ def __init__(self, changelog_file: Path, repo_url: str):
def update_version(self, next_version: str):
"Move all unreleased changes under the new version."
if not self.versions or self.versions[0].version != "Unreleased":
print("WARNING: No Unreleased section - adding a new empty section")
logging.getLogger(__name__).warning(
"No Unreleased section - adding a new empty section"
)
self.versions.insert(0, Version.blank_unreleased())

# Change the version and date of the unreleased section. For now
Expand Down
13 changes: 5 additions & 8 deletions bumpchanges/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ def filter(self, record):
return True


def setup_logging() -> logging.Logger:
"Set up logging to GitHub Actions and return the configured logger."
def setup_logging():
"Set up logging to GitHub Actions.logger."
# Does this need to be re-entrant like this?
logger_name = "bumpchanges"

if logging.getLevelName("NOTICE") == NOTICE:
return logging.getLogger(logger_name)
return

logging.addLevelName(NOTICE, "NOTICE")

Expand All @@ -49,7 +47,6 @@ def setup_logging() -> logging.Logger:
defaults={"ghaprefix": ""}
))

root_logger = logging.getLogger(logger_name)
# Set these handlers on the root logger of this module
root_logger = logging.getLogger(__name__.rpartition('.')[0])
root_logger.addHandler(handler)

return root_logger

0 comments on commit 21178b8

Please sign in to comment.