Skip to content

Commit

Permalink
fix: make additional attributes available for template authors
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardcooke53 committed Jun 4, 2023
1 parent 49d9e27 commit 4e2fcbe
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
14 changes: 10 additions & 4 deletions docs/commit-parsing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ as the return type from an unsuccessful parse of a commit. :py:class:`semantic_r
``BREAKING CHANGE:``.
* commit: The original commit object that was parsed.

:py:class:`ParsedCommit` objects also make the commit ``message`` available as an
attribute, though this is implemented as a :py:class:`property` referencing the
``message`` attribute of the ``commit``.

:py:class:`semantic_release.ParseError` is a `namedtuple`_ which has the following fields:

* commit: The original commit object that was parsed.
Expand All @@ -246,6 +242,16 @@ In addition, :py:class:`semantic_release.ParseError` implements an additional me
This method raises a :py:class:`semantic_release.CommitParseError` with the message contained in the
``error`` field, as a convenience.

:py:class:`ParsedCommit` and :py:class:`ParseError` objects also make the following
attributes available, each implemented as a ``property`` which is computed, as a
convenience for template authors - therefore custom implementations should ensure
these properties can also be computed:

* message: the ``message`` attribute of the ``commit``; where the message is of type ``bytes``
this should be decoded to a ``UTF-8`` string.
* hexsha: the ``hexsha`` attribute of the ``commit``, representing its hash.
* short_hash: the first 7 characters of the ``hexsha`` attribute of the ``commit``.

In Python Semantic Release, the class :py:class:`semantic_release.ParseResult`
is defined as ``ParseResultType[ParsedCommit, ParseError]``, as a convenient shorthand.

Expand Down
21 changes: 21 additions & 0 deletions semantic_release/commit_parser/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,32 @@ def message(self) -> str:
m = self.commit.message
return m.decode("utf-8") if isinstance(m, bytes) else m

@property
def hexsha(self) -> str:
return self.commit.hexsha

@property
def short_hash(self) -> str:
return self.commit.hexsha[:7]


class ParseError(NamedTuple):
commit: Commit
error: str

@property
def message(self) -> str:
m = self.commit.message
return m.decode("utf-8") if isinstance(m, bytes) else m

@property
def hexsha(self) -> str:
return self.commit.hexsha

@property
def short_hash(self) -> str:
return self.commit.hexsha[:7]

def raise_error(self) -> NoReturn:
raise CommitParseError(self.error)

Expand Down
8 changes: 4 additions & 4 deletions semantic_release/data/templates/CHANGELOG.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
{% for type_, commits in context.history.unreleased | dictsort %}
### {{ type_ | capitalize }}
{% for commit in commits %}{% if type_ != "unknown" %}
* {{ commit.message.rstrip() }} ([`{{ commit.hexsha[:7] }}`]({{ commit.hexsha | commit_hash_url }}))
* {{ commit.message.rstrip() }} ([`{{ commit.short_hash }}`]({{ commit.hexsha | commit_hash_url }}))
{% else %}
* {{ commit.message.rstrip() }} ([`{{ commit.hexsha[:7] }}`]({{ commit.hexsha | commit_hash_url }}))
* {{ commit.message.rstrip() }} ([`{{ commit.short_hash }}`]({{ commit.hexsha | commit_hash_url }}))
{% endif %}{% endfor %}{% endfor %}{% endif %}
{% for version, release in context.history.released.items() %}
{# RELEASED #}
## {{ version.as_tag() }} ({{ release.tagged_date.strftime("%Y-%m-%d") }})
{% for type_, commits in release["elements"] | dictsort %}
### {{ type_ | capitalize }}
{% for commit in commits %}{% if type_ != "unknown" %}
* {{ commit.message.rstrip() }} ([`{{ commit.hexsha[:7] }}`]({{ commit.hexsha | commit_hash_url }}))
* {{ commit.message.rstrip() }} ([`{{ commit.short_hash }}`]({{ commit.hexsha | commit_hash_url }}))
{% else %}
* {{ commit.message.rstrip() }} ([`{{ commit.hexsha[:7] }}`]({{ commit.hexsha | commit_hash_url }}))
* {{ commit.message.rstrip() }} ([`{{ commit.short_hash }}`]({{ commit.hexsha | commit_hash_url }}))
{% endif %}{% endfor %}{% endfor %}{% endfor %}
4 changes: 2 additions & 2 deletions semantic_release/data/templates/release_notes.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% for type_, commits in release["elements"] | dictsort %}
## {{ type_ | capitalize }}
{% for commit in commits %}{% if type_ != "unknown" %}
* {{ commit.message.rstrip() }} ([`{{ commit.hexsha[:7] }}`]({{ commit.hexsha | commit_hash_url }}))
* {{ commit.message.rstrip() }} ([`{{ commit.short_hash }}`]({{ commit.hexsha | commit_hash_url }}))
{% else %}
* {{ commit.message.rstrip() }} ([`{{ commit.hexsha[:7] }}`]({{ commit.hexsha | commit_hash_url }}))
* {{ commit.message.rstrip() }} ([`{{ commit.short_hash }}`]({{ commit.hexsha | commit_hash_url }}))
{% endif %}{% endfor %}{% endfor %}

0 comments on commit 4e2fcbe

Please sign in to comment.