pawamoy / git-changelog

Automatic Changelog generator using Jinja2 templates.
https://pawamoy.github.io/git-changelog
ISC License
136 stars 34 forks source link

feat: Support for additional template variables #73

Closed chme closed 6 months ago

chme commented 6 months ago

When using a custom Jinja template to render the changelog, it would be great, if additional context information could be accessed in that template. This feature request should be related to #17.

With the changes in this PR, additional context can be passed as key/value pairs either via CLI or via the toml config file.

To pass variables via CLI, the option --template-var KEY VALUE can be given multiple times.

To define variables in a configuration file, the variables can be defined in a "[template_vars]" table.

[template_vars]
KEY = "VALUE"

When template vars are given via CLI and via config file, they are merged with the CLI vars taking precedence.

All values are accessible in the template through the "template_vars" (dict) variable.

The parsing of the CLI option into a dict is based on this gist: https://gist.github.com/vadimkantorov/37518ff88808af840884355c845049ea

(Unfortunately argparse does not support it out of the box, or at least I could not find a better way to achieve this)

chme commented 6 months ago

This all looks good, but I think I would prefer the following syntax on the CLI: --template-var key=value. We can do key_value.split("=", 1) so that values can have the = character too. What do you think?

I first thought, that having key and value as separate parameters, would make it a bit easier, when calling it from a shell script (if the value has to be escaped). But searching a bit, using the = between key and value seems more common (though its difficult to find good examples).

I am fine with --template-var key=value.

Also, I don't think I like "template var" for the name and variable/parameter names in the code. What do you think of "jinja context" instead? This way we can also add a short option with -j which is not yet taken 🙂 I would then pass this context as context or user_context when rendering the template ("jinja" would be a bit redundant there).

Naming is not something I am especially good at :grin:, so whatever you prefer is fine for me. That said, "template variable" is the term used in the jinja documentation.

Finally, it will need some documentation too! I can help with that if needed.

I can add some documentation (though help on that would be appreciated).

pawamoy commented 6 months ago

I've pushed some docs :smile: Feel free to tweak them.

chme commented 6 months ago

I've pushed some docs 😄 Feel free to tweak them.

Awesome, thanks! I only, did a small change in the config file example.

I changed it now to use KEY=VALUE and I hope, I addressed all other comments as well.