Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Feature/customize bump #18

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
woile merged 3 commits into master from feature/customize-bump
Apr 19, 2019
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# CHANGELOG

## v1.2.0

### Feature
- custom cz plugins now support bumping version

## v1.1.1

### Fix
- breaking change is now part of the body, instead of being in the subject

## v1.1.0

Expand Down
2 changes: 1 addition & 1 deletion commitizen/__version__.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.1"
__version__ = "1.2.0"
11 changes: 3 additions & 8 deletions commitizen/bump.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@
from string import Template
from packaging.version import Version
from typing import List, Optional, Union

MAJOR = "MAJOR"
MINOR = "MINOR"
PATCH = "PATCH"
conventional_commits_pattern = r"^(BREAKING CHANGE|feat)"
conventional_commits_map = {"BREAKING CHANGE": MAJOR, "feat": MINOR}
from commitizen.defaults import MAJOR, MINOR, PATCH, bump_pattern, bump_map


def find_increment(
messages: List[str],
regex: str = conventional_commits_pattern,
increments_map: dict = conventional_commits_map,
regex: str = bump_pattern,
increments_map: dict = bump_map,
) -> str:

# Most important cases are major and minor.
Expand Down
14 changes: 12 additions & 2 deletions commitizen/commands/bump.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from typing import Optional

import questionary
from commitizen import bump, git, config, out
from commitizen import bump, git, config, out, factory

NO_COMMITS_FOUND = 3
NO_VERSION_SPECIFIED = 4
NO_PATTERN_MAP = 7


class Bump:
Expand All @@ -22,6 +23,7 @@ def __init__(self, config: dict, arguments: dict):
if arguments[key] is not None
},
}
self.cz = factory.commiter_factory(self.config)

def __call__(self):
"""Steps executed to bump."""
Expand All @@ -32,6 +34,7 @@ def __call__(self):
out.error("Check if current version is specified in config file, like:")
out.error("version = 0.4.3")
raise SystemExit(NO_VERSION_SPECIFIED)

current_version: str = self.config["version"]
tag_format: str = self.parameters["tag_format"]
current_tag_version: str = bump.create_tag(
Expand Down Expand Up @@ -66,7 +69,14 @@ def __call__(self):
raise SystemExit(NO_COMMITS_FOUND)

if increment is None:
increment = bump.find_increment(commits)
bump_pattern = self.cz.bump_pattern
bump_map = self.cz.bump_map
if not bump_map or not bump_pattern:
out.error(f"'{self.config['name']}' rule does not support bump")
raise SystemExit(NO_PATTERN_MAP)
increment = bump.find_increment(
commits, regex=bump_pattern, increments_map=bump_map
)

# Increment is removed when current and next version
# are expected to be prereleases.
Expand Down
4 changes: 4 additions & 0 deletions commitizen/cz/base.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from typing import Optional
from abc import ABCMeta, abstractmethod


class BaseCommitizen(metaclass=ABCMeta):
bump_pattern: Optional[str] = None
bump_map: Optional[dict] = None

def __init__(self, config: dict):
self.config = config

Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from commitizen.cz.base import BaseCommitizen

from commitizen import defaults
__all__ = ["ConventionalCommitsCz"]


Expand Down Expand Up @@ -30,6 +30,9 @@ def parse_subject(text):


class ConventionalCommitsCz(BaseCommitizen):
bump_pattern = defaults.bump_pattern
bump_map = defaults.bump_map

def questions(self) -> list:
questions = [
{
Expand Down
7 changes: 7 additions & 0 deletions commitizen/defaults.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@
"files": [],
"tag_format": None, # example v$version
}

MAJOR = "MAJOR"
MINOR = "MINOR"
PATCH = "PATCH"

bump_pattern = r"^(BREAKING CHANGE|feat)"
bump_map = {"BREAKING CHANGE": MAJOR, "feat": MINOR}
6 changes: 5 additions & 1 deletion docs/bump.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,9 @@ files = [
]
```

## Custom bump

Read the [customizing section](./customization.md).

[pep440]: https://www.python.org/dev/peps/pep-0440/
[semver]: https://semver.org/
[semver]: https://semver.org/
44 changes: 44 additions & 0 deletions docs/customization.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
Customizing commitizen is not hard at all.

The basic steps are:

1. Inheriting from `BaseCommitizen`
2. Give a name to your rules.
3. expose the class at the end of your file assigning it to `discover_this`
4. Create a python package starting with `cz_` using `setup.py`, `poetry`, etc


Check an [example](convcomms) on how to configure `BaseCommitizen`.

## Custom commit rules

Create a file starting with `cz_` for example `cz_jira.py`. This prefix
Expand Down Expand Up @@ -83,3 +95,35 @@ doing `pip install .`
If you feel like it should be part of this repo, create a PR.

[flask uses]: http://flask.pocoo.org/docs/0.12/extensiondev/

## Custom bump rules

You need to define 2 parameters inside `BaseCommitizen`.

| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| `bump_pattern` | `str` | `None` | Regex to extract information from commit (subject and body) |
| `bump_map` | `dict` | `None` | Dictionary mapping the extracted information to a `SemVer` increment type (`MAJOR`, `MINOR`, `PATCH`) |

Let's see an exampple

```python
from commitizen.cz.base import BaseCommitizen


class StrangeCommitizen(BaseCommitizen):
bump_pattern = r"^(break|new|fix|hotfix)"
bump_map = {"break": "MAJOR", "new": "MINOR"}
```

As you can see we have ommitted `fix` and `hotfix`, both are gonna be picked up
by the regex, but because they are not present in `bump_map` they will be mapped
by default to `PATCH`

That's it, your commitizen now supports custom rules and you can run

```bash
cz -n cz_strange bump
```

[convcomms]: https://github.com/Woile/commitizen/blob/master/commitizen/cz/conventional_commits/conventional_commits.py
4 changes: 2 additions & 2 deletions pyproject.toml
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.commitizen]
version = "1.1.1"
version = "1.2.0"
tag_format = "v$version"
files = [
"pyproject.toml",
Expand Down Expand Up @@ -29,7 +29,7 @@ exclude = '''

[tool.poetry]
name = "commitizen"
version = "1.1.1"
version = "1.2.0"
description = "Python commitizen client tool"
authors = ["Santiago Fraire <santiwilly@gmail.com>"]
license = "MIT"
Expand Down

AltStyle によって変換されたページ (->オリジナル) /