From 14028bcef2964e9f1fc3cef689148955c529b475 Mon Sep 17 00:00:00 2001 From: Jerry Ng Date: Fri, 6 May 2022 13:12:09 +0800 Subject: [PATCH 1/3] fix: update get_commit_log to return commit author info --- semantic_release/vcs_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semantic_release/vcs_helpers.py b/semantic_release/vcs_helpers.py index 9a6a72469..b8ca67fae 100644 --- a/semantic_release/vcs_helpers.py +++ b/semantic_release/vcs_helpers.py @@ -58,7 +58,7 @@ def get_commit_log(from_rev=None): ) for commit in repo.iter_commits(rev): - yield (commit.hexsha, commit.message.replace("\r\n", "\n")) + yield (commit.hexsha, commit.message.replace("\r\n", "\n"), commit.author) @check_repo From 167c3cee6679b45f3728bbf3b2ad470742576bd3 Mon Sep 17 00:00:00 2001 From: Jerry Ng Date: Fri, 6 May 2022 13:12:14 +0800 Subject: [PATCH 2/3] fix: add commit author check to get_current_release_version_by_commits --- semantic_release/history/__init__.py | 8 ++++---- semantic_release/history/logs.py | 4 ++-- tests/history/__init__.py | 18 ++++++++++++------ tests/history/test_changelog.py | 13 +++++++------ tests/history/test_version.py | 28 ++++++++++++++++++++-------- tests/history/test_version_bump.py | 2 +- 6 files changed, 46 insertions(+), 27 deletions(-) diff --git a/semantic_release/history/__init__.py b/semantic_release/history/__init__.py index 7e0d617f0..c5f9d540c 100644 --- a/semantic_release/history/__init__.py +++ b/semantic_release/history/__init__.py @@ -324,7 +324,7 @@ def get_previous_version(version: str) -> Optional[str]: :return: A string with the previous version number. """ found_version = False - for commit_hash, commit_message in get_commit_log(): + for commit_hash, commit_message, _ in get_commit_log(): logger.debug(f"Checking commit {commit_hash}") if version in commit_message: found_version = True @@ -349,7 +349,7 @@ def get_previous_release_version(version: str) -> Optional[str]: :return: A string with the previous version number. """ found_version = False - for commit_hash, commit_message in get_commit_log(): + for commit_hash, commit_message, _ in get_commit_log(): logger.debug(f"Checking commit {commit_hash}") if version in commit_message: found_version = True @@ -372,10 +372,10 @@ def get_current_release_version_by_commits() -> str: :return: A string with the current version number. """ - for commit_hash, commit_message in get_commit_log(): + for commit_hash, commit_message, commit_author in get_commit_log(): logger.debug(f"Checking commit {commit_hash}") match = re.search(rf"{release_version_pattern}", commit_message) - if match: + if match and commit_author.name == "github-actions" and commit_author.email == "action@github.com": logger.debug(f"Version matches regex {commit_message}") return match.group(1).strip() diff --git a/semantic_release/history/logs.py b/semantic_release/history/logs.py index e4d274847..7e3c29dbe 100644 --- a/semantic_release/history/logs.py +++ b/semantic_release/history/logs.py @@ -36,7 +36,7 @@ def evaluate_version_bump(current_version: str, force: str = None) -> Optional[s changes = [] commit_count = 0 - for _hash, commit_message in get_commit_log(current_version): + for _hash, commit_message, _ in get_commit_log(current_version): if commit_message.startswith(current_version): # Stop once we reach the current version # (we are looping in the order of newest -> oldest) @@ -98,7 +98,7 @@ def generate_changelog(from_version: str, to_version: str = None) -> dict: rev = from_version found_the_release = to_version is None - for _hash, commit_message in get_commit_log(rev): + for _hash, commit_message, _ in get_commit_log(rev): if not found_the_release: # Skip until we find the last commit in this release # (we are looping in the order of newest -> oldest) diff --git a/tests/history/__init__.py b/tests/history/__init__.py index 43e02d9d7..2583377b0 100644 --- a/tests/history/__init__.py +++ b/tests/history/__init__.py @@ -2,16 +2,19 @@ "221", "feat(x): Add super-feature\n\n" "BREAKING CHANGE: Uses super-feature as default instead of dull-feature.", + "Alice ", ) MAJOR2 = ( "222", "feat(x): Add super-feature\n\nSome explanation\n\n" "BREAKING CHANGE: Uses super-feature as default instead of dull-feature.", + "Alice ", ) MAJOR_MENTIONING_1_0_0 = ( "223", "feat(x): Add super-feature\n\nSome explanation\n\n" "BREAKING CHANGE: Uses super-feature as default instead of dull-feature from v1.0.0.", + "Alice ", ) MAJOR_MULTIPLE_FOOTERS = ( "244", @@ -19,23 +22,26 @@ "BREAKING CHANGE: Breaking change 1\n\n" "Not a BREAKING CHANGE\n\n" "BREAKING CHANGE: Breaking change 2", + "Alice ", ) MAJOR_EXCL_WITH_FOOTER = ( "231", "feat(x)!: Add another feature\n\n" "BREAKING CHANGE: Another feature, another breaking change", + "Alice ", ) MAJOR_EXCL_NOT_FOOTER = ( "232", "fix!: Fix a big bug that everyone exploited\n\nThis is the reason you should not exploit bugs", + "Alice ", ) -MINOR = ("111", "feat(x): Add non-breaking super-feature") -PATCH = ("24", "fix(x): Fix bug in super-feature") -NO_TAG = ("191", "docs(x): Add documentation for super-feature") -UNKNOWN_STYLE = ("7", "random commits are the worst") +MINOR = ("111", "feat(x): Add non-breaking super-feature", "Bob ") +PATCH = ("24", "fix(x): Fix bug in super-feature", "Bob ") +NO_TAG = ("191", "docs(x): Add documentation for super-feature", "John ") +UNKNOWN_STYLE = ("7", "random commits are the worst", "John ") ALL_KINDS_OF_COMMIT_MESSAGES = [MINOR, MAJOR, MINOR, PATCH] MINOR_AND_PATCH_COMMIT_MESSAGES = [MINOR, PATCH] PATCH_COMMIT_MESSAGES = [PATCH, PATCH] -MAJOR_LAST_RELEASE_MINOR_AFTER = [MINOR, ("22", "1.1.0"), MAJOR] -MAJOR_MENTIONING_LAST_VERSION = [MAJOR_MENTIONING_1_0_0, ("22", "1.0.0"), MAJOR] +MAJOR_LAST_RELEASE_MINOR_AFTER = [MINOR, ("22", "1.1.0", "Doe "), MAJOR] +MAJOR_MENTIONING_LAST_VERSION = [MAJOR_MENTIONING_1_0_0, ("22", "1.0.0", "Doe "), MAJOR] diff --git a/tests/history/test_changelog.py b/tests/history/test_changelog.py index b2b3bd534..a7586bde0 100644 --- a/tests/history/test_changelog.py +++ b/tests/history/test_changelog.py @@ -94,7 +94,7 @@ def test_scope_is_included_in_changelog(commit, commit_type, expected): @mock.patch( "semantic_release.history.logs.get_commit_log", - lambda *a, **k: [("24", "fix: Fix another bug")], + lambda *a, **k: [("24", "fix: Fix another bug", "Alice ")], ) def test_scope_is_omitted_with_empty_scope(): changelog = generate_changelog("0.0.0") @@ -123,15 +123,16 @@ def test_scope_included_in_changelog_configurable(commit, commit_type): @mock.patch( "semantic_release.history.logs.get_commit_log", - lambda *a, **k: [("23", "fix(x): abCD"), ("23", "fix: abCD")], + lambda *a, **k: [("23", "fix(x): abCD", "Alice "), + ("23", "fix: abCD", "Alice ")], ) @pytest.mark.parametrize( "commit,config_setting,expected_description", [ - (("23", "fix(x): abCD"), True, "**x:** AbCD"), - (("23", "fix(x): abCD"), False, "**x:** abCD"), - (("23", "fix: abCD"), True, "AbCD"), - (("23", "fix: abCD"), False, "abCD"), + (("23", "fix(x): abCD", "Alice "), True, "**x:** AbCD"), + (("23", "fix(x): abCD", "Alice "), False, "**x:** abCD"), + (("23", "fix: abCD", "Alice "), True, "AbCD"), + (("23", "fix: abCD", "Alice "), False, "abCD"), ], ) def test_message_capitalization_is_configurable( diff --git a/tests/history/test_version.py b/tests/history/test_version.py index 5bffa36cb..4bd23993d 100644 --- a/tests/history/test_version.py +++ b/tests/history/test_version.py @@ -3,6 +3,7 @@ import mock import pytest +from git.util import Actor import semantic_release from semantic_release.history import ( @@ -92,28 +93,33 @@ def test_current_release_version_should_return_default_version( class TestGetPreviousVersion: @mock.patch( "semantic_release.history.get_commit_log", - lambda: [("211", "0.10.0"), ("13", "0.9.0")], + lambda: [("211", "0.10.0", Actor("github-actions", "action@github.com")), + ("13", "0.9.0", Actor("github-actions", "action@github.com"))], ) def test_should_return_correct_version(self): assert get_previous_version("0.10.0") == "0.9.0" @mock.patch( "semantic_release.history.get_commit_log", - lambda: [("211", "v0.10.0"), ("13", "v0.9.0")], + lambda: [("211", "v0.10.0", Actor("github-actions", "action@github.com")), + ("13", "v0.9.0", Actor("github-actions", "action@github.com"))], ) def test_should_return_correct_version_with_v(self): assert get_previous_version("0.10.0") == "0.9.0" @mock.patch( "semantic_release.history.get_commit_log", - lambda: [("211", "0.10.0-beta"), ("13", "0.9.0")], + lambda: [("211", "0.10.0-beta", Actor("github-actions", "action@github.com")), + ("13", "0.9.0", Actor("github-actions", "action@github.com"))], ) def test_should_return_correct_version_from_prerelease(self): assert get_previous_version("0.10.0-beta") == "0.9.0" @mock.patch( "semantic_release.history.get_commit_log", - lambda: [("211", "0.10.0"), ("13", "0.10.0-beta"), ("13", "0.9.0")], + lambda: [("211", "0.10.0", Actor("github-actions", "action@github.com")), + ("13", "0.10.0-beta", Actor("github-actions", "action@github.com")), + ("13", "0.9.0", Actor("github-actions", "action@github.com"))], ) def test_should_return_correct_version_skip_prerelease(self): assert get_previous_version("0.10.0-beta") == "0.9.0" @@ -122,28 +128,34 @@ def test_should_return_correct_version_skip_prerelease(self): class TestGetCurrentReleaseVersionByCommits: @mock.patch( "semantic_release.history.get_commit_log", - lambda: [("211", "0.10.0-beta.1"), ("13", "0.9.1-beta.1"), ("13", "0.9.0")], + lambda: [("211", "0.10.0-beta.1", Actor("github-actions", "action@github.com")), + ("13", "0.9.1-beta.1", Actor("github-actions", "action@github.com")), + ("13", "0.9.0", Actor("github-actions", "action@github.com"))], ) def test_should_return_correct_version(self): assert get_current_release_version_by_commits() == "0.9.0" @mock.patch( "semantic_release.history.get_commit_log", - lambda: [("211", "v0.10.0-beta.1"), ("13", "0.9.1-beta.1"), ("13", "v0.9.0")], + lambda: [("211", "v0.10.0-beta.1", Actor("github-actions", "action@github.com")), + ("13", "0.9.1-beta.1", Actor("github-actions", "action@github.com")), + ("13", "v0.9.0", Actor("github-actions", "action@github.com"))], ) def test_should_return_correct_version_with_v(self): assert get_current_release_version_by_commits() == "0.9.0" @mock.patch( "semantic_release.history.get_commit_log", - lambda: [("211", "0.10.0-beta.0"), ("13", "0.9.0")], + lambda: [("211", "0.10.0-beta.0", Actor("github-actions", "action@github.com")), + ("13", "0.9.0", Actor("github-actions", "action@github.com"))], ) def test_should_return_correct_version_from_prerelease(self): assert get_current_release_version_by_commits() == "0.9.0" @mock.patch( "semantic_release.history.get_commit_log", - lambda: [("211", "7.28.0"), ("13", "7.27.0")], + lambda: [("211", "7.28.0", Actor("github-actions", "action@github.com")), + ("13", "7.27.0", Actor("github-actions", "action@github.com"))], ) def test_should_return_correct_version_without_prerelease(self): assert get_current_release_version_by_commits() == "7.28.0" diff --git a/tests/history/test_version_bump.py b/tests/history/test_version_bump.py index 737643dc0..92d9b2c8b 100644 --- a/tests/history/test_version_bump.py +++ b/tests/history/test_version_bump.py @@ -33,7 +33,7 @@ def test_patch(): def test_nothing_if_no_tag(): with mock.patch( "semantic_release.history.logs.get_commit_log", - lambda *a, **kw: [("", "...")], + lambda *a, **kw: [("", "...", "")], ): assert evaluate_version_bump("0.0.0") is None From c7e444e44d4d27ac80f49698b7aee3e6cef29850 Mon Sep 17 00:00:00 2001 From: Jerry Ng Date: Sun, 8 May 2022 10:29:49 +0800 Subject: [PATCH 3/3] fix: skip mypy check for error in hvcs --- semantic_release/hvcs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/semantic_release/hvcs.py b/semantic_release/hvcs.py index 798d60250..7428260d4 100644 --- a/semantic_release/hvcs.py +++ b/semantic_release/hvcs.py @@ -3,7 +3,7 @@ import logging import mimetypes import os -from typing import Any, Optional, Union, cast +from typing import Any, Optional, Union from urllib.parse import urlsplit from gitlab import exceptions, gitlab @@ -578,7 +578,7 @@ def upload_asset( url, params={"name": name}, data={}, - files=[ + files=[ # type: ignore ( "attachment", (