diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6438be06e78..d3f36ec00ea 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -6,6 +6,12 @@ on: - main - '3.1.x' - '3.0.x' + workflow_dispatch: + inputs: + release_commit: + description: 'Full commit SHA containing the committed release artifacts to recover' + required: true + type: string concurrency: ${{ github.workflow }}-${{ github.ref }} @@ -17,12 +23,50 @@ jobs: contents: read outputs: relevant: ${{ steps.release-relevance.outputs.relevant }} + target_commit: ${{ steps.release-target.outputs.commit }} steps: - name: Checkout Repo uses: actions/checkout@v7.0.1 with: + ref: ${{ github.event_name == 'workflow_dispatch' && inputs.release_commit || github.sha }} fetch-depth: 0 + - name: Resolve release target + id: release-target + env: + REQUESTED_COMMIT: ${{ github.event_name == 'workflow_dispatch' && inputs.release_commit || github.sha }} + run: | + set -euo pipefail + + if [[ ! "${REQUESTED_COMMIT}" =~ ^[0-9a-f]{40}$ ]]; then + echo "::error::Release recovery requires a full lowercase 40-character commit SHA." + exit 1 + fi + + target_commit="$(git rev-parse HEAD)" + if [ "${target_commit}" != "${REQUESTED_COMMIT}" ]; then + echo "::error::Checked out ${target_commit}, expected ${REQUESTED_COMMIT}." + exit 1 + fi + + if [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then + case "${GITHUB_REF_NAME}" in + main|3.1.x|3.0.x) ;; + *) + echo "::error::Release recovery must run from a supported release branch." + exit 1 + ;; + esac + + git fetch --no-tags origin "+${GITHUB_REF}:refs/remotes/origin/${GITHUB_REF_NAME}" + if ! git merge-base --is-ancestor "${target_commit}" "refs/remotes/origin/${GITHUB_REF_NAME}"; then + echo "::error::Release recovery target ${target_commit} is not on ${GITHUB_REF_NAME}." + exit 1 + fi + fi + + echo "commit=${target_commit}" >> "$GITHUB_OUTPUT" + - name: Setup Node.js uses: actions/setup-node@v7 with: @@ -37,7 +81,7 @@ jobs: TAG="v${VERSION}" echo "version=${VERSION}" >> "$GITHUB_OUTPUT" - changed_files="$(git show --format= --name-only --no-renames "${GITHUB_SHA}")" + changed_files="$(git show --format= --name-only --no-renames "${{ steps.release-target.outputs.commit }}")" if grep -Eq '^(\.changeset/|package(-lock)?[.]json$|dist/(schemas|compliance|protocol)/|static/(schemas|compliance)/source/|server/src/training-agent/|scripts/(build-schemas[.]cjs|build-compliance[.]cjs|build-protocol-tarball[.]cjs|sign-protocol-tarball[.]sh|run-storyboards-([^/]+[.]sh|isolated[.]mjs))|[.]github/workflows/(release|training-agent-storyboards|validate-schema-bundle)[.]yml$)' <<< "${changed_files}"; then echo "relevant=true" >> "$GITHUB_OUTPUT" echo "Release-relevant files changed." @@ -80,6 +124,8 @@ jobs: contents: write pull-requests: write id-token: write + env: + RELEASE_COMMIT: ${{ needs.verify-release.outputs.target_commit }} steps: # Mint the GitHub App installation token only after the long-running # verification job succeeds. Installation tokens expire after one hour; @@ -102,6 +148,7 @@ jobs: - name: Checkout Repo uses: actions/checkout@v7.0.1 with: + ref: ${{ needs.verify-release.outputs.target_commit }} fetch-depth: 0 token: ${{ steps.app-token.outputs.token }} @@ -131,19 +178,24 @@ jobs: TAG="v${VERSION}" echo "version=${VERSION}" >> "$GITHUB_OUTPUT" - changed_files="$(git show --format= --name-only --no-renames "${GITHUB_SHA}")" + changed_files="$(git show --format= --name-only --no-renames "${RELEASE_COMMIT}")" if grep -Eq "^dist/(schemas|compliance)/${VERSION}/|^dist/protocol/${VERSION}[.]" <<< "${changed_files}"; then echo "has_release_artifacts=true" >> "$GITHUB_OUTPUT" echo "Detected committed release artifact changes for ${VERSION}." else echo "has_release_artifacts=false" >> "$GITHUB_OUTPUT" + if [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then + echo "::error::Release recovery target ${RELEASE_COMMIT} does not commit artifacts for ${VERSION}." + exit 1 + fi echo "No committed release artifacts detected for ${VERSION}." fi # Branch rules can be bypassed by repository administrators and GitHub # Apps. Before publishing committed artifacts, independently verify that - # the merged release PR received a human approval on its final head SHA. - - name: Require human approval for committed release artifacts + # a human authorized the merged release PR. A final-head approval always + # counts; for a bot-authored release PR, a human merge counts too. + - name: Require human authorization for committed release artifacts if: steps.release-artifacts.outputs.has_release_artifacts == 'true' env: GH_TOKEN: ${{ steps.app-token.outputs.token }} @@ -152,20 +204,35 @@ jobs: pulls="$(gh api \ -H 'Accept: application/vnd.github+json' \ - "/repos/${GITHUB_REPOSITORY}/commits/${GITHUB_SHA}/pulls")" + "/repos/${GITHUB_REPOSITORY}/commits/${RELEASE_COMMIT}/pulls")" pr_number="$(jq -r \ --arg base "${GITHUB_REF_NAME}" \ '[.[] | select(.merged_at != null and .base.ref == $base)] | sort_by(.merged_at) | last | .number // empty' \ <<< "${pulls}")" if [ -z "${pr_number}" ]; then - echo "::error::Release artifact commit ${GITHUB_SHA} is not associated with a merged PR targeting ${GITHUB_REF_NAME}." + echo "::error::Release artifact commit ${RELEASE_COMMIT} is not associated with a merged PR targeting ${GITHUB_REF_NAME}." exit 1 fi pr="$(gh api "/repos/${GITHUB_REPOSITORY}/pulls/${pr_number}")" head_sha="$(jq -r '.head.sha' <<< "${pr}")" author="$(jq -r '.user.login' <<< "${pr}")" + author_type="$(jq -r '.user.type' <<< "${pr}")" + merger="$(jq -r '.merged_by.login // empty' <<< "${pr}")" + merger_type="$(jq -r '.merged_by.type // empty' <<< "${pr}")" + timeline="$(gh api --paginate "/repos/${GITHUB_REPOSITORY}/issues/${pr_number}/timeline" | jq -s 'add')" + auto_merge_state="$(jq -r \ + '[.[] | select( + .event == "auto_merge_enabled" or + .event == "auto_squash_enabled" or + .event == "auto_rebase_enabled" or + .event == "auto_merge_disabled" + )] + | sort_by(.created_at) + | last + | .event // "not_enabled"' \ + <<< "${timeline}")" reviews="$(gh api --paginate "/repos/${GITHUB_REPOSITORY}/pulls/${pr_number}/reviews" | jq -s 'add')" approved_count="$(jq \ --arg head "${head_sha}" \ @@ -177,12 +244,23 @@ jobs: | length' \ <<< "${reviews}")" - if [ "${approved_count}" -lt 1 ]; then - echo "::error::Release PR #${pr_number} by ${author} has no human approval on final head ${head_sha}. Refusing to publish artifacts." + direct_merge_authorized=false + if [ "${author_type}" = "Bot" ] && \ + [ "${merger_type}" = "User" ] && \ + { [ "${auto_merge_state}" = "not_enabled" ] || [ "${auto_merge_state}" = "auto_merge_disabled" ]; }; then + direct_merge_authorized=true + fi + + if [ "${approved_count}" -lt 1 ] && [ "${direct_merge_authorized}" != "true" ]; then + echo "::error::Release PR #${pr_number} by ${author} has neither a human approval on final head ${head_sha} nor a direct human merge of a bot-authored PR. Auto-merge state: ${auto_merge_state}. Refusing to publish artifacts." exit 1 fi - echo "Release PR #${pr_number} has ${approved_count} human approval(s) on final head ${head_sha}." + if [ "${approved_count}" -ge 1 ]; then + echo "Release PR #${pr_number} has ${approved_count} human approval(s) on final head ${head_sha}." + else + echo "Release PR #${pr_number} was authored by bot ${author} and directly merged by human ${merger}." + fi - name: Create Release Pull Request or Tag Release if: steps.release-artifacts.outputs.has_release_artifacts != 'true' @@ -270,7 +348,7 @@ jobs: release_args+=(--prerelease) fi gh release create "${TAG}" \ - --target "${GITHUB_SHA}" \ + --target "${RELEASE_COMMIT}" \ --title "${TAG}" \ --notes "AdCP ${VERSION}" \ "${release_args[@]}" diff --git a/scripts/check-changeset-protocol-scope.cjs b/scripts/check-changeset-protocol-scope.cjs index e3b387809f9..b00c4135d23 100644 --- a/scripts/check-changeset-protocol-scope.cjs +++ b/scripts/check-changeset-protocol-scope.cjs @@ -38,6 +38,7 @@ const CHANGESET_POLICY_CODE_PATHS = new Set([ const CHANGESET_STATUS_EXEMPT_MAINTENANCE_PATHS = new Set([ ...CHANGESET_POLICY_CODE_PATHS, + '.github/workflows/release.yml', '.agents/playbook.md', '.agents/routines/context-refresh-prompt.md', '.agents/routines/triage-prompt.md', @@ -48,6 +49,7 @@ const CHANGESET_STATUS_EXEMPT_MAINTENANCE_PATHS = new Set([ '.agents/shortcuts/prep-for-pr.md', 'docs/reference/changelog.mdx', 'docs/spec-guidelines.md', + 'tests/release-workflow-immutability.test.cjs', ]); function normalizePath(filePath) { diff --git a/tests/changeset-protocol-scope.test.cjs b/tests/changeset-protocol-scope.test.cjs index b19f08fd104..9c6e7b3d63f 100644 --- a/tests/changeset-protocol-scope.test.cjs +++ b/tests/changeset-protocol-scope.test.cjs @@ -257,6 +257,17 @@ assert.strictEqual( 'Policy-only maintenance can bypass changesets status even when no changeset file is touched' ); +assert.strictEqual( + isChangesetStatusExemptMaintenance([ + { status: 'M', paths: ['.github/workflows/release.yml'] }, + { status: 'M', paths: ['tests/release-workflow-immutability.test.cjs'] }, + { status: 'M', paths: ['scripts/check-changeset-protocol-scope.cjs'] }, + { status: 'M', paths: ['tests/changeset-protocol-scope.test.cjs'] }, + ]), + true, + 'Release workflow maintenance can bypass changesets status without creating a package release' +); + assert.strictEqual( isChangesetStatusExemptMaintenance([ { status: 'M', paths: ['.agents/playbook.md'] }, diff --git a/tests/release-workflow-immutability.test.cjs b/tests/release-workflow-immutability.test.cjs index bcf15869d1d..6b36e0db6de 100644 --- a/tests/release-workflow-immutability.test.cjs +++ b/tests/release-workflow-immutability.test.cjs @@ -53,8 +53,9 @@ function extractStep(name) { } const releaseRelevance = extractStep('Detect release-relevant push'); +const releaseTarget = extractStep('Resolve release target'); const artifactDetection = extractStep('Detect committed release artifacts'); -const approvalGate = extractStep('Require human approval for committed release artifacts'); +const approvalGate = extractStep('Require human authorization for committed release artifacts'); const changesetsStep = extractStep('Create Release Pull Request or Tag Release'); const uploadStep = extractStep('Upload protocol tarball to GitHub Release'); const changesetsStepConfig = workflowConfig.jobs.release.steps.find( @@ -188,6 +189,25 @@ assert.strictEqual( 'The verification job must expose release relevance to the mutation job.' ); +assert.strictEqual( + workflowConfig.on.workflow_dispatch.inputs.release_commit.required, + true, + 'Manual release recovery must require an explicit release commit.' +); + +assert.strictEqual( + verificationJob.outputs.target_commit, + '${{ steps.release-target.outputs.commit }}', + 'The verification job must expose the validated release target to the mutation job.' +); + +assert( + releaseTarget.includes('^[0-9a-f]{40}$') && + releaseTarget.includes('git merge-base --is-ancestor "${target_commit}" "refs/remotes/origin/${GITHUB_REF_NAME}"') && + releaseTarget.includes('main|3.1.x|3.0.x'), + 'Manual recovery must require a full SHA already reachable from a supported release branch.' +); + assert.strictEqual( releaseJob.if, "needs.verify-release.outputs.relevant == 'true'", @@ -224,6 +244,11 @@ assert.strictEqual( '${{ steps.app-token.outputs.token }}', 'The release checkout must persist the App token used by Changesets git-CLI pushes.' ); +assert.strictEqual( + releaseCheckout.with.ref, + '${{ needs.verify-release.outputs.target_commit }}', + 'The release job must check out the validated release target.' +); assert( !artifactDetection.includes('[ -d "dist/schemas/${VERSION}" ]'), @@ -241,7 +266,7 @@ assert( ); assert( - approvalGate.includes('/commits/${GITHUB_SHA}/pulls') && + approvalGate.includes('/commits/${RELEASE_COMMIT}/pulls') && approvalGate.includes('.base.ref == $base') && approvalGate.includes('.merged_at != null'), 'The approval gate must resolve the merged PR associated with the release commit and branch.' @@ -251,7 +276,31 @@ assert( approvalGate.includes('select(.user.type == "User")') && approvalGate.includes('map(last)') && approvalGate.includes('select(.state == "APPROVED" and .commit_id == $head)'), - 'Only human approvals submitted against the final release PR head may authorize publication.' + 'Human review authorization must require an approval submitted against the final release PR head.' +); + +assert( + approvalGate.includes('[ "${author_type}" = "Bot" ]') && + approvalGate.includes('[ "${merger_type}" = "User" ]') && + approvalGate.includes('/issues/${pr_number}/timeline') && + approvalGate.includes('.event == "auto_squash_enabled"') && + approvalGate.includes('.event == "auto_merge_disabled"') && + approvalGate.includes('[ "${auto_merge_state}" = "not_enabled" ]') && + approvalGate.includes('[ "${auto_merge_state}" = "auto_merge_disabled" ]') && + approvalGate.includes('[ "${approved_count}" -lt 1 ] && [ "${direct_merge_authorized}" != "true" ]'), + 'A human merge may authorize publication only for a bot-authored release PR.' +); + +assert( + artifactDetection.includes('git show --format= --name-only --no-renames "${RELEASE_COMMIT}"') && + uploadStep.includes('--target "${RELEASE_COMMIT}"'), + 'Recovery must detect and publish artifacts from the validated release commit.' +); + +assert( + artifactDetection.includes('[ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]') && + artifactDetection.includes('does not commit artifacts for ${VERSION}'), + 'Manual recovery must fail instead of running Changesets when its target has no committed release artifacts.' ); assert(