twisted / towncrier

Manage the release notes for your project.
https://towncrier.readthedocs.io
MIT License
755 stars 107 forks source link

How to add dynamic line(s) above sections? #542

Closed MetRonnie closed 10 months ago

MetRonnie commented 10 months ago

Use case: We have a project that bundles a web UI which usually (but not always) gets updated with each release. So we want to include a line saying which UI version is bundled in this version of the project, like this:

## Project name 1.2.3 (2023-08-10)

UI version: 4.5.6

### Enhancements
- Blah blah

### Fixes
- Blah blah

The bundled UI version can be got from either the directory name ui/4.5.6/ or we could create a fragment file for it in our CI process (but it wouldn't know the number of the PR it's creating).

If it's not currently possible I think the simplest way to do this would be have a way to include in the Jinja2 template the contents of a custom pseudo-fragment file in the changes.d directory.

MetRonnie commented 10 months ago

Ok, I was able to achieve this by making the template like this

 {% if sections[""] %}
+{% if "ui-version" in sections[""] %}
+{{ sections[""]["ui-version"].keys()|first }}
+
+{% endif %}
-{% for category, val in definitions.items() if category in sections[""] %}
+{% for category, val in definitions.items() if category in sections[""] and category != "ui-version" %}
 ### {{ definitions[category]['name'] }}

 {% for text, pulls in sections[""][category].items() %}
 {{ pulls|join(', ') }} - {{ text }}

 {% endfor %}
 {% endfor %}
 {% else %}
 No significant changes.

 {% endif %}

And in the CI step it runs

towncrier create +.ui-version.md --content "UI version: ${UI_VERSION}"