Skip to content

Commit

Permalink
feat(history): support linking compare page in changelog
Browse files Browse the repository at this point in the history
Fixes #218
  • Loading branch information
danth committed May 8, 2020
1 parent 7a85403 commit 79a8e02
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
5 changes: 4 additions & 1 deletion semantic_release/cli.py
Expand Up @@ -248,7 +248,10 @@ def publish(**kwargs):
owner,
name,
new_version,
markdown_changelog(new_version, log, header=False),
markdown_changelog(
new_version, log, header=False,
previous_version=current_version
),
)
except GitError:
logger.error("Posting changelog failed")
Expand Down
1 change: 1 addition & 0 deletions semantic_release/defaults.cfg
Expand Up @@ -15,3 +15,4 @@ remove_dist=true
build_command=python setup.py sdist bdist_wheel
branch=master
changelog_sections=feature,fix,breaking,documentation,performance
compare_link=false
29 changes: 27 additions & 2 deletions semantic_release/history/logs.py
Expand Up @@ -6,7 +6,7 @@
from ..errors import UnknownCommitMessageStyleError
from ..helpers import LoggedFunction
from ..settings import config, current_commit_parser
from ..vcs_helpers import get_commit_log
from ..vcs_helpers import get_commit_log, get_repository_owner_and_name
from .parser_helpers import re_breaking

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -160,12 +160,29 @@ def generate_changelog(from_version: str, to_version: str = None) -> dict:
return changes


def get_github_compare_url(from_version: str, to_version: str) -> str:
"""
Get the GitHub comparison link between two version tags.
:param from_version: The older version to compare.
:param to_version: The newer version to compare.
:return: Link to view a comparison between the two versions.
"""
owner, name = get_repository_owner_and_name()
return (
f"https://github.com/{owner}/{name}"
f"/compare/v{from_version}...v{to_version}"
)


@LoggedFunction(logger)
def markdown_changelog(version: str, changelog: dict, header: bool = False) -> str:
def markdown_changelog(version: str, changelog: dict, header: bool = False, previous_version: str = None) -> str:
"""
Generate a markdown version of the changelog.
:param version: A string with the version number.
:param previous_version: A string with the last version number, to
use for the comparison URL. If omitted, the URL will not be included.
:param changelog: A parsed changelog dict from generate_changelog.
:param header: A boolean that decides whether a version number header should be included.
:return: The markdown formatted changelog.
Expand All @@ -175,6 +192,14 @@ def markdown_changelog(version: str, changelog: dict, header: bool = False) -> s
# Add a heading with the version number
output += "## v{0}\n".format(version)

if (
config.getboolean("semantic_release", "compare_link")
and config.get("semantic_release", "hvcs").lower() == 'github'
and previous_version
):
compare_url = get_github_compare_url(previous_version, version)
output += f"**[See all commits in this version]({compare_url})**\n"

# Sections which will be shown in the Markdown changelog.
# This is NOT related to supported commit types.
changelog_sections = config.get("semantic_release", "changelog_sections")
Expand Down
43 changes: 42 additions & 1 deletion tests/history/test_markdown_changelog.py
@@ -1,4 +1,17 @@
from semantic_release.history.logs import markdown_changelog
import mock

from semantic_release.history.logs import markdown_changelog, get_github_compare_url


def test_github_compare_url():
with mock.patch(
'semantic_release.history.logs.get_repository_owner_and_name',
return_value=['owner', 'name']
):
assert (
get_github_compare_url('1.0.0', '2.0.0') ==
"https://github.com/owner/name/compare/v1.0.0...v2.0.0"
)


def test_should_output_all_sections():
Expand Down Expand Up @@ -78,3 +91,31 @@ def test_should_output_heading():
},
header=True,
)


def test_compare_url(mocker):
mocker.patch(
'semantic_release.history.logs.config.getboolean',
return_value=True
)
mocker.patch(
'semantic_release.history.logs.get_repository_owner_and_name',
return_value=['owner', 'name']
)

assert (
"**[See all commits in this version]"
"(https://github.com/owner/name/compare/v1.0.0...v2.0.0)**\n"
in markdown_changelog(
"2.0.0",
{
"refactor": [],
"breaking": [],
"feature": [],
"fix": [],
"documentation": [],
"performance": [],
},
previous_version="1.0.0",
)
)
5 changes: 4 additions & 1 deletion tests/test_cli.py
Expand Up @@ -514,7 +514,10 @@ def test_publish_giterror_when_posting(mocker):
)
mock_check_token.assert_called_once_with()
mock_generate.assert_called_once_with("current", "new")
mock_markdown.assert_called_once_with("new", "super changelog", header=False)
mock_markdown.assert_called_once_with(
"new", "super changelog",
header=False, previous_version='current'
)
mock_post.assert_called_once_with("owner", "name", "new", "super md changelog")


Expand Down

0 comments on commit 79a8e02

Please sign in to comment.