diff --git a/.github/actions/update-release-changelog/action.yml b/.github/actions/update-release-changelog/action.yml index d5003fa2..2a119e93 100644 --- a/.github/actions/update-release-changelog/action.yml +++ b/.github/actions/update-release-changelog/action.yml @@ -15,12 +15,68 @@ inputs: github-token: required: true description: 'GitHub token with write access to the invoking repository and read access to planning resources.' + skip-if-tag-exists: + required: false + default: 'true' + description: >- + Skip the changelog update, and finish successfully, when the release tag + (v, derived from the release branch name) already exists. A tagged + release has already shipped, and `auto-changelog` rejects the update for it. + The skip is reported in the job summary, with the number of commits on the + branch after the tag. Set to 'false' to always run the update. runs: using: composite steps: - # Step 1: Checkout invoking repository (metamask-mobile | metamask-extension) + # Step 1: Once a release is tagged it has shipped, and `auto-changelog` fails when asked to + # update the changelog for it. Skip instead of failing. This runs before the checkout (a + # full-history fetch that can take minutes) and only needs one API call. It fails open: any + # problem checking the tag falls back to running the update, which is the behaviour without + # this step. + - name: Check whether the release tag already exists + id: tag-check + if: ${{ inputs.skip-if-tag-exists == 'true' }} + shell: bash + env: + AI_ANALYZER_LITELLM_KEY: '' + GH_TOKEN: ${{ inputs.github-token }} + RELEASE_BRANCH: ${{ inputs.release-branch }} + REPOSITORY: ${{ github.repository }} + run: | + set -euo pipefail + + # release/8.20.2-ota and release/13.59.0 -> v8.20.2 and v13.59.0 (`-ota` is a + # branch-name convention only). The pattern matches the one the updater accepts + # (parseReleaseBranch in .github/scripts/release-version-utils.mts). + # Strip newlines before echoing untrusted text, so it cannot start a workflow command. + SAFE_BRANCH="${RELEASE_BRANCH//[$'\r\n']/ }" + if [[ ! "$RELEASE_BRANCH" =~ ^release/([0-9]+\.[0-9]+\.[0-9]+)(-ota)?$ ]]; then + echo "::notice::Cannot derive a version from '${SAFE_BRANCH}'; not checking for an existing tag." + echo "skip=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + TAG="v${BASH_REMATCH[1]}" + + # `git/ref/` matches one exact ref. (`git/refs/` would match every tag with this prefix.) + STDERR_FILE="$(mktemp)" + if gh api "repos/${REPOSITORY}/git/ref/tags/${TAG}" --silent 2>"$STDERR_FILE"; then + echo "skip=true" >> "$GITHUB_OUTPUT" + # Informational and best effort: the number of commits on the branch after the tag + # (usually the changelog PR merge or a merge of `stable`), so a skipped run is not silent. + AHEAD="$(gh api "repos/${REPOSITORY}/compare/${TAG}...${RELEASE_BRANCH}" --jq .ahead_by 2>/dev/null || echo unknown)" + echo "::notice::Tag ${TAG} already exists, so the release has shipped. Skipping the changelog update. Commits on ${SAFE_BRANCH} after the tag: ${AHEAD}." + echo "Skipped the changelog update for \`${SAFE_BRANCH}\`: tag \`${TAG}\` already exists. Commits on the branch after the tag: ${AHEAD}." >> "$GITHUB_STEP_SUMMARY" + elif grep -q 'HTTP 404' "$STDERR_FILE"; then + echo "skip=false" >> "$GITHUB_OUTPUT" + echo "Tag ${TAG} does not exist yet. Updating the changelog." + else + echo "::warning::Could not check whether tag ${TAG} exists; updating the changelog anyway. $(head -c 300 "$STDERR_FILE" | tr '\r\n' ' ')" + echo "skip=false" >> "$GITHUB_OUTPUT" + fi + + # Step 2: Checkout invoking repository (metamask-mobile | metamask-extension) - name: Checkout invoking repository + if: ${{ steps.tag-check.outputs.skip != 'true' }} uses: actions/checkout@v6 env: AI_ANALYZER_LITELLM_KEY: '' @@ -30,6 +86,7 @@ runs: token: ${{ inputs.github-token }} - name: Set up Node 24 for release changelog automation + if: ${{ steps.tag-check.outputs.skip != 'true' }} uses: actions/setup-node@v6 env: AI_ANALYZER_LITELLM_KEY: '' @@ -39,6 +96,7 @@ runs: # Corepack resolves Yarn from the working directory, so install from the # github-tools root to use its own pinned Yarn and lockfile. - name: Install github-tools dependencies + if: ${{ steps.tag-check.outputs.skip != 'true' }} shell: bash working-directory: ${{ github.action_path }}/../../.. env: @@ -49,6 +107,7 @@ runs: corepack yarn install --immutable - name: Update changelog branch + if: ${{ steps.tag-check.outputs.skip != 'true' }} shell: bash env: GITHUB_TOKEN: ${{ inputs.github-token }} @@ -67,7 +126,7 @@ runs: "$REPOSITORY_URL" - name: Upload clean-room proofread report - if: ${{ always() && hashFiles('.tmp/release-changelog-ai/*.json') != '' }} + if: ${{ always() && steps.tag-check.outputs.skip != 'true' && hashFiles('.tmp/release-changelog-ai/*.json') != '' }} uses: actions/upload-artifact@v7 env: AI_ANALYZER_LITELLM_KEY: '' diff --git a/CHANGELOG.md b/CHANGELOG.md index 186a7ca4..e3000a9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add a `skip-if-tag-exists` input to the `update-release-changelog` action, defaulting to `true` ([#351](https://github.com/MetaMask/github-tools/pull/351)) + - When the release tag (`v`, derived from a `release/X.Y.Z` or `release/X.Y.Z-ota` branch name) already exists, the action skips the update and finishes successfully, instead of failing in `auto-changelog` with "Current version already has a tag". Pushes that land after tagging, such as the merge of the changelog PR or a merge of `stable`, no longer cause failed runs. + - The skip is reported in the job summary, with the number of commits on the branch after the tag. + - The skip applies whenever the tag exists, including a release that is not the most recent one. `auto-changelog` only rejected the most recent tag, so pushes to an older tagged release branch are now skipped too. + - The check runs before the repository checkout, uses one GitHub API call, and falls back to running the update if the tag cannot be checked. + - Set `skip-if-tag-exists: 'false'` to keep the previous behaviour. + ## [1.28.0] ### Added