# NOTE: Do not rely on `make` commands here as this action is used across different repos
# where the Makefile will not be available
name: 'Sentry Setup'
description: 'Sets up a Sentry test environment'
inputs:
  workdir:
    description: 'Directory where the sentry source is located'
    required: false
    default: '.'
  snuba:
    description: 'Is snuba required?'
    required: false
    default: 'false'
  clickhouse:
    description: 'Is clickhouse required?'
    required: false
    default: 'false'
  kafka:
    description: 'Is kafka required?'
    required: false
    default: 'false'
  chartcuterie:
    description: 'Is chartcuterie required?'
    required: false
    default: 'false'
  bigtable:
    description: 'Is bigtable required?'
    required: false
    default: 'false'
  redis_cluster:
    description: 'Is redis cluster required?'
    required: false
    default: 'false'
  symbolicator:
    description: 'Is symbolicator required?'
    required: false
    default: 'false'
  python-version:
    description: 'python version to install'
    required: false
    default: '3.11.9'
  pg-version:
    description: 'PostgreSQL version to use'
    default: '14'
    required: false

outputs:
  yarn-cache-dir:
    description: 'Path to yarn cache'
    value: ${{ steps.config.outputs.yarn-cache-dir }}
  matrix-instance-number:
    description: 'The matrix instance number (starting at 1)'
    value: ${{ steps.config.outputs.matrix-instance-number }}
  matrix-instance-total:
    description: 'Reexport of MATRIX_INSTANCE_TOTAL.'
    value: ${{ steps.config.outputs.matrix-instance-total }}

runs:
  using: 'composite'
  steps:
    - name: Setup default environment variables
      # the default for "bash" is:
      #      bash --noprofile --norc -eo pipefail {0}
      shell: bash --noprofile --norc -eo pipefail -ux {0}
      env:
        MATRIX_INSTANCE: ${{ matrix.instance }}
        # XXX: We should be using something like len(strategy.matrix.instance) (not possible atm)
        # If you have other things like python-version: [foo, bar, baz] then the sharding logic
        # isn't right because job-total will be 3x larger and you'd never run 2/3 of the tests.
        # MATRIX_INSTANCE_TOTAL: ${{ strategy.job-total }}
      run: |
        echo "PIP_DISABLE_PIP_VERSION_CHECK=on" >> $GITHUB_ENV
        echo "PIP_INDEX_URL=https://pypi.devinfra.sentry.io/simple" >> $GITHUB_ENV
        echo "SENTRY_SKIP_BACKEND_VALIDATION=1" >> $GITHUB_ENV

        ### node configuration ###
        echo "NODE_ENV=development" >> $GITHUB_ENV

        ### pytest configuration ###
        echo "PY_COLORS=1" >> "$GITHUB_ENV"
        echo "PYTEST_ADDOPTS=--reruns=5 --durations=10 --fail-slow=60s" >> $GITHUB_ENV

        ### pytest-sentry configuration ###
        if [ "$GITHUB_REPOSITORY" = "getsentry/sentry" ]; then
          echo "PYTEST_SENTRY_DSN=https://6fd5cfea2d4d46b182ad214ac7810508@sentry.io/2423079" >> $GITHUB_ENV
          echo "PYTEST_SENTRY_TRACES_SAMPLE_RATE=0" >> $GITHUB_ENV

          # This records failures on master to sentry in order to detect flakey tests, as it's
          # expected that people have failing tests on their PRs
          if [ "$GITHUB_REF" = "refs/heads/master" ]; then
            echo "PYTEST_SENTRY_ALWAYS_REPORT=1" >> $GITHUB_ENV
          fi
        fi

        # Configure a different release version, otherwise it defaults to the
        # commit sha which will conflict with our actual prod releases. This is a
        # confusing experience because it looks like these are "empty" releases
        # because no commits are attached and associates the release with our
        # javascript + sentry projects.
        echo "SENTRY_RELEASE=ci@$GITHUB_SHA" >> $GITHUB_ENV

        # this handles pytest test sharding
        if [ "$MATRIX_INSTANCE" ]; then
          if ! [ "${MATRIX_INSTANCE_TOTAL:-}" ]; then
            echo "MATRIX_INSTANCE_TOTAL is required."
            exit 1
          fi
          echo "TEST_GROUP=$MATRIX_INSTANCE" >> $GITHUB_ENV
          echo "TOTAL_TEST_GROUPS=$MATRIX_INSTANCE_TOTAL" >> $GITHUB_ENV
        fi

    - uses: getsentry/action-setup-venv@a133e6fd5fa6abd3f590a1c106abda344f5df69f # v2.1.0
      with:
        python-version: ${{ inputs.python-version }}
        cache-dependency-path: ${{ inputs.workdir }}/requirements-dev-frozen.txt
        install-cmd: cd ${{ inputs.workdir }} && python3 -m tools.hack_pip && pip install -r requirements-dev-frozen.txt

    - name: Set up outputs
      id: config
      env:
        MATRIX_INSTANCE: ${{ matrix.instance }}
      shell: bash --noprofile --norc -eo pipefail -ux {0}
      run: |
        echo "yarn-cache-dir=$(yarn cache dir)" >> "$GITHUB_OUTPUT"
        echo "matrix-instance-number=$(($MATRIX_INSTANCE+1))" >> "$GITHUB_OUTPUT"
        echo "matrix-instance-total=$((${MATRIX_INSTANCE_TOTAL:-}))" >> "$GITHUB_OUTPUT"

    - name: Install python dependencies
      shell: bash --noprofile --norc -eo pipefail -ux {0}
      env:
        # This is necessary when other repositories (e.g. relay) want to take advantage of this workflow
        # without needing to fork it. The path needed is the one where setup.py is located
        WORKDIR: ${{ inputs.workdir }}
      run: |
        cd "$WORKDIR"
        # We need to install editable otherwise things like check migration will fail.
        python3 -m tools.fast_editable --path .

    - name: Start devservices
      shell: bash --noprofile --norc -eo pipefail -ux {0}
      env:
        NEED_KAFKA: ${{ inputs.kafka }}
        NEED_SNUBA: ${{ inputs.snuba }}
        NEED_CLICKHOUSE: ${{ inputs.clickhouse }}
        NEED_BIGTABLE: ${{ inputs.bigtable }}
        NEED_CHARTCUTERIE: ${{ inputs.chartcuterie }}
        NEED_REDIS_CLUSTER: ${{ inputs.redis_cluster }}
        NEED_SYMBOLICATOR: ${{ inputs.symbolicator }}
        WORKDIR: ${{ inputs.workdir }}
        PG_VERSION: ${{ inputs.pg-version }}
        ENABLE_AUTORUN_MIGRATION_SEARCH_ISSUES: '1'
      run: |
        sentry init

        # redis, postgres are needed for almost every code path.
        services=(redis postgres)

        if [ "$NEED_CLICKHOUSE" = "true" ] || [ "$NEED_SNUBA" = "true" ]; then
          services+=(clickhouse)
        fi

        if [ "$NEED_SNUBA" = "true" ]; then
          services+=(snuba)
        fi

        if [ "$NEED_BIGTABLE" = "true" ]; then
          echo "BIGTABLE_EMULATOR_HOST=127.0.0.1:8086" >> $GITHUB_ENV
          services+=(bigtable)
        fi

        if [ "$NEED_CHARTCUTERIE" = "true" ]; then
          services+=(chartcuterie)
        fi

        if [ "$NEED_REDIS_CLUSTER" = "true" ]; then
          services+=(redis-cluster)
        fi

        if [ "$NEED_SYMBOLICATOR" = "true" ]; then
          services+=(symbolicator)
        fi

        if [ "$NEED_KAFKA" = "true" ]; then
          services+=(kafka)
        fi

        sentry devservices up "${services[@]}"

        # have tests listen on the docker gateway ip so loopback can occur
        echo "DJANGO_LIVE_TEST_SERVER_ADDRESS=$(docker network inspect bridge --format='{{(index .IPAM.Config 0).Gateway}}')" >> "$GITHUB_ENV"

        docker ps -a

        # This is necessary when other repositories (e.g. relay) want to take advantage of this workflow
        # without needing to fork it. The path needed is the one where tools are located
        cd "$WORKDIR"