-
-
Notifications
You must be signed in to change notification settings - Fork 295
Feature: version providers #646
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
Lee-W
merged 6 commits into
commitizen-tools:v3
from
noirbizarre:feature/version-provider
Feb 12, 2023
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2de2ea4
feat(providers): add a `commitizen.provider` endpoint for alternative…
noirbizarre 3bf2021
feat(providers): add support for some TOML-based versions (PEP621, Po…
noirbizarre 4a5fda3
feat(providers): add support for some JSON-based version providers (N…
noirbizarre ec5caf0
docs(deps): upgrade mkdocs-material and tidy some snippets
noirbizarre 2db5c75
feat(providers): add a `scm` version provider
noirbizarre 5672eaf
test(providers): factorize some version providers tests
noirbizarre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat(providers): add a
commitizen.provider
endpoint for alternative…
… versions providers
- Loading branch information
commit 2de2ea4b4f4ef892fe773fdc5e3343aba31e7921
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import cast | ||
|
||
import importlib_metadata as metadata | ||
|
||
from commitizen.config.base_config import BaseConfig | ||
from commitizen.exceptions import VersionProviderUnknown | ||
|
||
PROVIDER_ENTRYPOINT = "commitizen.provider" | ||
DEFAULT_PROVIDER = "commitizen" | ||
|
||
|
||
class VersionProvider(ABC): | ||
""" | ||
Abstract base class for version providers. | ||
|
||
Each version provider should inherit and implement this class. | ||
""" | ||
|
||
config: BaseConfig | ||
|
||
def __init__(self, config: BaseConfig): | ||
self.config = config | ||
|
||
@abstractmethod | ||
def get_version(self) -> str: | ||
""" | ||
Get the current version | ||
""" | ||
... | ||
|
||
@abstractmethod | ||
def set_version(self, version: str): | ||
""" | ||
Set the new current version | ||
""" | ||
... | ||
|
||
|
||
class CommitizenProvider(VersionProvider): | ||
""" | ||
Default version provider: Fetch and set version in commitizen config. | ||
""" | ||
|
||
def get_version(self) -> str: | ||
return self.config.settings["version"] # type: ignore | ||
|
||
def set_version(self, version: str): | ||
self.config.set_key("version", version) | ||
|
||
|
||
def get_provider(config: BaseConfig) -> VersionProvider: | ||
""" | ||
Get the version provider as defined in the configuration | ||
|
||
:raises VersionProviderUnknown: if the provider named by `version_provider` is not found. | ||
""" | ||
provider_name = config.settings["version_provider"] or DEFAULT_PROVIDER | ||
try: | ||
(ep,) = metadata.entry_points(name=provider_name, group=PROVIDER_ENTRYPOINT) | ||
except ValueError: | ||
raise VersionProviderUnknown(f'Version Provider "{provider_name}" unknown.') | ||
provider_cls = ep.load() | ||
return cast(VersionProvider, provider_cls(config)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
|
||
from commitizen.config.base_config import BaseConfig | ||
from commitizen.exceptions import VersionProviderUnknown | ||
from commitizen.providers import CommitizenProvider, get_provider | ||
|
||
if TYPE_CHECKING: | ||
from pytest_mock import MockerFixture | ||
|
||
|
||
def test_default_version_provider_is_commitizen_config(config: BaseConfig): | ||
provider = get_provider(config) | ||
|
||
assert isinstance(provider, CommitizenProvider) | ||
|
||
|
||
def test_raise_for_unknown_provider(config: BaseConfig): | ||
config.settings["version_provider"] = "unknown" | ||
with pytest.raises(VersionProviderUnknown): | ||
get_provider(config) | ||
|
||
|
||
def test_commitizen_provider(config: BaseConfig, mocker: MockerFixture): | ||
noirbizarre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
config.settings["version"] = "42" | ||
mock = mocker.patch.object(config, "set_key") | ||
|
||
provider = CommitizenProvider(config) | ||
assert provider.get_version() == "42" | ||
|
||
provider.set_version("43.1") | ||
mock.assert_called_once_with("version", "43.1") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of using protocols?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this precise case, I prefer
ABC
overProtocol
because:config
in the method signatures, but this makes more verbose code without benefits in my opinion)ABC
force to implement methods and raise an explicit error if not, which I think is easier to understand (so more discoverable for them, less support for you)But if you prefer
Protocol
overABC
I can switch 👍🏼There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@woile What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer
Protocol
🤣 but usingABC
is fine as well. Point 2 is the key, withProtocols
people would have to usemypy
to catch the problems, I like the explicit error.