-
-
Notifications
You must be signed in to change notification settings - Fork 298
refactor(ChangelogFormat): refactor get_metadata_from_file for better… #1596
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
Open
bearomorphism
wants to merge
1
commit into
commitizen-tools:master
Choose a base branch
from
bearomorphism:refactor-getmetadata
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
import os | ||
from abc import ABCMeta | ||
from typing import IO, Any, ClassVar | ||
from typing import IO, ClassVar | ||
|
||
from commitizen.changelog import Metadata | ||
from commitizen.config.base_config import BaseConfig | ||
|
@@ -40,34 +40,40 @@ def get_metadata(self, filepath: str) -> Metadata: | |
with open(filepath, encoding=self.encoding) as changelog_file: | ||
return self.get_metadata_from_file(changelog_file) | ||
|
||
def get_metadata_from_file(self, file: IO[Any]) -> Metadata: | ||
meta = Metadata() | ||
def get_metadata_from_file(self, file: IO[str]) -> Metadata: | ||
out_metadata = Metadata() | ||
unreleased_level: int | None = None | ||
for index, line in enumerate(file): | ||
line = line.strip().lower() | ||
|
||
unreleased: int | None = None | ||
if "unreleased" in line: | ||
unreleased = self.parse_title_level(line) | ||
lines = [line.strip().lower() for line in file.readlines()] | ||
for index, line in enumerate(lines): | ||
parsed_unreleased_level = self.parse_title_level(line) | ||
current_unreleased_level = ( | ||
parsed_unreleased_level if "unreleased" in line else None | ||
) | ||
|
||
# Try to find beginning and end lines of the unreleased block | ||
if unreleased: | ||
meta.unreleased_start = index | ||
unreleased_level = unreleased | ||
if current_unreleased_level: | ||
out_metadata.unreleased_start = index | ||
unreleased_level = current_unreleased_level | ||
continue | ||
elif unreleased_level and self.parse_title_level(line) == unreleased_level: | ||
meta.unreleased_end = index | ||
|
||
if unreleased_level and parsed_unreleased_level == unreleased_level: | ||
out_metadata.unreleased_end = index | ||
|
||
# Try to find the latest release done | ||
parsed = self.parse_version_from_title(line) | ||
if parsed: | ||
meta.latest_version = parsed.version | ||
meta.latest_version_tag = parsed.tag | ||
meta.latest_version_position = index | ||
if parsed_version_tag := self.parse_version_from_title(line): | ||
out_metadata.latest_version = parsed_version_tag.version | ||
out_metadata.latest_version_tag = parsed_version_tag.tag | ||
out_metadata.latest_version_position = index | ||
break # there's no need for more info | ||
if meta.unreleased_start is not None and meta.unreleased_end is None: | ||
meta.unreleased_end = index | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
|
||
return meta | ||
if ( | ||
out_metadata.unreleased_start is not None | ||
and out_metadata.unreleased_end is None | ||
): | ||
out_metadata.unreleased_end = len(lines) - 1 | ||
|
||
return out_metadata | ||
|
||
def parse_version_from_title(self, line: str) -> VersionTag | None: | ||
""" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can assume that
self.parse_title_level
is always a pure function? Then we don't have to call it twice here.