From 837ddda876fc2c5a3d20c2b69e8dcf0547b0b863 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Fri, 27 Dec 2019 10:44:54 +0800 Subject: [PATCH 1/8] feat(commands/version): enable showing both project and installed commitizen version through version command #73 --- commitizen/commands/version.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/commitizen/commands/version.py b/commitizen/commands/version.py index 6375347f5e..238941e972 100644 --- a/commitizen/commands/version.py +++ b/commitizen/commands/version.py @@ -4,10 +4,20 @@ 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] + print(args) def __call__(self): - out.write(__version__) + if self.parameter.get("project"): + 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"Installed Commitizen Version: {__version__}") From 0d2fae417164c15b7be8807a099fa6d5adeb522d Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Fri, 27 Dec 2019 10:45:29 +0800 Subject: [PATCH 2/8] refactor(cli): add argument for version command 73 --- commitizen/cli.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index e3af3a8073..f8db4fcf6e 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -116,8 +116,25 @@ }, { "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": ["check"], From 5718ff373bde47aeeb911bd44c3bbb5a13a2648c Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Fri, 27 Dec 2019 10:46:05 +0800 Subject: [PATCH 3/8] test(commands/version): fix test break due to newly design version command --- tests/test_commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 983b8a0073..780631ec06 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -172,7 +172,7 @@ def test_list_cz(config): def test_version(config): with mock.patch("commitizen.out.write") as mocked_write: - commands.Version(config)() + commands.Version(config, {})() mocked_write.assert_called_once() From 54542e0279c4b7c31b72f0afa68f1ed42be7e438 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Fri, 27 Dec 2019 10:58:36 +0800 Subject: [PATCH 4/8] fix(commands/version): fix wrongly print commitizen version when it should print project version --- commitizen/commands/version.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/commitizen/commands/version.py b/commitizen/commands/version.py index 238941e972..a4c0b71ba0 100644 --- a/commitizen/commands/version.py +++ b/commitizen/commands/version.py @@ -9,13 +9,12 @@ class Version: def __init__(self, config: BaseConfig, *args): self.config: BaseConfig = config self.parameter = args[0] - print(args) def __call__(self): if self.parameter.get("project"): version = self.config.settings["version"] if version: - out.write(f"Project Version: {__version__}") + out.write(f"Project Version: {version}") else: out.error(f"No project information in this project.") else: From baf648f0e971f4501d6fbf483888b633ed72b5d9 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Fri, 27 Dec 2019 11:02:24 +0800 Subject: [PATCH 5/8] test(command/version): add test case for showing commitizen and project version --- tests/test_commands.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 780631ec06..91faff1314 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,22 @@ 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})() + 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})() + captured = capsys.readouterr() + assert "Project Version: v0.0.1" in captured.out + + +def test_version_for_showing_commitizen_version(config, capsys): + commands.Version(config, {"project": False, "commitizen": True})() + captured = capsys.readouterr() + assert f"Installed Commitizen Version: {__version__}" in captured.out def test_check_no_conventional_commit(config, mocker): From c7cb18c04262c9957902e932782b0643b54de01b Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Mon, 30 Dec 2019 08:54:42 +0800 Subject: [PATCH 6/8] feat(commands/version): add --verbose to show both versions --- commitizen/cli.py | 9 +++++++++ commitizen/commands/version.py | 9 ++++++++- tests/test_commands.py | 30 ++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index f8db4fcf6e..9d2e52509a 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -134,6 +134,15 @@ "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", + }, ], }, { diff --git a/commitizen/commands/version.py b/commitizen/commands/version.py index a4c0b71ba0..d64d8d82af 100644 --- a/commitizen/commands/version.py +++ b/commitizen/commands/version.py @@ -12,6 +12,13 @@ def __init__(self, config: BaseConfig, *args): def __call__(self): 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}") @@ -19,4 +26,4 @@ def __call__(self): out.error(f"No project information in this project.") else: # if no argument is given, show installed commitizen version - out.write(f"Installed Commitizen Version: {__version__}") + out.write(f"{__version__}") diff --git a/tests/test_commands.py b/tests/test_commands.py index 91faff1314..c88a2716ba 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -173,20 +173,42 @@ def test_list_cz(config): def test_version_for_showing_project_version(config, capsys): # No version exist - commands.Version(config, {"project": True, "commitizen": False})() + 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})() + commands.Version(config, {"project": True, "commitizen": False, "verbose": False})() captured = capsys.readouterr() - assert "Project Version: v0.0.1" in captured.out + assert "v0.0.1" in captured.out def test_version_for_showing_commitizen_version(config, capsys): - commands.Version(config, {"project": False, "commitizen": True})() + 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): From 0e3a808d5a6dacadf251a8c9734e27d93c7f70c4 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Mon, 30 Dec 2019 09:03:58 +0800 Subject: [PATCH 7/8] docs(index): add version command --- docs/index.md | 2 ++ 1 file changed, 2 insertions(+) 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 ``` From 84a616f69a6f904b3f9afed343af6bee4f404f76 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Mon, 30 Dec 2019 09:04:57 +0800 Subject: [PATCH 8/8] style(focs): fix style warn by mdlint --- docs/bump.md | 6 +++--- docs/tutorials/github_actions.md | 2 +- docs/tutorials/gitlab_ci.md | 13 ++++++------- 3 files changed, 10 insertions(+), 11 deletions(-) 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/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: