Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
20 changes: 18 additions & 2 deletions commitizen/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}")
6 changes: 3 additions & 3 deletions docs/bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -144,7 +144,7 @@ version_files = [

`.cz` (TO BE DEPRECATED)

```
```ini
[commitizen]
version_files = [
"src/__version__.py",
Expand Down Expand Up @@ -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]
```
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/github_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Push the changes and that's it.
13 changes: 6 additions & 7 deletions docs/tutorials/gitlab_ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
```

Expand All @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down
43 changes: 39 additions & 4 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down