diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 78ade79..14a634d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -401,7 +401,7 @@ jobs: docker buildx imagetools create --tag "$tag" "${IMAGES[@]}" done - - name: Overwrite GitHub release notes + - name: Update GitHub release notes if: startsWith(github.ref, 'refs/tags/v') && env.REGISTRY == '' uses: actions/github-script@v6 with: @@ -409,40 +409,53 @@ jobs: script: | const tag = context.ref.replace('refs/tags/', ''); - const latestRelease = await github.rest.repos.listReleases({ + const { data: releases } = await github.rest.repos.listReleases({ owner: context.repo.owner, repo: context.repo.repo, }); - const release = latestRelease.data.find(r => r.tag_name === tag); + const release = releases.find(r => r.tag_name === tag); if (!release) { core.setFailed(`Release with tag ${tag} not found`); return; } + const baseTag = releases[1]?.tag_name || ''; + if (!baseTag) { + core.setFailed('Could not determine a base tag (no previous release found)'); + return; + } + const { data: comparison } = await github.rest.repos.compareCommits({ owner: context.repo.owner, repo: context.repo.repo, - base: latestRelease.data[1]?.tag_name || '', + base: baseTag, head: tag, }); - const commits = comparison.commits.filter(c => 1 === c.parents.length); + const commits = comparison.commits.filter(c => c.parents.length === 1); - const changelog = commits.map( - c => `- ${c.sha.substring(0, 7)} ${c.commit.message.split('\n')[0]}` - ).join('\n'); + const changelog = commits + .map(c => `- ${c.sha.substring(0, 7)} ${c.commit.message.split('\n')[0]}`) + .join('\n'); if (!changelog) { core.setFailed('No commits found for the changelog'); return; } + const existingBody = (release.body || '').trim(); + + const separator = `\n\n---\n\n## Commits since ${baseTag}\n\n`; + const newBody = existingBody + ? `${existingBody}${separator}${changelog}` + : `## Commits since ${baseTag}\n\n${changelog}`; + await github.rest.repos.updateRelease({ owner: context.repo.owner, repo: context.repo.repo, release_id: release.id, - body: changelog + body: newBody, }); dockerhub-sync-readme: