-
-
Notifications
You must be signed in to change notification settings - Fork 281
feat(bump): added support for running arbitrary hooks during bump #644
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 3 commits into
commitizen-tools:master
from
skoef:add-hooks-for-bump-command
Feb 8, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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,34 @@ | ||
from __future__ import annotations | ||
|
||
from commitizen import cmd, out | ||
from commitizen.exceptions import RunHookError | ||
|
||
|
||
def run(hooks, _env_prefix="CZ_", **env): | ||
if isinstance(hooks, str): | ||
hooks = [hooks] | ||
|
||
for hook in hooks: | ||
out.info(f"Running hook '{hook}'") | ||
|
||
c = cmd.run(hook, env=_format_env(_env_prefix, env)) | ||
|
||
if c.out: | ||
out.write(c.out) | ||
if c.err: | ||
out.error(c.err) | ||
|
||
if c.return_code != 0: | ||
raise RunHookError(f"Running hook '{hook}' failed") | ||
|
||
|
||
def _format_env(prefix: str, env: dict[str, str]) -> dict[str, str]: | ||
"""_format_env() prefixes all given environment variables with the given | ||
prefix so it can be passed directly to cmd.run().""" | ||
penv = dict() | ||
for name, value in env.items(): | ||
name = prefix + name.upper() | ||
value = str(value) if value is not None else "" | ||
penv[name] = value | ||
|
||
return penv |
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,38 @@ | ||
from unittest.mock import call | ||
|
||
import pytest | ||
from pytest_mock import MockFixture | ||
|
||
from commitizen import cmd, hooks | ||
from commitizen.exceptions import RunHookError | ||
|
||
|
||
def test_run(mocker: MockFixture): | ||
bump_hooks = ["pre_bump_hook", "pre_bump_hook_1"] | ||
|
||
cmd_run_mock = mocker.Mock() | ||
cmd_run_mock.return_value.return_code = 0 | ||
mocker.patch.object(cmd, "run", cmd_run_mock) | ||
|
||
hooks.run(bump_hooks) | ||
|
||
cmd_run_mock.assert_has_calls( | ||
[call("pre_bump_hook", env={}), call("pre_bump_hook_1", env={})] | ||
) | ||
|
||
|
||
def test_run_error(mocker: MockFixture): | ||
bump_hooks = ["pre_bump_hook", "pre_bump_hook_1"] | ||
|
||
cmd_run_mock = mocker.Mock() | ||
cmd_run_mock.return_value.return_code = 1 | ||
mocker.patch.object(cmd, "run", cmd_run_mock) | ||
|
||
with pytest.raises(RunHookError): | ||
hooks.run(bump_hooks) | ||
|
||
|
||
def test_format_env(): | ||
result = hooks._format_env("TEST_", {"foo": "bar", "bar": "baz"}) | ||
assert "TEST_FOO" in result and result["TEST_FOO"] == "bar" | ||
assert "TEST_BAR" in result and result["TEST_BAR"] == "baz" |
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
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.
Uh oh!
There was an error while loading. Please reload this page.