commitizen-tools / commitizen

Create committing rules for projects :rocket: auto bump versions :arrow_up: and auto changelog generation :open_file_folder:
https://commitizen-tools.github.io/commitizen/
MIT License
2.36k stars 257 forks source link

changelog does not include body or footer information #746

Open jlanzarotta opened 1 year ago

jlanzarotta commented 1 year ago

Description

When generating a changelog where commits contain conventional commits, the body (the extended commit information) along with the footer information (ticket references information, etc.) is not written to the output file.

Steps to reproduce

cz changelog

Current behavior

Commits that have body and footer information, have this information missing from the changelog.

Desired behavior

Commits that have body and footer information, would have this information shown in the changelog.

Screenshots

No response

Environment

AGiantSquid commented 2 months ago

It is possible to add the body and footer information by customizing the config_parser regex and the template.

For example, set up a cz.yaml file with something like this:

---
commitizen:
  customize:
    commit_parser: ^((?P<change_type>feat|fix|refactor|perf|BREAKING CHANGE)(?:\((?P<scope>[^()\r\n]*)\)|\()?(?P<breaking>!)?|\w+!):\s(?P<subject>[^\n]*)(?P<body>(?:\n(?!Refs:|:\s#)[^\n]*)*)(?:\n(?P<footer>(?:Refs:[\s\S]*)?))?$
  name: cz_customize
  tag_format: v$version
  update_changelog_on_bump: true
  version_provider: npm
  version_scheme: semver
  template: CHANGELOG.md.j2

The commit_parser will parse your commit, and make the capture groups available to your formatter. In the regex string above, we're getting a section after the subject and labeling it "body", and getting any content that starts "Refs: " and labeling it footer.

You can then create a template CHANGELOG.md.j2 like this:

{% for entry in tree %}
{% set entry_title -%}
{{ entry.version }}{% if entry.date %} ({{ entry.date }}){% endif -%}
{%- endset %}

{{ entry_title }}
{{ "=" * entry_title|length }}

{% for change_key, changes in entry.changes.items() %}
{% if change_key %}
{{ change_key }}
{{ "-" * change_key|length }}
{% endif %}

{% for change in changes %}
{% if change.footer %}
{% set ticket = change.footer.split('#')[1] %}
{% endif %}
{% set ticket_link = "[[" + ticket + "](https://blah.atlassian.net/browse/" + ticket + ")] " if ticket else ""  %}
{% set commit_link = "[" + change.sha1[:7] + "](" + change.sha1 + ")" %}
{% set scope = "**" + change.scope + "**: " if change.scope else ""  %}
- {{ ticket_link }}{{ scope }}{{ change.subject }} {{commit_link}}
{% if change.body %}
{% set lines = change.body.split('\n') %}
{% for line in lines %}
{% set stripped_line = line.strip() -%}
{% if stripped_line.startswith('-') %}
    {{ stripped_line }}
{% elif stripped_line %}
    - {{ stripped_line }}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}

{% endfor %}
{% endfor %}

Which will use the capture groups subject body and footer from the commit_parser is the cz.yaml.

Adjust the regex according to your commit formatting:

image