Skip to content
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

feat: Allow prepending the generated changelog to an existing one in git-cliff-release #142

Merged
merged 18 commits into from
Oct 25, 2024
Merged
2 changes: 2 additions & 0 deletions git-cliff-release/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This action uses conventional commit history to determine the recommended versio
- **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.
- **custom_version**: Optional unless the `release_type` is set to `custom`.
- **cliff_config_path**: Path to a configuration file for git-cliff. If none is given, a built-in configuration will be used.
- **existing_changelog_path**: Path to an existing changelog. If given, the new changelog contents will be prepended to it intelligently.
- **token**: Github token to be used by github CLI (should be relevant for private repositories only)

## Outputs

Expand Down
58 changes: 45 additions & 13 deletions git-cliff-release/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ inputs:
required: false
type: string
default: cliff.toml
existing_changelog_path:
description: Path to an existing changelog. If given, the new changelog contents will be prepended to it intelligently.
required: false
type: string
default: ""
token:
description: Github token to be used by github CLI
required: false
type: string
default: ${{ github.token }}
outputs:
is_prerelease:
description: For convenience - was the action triggered with release_type = "prerelease"?
Expand Down Expand Up @@ -65,8 +75,9 @@ runs:
GIT_CLIFF_REPOSITORY: ${{ github.workspace }}/__release_metadata_repo
GIT_CLIFF_WORKDIR: ${{ github.action_path }}
GITHUB_REPO: ${{ github.repository }}
GH_TOKEN: ${{ github.token }}
GITHUB_TOKEN: ${{ inputs.token }}
run: |
set -x
if [[ '${{ inputs.release_type }}' = custom ]]; then
version_number=$(echo ${{ inputs.custom_version }} | sed s/^v//)
elif [[ '${{ inputs.release_type }}' = auto || '${{ inputs.release_type }}' = prerelease ]]; then
Expand All @@ -85,9 +96,10 @@ runs:
GIT_CLIFF_REPOSITORY: ${{ github.workspace }}/__release_metadata_repo
GIT_CLIFF_WORKDIR: ${{ github.action_path }}
GITHUB_REPO: ${{ github.repository }}
GH_TOKEN: ${{ github.token }}
GITHUB_TOKEN: ${{ inputs.token }}
working-directory: ${{ github.action_path }}
run: |
set -x
echo 'release_notes<<EOF' >> $GITHUB_OUTPUT
git-cliff --tag "${{ steps.version_number.outputs.tag_name }}" --unreleased --context |
python enhance_context.py --repo $GITHUB_REPO --release-notes |
Expand All @@ -102,22 +114,42 @@ runs:
GIT_CLIFF_REPOSITORY: ${{ github.workspace }}/__release_metadata_repo
GIT_CLIFF_WORKDIR: ${{ github.action_path }}
GITHUB_REPO: ${{ github.repository }}
GH_TOKEN: ${{ github.token }}
GITHUB_TOKEN: ${{ inputs.token }}
working-directory: ${{ github.action_path }}
run: |
set -x
dump_context_args=()
enhance_context_args=()
print_changelog_args=()

out_file=$GIT_CLIFF_WORKDIR/new_changelog.md

if [[ -n "${{ inputs.existing_changelog_path }}" ]]; then
out_file=$GIT_CLIFF_WORKDIR/existing_changelog.md
cp "${{ github.workspace }}/${{ inputs.existing_changelog_path }}" $out_file
sed '/<!-- git-cliff-unreleased-start -->/,/<!-- git-cliff-unreleased-end -->/d' -i $out_file

dump_context_args+=(--unreleased)
print_changelog_args+=(--prepend "$out_file" --unreleased)
else
print_changelog_args+=(--output "$out_file")
fi

if [[ ${{ inputs.release_type }} = prerelease ]]; then
echo 'changelog<<EOF' >> $GITHUB_OUTPUT
git-cliff --context |
python enhance_context.py --repo $GITHUB_REPO --unreleased-version "${{ steps.version_number.outputs.tag_name }}" |
git-cliff --from-context - >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
enhance_context_args+=(--unreleased-version "${{ steps.version_number.outputs.tag_name }}")
else
echo 'changelog<<EOF' >> $GITHUB_OUTPUT
git-cliff --tag "${{ steps.version_number.outputs.tag_name }}" --context |
python enhance_context.py --repo $GITHUB_REPO |
git-cliff --from-context - >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
dump_context_args+=(--tag "${{ steps.version_number.outputs.tag_name }}")
fi

git-cliff --context "${dump_context_args[@]}" |
python enhance_context.py --repo $GITHUB_REPO "${enhance_context_args[@]}" |
git-cliff --from-context - "${print_changelog_args[@]}"

sed -i '$a\' $out_file # Make sure there is a newline at the end of the output file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work? Considering the ' ends up escaped

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what escaping you have in mind, but experiment results suggest that it works. Well, either it works or the whole "unreleased" comment thing is not necessary.


echo 'changelog<<EOF' >> $GITHUB_OUTPUT
cat $out_file >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
- name: Clean up
shell: bash
env:
Expand Down
6 changes: 5 additions & 1 deletion git-cliff-release/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ All notable changes to this project will be documented in this file.\n
# template for the changelog body
# https://keats.github.io/tera/docs/#introduction
body = """
{% if not version %}<!-- git-cliff-unreleased-start -->
{% endif %}\
{% if version and extra.release_link %}\
## [{{ version | trim_start_matches(pat="v") }}]({{ extra.release_link }}) ({{ timestamp | date(format="%Y-%m-%d") }})
{% elif extra.unreleased_version %}\
Expand All @@ -33,7 +35,7 @@ body = """
{% else %}\
{{ commit.message | upper_first }}\
{% endif %}\
{{" "}}([{{ commit.id | truncate(length = 7, end = "") }}]({{ commit.extra.commit_link }}))\
{% if commit.extra.commit_link %} ([{{ commit.id | truncate(length = 7, end = "") }}]({{ commit.extra.commit_link }})){% endif %}\
{% if commit.remote.username and extra.is_release_notes %}\
{{" "}}by @{{ commit.remote.username }}\
{% elif commit.remote.username %}\
Expand All @@ -44,6 +46,8 @@ body = """
{%- endif %}\
{% endfor %}
{% endfor %}\n
{% if not version %}<!-- git-cliff-unreleased-end -->
{% endif %}\
"""
# template for the changelog footer
footer = """
Expand Down
5 changes: 4 additions & 1 deletion git-cliff-release/enhance_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def load_pr_issues(owner: str, repo: str) -> dict[int, list[int]]:
print(f"fetch_pr_issues.sh output: {output}")
raise

if pr_issues is None:
return {}

return {int(key): value for key, value in pr_issues.items()}


Expand All @@ -41,7 +44,7 @@ def enhance_release(


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

commit["extra"] = commit["extra"] or {}
commit["extra"]["commit_link"] = f"{repo_url}/commit/{commit['id']}"
Expand Down