Skip to content

Added conventional commits with JIRA scope. #158

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

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v1.18.0

### Feature

- added a derivative of conventional commits enforcing JIRA issues as scope.

## v1.12.0

### Feature
Expand Down
2 changes: 1 addition & 1 deletion commitizen/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.17.1"
__version__ = "1.18.0"
2 changes: 2 additions & 0 deletions commitizen/cz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import pkgutil

from commitizen.cz.conventional_commits import ConventionalCommitsCz
from commitizen.cz.conventional_jira import ConventionalJiraCz
from commitizen.cz.customize import CustomizeCommitsCz
from commitizen.cz.jira import JiraSmartCz

registry = {
"cz_conventional_commits": ConventionalCommitsCz,
"cz_conventional_jira": ConventionalJiraCz,
"cz_jira": JiraSmartCz,
"cz_customize": CustomizeCommitsCz,
}
Expand Down
1 change: 1 addition & 0 deletions commitizen/cz/conventional_jira/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .conventional_jira import ConventionalJiraCz # noqa
189 changes: 189 additions & 0 deletions commitizen/cz/conventional_jira/conventional_jira.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import os
import re

from commitizen import defaults
from commitizen.cz import exceptions
from commitizen.cz.base import BaseCommitizen
from commitizen.cz.utils import multiple_line_breaker, required_validator

__all__ = ["ConventionalJiraCz"]


def parse_jira_issues(text):
if not text:
return ""

issues = text.strip().split(", ")
issueRE = re.compile(r"\w+-\d+")

for issue in issues:
if not issueRE.fullmatch(issue):
raise exceptions.InvalidAnswerError(f"JIRA scope of '{issue}' is invalid")

if len(issues) == 1:
return issues[0]

return required_validator(", ".join(issues), msg="JIRA scope is required")


def parse_subject(text):
if isinstance(text, str):
text = text.strip(".").strip()

return required_validator(text, msg="Subject is required.")


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

def questions(self) -> list:
questions = [
{
"type": "list",
"name": "prefix",
"message": "Select the type of change you are committing",
"choices": [
{
"value": "fix",
"name": "fix: A bug fix. Correlates with PATCH in SemVer",
},
{
"value": "feat",
"name": "feat: A new feature. Correlates with MINOR in SemVer",
},
{"value": "docs", "name": "docs: Documentation only changes"},
{
"value": "style",
"name": (
"style: Changes that do not affect the "
"meaning of the code (white-space, formatting,"
" missing semi-colons, etc)"
),
},
{
"value": "refactor",
"name": (
"refactor: A code change that neither fixes "
"a bug nor adds a feature"
),
},
{
"value": "perf",
"name": "perf: A code change that improves performance",
},
{
"value": "test",
"name": (
"test: Adding missing or correcting " "existing tests"
),
},
{
"value": "build",
"name": (
"build: Changes that affect the build system or "
"external dependencies (example scopes: pip, docker, npm)"
),
},
{
"value": "ci",
"name": (
"ci: Changes to our CI configuration files and "
"scripts (example scopes: GitLabCI)"
),
},
],
},
{
"type": "input",
"name": "jira_issue",
"message": ("JIRA issue. Of form $JIRA_Project$-$Issue_Number$:"),
"filter": parse_jira_issues,
},
{
"type": "input",
"name": "subject",
"filter": parse_subject,
"message": (
"Subject. Concise description of the changes. "
"Imperative, lower case and no final dot:\n"
),
},
{
"type": "confirm",
"message": "Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer",
"name": "is_breaking_change",
"default": False,
},
{
"type": "input",
"name": "body",
"message": (
"Body. Motivation for the change and contrast this "
"with previous behavior:\n"
),
"filter": multiple_line_breaker,
},
{
"type": "input",
"name": "footer",
"message": (
"Footer. Information about Breaking Changes and "
"reference issues that this commit impacts:\n"
),
},
]
return questions

def message(self, answers: dict) -> str:
prefix = answers["prefix"]
jira_issue = answers["jira_issue"]
subject = answers["subject"]
body = answers["body"]
footer = answers["footer"]
is_breaking_change = answers["is_breaking_change"]

if jira_issue:
jira_issue = f"({jira_issue})"
if is_breaking_change:
body = f"BREAKING CHANGE: {body}"
if body:
body = f"\n\n{body}"
if footer:
footer = f"\n\n{footer}"

message = f"{prefix}{jira_issue}: {subject}{body}{footer}"

return message

def example(self) -> str:
return (
"fix(JIRA-1): correct minor typos in code\n"
"\n"
"see the issue for details on the typos fixed\n"
"\n"
"closes issue #12"
)

def schema(self) -> str:
return (
"<type>(<jira_issue>): <subject>\n"
"<BLANK LINE>\n"
"(BREAKING CHANGE: )<body>\n"
"<BLANK LINE>\n"
"<footer>"
)

def schema_pattern(self) -> str:
PATTERN = (
r"(build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert|bump)"
r"(\(\s+-\d+(, \s+-\d+)+\))?:\s.*"
)
return PATTERN

def info(self) -> str:
dir_path = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(dir_path, "conventional_jira_info.txt")
with open(filepath, "r") as f:
content = f.read()
return content
31 changes: 31 additions & 0 deletions commitizen/cz/conventional_jira/conventional_jira_info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
The commit contains the following structural elements, to communicate
intent to the consumers of your library:

fix(JIRA-ISSUE): a commit of the type fix patches a bug in your codebase
(this correlates with PATCH in semantic versioning).

feat(JIRA-ISSUE): a commit of the type feat introduces a new feature to the codebase
(this correlates with MINOR in semantic versioning).

BREAKING CHANGE(JIRA-ISSUE): a commit that has the text BREAKING CHANGE: at the beginning of
its optional body or footer section introduces a breaking API change
(correlating with MAJOR in semantic versioning).
A BREAKING CHANGE can be part of commits of any type.

Others: commit types other than fix: and feat: are allowed,
like chore:, docs:, style:, refactor:, perf:, test:, and others.

We also recommend improvement for commits that improve a current
implementation without adding a new feature or fixing a bug.

Notice these types are not mandated by the conventional commits specification,
and have no implicit effect in semantic versioning (unless they include a BREAKING CHANGE).

A scope may be provided to a commit’s type, to provide additional contextual
information and is contained within parenthesis, e.g., feat(parser): add ability to parse arrays.

<type>(JIRA-scope): <description>

[optional body]

[optional footer]
4 changes: 4 additions & 0 deletions commitizen/cz/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ class CzException(Exception):

class AnswerRequiredError(CzException):
...


class InvalidAnswerError(CzException):
...
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.commitizen]
version = "1.17.1"
version = "1.18.0"
tag_format = "v$version"
version_files = [
"pyproject.toml:version",
Expand Down Expand Up @@ -29,7 +29,7 @@ exclude = '''

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