Skip to content

Commit

Permalink
feat: Understand more issue refs in commit body
Browse files Browse the repository at this point in the history
Previously only `Issue: XXX` refs have been parsed for changelog needs,
now parse,

- `Closes: XXX`
- `Fixes: XXX`
- `Ref: XXX`
- `Relates: XXX`

as well.

Fixes: #17
  • Loading branch information
playpauseandstop committed Oct 26, 2020
1 parent d07831f commit 20c1713
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/badabump/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
r"^(?P<commit_type>[^\(]+)\((?P<scope>[^\)]+)\)$"
)

ISSUE_RE = re.compile(r"^Issue: (?P<issue>.+)$", re.M)
ISSUE_RE = re.compile(
r"^(Closes|Fixes|Issue|Ref|Relates): (?P<issue>.+)$", re.M
)


@attr.dataclass(frozen=True, slots=True)
Expand Down Expand Up @@ -88,7 +90,7 @@ def is_breaking_change(self) -> bool:
def issues(self) -> Tuple[str, ...]:
if self.body is None:
return ()
return tuple(item.strip() for item in ISSUE_RE.findall(self.body))
return tuple(item.strip() for _, item in ISSUE_RE.findall(self.body))

@property
def scope(self) -> Optional[str]:
Expand Down
84 changes: 80 additions & 4 deletions tests/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

DOCS_SCOPE_COMMIT = """docs(openapi): Update descriptions in OpenAPI schema
Issue: #123
Ref: #123
"""

FEATURE_COMMIT = """feat: Export necessary types from the package (#31)
Expand All @@ -23,12 +23,25 @@
Issue: IFXND-55
"""

FIX_COMMIT = "fix: Update logic behind math operations"

INVALID_COMMIT = "something"

REFACTOR_COMMIT = """refactor: Change algorigthm to use
Fixes: DEV-1010
"""

CHANGELOG_EMPTY = "No changes since last pre-release"

CHANGELOG_FILE_MD = """## Features:
- [IFXND-55] Export necessary types from the package (#31)
## Fixes:
- Update logic behind math operations
## Other:
- **BREAKING CHANGE:** Use badabump release bot for pushing tags
Expand All @@ -38,20 +51,44 @@
- [IFXND-55] Export necessary types from the package (#31)
### Fixes:
- Update logic behind math operations
### Other:
- **BREAKING CHANGE:** Use badabump release bot for pushing tags
- [#123] (**openapi**) Update descriptions in OpenAPI schema"""

CHANGELOG_FILE_RST = """**Features:**
CHANGELOG_FILE_RST = CHANGELOG_GIT_RST = """**Features:**
- [IFXND-55] Export necessary types from the package (#31)
**Fixes:**
- Update logic behind math operations
**Other:**
- **BREAKING CHANGE:** Use badabump release bot for pushing tags
- [#123] (**openapi**) Update descriptions in OpenAPI schema"""

CHANGELOG_GIT_MD = """Features:
---------
- [IFXND-55] Export necessary types from the package (#31)
Fixes:
------
- Update logic behind math operations
Other:
------
- **BREAKING CHANGE:** Use badabump release bot for pushing tags
- [#123] (**openapi**) Update descriptions in OpenAPI schema"""


@pytest.mark.parametrize(
"changelog_type, format_type, expected",
Expand Down Expand Up @@ -91,7 +128,7 @@ def test_changelog_empty(changelog_type, format_type, expected):
)
def test_changelog_format_file(format_type, is_pre_release, expected):
changelog = ChangeLog.from_git_commits(
[FEATURE_COMMIT, CI_BREAKING_COMMIT, DOCS_SCOPE_COMMIT]
[FEATURE_COMMIT, FIX_COMMIT, CI_BREAKING_COMMIT, DOCS_SCOPE_COMMIT]
)
content = changelog.format(
ChangeLogTypeEnum.changelog_file,
Expand All @@ -101,13 +138,46 @@ def test_changelog_format_file(format_type, is_pre_release, expected):
assert content == expected


@pytest.mark.parametrize(
"format_type, is_pre_release, expected",
(
(FormatTypeEnum.markdown, False, CHANGELOG_GIT_MD),
(FormatTypeEnum.markdown, True, CHANGELOG_GIT_MD),
(FormatTypeEnum.rst, False, CHANGELOG_GIT_RST),
(FormatTypeEnum.rst, True, CHANGELOG_GIT_RST),
),
)
def test_changelog_format_git(format_type, is_pre_release, expected):
changelog = ChangeLog.from_git_commits(
[FEATURE_COMMIT, FIX_COMMIT, CI_BREAKING_COMMIT, DOCS_SCOPE_COMMIT]
)
content = changelog.format(
ChangeLogTypeEnum.git_commit,
format_type,
is_pre_release=is_pre_release,
)
assert content == expected


def test_changelog_invalid_commit():
with pytest.raises(ValueError):
ChangeLog.from_git_commits([INVALID_COMMIT])


def test_changelog_with_feature_commit():
changelog = ChangeLog.from_git_commits([FEATURE_COMMIT])
assert changelog.has_breaking_change is False
assert changelog.has_minor_change is True
assert changelog.has_micro_change is False


def test_changelog_with_fix_commit():
changelog = ChangeLog.from_git_commits([FIX_COMMIT])
assert changelog.has_breaking_change is False
assert changelog.has_minor_change is False
assert changelog.has_micro_change is True


def test_commit_ci_breaking():
commit = ConventionalCommit.from_git_commit(CI_BREAKING_COMMIT)
assert commit.commit_type == "ci"
Expand All @@ -129,7 +199,7 @@ def test_commit_docs_scope():
assert commit.commit_type == "docs"
assert commit.description == "Update descriptions in OpenAPI schema"
assert commit.is_breaking_change is False
assert commit.body == "Issue: #123"
assert commit.body == "Ref: #123"
assert commit.scope == "openapi"
assert (
commit.format(FormatTypeEnum.markdown)
Expand Down Expand Up @@ -163,3 +233,9 @@ def test_commit_feature():
assert commit.format(FormatTypeEnum.markdown) == commit.format(
FormatTypeEnum.rst
)


def test_commit_refactor():
commit = ConventionalCommit.from_git_commit(REFACTOR_COMMIT)
assert commit.commit_type == "refactor"
assert commit.issues == ("DEV-1010",)

0 comments on commit 20c1713

Please sign in to comment.