From 709f90a4c01ce3a919e2928b05b81d00c6b271d6 Mon Sep 17 00:00:00 2001 From: David J Pugh Date: Wed, 27 Dec 2023 11:14:02 +0000 Subject: [PATCH 1/3] Adding logging --- pyproject.toml | 6 ++-- src/mkdocs_github_changelog/__init__.py | 31 +++++++++++++++++++++ src/mkdocs_github_changelog/extension.py | 16 +++++++++-- src/mkdocs_github_changelog/get_releases.py | 5 ++++ tests/unit/test_processor.py | 4 +-- 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 41b1819..0c88592 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -196,10 +196,10 @@ import-order-style="appnexus" profile = "appnexus" src_paths = ['src/'] known_first_party = [ - "mkdocs_github_changelog.*" + "mkdocs_github_changelog.*", + "mkdocs_github_changelog*" ] - -known_application = 'mkdocs_github_changelog.*' +known_application = 'mkdocs_github_changelog*' force_alphabetical_sort_within_sections = true force_sort_within_sections = true reverse_relative = true \ No newline at end of file diff --git a/src/mkdocs_github_changelog/__init__.py b/src/mkdocs_github_changelog/__init__.py index 5d312dd..df81ece 100644 --- a/src/mkdocs_github_changelog/__init__.py +++ b/src/mkdocs_github_changelog/__init__.py @@ -2,6 +2,8 @@ mkdocs extension to autogenerate changelog from github. """ +import logging +from typing import Any, MutableMapping def __get_version() -> str: @@ -45,3 +47,32 @@ def __get_version() -> str: __version__ = __get_version() + + +class _PluginLogger(logging.LoggerAdapter): + """A logger adapter to prefix messages with the originating package name.""" + + def __init__(self, prefix: str, logger: logging.Logger): + """Initialize the object. + + Arguments: + prefix: The string to insert in front of every message. + logger: The logger instance. + """ + super().__init__(logger, {}) + self.prefix = prefix + + def process(self, msg: str, kwargs: MutableMapping[str, Any]) -> tuple[str, Any]: + """Process the message. + + Arguments: + msg: The message: + kwargs: Remaining arguments. + + Returns: + The processed message. + """ + return f"{self.prefix}: {msg}", kwargs + + +logger = _PluginLogger('mkdocs_github_changelog', logging.getLogger("mkdocs.plugins.mkdocs_github_changelog")) diff --git a/src/mkdocs_github_changelog/extension.py b/src/mkdocs_github_changelog/extension.py index b2f08c9..490dfa5 100644 --- a/src/mkdocs_github_changelog/extension.py +++ b/src/mkdocs_github_changelog/extension.py @@ -32,6 +32,7 @@ from markdown.extensions import Extension from mkdocs.utils.yaml import get_yaml_loader, yaml_load +from mkdocs_github_changelog import logger from mkdocs_github_changelog.get_releases import get_releases_as_markdown if TYPE_CHECKING: @@ -42,7 +43,7 @@ class GithubReleaseChangelogProcessor(BlockProcessor): """Changelog Markdown block processor.""" - regex = re.compile(r"^(?P#{1,6} *|)::github-release-changelog ?(?P[a-zA-Z0-9-]+?)\/(?P.+?) *$", flags=re.MULTILINE) + regex = re.compile(r"^(?P#{1,6} *|)::github-release-changelog ?(?P[a-zA-Z\d-]+?)\/(?P.+?) *$", flags=re.MULTILINE) def __init__( self, @@ -55,14 +56,21 @@ def __init__( def test(self, parent: Element, block: str) -> bool: # noqa: U100 """Match the extension instructions.""" - return bool(self.regex.search(block)) + logger.debug(f'Checking {block}') + result = bool(self.regex.search(block)) + if '::github-release-changelog' in block and not result: + logger.warn(f"Block: {block} might be expected to match, but didn't") + return result def run(self, parent: Element, blocks: MutableSequence[str]) -> None: """Run code on the matched blocks to get the markdown.""" block = blocks.pop(0) match = self.regex.search(block) + logger.debug(f'Processing {block}') if match: + logger.debug(f'Matched {block}') + if match.start() > 0: self.parser.parseBlocks(parent, [block[: match.start()]]) # removes the first line @@ -77,9 +85,11 @@ def run(self, parent: Element, blocks: MutableSequence[str]) -> None: if match: heading_level = match["heading"].count("#") + logger.debug(f'Heading level: {heading_level}') # We are going to process the markdown from the releases and then # insert it back into the blocks to be processed as markdown block = self._process_block(match.groupdict()['org'], match.groupdict()['repo'], block, heading_level) + logger.debug('Block processed and releases generated') blocks.insert(0, block) def _process_block( @@ -99,6 +109,8 @@ def _process_block( release_template = config.get('release_template', self._config.get('release_template', None)) match = config.get('match', self._config.get('match', None)) autoprocess = config.get('autoprocess', self._config.get('autoprocess', True)) + logger.info('Getting releases for {org}/{repo}') + logger.debug('Config:: \nrelease_template: {release_template}\ngithub_api_url: {github_api_url}\nmatch: {match}\nautoprocess: {autoprocess}') block = '\n\n'.join(get_releases_as_markdown( organisation_or_user=org, repository=repo, diff --git a/src/mkdocs_github_changelog/get_releases.py b/src/mkdocs_github_changelog/get_releases.py index 3717337..1ac6f90 100644 --- a/src/mkdocs_github_changelog/get_releases.py +++ b/src/mkdocs_github_changelog/get_releases.py @@ -19,6 +19,8 @@ from ghapi.all import GhApi, paged from jinja2 import Environment +from mkdocs_github_changelog import logger + RELEASE_TEMPLATE = "# [{{release.name}}]({{release.html_url}})\n*Released at {{release.published_at.isoformat()}}*\n\n{{release.body}}" @@ -121,12 +123,15 @@ def get_releases_as_markdown( """Get the releases from github as a list of rendered markdown strings.""" if github_api_url is not None: github_api_url = github_api_url.rstrip('/') + logger.info('Getting releases from github') api = GhApi(token=token, gh_host=github_api_url) releases = [] for page in paged(api.repos.list_releases, organisation_or_user, repository, per_page=100): releases += page + logger.info(f'Processing releases from github, {len(releases)} found') jinja_environment = JINJA_ENVIRONMENT_FACTORY.environment selected_releases = _process_releases(releases, match=match, autoprocess=autoprocess) if release_template is None: release_template = RELEASE_TEMPLATE + logger.info(f'Rendering releases from github, {len(releases)} selected') return [jinja_environment.from_string(release_template).render(release=release) for release in selected_releases] diff --git a/tests/unit/test_processor.py b/tests/unit/test_processor.py index 9088273..80e71b4 100644 --- a/tests/unit/test_processor.py +++ b/tests/unit/test_processor.py @@ -6,7 +6,6 @@ from nskit.common.contextmanagers import Env from mkdocs_github_changelog import extension - from mkdocs_github_changelog.extension import GithubReleaseChangelogProcessor @@ -17,7 +16,8 @@ def test_regexp_matching(self): test_map = { '::github-release-changelog abc/def': {'heading': '', 'org': 'abc', 'repo': 'def'}, '## ::github-release-changelog abc-0123/def.xyz': {'heading': '## ', 'org': 'abc-0123', 'repo': 'def.xyz'}, - '### ::github-release-changelog abc-0123/def.xyz_123': {'heading': '### ', 'org': 'abc-0123', 'repo': 'def.xyz_123'} + '### ::github-release-changelog abc-0123/def.xyz_123': {'heading': '### ', 'org': 'abc-0123', 'repo': 'def.xyz_123'}, + '## ::github-release-changelog djpugh/mkdocs_licenseinfo': {'heading': '## ', 'org': 'djpugh', 'repo': 'mkdocs_licenseinfo'} } for test_string, expected in test_map.items(): with self.subTest(test=test_string): From e62db7d24db658591f1583d5754afb71d300fc40 Mon Sep 17 00:00:00 2001 From: David J Pugh Date: Wed, 27 Dec 2023 11:22:52 +0000 Subject: [PATCH 2/3] Fixing python 3.8 issue --- src/mkdocs_github_changelog/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mkdocs_github_changelog/__init__.py b/src/mkdocs_github_changelog/__init__.py index df81ece..aeafcb4 100644 --- a/src/mkdocs_github_changelog/__init__.py +++ b/src/mkdocs_github_changelog/__init__.py @@ -2,6 +2,8 @@ mkdocs extension to autogenerate changelog from github. """ +from __future__ import annotations + import logging from typing import Any, MutableMapping From 7bc586cd234dd2114d271b945692d676e099f0cc Mon Sep 17 00:00:00 2001 From: David J Pugh Date: Wed, 27 Dec 2023 11:26:19 +0000 Subject: [PATCH 3/3] Fixing lint --- pyproject.toml | 1 - tests/unit/test_get_releases.py | 1 - 2 files changed, 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0c88592..d6ca90f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -197,7 +197,6 @@ profile = "appnexus" src_paths = ['src/'] known_first_party = [ "mkdocs_github_changelog.*", - "mkdocs_github_changelog*" ] known_application = 'mkdocs_github_changelog*' force_alphabetical_sort_within_sections = true diff --git a/tests/unit/test_get_releases.py b/tests/unit/test_get_releases.py index 3f9468c..3bfc36c 100644 --- a/tests/unit/test_get_releases.py +++ b/tests/unit/test_get_releases.py @@ -9,7 +9,6 @@ from nskit.common.contextmanagers import Env, TestExtension from mkdocs_github_changelog import get_releases - from mkdocs_github_changelog.get_releases import ( _EnvironmentFactory, autoprocess_github_links,