Skip to content
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

feat: Allow user customization of release notes template #736

Conversation

bryant-finney
Copy link
Contributor

@bryant-finney bryant-finney commented Oct 19, 2023

Description

Allow end users to override the default Jinja2 template for rendering release notes.

If a file named .release_notes.md.j2 exists in the configured template_dir, it is used to render the release notes of a given VCS release.

Implements #660

Checklist

  • test implementation: see the following commits for this feature's two acceptance tests:
    1. c3f54c8
    2. 476cedb
    3. d648d79
    4. 3565dcd
  • feature implementation: see 63d952a for the implementation of this feature
  • documentation updates:
    • 8dc1283 and 884bbf8 update the changelog_templates.html Sphinx page to describe how to use this feature
    • 3f2a96a updates how the --noop argument is handled in the version and changelog commands to display the rendered release notes

Misc. Notes

  • f41fc57 was needed to fix a couple of preexisting pre-commit failures
  • 1484305 was later reverted by 63d952a
  • 19a01f0 and baf050a cleaned up some of the type annotations and pathlib usage
  • in addition to the acceptance tests, I also validated this change by running the following in one of my projects:
    # install python-semantic-release from my local clone of the project
    pip uninstall -y python-semantic-release    # remove the distribution from PyPI
    pip install -e ../python-semantic-release
    
    # create a release notes template
    cat <<EOF >templates/.release_notes.md.j2
    ## What's Changed
    {% for type_, commits in release["elements"] | dictsort %}
    {%- if type_ != "unknown" %}
    ### {{ type_ | capitalize }}
    {% for commit in commits %}
    * {{ commit.descriptions[0] }} by {{commit.commit.author.name}} in [`{{ commit.short_hash }}`]({{ commit.hexsha | commit_hash_url }})
    {%- endfor %}
    {% endif %}{% endfor %}
    EOF
    
    # simulate creating a new release
    semantic-release --noop version

@bryant-finney
Copy link
Contributor Author

@danth @relekang @bernardcooke53 could I ask for some help with merging this PR?

Based on a parallel PR in my fork (bryant-finney#1), I think it's ready to merge now

CC @jmgate

@jmgate
Copy link

jmgate commented Oct 19, 2023

Excited to be able to use this feature when it makes it in 😀

Copy link
Contributor

@bernardcooke53 bernardcooke53 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good - I spent a fair bit of time going over all the changes and testing this locally, I think it's really easy to follow, well-commented and well-tested, thanks for all the effort! 🚀
I've left a couple of comments/questions but nothing major

semantic_release/cli/commands/changelog.py Outdated Show resolved Hide resolved
docs/changelog_templates.rst Show resolved Hide resolved
semantic_release/cli/commands/version.py Outdated Show resolved Hide resolved
semantic_release/cli/commands/version.py Outdated Show resolved Hide resolved
semantic_release/cli/common.py Outdated Show resolved Hide resolved
tests/command_line/conftest.py Outdated Show resolved Hide resolved
tests/command_line/conftest.py Outdated Show resolved Hide resolved
tests/fixtures/git_repo.py Show resolved Hide resolved
tests/util.py Outdated Show resolved Hide resolved
tests/util.py Show resolved Hide resolved
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 20, 2023
- add an annotation to the `runtime` object for type analysis
    - tweak some other annotations to resolve errors
- tweak file I/O to use `pathlib.Path.write_text()`

ref: python-semantic-release#736 (comment)

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 20, 2023
ref: python-semantic-release#736 (comment)

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 20, 2023
- similar to 17f9d7a, annotate the
    `RuntimeContext` and update the annotations in some affected locations
- update `pyproject.toml` to report test context responsible for line
    coverage
- remove an unnecessary `if` statement in the `version` command

ref: python-semantic-release#736 (comment)

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 20, 2023
it's available as part of the `pytest` public API, so importing it from
there is more robust than using a private location

ref: python-semantic-release#736 (comment)

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 20, 2023
for consistency with other tests, use `requests_mock` functionality
instead of targeted `unittest.mock` patching

ref: python-semantic-release#736

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 20, 2023
the `TypeAlias` class was introduced in python 3.10 with PEP 613

ref: https://docs.python.org/3/library/typing.html#typing.TypeAlias
ref: https://peps.python.org/pep-0613/
ref: python-semantic-release#736

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
- `CHANGELOG.md`: the `trailing-whitespace` and `mixed-line-ending`
  hooks were failing
- `semantic_release.hvcs.util`: `mypy` was failing for Python 3.11:
  ```
  mypy.....................................................................Failed
  - hook id: mypy
  - exit code: 1
  semantic_release/hvcs/util.py: note: In function "suppress_http_error_for_codes":
  semantic_release/hvcs/util.py:74:20: error: Item "None" of "Optional[Response]"
  has no attribute "status_code"  [union-attr]
                      if err.response.status_code in codes:
                      ^~~~~~~~~~~~~~~~~~~~~~~~
  semantic_release/hvcs/util.py:78:25: error: Item "None" of "Optional[Response]"
  has no attribute "status_code"  [union-attr]
                              err.response.status_code,
                              ^~~~~~~~~~~~~~~~~~~~~~~~
  Found 2 errors in 1 file (checked 45 source files)
  ```

now, executing `pre-commit run --all-files` from recent Python versions
succeeds

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
…tes()`

... for consistency with `semantic_release.cli.common.render_default_changelog_file()`

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
- add a new fixture to `tests.fixtures.example_project` to write a
  template named `.release_notes.md.j2` to the `example_project`
  fixture's `templates` directory
- `tests.command_line.test_changelog:test_custom_release_notes_template()`
  verifies that that a template named `.release_notes.md.j2` is used to
  render the release notes
  - the implementation of feature python-semantic-release#660 cannot be complete until this
    test passes

additionally, update the repo configuration in `tests.fixtures.git_repo`
to disable gpg commit signatures

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
move the fixtures introduced in acde492
to `tests.command_line.conftest` so that they can be reused by other
test modules

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
some of the fixtures from 13a7af6 were
doing too much for reuse in other tests; reducing them allows extraction
for the second acceptance test

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
- add two new fixtures to tests.command_line.conftest for testing the
  `version` command
- tests.command_line.test_version:test_custom_release_notes_template()
  verifies that that a template named .release_notes.md.j2 is used to
  render the release notes
  - the implementation of feature python-semantic-release#660 cannot be complete until this
    test passes

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
…ject

without using the fixture, the repo was not being initialized with
example files

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
- convert `RuntimeContext.template_dir` from `str` → `Path`
- rename `render_default_release_notes` → `render_release_notes`
    - expect the template to be provided as an argument
    - a separate helper function is used to read the template file
- update the `changelog` and `version` commands accordingly

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
- `Repo.working_tree_dir` can be `None` under certain conditions, so
    initialize `pathlib.Path` with `'.'` if this happens
- the `args` and `kwargs` properties were added to
    `unittest.mock.Mock.call_args` in Python 3.8, so skip the new tests
    for Python 3.7
    - ref: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.call_args
    - Python 3.7 reached EOL status on 2023-06-27, so skipping the tests
      is acceptable
        - ref: https://devguide.python.org/versions/

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
- add sections to the `changelog_templates` page

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
refactor the test `test_changelog_noop_is_noop()` in
`tests/command_line/test_changelog.py` to provide valid tags (formerly,
the tag passed with the --post-to-release-tag argument didn't exist)

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 21, 2023
- add an annotation to the `runtime` object for type analysis
    - tweak some other annotations to resolve errors
- tweak file I/O to use `pathlib.Path.write_text()`

ref: python-semantic-release#736 (comment)

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
@bryant-finney bryant-finney force-pushed the feature/660/release-notes-template branch from 98c68cb to 5071e0f Compare October 21, 2023 02:50
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 21, 2023
ref: python-semantic-release#736 (comment)

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 21, 2023
- similar to 17f9d7a, annotate the
    `RuntimeContext` and update the annotations in some affected locations
- update `pyproject.toml` to report test context responsible for line
    coverage
- remove an unnecessary `if` statement in the `version` command

ref: python-semantic-release#736 (comment)

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 21, 2023
it's available as part of the `pytest` public API, so importing it from
there is more robust than using a private location

ref: python-semantic-release#736 (comment)

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 21, 2023
for consistency with other tests, use `requests_mock` functionality
instead of targeted `unittest.mock` patching

ref: python-semantic-release#736

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
bryant-finney added a commit to bryant-finney/python-semantic-release that referenced this pull request Oct 21, 2023
the `TypeAlias` class was introduced in python 3.10 with PEP 613

ref: https://docs.python.org/3/library/typing.html#typing.TypeAlias
ref: https://peps.python.org/pep-0613/
ref: python-semantic-release#736

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
- add an annotation to the `runtime` object for type analysis
    - tweak some other annotations to resolve errors
- tweak file I/O to use `pathlib.Path.write_text()`

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
- similar to 17f9d7a, annotate the
    `RuntimeContext` and update the annotations in some affected locations
- update `pyproject.toml` to report test context responsible for line
    coverage
- remove an unnecessary `if` statement in the `version` command

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
it's available as part of the `pytest` public API, so importing it from
there is more robust than using a private location

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
for consistency with other tests, use `requests_mock` functionality
instead of targeted `unittest.mock` patching

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
incorrectly documented code is so much worse than undocumented code

also convert a `Path` instance to a `str` to appease `mypy`

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
the `TypeAlias` class was introduced in python 3.10 with PEP 613

Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
@bryant-finney bryant-finney force-pushed the feature/660/release-notes-template branch from 5071e0f to d4f731f Compare October 21, 2023 02:58
Signed-off-by: Bryant Finney <bryant.finney@outlook.com>
@bernardcooke53 bernardcooke53 merged commit 94a1311 into python-semantic-release:master Oct 23, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants