Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding logging #6

Merged
merged 4 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,9 @@ import-order-style="appnexus"
profile = "appnexus"
src_paths = ['src/']
known_first_party = [
"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
33 changes: 33 additions & 0 deletions src/mkdocs_github_changelog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

mkdocs extension to autogenerate changelog from github.
"""
from __future__ import annotations

import logging
from typing import Any, MutableMapping


def __get_version() -> str:
Expand Down Expand Up @@ -45,3 +49,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"))
16 changes: 14 additions & 2 deletions src/mkdocs_github_changelog/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -42,7 +43,7 @@
class GithubReleaseChangelogProcessor(BlockProcessor):
"""Changelog Markdown block processor."""

regex = re.compile(r"^(?P<heading>#{1,6} *|)::github-release-changelog ?(?P<org>[a-zA-Z0-9-]+?)\/(?P<repo>.+?) *$", flags=re.MULTILINE)
regex = re.compile(r"^(?P<heading>#{1,6} *|)::github-release-changelog ?(?P<org>[a-zA-Z\d-]+?)\/(?P<repo>.+?) *$", flags=re.MULTILINE)

def __init__(
self,
Expand All @@ -55,14 +56,21 @@

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")

Check warning on line 62 in src/mkdocs_github_changelog/extension.py

View check run for this annotation

Codecov / codecov/patch

src/mkdocs_github_changelog/extension.py#L62

Added line #L62 was not covered by tests
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
Expand All @@ -77,9 +85,11 @@

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(
Expand All @@ -99,6 +109,8 @@
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,
Expand Down
5 changes: 5 additions & 0 deletions src/mkdocs_github_changelog/get_releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"


Expand Down Expand Up @@ -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]
1 change: 0 additions & 1 deletion tests/unit/test_get_releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from nskit.common.contextmanagers import Env

from mkdocs_github_changelog import extension

from mkdocs_github_changelog.extension import GithubReleaseChangelogProcessor


Expand All @@ -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):
Expand Down