Skip to content

Commit f4a3cc0

Browse files
authored
feat: Allow prepending the generated changelog to an existing one in git-cliff-release (#142)
1 parent e8a2722 commit f4a3cc0

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

git-cliff-release/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This action uses conventional commit history to determine the recommended versio
77
- **release_type**: One of `auto` (default), `patch`, `minor`, `major` and `custom`. `auto` means that the version will be determined based on the commit history, `custom` will use the value of the `custom_version` input parameter, and `patch`, `minor` and `major` allow forcing the bump type.
88
- **custom_version**: Optional unless the `release_type` is set to `custom`.
99
- **cliff_config_path**: Path to a configuration file for git-cliff. If none is given, a built-in configuration will be used.
10+
- **existing_changelog_path**: Path to an existing changelog. If given, the new changelog contents will be prepended to it intelligently.
11+
- **token**: Github token to be used by github CLI (should be relevant for private repositories only)
1012

1113
## Outputs
1214

git-cliff-release/action.yaml

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ inputs:
2323
required: false
2424
type: string
2525
default: cliff.toml
26+
existing_changelog_path:
27+
description: Path to an existing changelog. If given, the new changelog contents will be prepended to it intelligently.
28+
required: false
29+
type: string
30+
default: ""
31+
token:
32+
description: Github token to be used by github CLI
33+
required: false
34+
type: string
35+
default: ${{ github.token }}
2636
outputs:
2737
is_prerelease:
2838
description: For convenience - was the action triggered with release_type = "prerelease"?
@@ -65,8 +75,9 @@ runs:
6575
GIT_CLIFF_REPOSITORY: ${{ github.workspace }}/__release_metadata_repo
6676
GIT_CLIFF_WORKDIR: ${{ github.action_path }}
6777
GITHUB_REPO: ${{ github.repository }}
68-
GH_TOKEN: ${{ github.token }}
78+
GITHUB_TOKEN: ${{ inputs.token }}
6979
run: |
80+
set -x
7081
if [[ '${{ inputs.release_type }}' = custom ]]; then
7182
version_number=$(echo ${{ inputs.custom_version }} | sed s/^v//)
7283
elif [[ '${{ inputs.release_type }}' = auto || '${{ inputs.release_type }}' = prerelease ]]; then
@@ -85,9 +96,10 @@ runs:
8596
GIT_CLIFF_REPOSITORY: ${{ github.workspace }}/__release_metadata_repo
8697
GIT_CLIFF_WORKDIR: ${{ github.action_path }}
8798
GITHUB_REPO: ${{ github.repository }}
88-
GH_TOKEN: ${{ github.token }}
99+
GITHUB_TOKEN: ${{ inputs.token }}
89100
working-directory: ${{ github.action_path }}
90101
run: |
102+
set -x
91103
echo 'release_notes<<EOF' >> $GITHUB_OUTPUT
92104
git-cliff --tag "${{ steps.version_number.outputs.tag_name }}" --unreleased --context |
93105
python enhance_context.py --repo $GITHUB_REPO --release-notes |
@@ -102,22 +114,42 @@ runs:
102114
GIT_CLIFF_REPOSITORY: ${{ github.workspace }}/__release_metadata_repo
103115
GIT_CLIFF_WORKDIR: ${{ github.action_path }}
104116
GITHUB_REPO: ${{ github.repository }}
105-
GH_TOKEN: ${{ github.token }}
117+
GITHUB_TOKEN: ${{ inputs.token }}
106118
working-directory: ${{ github.action_path }}
107119
run: |
120+
set -x
121+
dump_context_args=()
122+
enhance_context_args=()
123+
print_changelog_args=()
124+
125+
out_file=$GIT_CLIFF_WORKDIR/new_changelog.md
126+
127+
if [[ -n "${{ inputs.existing_changelog_path }}" ]]; then
128+
out_file=$GIT_CLIFF_WORKDIR/existing_changelog.md
129+
cp "${{ github.workspace }}/${{ inputs.existing_changelog_path }}" $out_file
130+
sed '/<!-- git-cliff-unreleased-start -->/,/<!-- git-cliff-unreleased-end -->/d' -i $out_file
131+
132+
dump_context_args+=(--unreleased)
133+
print_changelog_args+=(--prepend "$out_file" --unreleased)
134+
else
135+
print_changelog_args+=(--output "$out_file")
136+
fi
137+
108138
if [[ ${{ inputs.release_type }} = prerelease ]]; then
109-
echo 'changelog<<EOF' >> $GITHUB_OUTPUT
110-
git-cliff --context |
111-
python enhance_context.py --repo $GITHUB_REPO --unreleased-version "${{ steps.version_number.outputs.tag_name }}" |
112-
git-cliff --from-context - >> $GITHUB_OUTPUT
113-
echo 'EOF' >> $GITHUB_OUTPUT
139+
enhance_context_args+=(--unreleased-version "${{ steps.version_number.outputs.tag_name }}")
114140
else
115-
echo 'changelog<<EOF' >> $GITHUB_OUTPUT
116-
git-cliff --tag "${{ steps.version_number.outputs.tag_name }}" --context |
117-
python enhance_context.py --repo $GITHUB_REPO |
118-
git-cliff --from-context - >> $GITHUB_OUTPUT
119-
echo 'EOF' >> $GITHUB_OUTPUT
141+
dump_context_args+=(--tag "${{ steps.version_number.outputs.tag_name }}")
120142
fi
143+
144+
git-cliff --context "${dump_context_args[@]}" |
145+
python enhance_context.py --repo $GITHUB_REPO "${enhance_context_args[@]}" |
146+
git-cliff --from-context - "${print_changelog_args[@]}"
147+
148+
sed -i '$a\' $out_file # Make sure there is a newline at the end of the output file
149+
150+
echo 'changelog<<EOF' >> $GITHUB_OUTPUT
151+
cat $out_file >> $GITHUB_OUTPUT
152+
echo 'EOF' >> $GITHUB_OUTPUT
121153
- name: Clean up
122154
shell: bash
123155
env:

git-cliff-release/cliff.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ All notable changes to this project will be documented in this file.\n
1414
# template for the changelog body
1515
# https://keats.github.io/tera/docs/#introduction
1616
body = """
17+
{% if not version %}<!-- git-cliff-unreleased-start -->
18+
{% endif %}\
1719
{% if version and extra.release_link %}\
1820
## [{{ version | trim_start_matches(pat="v") }}]({{ extra.release_link }}) ({{ timestamp | date(format="%Y-%m-%d") }})
1921
{% elif extra.unreleased_version %}\
@@ -33,7 +35,7 @@ body = """
3335
{% else %}\
3436
{{ commit.message | upper_first }}\
3537
{% endif %}\
36-
{{" "}}([{{ commit.id | truncate(length = 7, end = "") }}]({{ commit.extra.commit_link }}))\
38+
{% if commit.extra.commit_link %} ([{{ commit.id | truncate(length = 7, end = "") }}]({{ commit.extra.commit_link }})){% endif %}\
3739
{% if commit.remote.username and extra.is_release_notes %}\
3840
{{" "}}by @{{ commit.remote.username }}\
3941
{% elif commit.remote.username %}\
@@ -44,6 +46,8 @@ body = """
4446
{%- endif %}\
4547
{% endfor %}
4648
{% endfor %}\n
49+
{% if not version %}<!-- git-cliff-unreleased-end -->
50+
{% endif %}\
4751
"""
4852
# template for the changelog footer
4953
footer = """

git-cliff-release/enhance_context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def load_pr_issues(owner: str, repo: str) -> dict[int, list[int]]:
2323
print(f"fetch_pr_issues.sh output: {output}")
2424
raise
2525

26+
if pr_issues is None:
27+
return {}
28+
2629
return {int(key): value for key, value in pr_issues.items()}
2730

2831

@@ -41,7 +44,7 @@ def enhance_release(
4144

4245

4346
def enhance_commit(commit: dict[str, Any], pr_issues: dict[int, list[int]]) -> None:
44-
pr_number = commit["remote"]["pr_number"]
47+
pr_number = commit.get("remote", {}).get("pr_number")
4548

4649
commit["extra"] = commit["extra"] or {}
4750
commit["extra"]["commit_link"] = f"{repo_url}/commit/{commit['id']}"

0 commit comments

Comments
 (0)