name: "HashiCorp Terraform Compatibility Matrix"
run-name: 'HashiCorp Terraform Compatibility Matrix ${{ inputs.atlas_cloud_env }}'

on:
  schedule:
    - cron: "0 0 1 * *" # runs first day of the month at midnight UTC
  workflow_dispatch:
    inputs:
      atlas_cloud_env:
        description: 'Atlas cloud environment used, can be either `dev` or `qa`, empty for `dev`. Migration tests will always use `dev`'     
        type: string
        required: false
      test_group:
        description: 'Test group to run, e.g. advanced_cluster, empty for all'
        type: string
        required: false

jobs:
  get-supported-versions:
    runs-on: ubuntu-latest
    permissions: {}
    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
      - name: Get HashiCorp Terraform supported versions
        shell: bash
        id: get-terraform-supported-versions
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          output=$(./scripts/get-terraform-supported-versions.sh "false")
          quoted_output=$(echo "${output}" | jq -c .)
          echo "supported_versions=${quoted_output}" >> "${GITHUB_OUTPUT}"
    outputs:
      supported_versions: ${{ steps.get-terraform-supported-versions.outputs.supported_versions }}

          
  run-test-supported-versions:
    needs: ["get-supported-versions"]
    if: ${{ !cancelled() }}
    strategy:
      max-parallel: 1
      fail-fast: false
      matrix:
        terraform_version: '${{ fromJSON(needs.get-supported-versions.outputs.supported_versions) }}'
    name: terrafrom-compatibility-${{ matrix.terraform_version }}-${{ inputs.atlas_cloud_env || 'dev' }}
    secrets: inherit
    uses: ./.github/workflows/test-suite.yml
    with:
      terraform_matrix: '["${{ matrix.terraform_version }}"]'
      atlas_cloud_env: ${{ inputs.atlas_cloud_env }}
      send_notification: false
  
  slack-notification:
    needs: ["run-test-supported-versions"]
    if: ${{ !cancelled() }}
    runs-on: ubuntu-latest
    permissions: {}
    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
      - name: Get content of slack message
        id: slack-payload
        run: |
          slack_message=$(./scripts/generate-slack-notification-content.sh ${{ needs.run-test-supported-versions.result }} ${{ secrets.SLACK_ONCALL_TAG }} ${{github.server_url}} ${{github.repository}} ${{github.run_id}})
          echo "slack_payload=${slack_message}" >> "${GITHUB_OUTPUT}"
      - name: Send Slack message
        id: slack
        uses: slackapi/slack-github-action@37ebaef184d7626c5f204ab8d3baff4262dd30f0
        with:
          payload: ${{ steps.slack-payload.outputs.slack_payload }}
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK