diff --git a/commitizen/cli.py b/commitizen/cli.py index e3af3a8073..9d2e52509a 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -116,8 +116,34 @@ }, { "name": ["version"], - "help": "get the version of the installed commitizen", + "help": ( + "get the version of the installed commitizen or the current project" + " (default: installed commitizen)" + ), "func": commands.Version, + "arguments": [ + { + "name": ["-p", "--project"], + "help": "get the version of the current project", + "action": "store_true", + "exclusive_group": "group1", + }, + { + "name": ["-c", "--commitizen"], + "help": "get the version of the installed commitizen", + "action": "store_true", + "exclusive_group": "group1", + }, + { + "name": ["-v", "--verbose"], + "help": ( + "get the version of both the installed commitizen " + "and the current project" + ), + "action": "store_true", + "exclusive_group": "group1", + }, + ], }, { "name": ["check"], diff --git a/commitizen/commands/version.py b/commitizen/commands/version.py index 6375347f5e..d64d8d82af 100644 --- a/commitizen/commands/version.py +++ b/commitizen/commands/version.py @@ -4,10 +4,26 @@ class Version: - """Get the version of the installed commitizen.""" + """Get the version of the installed commitizen or the current project.""" def __init__(self, config: BaseConfig, *args): self.config: BaseConfig = config + self.parameter = args[0] def __call__(self): - out.write(__version__) + if self.parameter.get("project"): + version = self.config.settings["version"] + if version: + out.write(f"{version}") + else: + out.error(f"No project information in this project.") + elif self.parameter.get("verbose"): + out.write(f"Installed Commitizen Version: {__version__}") + version = self.config.settings["version"] + if version: + out.write(f"Project Version: {version}") + else: + out.error(f"No project information in this project.") + else: + # if no argument is given, show installed commitizen version + out.write(f"{__version__}") diff --git a/docs/bump.md b/docs/bump.md index 10d86ba62e..5a222c18ce 100644 --- a/docs/bump.md +++ b/docs/bump.md @@ -101,7 +101,7 @@ tag_format = "v$minor.$major.$patch$prerelease" Or in your `.cz` (TO BE DEPRECATED) -``` +```ini [commitizen] tag_format = v$minor.$major.$patch$prerelease ``` @@ -144,7 +144,7 @@ version_files = [ `.cz` (TO BE DEPRECATED) -``` +```ini [commitizen] version_files = [ "src/__version__.py", @@ -180,7 +180,7 @@ bump_message = "release $current_version → $new_version [skip-ci]" `.cz` (TO BE DEPRECATED) -``` +```ini [commitizen] bump_message = release $current_version → $new_version [skip-ci] ``` diff --git a/docs/index.md b/docs/index.md index 6979a09bb5..8b20991081 100644 --- a/docs/index.md +++ b/docs/index.md @@ -96,6 +96,8 @@ commands: info show information about the cz schema show commit schema bump bump semantic version based on the git log + version get the version of the installed commitizen or the + current project (default: installed commitizen) check validates that a commit message matches the commitizen schema init init commitizen configuration ``` diff --git a/docs/tutorials/github_actions.md b/docs/tutorials/github_actions.md index b1b16dfb54..1a4eba292a 100644 --- a/docs/tutorials/github_actions.md +++ b/docs/tutorials/github_actions.md @@ -96,4 +96,4 @@ jobs: Notice that we are calling a bash script in `./scripts/publish`, you should configure it with your tools (twine, poetry, etc). Check [commitizen example](https://github.com/Woile/commitizen/blob/master/scripts/publish) -Push the changes and that's it. \ No newline at end of file +Push the changes and that's it. diff --git a/docs/tutorials/gitlab_ci.md b/docs/tutorials/gitlab_ci.md index c0d1d8d414..79fabf61a5 100644 --- a/docs/tutorials/gitlab_ci.md +++ b/docs/tutorials/gitlab_ci.md @@ -15,10 +15,10 @@ For this example, we have a `python/django` application and `Docker` as containe #### Gitlab Configuration: In order to be able to change files and push new changes with `Gitlab CI` runners, we need to have a `ssh` key and configure a git user. - + First, let's create a `ssh key`. The only requirement is to create it without a passphrase: -``` +```bash ssh-keygen -f deploy_key -N "" ``` @@ -32,7 +32,7 @@ Now, we need to create three environment variables that will be visible for the Create `SSH_PRIVATE_KEY`, `CI_EMAIL`, `CI_USERNAME` variables and fill them with the `private_key`, `email` and `username` that we have created previously. -The latest step is to create a `deploy key.` To do this, we should create it under the section `settings/repository` and fill it with the `public key` generated before. Check `Write access allowed`, otherwise, the runner won't be able to write the changes to the repository. +The latest step is to create a `deploy key.` To do this, we should create it under the section `settings/repository` and fill it with the `public key` generated before. Check `Write access allowed`, otherwise, the runner won't be able to write the changes to the repository. ![gitlab deploy key](../images/gitlab_ci/gitlab_deploy_key.png) @@ -45,9 +45,8 @@ tip: If the CI raise some errors, try to unprotect the private key. 1. Create a `.gitlab-ci.yaml` file that contains `stages` and `jobs` configurations. You can find more info [here](https://docs.gitlab.com/ee/ci/quick_start/). 2. Define `stages` and `jobs`. For this example, we define two `stages` with one `job` each one. - * Test the application. - * Auto bump the version. Means changing the file/s that reflects the version, creating a new commit and git tag. - + * Test the application. + * Auto bump the version. Means changing the file/s that reflects the version, creating a new commit and git tag. #### Stages and Jobs @@ -96,7 +95,7 @@ auto-bump: - git push origin master:$CI_COMMIT_REF_NAME - TAG=$(head -n 1 VERSION) # get the new software version and save into artifacts - echo "#!/bin/sh" >> variables - - echo "export TAG='$TAG'" >> variables + - echo "export TAG='$TAG'" >> variables - git push origin $TAG only: refs: diff --git a/tests/test_commands.py b/tests/test_commands.py index 983b8a0073..c88a2716ba 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -9,6 +9,7 @@ from commitizen import cmd, commands, defaults from commitizen.cz.exceptions import CzException from commitizen.config import BaseConfig +from commitizen.__version__ import __version__ @pytest.fixture() @@ -170,10 +171,44 @@ def test_list_cz(config): mocked_write.assert_called_once() -def test_version(config): - with mock.patch("commitizen.out.write") as mocked_write: - commands.Version(config)() - mocked_write.assert_called_once() +def test_version_for_showing_project_version(config, capsys): + # No version exist + commands.Version(config, {"project": True, "commitizen": False, "verbose": False})() + captured = capsys.readouterr() + assert "No project information in this project." in captured.err + + config.settings["version"] = "v0.0.1" + commands.Version(config, {"project": True, "commitizen": False, "verbose": False})() + captured = capsys.readouterr() + assert "v0.0.1" in captured.out + + +def test_version_for_showing_commitizen_version(config, capsys): + commands.Version(config, {"project": False, "commitizen": True, "verbose": False})() + captured = capsys.readouterr() + assert f"{__version__}" in captured.out + + # default showing commitizen version + commands.Version( + config, {"project": False, "commitizen": False, "verbose": False} + )() + captured = capsys.readouterr() + assert f"{__version__}" in captured.out + + +def test_version_for_showing_both_versions(config, capsys): + commands.Version(config, {"project": False, "commitizen": False, "verbose": True})() + captured = capsys.readouterr() + assert f"Installed Commitizen Version: {__version__}" in captured.out + assert "No project information in this project." in captured.err + + config.settings["version"] = "v0.0.1" + commands.Version(config, {"project": False, "commitizen": False, "verbose": True})() + captured = capsys.readouterr() + expected_out = ( + f"Installed Commitizen Version: {__version__}\n" f"Project Version: v0.0.1" + ) + assert expected_out in captured.out def test_check_no_conventional_commit(config, mocker):