From e65536de1cce68d7dbb53fd7734da825fe0d62f1 Mon Sep 17 00:00:00 2001 From: Zach Date: Fri, 17 Apr 2026 19:21:32 -0400 Subject: [PATCH] feat: add discord automated release notifications (#564) --- .../actions/compute-release-notes/action.yml | 139 +++++++++ .../notify-discord-release-notes.yml | 191 ++++++++++++ .github/workflows/publish-nightly.yml | 129 ++++++-- .github/workflows/release-candidate.yml | 113 +++++++ .github/workflows/release-main.yml | 65 +++++ tools/release/Justfile | 4 + tools/release/package.json | 5 +- tools/release/release-policy.cjs | 21 ++ tools/release/release.config.cjs | 48 +-- .../release/scripts/generate-range-notes.mjs | 276 ++++++++++++++++++ tools/release/yarn.lock | 6 +- 11 files changed, 938 insertions(+), 59 deletions(-) create mode 100644 .github/actions/compute-release-notes/action.yml create mode 100644 .github/workflows/notify-discord-release-notes.yml create mode 100644 .github/workflows/release-candidate.yml create mode 100644 tools/release/release-policy.cjs create mode 100644 tools/release/scripts/generate-range-notes.mjs diff --git a/.github/actions/compute-release-notes/action.yml b/.github/actions/compute-release-notes/action.yml new file mode 100644 index 000000000..6449870ef --- /dev/null +++ b/.github/actions/compute-release-notes/action.yml @@ -0,0 +1,139 @@ +name: Compute Release Notes +description: Build conventional-commit release notes for an explicit git range. + +inputs: + base_tag: + description: Stable tag that anchors the preview version calculation + required: false + default: '' + from_ref: + description: Git ref that marks the start of the changelog range + required: true + repository_url: + description: Repository URL used for compare and commit links + required: false + default: '' + to_ref: + description: Git ref that marks the end of the changelog range + required: true + +outputs: + compare_url: + description: Compare URL for the computed range + value: ${{ steps.extract.outputs.compare_url }} + details_url: + description: Workflow run URL that contains the full markdown summary + value: ${{ steps.extract.outputs.details_url }} + predicted_version: + description: Predicted semantic version for the supplied range + value: ${{ steps.extract.outputs.predicted_version }} + release_notes: + description: Markdown notes generated from conventional commits + value: ${{ steps.extract.outputs.release_notes }} + release_type: + description: Highest semantic release type found in the range + value: ${{ steps.extract.outputs.release_type }} + released: + description: Whether the range contains any releasable commits + value: ${{ steps.extract.outputs.released }} + to_sha: + description: Resolved ending SHA for the range + value: ${{ steps.extract.outputs.to_sha }} + +runs: + using: composite + steps: + - name: Checkout Repository + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + with: + fetch-depth: 0 + ref: ${{ inputs.to_ref }} + + - name: Install just + uses: taiki-e/install-action@a1df9120380005ebdc788c62449b841b205541dd # v2.71.0 + with: + tool: just + + - name: Set Up Node.js + uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6 + with: + node-version: '24' + + - name: Enable Corepack + shell: bash + run: | + corepack enable + corepack yarn --version + + - name: Install Release Tooling Dependencies + shell: bash + run: just release install + + - name: Generate Range Notes + shell: bash + env: + BASE_TAG: ${{ inputs.base_tag }} + FROM_REF: ${{ inputs.from_ref }} + OUTPUT_JSON: range-release.json + OUTPUT_NOTES: range-release-notes.md + REPOSITORY_URL: ${{ inputs.repository_url != '' && inputs.repository_url || format('{0}/{1}', github.server_url, github.repository) }} + TO_REF: ${{ inputs.to_ref }} + run: | + just release range-notes | tee tools/release/range-release.log + + - name: Extract Range Outputs + id: extract + shell: bash + env: + BASE_TAG: ${{ inputs.base_tag }} + DETAILS_URL: ${{ format('{0}/{1}/actions/runs/{2}', github.server_url, github.repository, github.run_id) }} + FROM_REF: ${{ inputs.from_ref }} + TO_REF: ${{ inputs.to_ref }} + run: | + set -eu + + compare_url="$(jq -r '.compareUrl // empty' tools/release/range-release.json)" + predicted_version="$(jq -r '.nextVersion // empty' tools/release/range-release.json)" + release_type="$(jq -r '.releaseType // empty' tools/release/range-release.json)" + released="$(jq -r '.released' tools/release/range-release.json)" + to_sha="$(jq -r '.toSha // empty' tools/release/range-release.json)" + release_notes_delim="EOF_$(openssl rand -hex 16)" + + { + echo "compare_url=${compare_url}" + echo "details_url=${DETAILS_URL}" + echo "predicted_version=${predicted_version}" + echo "release_type=${release_type}" + echo "released=${released}" + echo "to_sha=${to_sha}" + echo "release_notes<<${release_notes_delim}" + cat tools/release/range-release-notes.md + printf '\n' + echo "${release_notes_delim}" + } >> "$GITHUB_OUTPUT" + + { + echo "## Full Changelog" + echo + echo "- Range: \`${FROM_REF}..${TO_REF}\`" + echo "- Predicted version: \`${predicted_version:-none}\`" + echo "- Release type: \`${release_type:-none}\`" + if [ -n "${BASE_TAG}" ]; then + echo "- Base stable tag: \`${BASE_TAG}\`" + fi + if [ -n "$compare_url" ]; then + echo "- Compare: ${compare_url}" + fi + echo + cat tools/release/range-release-notes.md + } >> "$GITHUB_STEP_SUMMARY" + + - name: Upload Range Notes Artifact + uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0 + with: + name: range-release-notes + path: | + tools/release/range-release.json + tools/release/range-release-notes.md + tools/release/range-release.log + retention-days: 14 diff --git a/.github/workflows/notify-discord-release-notes.yml b/.github/workflows/notify-discord-release-notes.yml new file mode 100644 index 000000000..c9c59d6f2 --- /dev/null +++ b/.github/workflows/notify-discord-release-notes.yml @@ -0,0 +1,191 @@ +name: Release - Notify Discord + +on: + workflow_call: + inputs: + channel: + description: 'Target message channel context (for template selection)' + required: true + type: string + candidate_sha: + description: 'Resolved commit SHA for the candidate build' + required: true + type: string + base_tag: + description: 'Stable base tag that the candidate is built on top of' + required: false + default: '' + type: string + mention_text: + description: 'Optional role mentions or intro text to prepend to the message' + required: false + default: '' + type: string + current_version: + description: 'Current release/build identifier to display in the embed' + required: true + type: string + predicted_version: + description: 'Predicted next semantic version (without v-prefix)' + required: false + default: '' + type: string + compare_url: + description: 'GitHub compare URL between base stable tag and candidate SHA' + required: false + default: '' + type: string + details_url: + description: 'Canonical details link for the full markdown notes' + required: false + default: '' + type: string + image_tag: + description: 'Container image tag for this build' + required: false + default: '' + type: string + secrets: + discord_webhook: + description: 'Discord webhook URL for release notifications' + required: true + +jobs: + notify: + name: Notify Discord + runs-on: ubuntu-latest + permissions: {} + + steps: + - name: Compose Message + id: compose + env: + CHANNEL: ${{ inputs.channel }} + CANDIDATE_SHA: ${{ inputs.candidate_sha }} + COMPARE_URL: ${{ inputs.compare_url }} + CURRENT_VERSION: ${{ inputs.current_version }} + DETAILS_URL: ${{ inputs.details_url }} + MENTION_TEXT: ${{ inputs.mention_text }} + PREDICTED_VERSION: ${{ inputs.predicted_version }} + run: | + set -eu + + short_sha="$(printf '%s' "$CANDIDATE_SHA" | cut -c1-7)" + if [ "$CHANNEL" = "rc" ]; then + header="๐Ÿงช **A new release candidate has been announced**" + elif [ "$CHANNEL" = "stable" ]; then + header="๐Ÿš€ **A new release is now available**" + else + header="๐ŸŒ™ **A new nightly build is now available**" + fi + + if [ -n "$MENTION_TEXT" ]; then + content="${header} ${MENTION_TEXT}" + else + content="${header}" + fi + + if [ -n "$DETAILS_URL" ]; then + title_url="$DETAILS_URL" + elif [ -n "$COMPARE_URL" ]; then + title_url="$COMPARE_URL" + else + title_url="" + fi + + description="- Current Version: \`${CURRENT_VERSION}\`" + if [ "$CHANNEL" != "stable" ]; then + if [ -n "$PREDICTED_VERSION" ] && [ "$PREDICTED_VERSION" != "no-release" ]; then + predicted_release="v${PREDICTED_VERSION}" + else + predicted_release="no predicted release" + fi + description="${description}\n- Predicted Release: \`${predicted_release}\`" + fi + + if [ -n "$COMPARE_URL" ]; then + description="${description}\n- Candidate SHA: \`${short_sha}\` ([compare](${COMPARE_URL}))" + else + description="${description}\n- Candidate SHA: \`${short_sha}\`" + fi + + { + echo 'content<> "$GITHUB_OUTPUT" + + - name: Send Discord Webhook + env: + DISCORD_WEBHOOK: ${{ secrets.discord_webhook }} + CONTENT: ${{ steps.compose.outputs.content }} + DESCRIPTION: ${{ steps.compose.outputs.description }} + TITLE_URL: ${{ steps.compose.outputs.title_url }} + run: | + set -eu + + payload="$(jq -n \ + --arg content "$CONTENT" \ + --arg description "$DESCRIPTION" \ + --arg title_url "$TITLE_URL" \ + ' + { + content: $content, + embeds: [ + { + title: "Build Details", + description: $description, + color: 5763719 + } + ] + } + + ( + if $title_url != "" then + { + components: [ + { + type: 1, + components: [ + { + type: 2, + style: 5, + label: "View Changelog", + url: $title_url + }, + { + type: 2, + style: 5, + label: "Report Issue", + url: "https://github.com/orgs/grimmory-tools/discussions/new?category=issue-triage" + } + ] + } + ] + } + else + { + components: [ + { + type: 1, + components: [ + { + type: 2, + style: 5, + label: "Report Issue", + url: "https://github.com/orgs/grimmory-tools/discussions/new?category=issue-triage" + } + ] + } + ] + } + end + ) + ')" + + curl --fail-with-body -sS -X POST "${DISCORD_WEBHOOK}?with_components=true" \ + -H 'Content-Type: application/json' \ + -d "$payload" diff --git a/.github/workflows/publish-nightly.yml b/.github/workflows/publish-nightly.yml index 95c47efcb..40757d4f6 100644 --- a/.github/workflows/publish-nightly.yml +++ b/.github/workflows/publish-nightly.yml @@ -4,40 +4,72 @@ on: schedule: - cron: '0 4 * * *' workflow_dispatch: + inputs: + publish_image: + description: 'Publish the nightly container image' + required: false + default: false + type: boolean jobs: resolve_ref: name: Resolve Nightly Ref runs-on: ubuntu-latest + permissions: + actions: read + contents: read outputs: + base_tag: ${{ steps.resolve.outputs.base_tag }} commit_sha: ${{ steps.resolve.outputs.commit_sha }} + from_ref: ${{ steps.resolve.outputs.from_ref }} + nightly_tag: ${{ steps.resolve.outputs.nightly_tag }} steps: - name: Checkout Repository uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 with: - fetch-depth: 1 + fetch-depth: 0 ref: develop - name: Record Resolved Commit id: resolve + env: + GH_TOKEN: ${{ github.token }} run: | - echo "commit_sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" + set -eu - test-suite: - name: Test Suite - needs: [resolve_ref] - uses: ./.github/workflows/test-suite.yml - permissions: - contents: read - checks: write - with: - checkout_ref: ${{ needs.resolve_ref.outputs.commit_sha }} - save_gradle_cache: true + git fetch origin main --tags --force + short_sha="$(git rev-parse --short HEAD)" + build_date="$(date -u +%Y%m%d)" + nightly_tag="nightly-${build_date}-${short_sha}" + base_tag="$(git describe --tags --abbrev=0 --match 'v*' origin/main)" + from_ref="$base_tag" + previous_nightly_sha="" + if previous_nightly_json="$( + gh api "/repos/${GITHUB_REPOSITORY}/actions/workflows/publish-nightly.yml/runs?branch=develop&event=schedule&status=success&per_page=1" + )"; then + previous_nightly_sha="$(printf '%s' "$previous_nightly_json" | jq -r '.workflow_runs[0].head_sha // ""')" + else + echo "::warning::Unable to resolve previous nightly run; using ${base_tag}" + fi + + if [ -n "$previous_nightly_sha" ] \ + && git rev-parse --verify --quiet "${previous_nightly_sha}^{commit}" >/dev/null \ + && git merge-base --is-ancestor "$previous_nightly_sha" HEAD; then + from_ref="$previous_nightly_sha" + elif [ -n "$previous_nightly_sha" ]; then + echo "::warning::Ignoring unusable nightly baseline ${previous_nightly_sha}; using ${base_tag}" + fi + + echo "base_tag=${base_tag}" >> "$GITHUB_OUTPUT" + echo "commit_sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" + echo "from_ref=${from_ref}" >> "$GITHUB_OUTPUT" + echo "nightly_tag=${nightly_tag}" >> "$GITHUB_OUTPUT" build-and-push: name: Build and Push Nightly Image + if: ${{ github.event_name == 'schedule' || inputs.publish_image }} needs: [resolve_ref, test-suite] runs-on: ubuntu-latest @@ -45,6 +77,9 @@ jobs: contents: read packages: write + outputs: + image_tag: ${{ steps.record.outputs.image_tag }} + steps: - name: Checkout Repository uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 @@ -52,14 +87,13 @@ jobs: fetch-depth: 1 ref: ${{ needs.resolve_ref.outputs.commit_sha }} - - name: Generate Nightly Tag + - name: Record Nightly Build Metadata + id: record run: | - short_sha=$(git rev-parse --short HEAD) - build_date=$(date -u +%Y%m%d) - nightly_tag="nightly-${build_date}-${short_sha}" - echo "nightly_tag=$nightly_tag" >> "$GITHUB_ENV" + echo "image_tag=${{ needs.resolve_ref.outputs.nightly_tag }}" >> "$GITHUB_OUTPUT" + echo "nightly_tag=${{ needs.resolve_ref.outputs.nightly_tag }}" >> "$GITHUB_ENV" echo "commit_sha=$(git rev-parse HEAD)" >> "$GITHUB_ENV" - echo "Nightly tag: $nightly_tag" + echo "Nightly tag: ${{ needs.resolve_ref.outputs.nightly_tag }}" - name: Set Up QEMU for Multi-Arch Builds uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0 @@ -103,3 +137,62 @@ jobs: cache-to: | type=gha,scope=image-shared,mode=max type=registry,ref=ghcr.io/grimmory-tools/grimmory:buildcache,mode=max + + generate-notes: + name: Nightly Changelog + if: ${{ always() && needs.test-suite.result == 'success' }} + needs: [resolve_ref, test-suite] + runs-on: ubuntu-latest + permissions: + contents: read + outputs: + compare_url: ${{ steps.notes.outputs.compare_url }} + details_url: ${{ steps.notes.outputs.details_url }} + predicted_version: ${{ steps.notes.outputs.predicted_version }} + release_notes: ${{ steps.notes.outputs.release_notes }} + release_type: ${{ steps.notes.outputs.release_type }} + released: ${{ steps.notes.outputs.released }} + to_sha: ${{ steps.notes.outputs.to_sha }} + steps: + - name: Checkout Workflow Repository + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + with: + fetch-depth: 1 + + - name: Build Full Changelog + id: notes + uses: ./.github/actions/compute-release-notes + with: + base_tag: ${{ needs.resolve_ref.outputs.base_tag }} + from_ref: ${{ needs.resolve_ref.outputs.from_ref }} + repository_url: ${{ format('{0}/{1}', github.server_url, github.repository) }} + to_ref: ${{ needs.resolve_ref.outputs.commit_sha }} + + notify-discord: + name: Notify Discord (Nightly) + if: ${{ always() && needs.generate-notes.result == 'success' && (needs.build-and-push.result == 'success' || needs.build-and-push.result == 'skipped') }} + needs: [resolve_ref, build-and-push, generate-notes] + permissions: {} + uses: ./.github/workflows/notify-discord-release-notes.yml + secrets: + discord_webhook: ${{ secrets.DISCORD_NIGHTLY_WEBHOOK != '' && secrets.DISCORD_NIGHTLY_WEBHOOK || secrets.DISCORD_RELEASE_WEBHOOK }} + with: + channel: nightly + candidate_sha: ${{ needs.resolve_ref.outputs.commit_sha }} + base_tag: ${{ needs.resolve_ref.outputs.base_tag }} + current_version: ${{ needs.resolve_ref.outputs.nightly_tag }} + details_url: ${{ needs.generate-notes.outputs.details_url }} + compare_url: ${{ needs.generate-notes.outputs.compare_url }} + mention_text: ${{ vars.DISCORD_NIGHTLY_MENTIONS }} + predicted_version: ${{ needs.generate-notes.outputs.predicted_version }} + + test-suite: + name: Test Suite + needs: [resolve_ref] + uses: ./.github/workflows/test-suite.yml + permissions: + contents: read + checks: write + with: + checkout_ref: ${{ needs.resolve_ref.outputs.commit_sha }} + save_gradle_cache: true diff --git a/.github/workflows/release-candidate.yml b/.github/workflows/release-candidate.yml new file mode 100644 index 000000000..e7c83e594 --- /dev/null +++ b/.github/workflows/release-candidate.yml @@ -0,0 +1,113 @@ +name: Release - Candidate + +on: + workflow_dispatch: + inputs: + candidate_ref: + description: 'Branch, tag, or commit SHA to build as a release candidate' + required: false + default: develop + type: string + +concurrency: + group: release-candidate + cancel-in-progress: false + +jobs: + resolve_ref: + name: Resolve Candidate Ref + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + + outputs: + candidate_sha: ${{ steps.resolve.outputs.candidate_sha }} + base_tag: ${{ steps.resolve.outputs.base_tag }} + current_version: ${{ steps.resolve.outputs.current_version }} + + steps: + - name: Checkout Repository + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + with: + fetch-depth: 0 + ref: ${{ inputs.candidate_ref }} + + - name: Resolve Candidate Metadata + id: resolve + env: + GH_TOKEN: ${{ github.token }} + run: | + set -eu + + git fetch origin main --tags --force + + if ! git merge-base --is-ancestor origin/main HEAD; then + echo "Selected ref does not contain origin/main and cannot be promoted cleanly to main" >&2 + exit 1 + fi + + candidate_sha="$(git rev-parse HEAD)" + base_tag="$(git describe --tags --abbrev=0 --match 'v*' origin/main)" + latest_nightly_json="$( + gh api "/repos/${GITHUB_REPOSITORY}/actions/workflows/publish-nightly.yml/runs?branch=develop&status=success&per_page=1" + )" + latest_nightly_sha="$(printf '%s' "$latest_nightly_json" | jq -r '.workflow_runs[0].head_sha // ""')" + latest_nightly_created_at="$(printf '%s' "$latest_nightly_json" | jq -r '.workflow_runs[0].created_at // ""')" + current_version="$base_tag" + if [ -n "$latest_nightly_sha" ] && [ -n "$latest_nightly_created_at" ]; then + latest_nightly_date="$(date -u -d "$latest_nightly_created_at" +%Y%m%d)" + latest_nightly_short_sha="$(printf '%s' "$latest_nightly_sha" | cut -c1-7)" + current_version="nightly-${latest_nightly_date}-${latest_nightly_short_sha}" + fi + + echo "candidate_sha=${candidate_sha}" >> "$GITHUB_OUTPUT" + echo "base_tag=${base_tag}" >> "$GITHUB_OUTPUT" + echo "current_version=${current_version}" >> "$GITHUB_OUTPUT" + + generate-notes: + name: Release Candidate Changelog + needs: [resolve_ref] + runs-on: ubuntu-latest + permissions: + contents: read + outputs: + compare_url: ${{ steps.notes.outputs.compare_url }} + details_url: ${{ steps.notes.outputs.details_url }} + predicted_version: ${{ steps.notes.outputs.predicted_version }} + release_notes: ${{ steps.notes.outputs.release_notes }} + release_type: ${{ steps.notes.outputs.release_type }} + released: ${{ steps.notes.outputs.released }} + to_sha: ${{ steps.notes.outputs.to_sha }} + steps: + - name: Checkout Workflow Repository + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + with: + fetch-depth: 1 + + - name: Build Full Changelog + id: notes + uses: ./.github/actions/compute-release-notes + with: + base_tag: ${{ needs.resolve_ref.outputs.base_tag }} + from_ref: ${{ needs.resolve_ref.outputs.base_tag }} + repository_url: ${{ format('{0}/{1}', github.server_url, github.repository) }} + to_ref: ${{ needs.resolve_ref.outputs.candidate_sha }} + + notify-discord: + name: Notify Discord (RC) + if: ${{ always() && needs.generate-notes.result == 'success' }} + needs: [resolve_ref, generate-notes] + permissions: {} + uses: ./.github/workflows/notify-discord-release-notes.yml + secrets: + discord_webhook: ${{ secrets.DISCORD_RC_WEBHOOK != '' && secrets.DISCORD_RC_WEBHOOK || secrets.DISCORD_RELEASE_WEBHOOK }} + with: + channel: rc + candidate_sha: ${{ needs.resolve_ref.outputs.candidate_sha }} + base_tag: ${{ needs.resolve_ref.outputs.base_tag }} + current_version: ${{ needs.resolve_ref.outputs.current_version }} + details_url: ${{ needs.generate-notes.outputs.details_url }} + predicted_version: ${{ needs.generate-notes.outputs.predicted_version }} + compare_url: ${{ needs.generate-notes.outputs.compare_url }} + mention_text: ${{ vars.DISCORD_RC_MENTIONS }} diff --git a/.github/workflows/release-main.yml b/.github/workflows/release-main.yml index 7e586285c..b907c69ef 100644 --- a/.github/workflows/release-main.yml +++ b/.github/workflows/release-main.yml @@ -131,6 +131,71 @@ jobs: release_tag: ${{ needs.semantic-release.outputs.release_tag }} commit_sha: ${{ needs.semantic-release.outputs.release_sha }} + prepare-release-notification: + name: Prepare Stable Release Notification + if: ${{ needs.semantic-release.outputs.released == 'true' && needs.publish-release.result == 'success' }} + needs: [semantic-release, publish-release] + runs-on: ubuntu-latest + + outputs: + base_tag: ${{ steps.metadata.outputs.base_tag }} + compare_url: ${{ steps.metadata.outputs.compare_url }} + release_url: ${{ steps.metadata.outputs.release_url }} + release_version: ${{ steps.metadata.outputs.release_version }} + + permissions: + contents: read + + steps: + - name: Checkout Repository + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + with: + fetch-depth: 0 + ref: ${{ needs.semantic-release.outputs.release_sha }} + + - name: Extract Stable Release Metadata + id: metadata + env: + GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_TOKEN != '' && secrets.RELEASE_BOT_TOKEN || github.token }} + RELEASE_TAG: ${{ needs.semantic-release.outputs.release_tag }} + run: | + set -eu + + git fetch origin main --tags --force + + release_json="$(gh release view "$RELEASE_TAG" --json body,url)" + release_version="${RELEASE_TAG#v}" + base_tag="$(git tag --sort=-version:refname 'v*' | grep -Fxv "$RELEASE_TAG" | head -n 1 || true)" + compare_url="" + release_url="$(printf '%s' "$release_json" | jq -r '.url')" + if [ -n "$base_tag" ]; then + compare_url="https://github.com/${GITHUB_REPOSITORY}/compare/${base_tag}...${RELEASE_TAG}" + fi + { + echo "base_tag=${base_tag}" + echo "compare_url=${compare_url}" + echo "release_url=${release_url}" + echo "release_version=${release_version}" + } >> "$GITHUB_OUTPUT" + + notify-discord: + name: Notify Discord (Stable) + if: ${{ needs.semantic-release.outputs.released == 'true' && needs.publish-release.result == 'success' && needs.prepare-release-notification.result == 'success' }} + needs: [semantic-release, publish-release, prepare-release-notification] + permissions: {} + uses: ./.github/workflows/notify-discord-release-notes.yml + secrets: + discord_webhook: ${{ secrets.DISCORD_STABLE_WEBHOOK != '' && secrets.DISCORD_STABLE_WEBHOOK || secrets.DISCORD_RELEASE_WEBHOOK }} + with: + channel: stable + candidate_sha: ${{ needs.semantic-release.outputs.release_sha }} + base_tag: ${{ needs.prepare-release-notification.outputs.base_tag }} + current_version: ${{ needs.semantic-release.outputs.release_tag }} + details_url: ${{ needs.prepare-release-notification.outputs.release_url }} + compare_url: ${{ needs.prepare-release-notification.outputs.compare_url }} + mention_text: ${{ vars.DISCORD_STABLE_MENTIONS }} + predicted_version: ${{ needs.prepare-release-notification.outputs.release_version }} + backport-release: name: Backport Release Commit to Develop if: ${{ needs.semantic-release.outputs.released == 'true' && needs.publish-release.result == 'success' }} diff --git a/tools/release/Justfile b/tools/release/Justfile index 5f530629e..d8ca06a84 100644 --- a/tools/release/Justfile +++ b/tools/release/Justfile @@ -15,6 +15,10 @@ install: preview: {{ yarn }} release:dry-run +# Generate release notes for an explicit commit range. +range-notes: + {{ yarn }} release:range-notes + # Run semantic-release for the current checkout. run: {{ yarn }} release diff --git a/tools/release/package.json b/tools/release/package.json index 649df1d06..6879e8d05 100644 --- a/tools/release/package.json +++ b/tools/release/package.json @@ -8,11 +8,14 @@ }, "scripts": { "release": "semantic-release", - "release:dry-run": "semantic-release --dry-run" + "release:dry-run": "semantic-release --dry-run", + "release:range-notes": "node ./scripts/generate-range-notes.mjs" }, "devDependencies": { "@semantic-release/changelog": "6.0.3", + "@semantic-release/commit-analyzer": "13.0.1", "@semantic-release/git": "10.0.1", + "@semantic-release/release-notes-generator": "14.1.0", "conventional-changelog-conventionalcommits": "9.3.1", "semantic-release": "25.0.3" } diff --git a/tools/release/release-policy.cjs b/tools/release/release-policy.cjs new file mode 100644 index 000000000..1ba0d1676 --- /dev/null +++ b/tools/release/release-policy.cjs @@ -0,0 +1,21 @@ +const types = [ + { type: "feat", section: "Features" }, + { type: "fix", section: "Bug Fixes" }, + { type: "perf", section: "Performance" }, + { type: "refactor", section: "Refactors", release: "patch" }, + { type: "chore", section: "Chores", release: false }, + { type: "docs", section: "Documentation", release: false }, + { type: "ci", section: "CI", release: false }, + { type: "build", section: "Build", release: false }, + { type: "test", section: "Tests", release: false }, + { type: "style", section: "Style", release: false }, + { type: "revert", section: "Reverts", release: "patch" } +]; + +const noteKeywords = ["BREAKING CHANGE", "BREAKING CHANGES", "BREAKING"]; + +module.exports = { + noteKeywords, + types, + writerSortFields: ["scope", "subject"] +}; diff --git a/tools/release/release.config.cjs b/tools/release/release.config.cjs index 52459a324..f7d6ee535 100644 --- a/tools/release/release.config.cjs +++ b/tools/release/release.config.cjs @@ -1,3 +1,5 @@ +const releasePolicy = require("./release-policy.cjs"); + module.exports = { repositoryUrl: "https://github.com/grimmory-tools/grimmory.git", branches: ["main"], @@ -8,32 +10,14 @@ module.exports = { { preset: "conventionalcommits", presetConfig: { - types: [ - { type: "feat", section: "Features", hidden: false }, - { type: "fix", section: "Bug Fixes", hidden: false }, - { type: "perf", section: "Performance", hidden: false }, - { type: "refactor", section: "Refactors", hidden: false }, - { type: "chore", section: "Chores", hidden: false }, - { type: "docs", section: "Documentation", hidden: false }, - { type: "ci", section: "CI", hidden: false }, - { type: "build", section: "Build", hidden: false }, - { type: "test", section: "Tests", hidden: false }, - { type: "style", section: "Style", hidden: false }, - { type: "revert", section: "Reverts", hidden: false } - ] + types: releasePolicy.types.map(({ section, type }) => ({ hidden: false, section, type })) }, parserOpts: { - noteKeywords: ["BREAKING CHANGE", "BREAKING CHANGES", "BREAKING"] + noteKeywords: releasePolicy.noteKeywords }, - releaseRules: [ - { type: "refactor", release: "patch" }, - { type: "docs", release: false }, - { type: "ci", release: false }, - { type: "build", release: false }, - { type: "chore", release: false }, - { type: "test", release: false }, - { type: "style", release: false } - ] + releaseRules: releasePolicy.types + .filter((entry) => Object.prototype.hasOwnProperty.call(entry, "release")) + .map(({ release, type }) => ({ release, type })) } ], [ @@ -41,25 +25,13 @@ module.exports = { { preset: "conventionalcommits", presetConfig: { - types: [ - { type: "feat", section: "Features", hidden: false }, - { type: "fix", section: "Bug Fixes", hidden: false }, - { type: "perf", section: "Performance", hidden: false }, - { type: "refactor", section: "Refactors", hidden: false }, - { type: "chore", section: "Chores", hidden: false }, - { type: "docs", section: "Documentation", hidden: false }, - { type: "ci", section: "CI", hidden: false }, - { type: "build", section: "Build", hidden: false }, - { type: "test", section: "Tests", hidden: false }, - { type: "style", section: "Style", hidden: false }, - { type: "revert", section: "Reverts", hidden: false } - ] + types: releasePolicy.types.map(({ section, type }) => ({ hidden: false, section, type })) }, parserOpts: { - noteKeywords: ["BREAKING CHANGE", "BREAKING CHANGES", "BREAKING"] + noteKeywords: releasePolicy.noteKeywords }, writerOpts: { - commitsSort: ["scope", "subject"] + commitsSort: releasePolicy.writerSortFields } } ], diff --git a/tools/release/scripts/generate-range-notes.mjs b/tools/release/scripts/generate-range-notes.mjs new file mode 100644 index 000000000..d5e71e648 --- /dev/null +++ b/tools/release/scripts/generate-range-notes.mjs @@ -0,0 +1,276 @@ +import { execFileSync } from "node:child_process"; +import { writeFileSync } from "node:fs"; +import releasePolicy from "../release-policy.cjs"; + +const repoRoot = execFileSync("git", ["rev-parse", "--show-toplevel"], { encoding: "utf8" }).trim(); +const fromRef = requiredEnv("FROM_REF"); +const toRef = process.env.TO_REF || "HEAD"; +const outputJsonPath = process.env.OUTPUT_JSON || "range-release.json"; +const outputNotesPath = process.env.OUTPUT_NOTES || "range-release-notes.md"; +const baseTag = process.env.BASE_TAG || ""; +const baseVersion = coerceVersion(process.env.BASE_VERSION || stripVersion(baseTag) || stripVersion(fromRef)); +const repositoryUrl = normalizeRepositoryUrl(process.env.REPOSITORY_URL || defaultRepositoryUrl()); +const compareBase = process.env.COMPARE_FROM_REF || fromRef; +const compareHead = process.env.COMPARE_TO_REF || toRef; + +const policyByType = new Map(releasePolicy.types.map((entry) => [entry.type, entry])); +const sections = releasePolicy.types.map((entry) => entry.section); + +const fromSha = runGit(["rev-parse", fromRef]).trim(); +const toSha = runGit(["rev-parse", toRef]).trim(); +const commits = parseCommits(listCommits(fromRef, toRef)); +const groupedCommits = new Map(); + +for (const section of sections) { + groupedCommits.set(section, []); +} +groupedCommits.set("Other Changes", []); + +let releaseType = "none"; +const breakingChanges = []; + +for (const commit of commits) { + groupedCommits.get(commit.section).push(commit); + releaseType = maxReleaseType(releaseType, commit.releaseType); + for (const note of commit.breakingNotes) { + breakingChanges.push({ hash: commit.hash, note }); + } +} + +const predictedVersion = releaseType !== "none" ? incrementVersion(baseVersion, releaseType) : ""; +const compareUrl = repositoryUrl ? `${repositoryUrl}/compare/${compareBase}...${compareHead}` : ""; +const releaseNotes = renderReleaseNotes(groupedCommits, breakingChanges, repositoryUrl); + +const payload = { + baseTag, + baseVersion, + compareUrl, + fromRef, + fromSha, + nextGitTag: predictedVersion ? `v${predictedVersion}` : "", + nextVersion: predictedVersion, + releaseNotes, + releaseType, + released: releaseType !== "none", + toRef, + toSha +}; + +writeFileSync(outputJsonPath, `${JSON.stringify(payload, null, 2)}\n`); +writeFileSync(outputNotesPath, releaseNotes ? `${releaseNotes}\n` : ""); + +function runGit(args) { + return execFileSync("git", args, { cwd: repoRoot, encoding: "utf8" }); +} + +function requiredEnv(name) { + const value = process.env[name]; + if (!value) { + throw new Error(`Missing required environment variable: ${name}`); + } + + return value; +} + +function defaultRepositoryUrl() { + if (process.env.GITHUB_SERVER_URL && process.env.GITHUB_REPOSITORY) { + return `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}`; + } + + return ""; +} + +function normalizeRepositoryUrl(value) { + if (!value) { + return ""; + } + + return value.replace(/\.git$/, ""); +} + +function stripVersion(value) { + if (!value) { + return ""; + } + + const match = value.match(/^v?(\d+\.\d+\.\d+)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/); + return match ? match[1] : ""; +} + +function coerceVersion(value) { + if (!value) { + return "0.0.0"; + } + + const match = value.match(/^v?(\d+)\.(\d+)\.(\d+)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/); + return match ? match[1] + "." + match[2] + "." + match[3] : "0.0.0"; +} + +function listCommits(startRef, endRef) { + return runGit([ + "log", + "--reverse", + "--no-merges", + "--format=%H%x1f%s%x1f%b%x1e", + `${startRef}..${endRef}` + ]); +} + +function parseCommits(rawLog) { + return rawLog + .split("\x1e") + .map((entry) => entry.trim()) + .filter(Boolean) + .map((entry) => { + const [hash, subject, body = ""] = entry.split("\x1f"); + const conventional = parseConventionalSubject(subject); + const type = conventional?.type || ""; + const policy = policyByType.get(type); + + return { + body, + breakingNotes: findBreakingNotes(body, subject), + hash, + releaseType: detectReleaseType(conventional, body), + scope: conventional?.scope || "", + section: policy?.section || "Other Changes", + subject: conventional?.subject || subject, + type + }; + }); +} + +function parseConventionalSubject(subject) { + const match = subject.match(/^(?[a-z]+)(?:\((?[^)]+)\))?(?!)?: (?.+)$/i); + if (!match?.groups) { + return null; + } + + return { + breaking: Boolean(match.groups.breaking), + scope: match.groups.scope || "", + subject: match.groups.subject, + type: match.groups.type.toLowerCase() + }; +} + +function findBreakingNotes(body, subject) { + const lines = body.split(/\r?\n/); + const notes = []; + + for (const line of lines) { + const trimmed = line.trim(); + if (!trimmed) { + continue; + } + + for (const keyword of releasePolicy.noteKeywords) { + const prefix = `${keyword}:`; + if (trimmed.startsWith(prefix)) { + notes.push(trimmed.slice(prefix.length).trim() || subject); + } + } + } + + return notes; +} + +function detectReleaseType(conventional, body) { + if (!conventional) { + return "none"; + } + + if (conventional.breaking || findBreakingNotes(body, conventional.subject).length > 0) { + return "major"; + } + + if (conventional.type === "feat") { + return "minor"; + } + + if (conventional.type === "fix" || conventional.type === "perf" || conventional.type === "revert") { + return "patch"; + } + + if (conventional.type === "refactor") { + return "patch"; + } + + const policy = policyByType.get(conventional.type); + if (policy?.release === false) { + return "none"; + } + + return "none"; +} + +function maxReleaseType(current, next) { + const rank = new Map([ + ["none", 0], + ["patch", 1], + ["minor", 2], + ["major", 3] + ]); + + return rank.get(next) > rank.get(current) ? next : current; +} + +function incrementVersion(version, releaseType) { + const [major, minor, patch] = version.split(".").map((value) => Number.parseInt(value, 10) || 0); + + if (releaseType === "major") { + return `${major + 1}.0.0`; + } + + if (releaseType === "minor") { + return `${major}.${minor + 1}.0`; + } + + if (releaseType === "patch") { + return `${major}.${minor}.${patch + 1}`; + } + + return ""; +} + +function renderReleaseNotes(grouped, breakingNotes, repository) { + const lines = []; + + if (breakingNotes.length > 0) { + lines.push("## Breaking Changes", ""); + for (const entry of breakingNotes) { + lines.push(`- ${entry.note} (${formatCommitRef(entry.hash, repository)})`); + } + lines.push(""); + } + + for (const [section, commits] of grouped.entries()) { + if (commits.length === 0) { + continue; + } + + const sortedCommits = [...commits].sort((left, right) => { + const leftKey = `${left.scope}\u0000${left.subject}`.toLowerCase(); + const rightKey = `${right.scope}\u0000${right.subject}`.toLowerCase(); + return leftKey.localeCompare(rightKey); + }); + + lines.push(`## ${section}`, ""); + for (const commit of sortedCommits) { + const scopePrefix = commit.scope ? `**${commit.scope}:** ` : ""; + lines.push(`- ${scopePrefix}${commit.subject} (${formatCommitRef(commit.hash, repository)})`); + } + lines.push(""); + } + + return lines.join("\n").trim(); +} + +function formatCommitRef(hash, repository) { + const shortHash = hash.slice(0, 7); + if (!repository) { + return `\`${shortHash}\``; + } + + return `[\`${shortHash}\`](${repository}/commit/${hash})`; +} diff --git a/tools/release/yarn.lock b/tools/release/yarn.lock index 5bd7b7997..f4bce6475 100644 --- a/tools/release/yarn.lock +++ b/tools/release/yarn.lock @@ -457,7 +457,7 @@ __metadata: languageName: node linkType: hard -"@semantic-release/commit-analyzer@npm:^13.0.1": +"@semantic-release/commit-analyzer@npm:13.0.1, @semantic-release/commit-analyzer@npm:^13.0.1": version: 13.0.1 resolution: "@semantic-release/commit-analyzer@npm:13.0.1" dependencies: @@ -559,7 +559,7 @@ __metadata: languageName: node linkType: hard -"@semantic-release/release-notes-generator@npm:^14.1.0": +"@semantic-release/release-notes-generator@npm:14.1.0, @semantic-release/release-notes-generator@npm:^14.1.0": version: 14.1.0 resolution: "@semantic-release/release-notes-generator@npm:14.1.0" dependencies: @@ -1585,7 +1585,9 @@ __metadata: resolution: "grimmory-release-tooling@workspace:." dependencies: "@semantic-release/changelog": "npm:6.0.3" + "@semantic-release/commit-analyzer": "npm:13.0.1" "@semantic-release/git": "npm:10.0.1" + "@semantic-release/release-notes-generator": "npm:14.1.0" conventional-changelog-conventionalcommits: "npm:9.3.1" semantic-release: "npm:25.0.3" languageName: unknown