From 9b6487490a5e21f9bb6606e019860cf190ed8819 Mon Sep 17 00:00:00 2001 From: Mickael Stanislas Date: Mon, 19 Aug 2024 21:42:36 +0200 Subject: [PATCH] ci: fix generate changelog --- .github/changelog/changelog.tmpl | 57 ++++++++++++++++++++++++ .github/changelog/generate-changelog.sh | 55 +++++++++++++++++++++++ .github/changelog/release-note.tmpl | 3 ++ .github/workflows/generate_changelog.yml | 43 +++++++++--------- 4 files changed, 138 insertions(+), 20 deletions(-) create mode 100644 .github/changelog/changelog.tmpl create mode 100644 .github/changelog/generate-changelog.sh create mode 100644 .github/changelog/release-note.tmpl diff --git a/.github/changelog/changelog.tmpl b/.github/changelog/changelog.tmpl new file mode 100644 index 0000000..9b23d27 --- /dev/null +++ b/.github/changelog/changelog.tmpl @@ -0,0 +1,57 @@ +{{- if .NotesByType.deprecation -}} +### :warning: **Deprecations** + +{{range .NotesByType.deprecation -}} +* {{ template "note" .}} +{{ end -}} +{{- end -}} + +{{- if index .NotesByType "breaking-change" -}} +### :rotating_light: **Breaking Changes** + +{{range index .NotesByType "breaking-change" -}} +* {{ template "note" .}} +{{ end -}} +{{- end -}} + +{{- $features := combineTypes .NotesByType.feature (index .NotesByType "new-resource" ) (index .NotesByType "new-data-source") -}} +{{- if $features }} +### :rocket: **New Features** + +{{range $features | sort -}} +* {{ template "note" . }} +{{ end -}} +{{- end -}} + +{{- $improvements := combineTypes .NotesByType.improvement .NotesByType.enhancement -}} +{{- if $improvements }} +### :tada: **Improvements** + +{{range $improvements | sort -}} +* {{ template "note" . }} +{{ end -}} +{{- end -}} + +{{- if .NotesByType.bug }} +### :bug: **Bug Fixes** + +{{range .NotesByType.bug -}} +* {{ template "note" . }} +{{ end -}} +{{- end -}} + +{{- if .NotesByType.note -}} +### :information_source: **Notes** + +{{range .NotesByType.note -}} +* {{ template "note" .}} +{{ end -}} +{{- end -}} + +{{- if .NotesByType.dependency }} +### :dependabot: **Dependencies** + +{{range .NotesByType.dependency | sort -}} +* {{ template "note" . }} +{{ end -}} +{{- end -}} diff --git a/.github/changelog/generate-changelog.sh b/.github/changelog/generate-changelog.sh new file mode 100644 index 0000000..610c176 --- /dev/null +++ b/.github/changelog/generate-changelog.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +set -x +set -o errexit +set -o nounset + +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__parent="$(dirname "$__dir")" +__root="$(dirname "$__parent")" + +CHANGELOG_FILE_NAME="CHANGELOG.md" +CHANGELOG_TMP_FILE_NAME="CHANGELOG.tmp" +TARGET_SHA=$(git rev-parse HEAD) +PREVIOUS_RELEASE_TAG=$(git tag -l | sort -V | grep -E 'v[0-9]+.[0-9]+.[0-9]+$' | tail -n 1) +PREVIOUS_RELEASE_SHA=$(git rev-list -n 1 $PREVIOUS_RELEASE_TAG) + +if [ $TARGET_SHA == $PREVIOUS_RELEASE_SHA ]; then + echo "Nothing to do" + exit 0 +fi + +PREVIOUS_CHANGELOG=$(sed -n -e "/# ${PREVIOUS_RELEASE_TAG#v}/,\$p" $__root/$CHANGELOG_FILE_NAME) + +if [ -z "$PREVIOUS_CHANGELOG" ] +then + echo "Unable to locate previous changelog contents." + exit 1 +fi + +CHANGELOG=$($(go env GOPATH)/bin/changelog-build -this-release $TARGET_SHA \ + -last-release $PREVIOUS_RELEASE_SHA \ + -git-dir $__root \ + -entries-dir .changelog \ + -changelog-template $__dir/changelog.tmpl \ + -note-template $__dir/release-note.tmpl) +if [ -z "$CHANGELOG" ] +then + echo "No changelog generated." + exit 0 +fi + +rm -f $CHANGELOG_TMP_FILE_NAME + +sed -n -e "1{/# /p;}" $__root/$CHANGELOG_FILE_NAME > $CHANGELOG_TMP_FILE_NAME +echo "$CHANGELOG" >> $CHANGELOG_TMP_FILE_NAME +echo >> $CHANGELOG_TMP_FILE_NAME +echo "$PREVIOUS_CHANGELOG" >> $CHANGELOG_TMP_FILE_NAME + +cp $CHANGELOG_TMP_FILE_NAME $CHANGELOG_FILE_NAME + +rm $CHANGELOG_TMP_FILE_NAME + +echo "Successfully generated changelog." + +exit 0 \ No newline at end of file diff --git a/.github/changelog/release-note.tmpl b/.github/changelog/release-note.tmpl new file mode 100644 index 0000000..7b7ed9e --- /dev/null +++ b/.github/changelog/release-note.tmpl @@ -0,0 +1,3 @@ +{{- define "note" -}} +{{if eq "new-resource" .Type}}**New Resource:** {{else if eq "new-data-source" .Type}}**New Data Source:** {{ end }}{{.Body}} (GH-{{- .Issue -}}) +{{- end -}} \ No newline at end of file diff --git a/.github/workflows/generate_changelog.yml b/.github/workflows/generate_changelog.yml index 965a776..b13559e 100644 --- a/.github/workflows/generate_changelog.yml +++ b/.github/workflows/generate_changelog.yml @@ -1,33 +1,36 @@ -name: Generate CHANGELOG +name: PR Close on: pull_request: types: [closed] workflow_dispatch: + +concurrency: + group: ${{ github.ref }}-${{ github.head_ref }}-changelog + cancel-in-progress: true + jobs: GenerateChangelog: if: github.event.pull_request.merged || github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest steps: - - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.5.4 + - uses: actions/checkout@v4 with: - fetch-depth: 0 - submodules: true token: ${{ secrets.CHANGELOG_PAT }} + fetch-depth: 0 + submodules: false + ref: main - uses: actions/setup-go@v5 with: - go-version-file: '.ci/tools/go.mod' - - run: go generate -tags tools .ci/tools/tools.go - - run: ./.ci/scripts/generate-changelog.sh - - run: | - if [[ `git status --porcelain` ]]; then - if ${{github.event_name == 'workflow_dispatch'}}; then - MSG="chore: update CHANGELOG.md (Manual Trigger)" - else - MSG="chore: update CHANGELOG.md for #${{ github.event.pull_request.number }}" - fi - git config --local user.email changelogbot@frangipane.io - git config --local user.name changelogbot - git add CHANGELOG.md - git commit -m "$MSG" - git push - fi + go-version-file: 'go.mod' + # * CHANGELOG + - run: go install github.com/hashicorp/go-changelog/cmd/changelog-build@latest + - run: bash .github/changelog/generate-changelog.sh + - uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "chore: Update CHANGELOG.md" + commit_options: '--no-verify --signoff' + push_options: '--force' + file_pattern: CHANGELOG.md + commit_user_name: github-actions[bot] + commit_user_email: github-actions[bot]@users.noreply.github.com + commit_author: Changelog Bot \ No newline at end of file