Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Feat/add pypi url filter #1160

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

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
6 changes: 1 addition & 5 deletions 6 config/release-templates/.release_notes.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@
{{
"- %s" | format(
format_link(
[
"https://pypi.org/project",
repo_name,
release.version | string,
] | join("/"),
repo_name | create_pypi_url(release.version | string),
"PyPi Registry",
)
)
Expand Down
21 changes: 21 additions & 0 deletions 21 docs/changelog_templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,27 @@ The filters provided vary based on the VCS configured and available features:
)
}}

* ``create_pypi_url(package_name: str, version: str = "")``: given a package name and an optional
version, return a URL to the PyPI page for the package. If a version is provided, the URL will
point to the specific version page. If no version is provided, the URL will point to the package
page.

*Introduced in ${NEW_RELEASE_TAG}.*

**Example Usage:**

.. code:: jinja

{{ "example-package" | create_pypi_url }}
{{ "example-package" | create_pypi_url("1.0.0") }}

**Markdown Output:**

.. code:: markdown

https://pypi.org/project/example-package
https://pypi.org/project/example-package/1.0.0

* ``create_server_url (Callable[[PathStr, AuthStr | None, QueryStr | None, FragmentStr | None], UrlStr])``:
when given a path, prepend the configured vcs server host and url scheme. Optionally you
can provide, a auth string, a query string or a url fragment to be normalized into the
Expand Down
17 changes: 16 additions & 1 deletion 17 src/semantic_release/changelog/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import os
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from pathlib import Path, PurePosixPath
from re import compile as regexp
from typing import TYPE_CHECKING, Any, Callable, Literal

from urllib3.util import Url

from semantic_release.const import PYPI_WEB_DOMAIN
from semantic_release.helpers import sort_numerically

if TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -86,6 +89,7 @@ def make_changelog_context(
hvcs_type=hvcs_client.__class__.__name__.lower(),
filters=(
*hvcs_client.get_changelog_context_filters(),
create_pypi_url,
read_file,
convert_md_to_rst,
autofit_text_width,
Expand All @@ -94,6 +98,17 @@ def make_changelog_context(
)


def create_pypi_url(package_name: str, version: str = "") -> str:
project_name = package_name.strip("/").strip()
if not project_name:
raise ValueError("package_name must not be empty!")
return Url(
scheme="https",
host=PYPI_WEB_DOMAIN,
path=str(PurePosixPath("project", project_name, version.strip("/").strip())),
).url.rstrip("/")


def read_file(filepath: str) -> str:
try:
if not filepath:
Expand Down
2 changes: 2 additions & 0 deletions 2 src/semantic_release/cli/changelog_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from semantic_release.changelog.context import (
ReleaseNotesContext,
autofit_text_width,
create_pypi_url,
make_changelog_context,
)
from semantic_release.changelog.template import environment, recursive_render
Expand Down Expand Up @@ -257,6 +258,7 @@ def generate_release_notes(
mask_initial_release=mask_initial_release,
filters=(
*hvcs_client.get_changelog_context_filters(),
create_pypi_url,
autofit_text_width,
sort_numerically,
),
Expand Down
2 changes: 2 additions & 0 deletions 2 src/semantic_release/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import re

PYPI_WEB_DOMAIN = "pypi.org"

# https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
SEMVER_REGEX = re.compile(
r"""
Expand Down
74 changes: 74 additions & 0 deletions 74 tests/unit/semantic_release/changelog/test_changelog_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,77 @@ def test_changelog_context_sort_numerically_reverse(

# Evaluate
assert expected_changelog == actual_changelog


def test_changelog_context_pypi_url_filter(
example_git_https_url: str,
artificial_release_history: ReleaseHistory,
changelog_md_file: Path,
):
changelog_tpl = dedent(
"""\
{{ "example-package" | create_pypi_url }}
"""
)

expected_changelog = dedent(
"""\
https://pypi.org/project/example-package
"""
)

env = environment(trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True)
context = make_changelog_context(
hvcs_client=Gitlab(example_git_https_url),
release_history=artificial_release_history,
mode=ChangelogMode.UPDATE,
prev_changelog_file=changelog_md_file,
insertion_flag="",
mask_initial_release=False,
)
context.bind_to_environment(env)

# Create changelog from template with environment
actual_changelog = env.from_string(changelog_tpl).render()

# Evaluate
assert expected_changelog == actual_changelog


def test_changelog_context_pypi_url_filter_tagged(
example_git_https_url: str,
artificial_release_history: ReleaseHistory,
changelog_md_file: Path,
):
version = "1.0.0"
changelog_tpl = dedent(
"""\
{% set release = context.history.released.values() | first
%}{{
"example-package" | create_pypi_url(release.version | string)
}}
"""
)

expected_changelog = dedent(
f"""\
https://pypi.org/project/example-package/{version}
"""
)

env = environment(trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True)
context = make_changelog_context(
hvcs_client=Gitlab(example_git_https_url),
release_history=artificial_release_history,
mode=ChangelogMode.UPDATE,
prev_changelog_file=changelog_md_file,
insertion_flag="",
mask_initial_release=False,
)
context.bind_to_environment(env)

# Create changelog from template with environment
actual_changelog = env.from_string(changelog_tpl).render()

# Evaluate
assert expected_changelog == actual_changelog
54 changes: 54 additions & 0 deletions 54 tests/unit/semantic_release/changelog/test_release_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,57 @@ def test_release_notes_context_sort_numerically_filter_reversed(
)

assert expected_content == actual_content


def test_release_notes_context_pypi_url_filter(
example_git_https_url: str,
single_release_history: ReleaseHistory,
example_project_dir: ExProjectDir,
change_to_ex_proj_dir: None,
):
version = list(single_release_history.released.keys())[-1]
release = single_release_history.released[version]

example_project_dir.joinpath(".release_notes.md.j2").write_text(
"""{{ "example-package" | create_pypi_url }}"""
)

expected_content = f"https://pypi.org/project/example-package{os.linesep}"

actual_content = generate_release_notes(
hvcs_client=Github(remote_url=example_git_https_url),
release=release,
template_dir=example_project_dir,
history=single_release_history,
style="angular",
mask_initial_release=False,
)

assert expected_content == actual_content


def test_release_notes_context_pypi_url_filter_tagged(
example_git_https_url: str,
single_release_history: ReleaseHistory,
example_project_dir: ExProjectDir,
change_to_ex_proj_dir: None,
):
version = list(single_release_history.released.keys())[-1]
release = single_release_history.released[version]

example_project_dir.joinpath(".release_notes.md.j2").write_text(
"""{{ "example-package" | create_pypi_url(release.version | string) }}"""
)

expected_content = f"https://pypi.org/project/example-package/{version}{os.linesep}"

actual_content = generate_release_notes(
hvcs_client=Github(remote_url=example_git_https_url),
release=release,
template_dir=example_project_dir,
history=single_release_history,
style="angular",
mask_initial_release=False,
)

assert expected_content == actual_content
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.