name: OpenClaw Release Checks

on:
  workflow_dispatch:
    inputs:
      ref:
        description: Existing release tag or current full 40-character main commit SHA to validate (for example v2026.4.12 or 0123456789abcdef0123456789abcdef01234567)
        required: true
        type: string
      provider:
        description: Provider lane for cross-OS onboarding and the end-to-end agent turn
        required: false
        default: openai
        type: choice
        options:
          - openai
          - anthropic
          - minimax
      mode:
        description: Which cross-OS release lanes to run
        required: false
        default: both
        type: choice
        options:
          - fresh
          - upgrade
          - both

concurrency:
  group: openclaw-release-checks-${{ inputs.ref }}
  cancel-in-progress: false

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"

jobs:
  resolve_target:
    runs-on: blacksmith-32vcpu-ubuntu-2404
    timeout-minutes: 30
    permissions:
      contents: read
    outputs:
      ref: ${{ steps.inputs.outputs.ref }}
      sha: ${{ steps.ref.outputs.sha }}
      provider: ${{ steps.inputs.outputs.provider }}
      mode: ${{ steps.inputs.outputs.mode }}
    steps:
      - name: Require main workflow ref for release checks
        env:
          WORKFLOW_REF: ${{ github.ref }}
        run: |
          set -euo pipefail
          if [[ "${WORKFLOW_REF}" != "refs/heads/main" ]]; then
            echo "Release checks must be dispatched from main so the workflow logic and secrets stay canonical." >&2
            exit 1
          fi

      - name: Validate ref input
        env:
          RELEASE_REF: ${{ inputs.ref }}
        run: |
          set -euo pipefail
          if [[ ! "${RELEASE_REF}" =~ ^v[0-9]{4}\.[1-9][0-9]*\.[1-9][0-9]*((-beta\.[1-9][0-9]*)|(-[1-9][0-9]*))?$ ]] && [[ ! "${RELEASE_REF}" =~ ^[0-9a-fA-F]{40}$ ]]; then
            echo "Expected an existing release tag or current full 40-character main commit SHA, got: ${RELEASE_REF}" >&2
            exit 1
          fi

      - name: Checkout selected ref
        uses: actions/checkout@v6
        with:
          ref: ${{ inputs.ref }}
          fetch-depth: 0

      - name: Resolve checked-out SHA
        id: ref
        run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"

      - name: Validate selected ref is on main
        env:
          RELEASE_REF: ${{ inputs.ref }}
        run: |
          set -euo pipefail
          git fetch --no-tags origin +refs/heads/main:refs/remotes/origin/main
          if [[ "${RELEASE_REF}" =~ ^[0-9a-fA-F]{40}$ ]]; then
            MAIN_SHA="$(git rev-parse origin/main)"
            if [[ "$(git rev-parse HEAD)" != "${MAIN_SHA}" ]]; then
              echo "Commit SHA mode only supports the current origin/main HEAD. Use a release tag for older commits." >&2
              exit 1
            fi
          else
            git merge-base --is-ancestor HEAD origin/main
          fi

      - name: Capture selected inputs
        id: inputs
        env:
          RELEASE_REF_INPUT: ${{ inputs.ref }}
          RELEASE_PROVIDER_INPUT: ${{ inputs.provider }}
          RELEASE_MODE_INPUT: ${{ inputs.mode }}
        run: |
          set -euo pipefail
          {
            printf 'ref=%s\n' "$RELEASE_REF_INPUT"
            printf 'provider=%s\n' "$RELEASE_PROVIDER_INPUT"
            printf 'mode=%s\n' "$RELEASE_MODE_INPUT"
          } >> "$GITHUB_OUTPUT"

      - name: Summarize validated ref
        env:
          RELEASE_REF: ${{ inputs.ref }}
          RELEASE_SHA: ${{ steps.ref.outputs.sha }}
          RELEASE_PROVIDER: ${{ inputs.provider }}
          RELEASE_MODE: ${{ inputs.mode }}
        run: |
          {
            echo "## Release checks"
            echo
            echo "- Requested ref: \`${RELEASE_REF}\`"
            echo "- Validated SHA: \`${RELEASE_SHA}\`"
            echo "- Cross-OS provider: \`${RELEASE_PROVIDER}\`"
            echo "- Cross-OS mode: \`${RELEASE_MODE}\`"
            echo "- This run will execute cross-OS release validation plus the non-Parallels Docker/live/openwebui coverage from the CI migration plan."
          } >> "$GITHUB_STEP_SUMMARY"

  cross_os_release_checks:
    needs: [resolve_target]
    permissions: read-all
    uses: ./.github/workflows/openclaw-cross-os-release-checks-reusable.yml
    with:
      ref: ${{ needs.resolve_target.outputs.ref }}
      provider: ${{ needs.resolve_target.outputs.provider }}
      mode: ${{ needs.resolve_target.outputs.mode }}
    secrets: inherit

  live_and_e2e_release_checks:
    needs: [resolve_target]
    permissions:
      contents: read
    uses: ./.github/workflows/openclaw-live-and-e2e-checks-reusable.yml
    with:
      ref: ${{ needs.resolve_target.outputs.ref }}
      include_repo_e2e: true
      include_release_path_suites: true
      include_openwebui: true
      include_live_suites: true
    secrets: inherit
