name: Finalize connector rollout

on:
  repository_dispatch:
    types: [finalize-connector-rollout]
  workflow_dispatch:
    inputs:
      connector_name:
        description: "Connector name"
        required: true
      action:
        description: "Action to perform"
        required: true
        options: ["promote", "rollback"]
jobs:
  finalize_rollout:
    name: Finalize connector rollout
    runs-on: connector-publish-large
    env:
      ACTION: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.action || github.event.client_payload.action }}
    steps:
      - name: Check action value
        run: |
          if [[ "${ACTION}" != "promote" && "${ACTION}" != "rollback" ]]; then
            echo "Invalid action: ${ACTION}"
            exit 1
          fi
        shell: bash
      - name: Checkout Airbyte
        uses: actions/checkout@v4
      - name: Promote {{ github.event.client_payload.connector_name }} release candidate
        id: promote-release-candidate
        if: ${{ env.ACTION == 'promote' }}
        uses: ./.github/actions/run-airbyte-ci
        with:
          context: "manual"
          dagger_cloud_token: ${{ secrets.DAGGER_CLOUD_TOKEN_2 }}
          docker_hub_password: ${{ secrets.DOCKER_HUB_PASSWORD }}
          docker_hub_username: ${{ secrets.DOCKER_HUB_USERNAME }}
          gcp_gsm_credentials: ${{ secrets.GCP_GSM_CREDENTIALS }}
          gcs_credentials: ${{ secrets.METADATA_SERVICE_PROD_GCS_CREDENTIALS }}
          github_token: ${{ secrets.GITHUB_TOKEN }}
          metadata_service_gcs_credentials: ${{ secrets.METADATA_SERVICE_PROD_GCS_CREDENTIALS }}
          sentry_dsn: ${{ secrets.SENTRY_AIRBYTE_CI_DSN }}
          slack_webhook_url: ${{ secrets.PUBLISH_ON_MERGE_SLACK_WEBHOOK }}
          subcommand: "connectors --name=${{ github.event.client_payload.connector_name }} publish --promote-release-candidate"
      - name: Rollback {{ github.event.client_payload.connector_name }} release candidate
        id: rollback-release-candidate
        if: ${{ env.ACTION == 'rollback' }}
        uses: ./.github/actions/run-airbyte-ci
        with:
          context: "manual"
          dagger_cloud_token: ${{ secrets.DAGGER_CLOUD_TOKEN_2 }}
          docker_hub_password: ${{ secrets.DOCKER_HUB_PASSWORD }}
          docker_hub_username: ${{ secrets.DOCKER_HUB_USERNAME }}
          gcp_gsm_credentials: ${{ secrets.GCP_GSM_CREDENTIALS }}
          gcs_credentials: ${{ secrets.METADATA_SERVICE_PROD_GCS_CREDENTIALS }}
          github_token: ${{ secrets.GITHUB_TOKEN }}
          metadata_service_gcs_credentials: ${{ secrets.METADATA_SERVICE_PROD_GCS_CREDENTIALS }}
          sentry_dsn: ${{ secrets.SENTRY_AIRBYTE_CI_DSN }}
          slack_webhook_url: ${{ secrets.PUBLISH_ON_MERGE_SLACK_WEBHOOK }}
          spec_cache_gcs_credentials: ${{ secrets.SPEC_CACHE_SERVICE_ACCOUNT_KEY_PUBLISH }}
          subcommand: "connectors --name=${{ github.event.client_payload.connector_name }} publish --rollback-release-candidate"
