Skip to content

chore(psr-changelog): consolidate & simplify jinja macro implementation #1265

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions config/release-templates/.components/changes.md.j2
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% from 'macros.md.j2' import apply_alphabetical_ordering_by_brk_descriptions
%}{% from 'macros.md.j2' import apply_alphabetical_ordering_by_descriptions
%}{% from 'macros.md.j2' import apply_alphabetical_ordering_by_release_notices
%}{% from 'macros.md.j2' import emoji_map, format_breaking_changes_description
%}{% from 'macros.md.j2' import format_commit_summary_line, format_release_notice
%}{% from 'macros.md.j2' import section_heading_order, section_heading_translations
{% from 'macros.common.j2' import apply_alphabetical_ordering_by_brk_descriptions
%}{% from 'macros.common.j2' import apply_alphabetical_ordering_by_descriptions
%}{% from 'macros.common.j2' import apply_alphabetical_ordering_by_release_notices
%}{% from 'macros.common.j2' import emoji_map, format_breaking_changes_description
%}{% from 'macros.common.j2' import format_release_notice, section_heading_order
%}{% from 'macros.common.j2' import section_heading_translations
%}{% from 'macros.md.j2' import format_commit_summary_line
%}{#
EXAMPLE:

Expand Down
17 changes: 9 additions & 8 deletions config/release-templates/.components/changes.rst.j2
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{% from 'macros.rst.j2' import apply_alphabetical_ordering_by_brk_descriptions
%}{% from 'macros.rst.j2' import apply_alphabetical_ordering_by_descriptions
%}{% from 'macros.rst.j2' import apply_alphabetical_ordering_by_release_notices
%}{% from 'macros.rst.j2' import emoji_map, extract_issue_link_references, extract_pr_link_reference
%}{% from 'macros.rst.j2' import format_breaking_changes_description, format_commit_summary_line
%}{% from 'macros.rst.j2' import format_link_reference, format_release_notice
%}{% from 'macros.rst.j2' import generate_heading_underline, section_heading_order
%}{% from 'macros.rst.j2' import section_heading_translations
{% from 'macros.common.j2' import apply_alphabetical_ordering_by_brk_descriptions
%}{% from 'macros.common.j2' import apply_alphabetical_ordering_by_descriptions
%}{% from 'macros.common.j2' import apply_alphabetical_ordering_by_release_notices
%}{% from 'macros.common.j2' import emoji_map, format_breaking_changes_description
%}{% from 'macros.common.j2' import format_release_notice, section_heading_order
%}{% from 'macros.common.j2' import section_heading_translations
%}{% from 'macros.rst.j2' import extract_issue_link_references, extract_pr_link_reference
%}{% from 'macros.rst.j2' import format_commit_summary_line, format_link_reference
%}{% from 'macros.rst.j2' import generate_heading_underline
%}{#

✨ Features
Expand Down
160 changes: 160 additions & 0 deletions config/release-templates/.components/macros.common.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{# TODO: move to configuration for user to modify #}
{% set section_heading_translations = {
'feat': 'features',
'fix': 'bug fixes',
'perf': 'performance improvements',
'docs': 'documentation',
'build': 'build system',
'refactor': 'refactoring',
'test': 'testing',
'ci': 'continuous integration',
'chore': 'chores',
'style': 'code style',
}
%}

{% set section_heading_order = section_heading_translations.values() %}

{% set emoji_map = {
'breaking': '💥',
'features': '✨',
'bug fixes': '🪲',
'performance improvements': '⚡',
'documentation': '📖',
'build system': '⚙️',
'refactoring': '♻️',
'testing': '✅',
'continuous integration': '🤖',
'chores': '🧹',
'code style': '🎨',
'unknown': '❗',
'release_note': '💡',
} %}


{#
MACRO: Capitalize the first letter of a string only
#}{% macro capitalize_first_letter_only(sentence)
%}{{ (sentence[0] | upper) ~ sentence[1:]
}}{% endmacro
%}


{#
MACRO: format a commit descriptions list by:
- Capitalizing the first line of the description
- Adding an optional scope prefix
- Joining the rest of the descriptions with a double newline
#}{% macro format_attr_paragraphs(commit, attribute)
%}{# NOTE: requires namespace because of the way Jinja2 handles variable scoping with loops
#}{% set ns = namespace(full_description="")
%}{#
#}{% if commit.error is undefined
%}{% for paragraph in commit | attr(attribute)
%}{% if paragraph | trim | length > 0
%}{#
#}{% set ns.full_description = [
ns.full_description,
capitalize_first_letter_only(paragraph) | trim | safe,
] | join("\n\n")
%}{#
#}{% endif
%}{% endfor
%}{#
#}{% set ns.full_description = ns.full_description | trim
%}{#
#}{% if commit.scope
%}{% set ns.full_description = "**%s**: %s" | format(
commit.scope, ns.full_description
)
%}{% endif
%}{% endif
%}{#
#}{{ ns.full_description
}}{% endmacro
%}


{#
MACRO: format the breaking changes description by:
- Capitalizing the description
- Adding an optional scope prefix
#}{% macro format_breaking_changes_description(commit)
%}{{ format_attr_paragraphs(commit, 'breaking_descriptions')
}}{% endmacro
%}


{#
MACRO: format the release notice by:
- Capitalizing the description
- Adding an optional scope prefix
#}{% macro format_release_notice(commit)
%}{{ format_attr_paragraphs(commit, "release_notices")
}}{% endmacro
%}


{#
MACRO: order commits alphabetically by scope and attribute
- Commits are sorted based on scope and then the attribute alphabetically
- Commits without scope are placed first and sorted alphabetically by the attribute
- parameter: ns (namespace) object with a commits list
- parameter: attr (string) attribute to sort by
- returns None but modifies the ns.commits list in place
#}{% macro order_commits_alphabetically_by_scope_and_attr(ns, attr)
%}{% set ordered_commits = []
%}{#
# # Eliminate any ParseError commits from input set
#}{% set filtered_commits = ns.commits | rejectattr("error", "defined") | list
%}{#
# # grab all commits with no scope and sort alphabetically by attr
#}{% for commit in filtered_commits | rejectattr("scope") | sort(attribute=attr)
%}{% set _ = ordered_commits.append(commit)
%}{% endfor
%}{#
# # grab all commits with a scope and sort alphabetically by the scope and then attr
#}{% for commit in filtered_commits | selectattr("scope") | sort(attribute=(['scope', attr] | join(",")))
%}{% set _ = ordered_commits.append(commit)
%}{% endfor
%}{#
# # Return the ordered commits
#}{% set ns.commits = ordered_commits
%}{% endmacro
%}


{#
MACRO: apply smart ordering of commits objects based on alphabetized summaries and then scopes
- Commits are sorted based on the commit type and the commit message
- Commits are grouped by the commit type
- parameter: ns (namespace) object with a commits list
- returns None but modifies the ns.commits list in place
#}{% macro apply_alphabetical_ordering_by_descriptions(ns)
%}{% set _ = order_commits_alphabetically_by_scope_and_attr(ns, 'descriptions.0')
%}{% endmacro
%}


{#
MACRO: apply smart ordering of commits objects based on alphabetized breaking changes and then scopes
- Commits are sorted based on the commit type and the commit message
- Commits are grouped by the commit type
- parameter: ns (namespace) object with a commits list
- returns None but modifies the ns.commits list in place
#}{% macro apply_alphabetical_ordering_by_brk_descriptions(ns)
%}{% set _ = order_commits_alphabetically_by_scope_and_attr(ns, 'breaking_descriptions.0')
%}{% endmacro
%}


{#
MACRO: apply smart ordering of commits objects based on alphabetized release notices and then scopes
- Commits are sorted based on the commit type and the commit message
- Commits are grouped by the commit type
- parameter: ns (namespace) object with a commits list
- returns None but modifies the ns.commits list in place
#}{% macro apply_alphabetical_ordering_by_release_notices(ns)
%}{% set _ = order_commits_alphabetically_by_scope_and_attr(ns, 'release_notices.0')
%}{% endmacro
%}
174 changes: 1 addition & 173 deletions config/release-templates/.components/macros.md.j2
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
{% set section_heading_translations = {
'feat': 'features',
'fix': 'bug fixes',
'perf': 'performance improvements',
'docs': 'documentation',
'build': 'build system',
'refactor': 'refactoring',
'test': 'testing',
'ci': 'continuous integration',
'chore': 'chores',
'style': 'code style',
} %}

{% set section_heading_order = section_heading_translations.values() %}

{% set emoji_map = {
'breaking': '💥',
'features': '✨',
'bug fixes': '🪲',
'performance improvements': '⚡',
'documentation': '📖',
'build system': '⚙️',
'refactoring': '♻️',
'testing': '✅',
'continuous integration': '🤖',
'chores': '🧹',
'code style': '🎨',
'unknown': '❗',
'release_note': '💡',
} %}
{% from 'macros.common.j2' import capitalize_first_letter_only %}


{#
Expand All @@ -38,14 +9,6 @@
%}


{#
MACRO: Capitalize the first letter of a string only
#}{% macro capitalize_first_letter_only(sentence)
%}{{ (sentence[0] | upper) ~ sentence[1:]
}}{% endmacro
%}


{#
MACRO: commit message links or PR/MR links of commit
#}{% macro commit_msg_links(commit)
Expand Down Expand Up @@ -109,138 +72,3 @@
}}{% endif
%}{% endmacro
%}


{#
MACRO: format the breaking changes description by:
- Capitalizing the description
- Adding an optional scope prefix
#}{% macro format_breaking_changes_description(commit)
%}{% set ns = namespace(full_description="")
%}{#
#}{% if commit.error is undefined
%}{% for paragraph in commit.breaking_descriptions
%}{% if paragraph | trim | length > 0
%}{#
#}{% set paragraph_text = capitalize_first_letter_only(paragraph) | trim | safe
%}{#
#}{% set ns.full_description = [
ns.full_description,
paragraph_text
] | join("\n\n")
%}{#
#}{% endif
%}{% endfor
%}{#
#}{% set ns.full_description = ns.full_description | trim
%}{#
#}{% if commit.scope
%}{% set ns.full_description = "**%s**: %s" | format(
commit.scope, ns.full_description
)
%}{% endif
%}{% endif
%}{#
#}{{ ns.full_description
}}{% endmacro
%}


{#
MACRO: format the release notice by:
- Capitalizing the description
- Adding an optional scope prefix
#}{% macro format_release_notice(commit)
%}{% set ns = namespace(full_description="")
%}{#
#}{% if commit.error is undefined
%}{% for paragraph in commit.release_notices
%}{% if paragraph | trim | length > 0
%}{#
#}{% set paragraph_text = capitalize_first_letter_only(paragraph) | trim | safe
%}{#
#}{% set ns.full_description = [
ns.full_description,
paragraph_text
] | join("\n\n")
%}{#
#}{% endif
%}{% endfor
%}{#
#}{% set ns.full_description = ns.full_description | trim
%}{#
#}{% if commit.scope
%}{% set ns.full_description = "**%s**: %s" | format(
commit.scope, ns.full_description
)
%}{% endif
%}{% endif
%}{#
#}{{ ns.full_description
}}{% endmacro
%}


{#
MACRO: order commits alphabetically by scope and attribute
- Commits are sorted based on scope and then the attribute alphabetically
- Commits without scope are placed first and sorted alphabetically by the attribute
- parameter: ns (namespace) object with a commits list
- parameter: attr (string) attribute to sort by
- returns None but modifies the ns.commits list in place
#}{% macro order_commits_alphabetically_by_scope_and_attr(ns, attr)
%}{% set ordered_commits = []
%}{#
# # Eliminate any ParseError commits from input set
#}{% set filtered_commits = ns.commits | rejectattr("error", "defined") | list
%}{#
# # grab all commits with no scope and sort alphabetically by attr
#}{% for commit in filtered_commits | rejectattr("scope") | sort(attribute=attr)
%}{% set _ = ordered_commits.append(commit)
%}{% endfor
%}{#
# # grab all commits with a scope and sort alphabetically by the scope and then attr
#}{% for commit in filtered_commits | selectattr("scope") | sort(attribute=(['scope', attr] | join(",")))
%}{% set _ = ordered_commits.append(commit)
%}{% endfor
%}{#
# # Return the ordered commits
#}{% set ns.commits = ordered_commits
%}{% endmacro
%}


{#
MACRO: apply smart ordering of commits objects based on alphabetized summaries and then scopes
- Commits are sorted based on the commit type and the commit message
- Commits are grouped by the commit type
- parameter: ns (namespace) object with a commits list
- returns None but modifies the ns.commits list in place
#}{% macro apply_alphabetical_ordering_by_descriptions(ns)
%}{% set _ = order_commits_alphabetically_by_scope_and_attr(ns, 'descriptions.0')
%}{% endmacro
%}


{#
MACRO: apply smart ordering of commits objects based on alphabetized breaking changes and then scopes
- Commits are sorted based on the commit type and the commit message
- Commits are grouped by the commit type
- parameter: ns (namespace) object with a commits list
- returns None but modifies the ns.commits list in place
#}{% macro apply_alphabetical_ordering_by_brk_descriptions(ns)
%}{% set _ = order_commits_alphabetically_by_scope_and_attr(ns, 'breaking_descriptions.0')
%}{% endmacro
%}


{#
MACRO: apply smart ordering of commits objects based on alphabetized release notices and then scopes
- Commits are sorted based on the commit type and the commit message
- Commits are grouped by the commit type
- parameter: ns (namespace) object with a commits list
- returns None but modifies the ns.commits list in place
#}{% macro apply_alphabetical_ordering_by_release_notices(ns)
%}{% set _ = order_commits_alphabetically_by_scope_and_attr(ns, 'release_notices.0')
%}{% endmacro
%}
Loading