name: Integration tests

on:
  pull_request_target:
    types:
      - opened
      - reopened
      - labeled
      - unlabeled
      - synchronize
    branches:
      - main
      - stable-*

jobs:
  safe-to-test:
    if: ${{ github.event.label.name == 'safe to test' }} || ${{ github.event.action != 'labeled' }}
    uses: ansible-network/github_actions/.github/workflows/safe-to-test.yml@main

  integration-tests:
    needs:
      - safe-to-test
    runs-on: ubuntu-latest
    env:
      source: "./source"
      ansible_version: "milestone"
      python_version: "3.12"
    steps:
      - name: Checkout collection
        uses: actions/checkout@v4
        with:
          path: ${{ env.source }}
          ref: ${{ github.event.pull_request.head.sha }}

      - name: Set up Python ${{ env.python_version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ env.python_version }}

      - name: Install ansible-core (${{ env.ansible_version }})
        run: >-
          python3 -m pip install
          https://github.com/ansible/ansible/archive/${{ env.ansible_version }}.tar.gz
        shell: bash

      - name: Pre install collections dependencies first so the collection install does not
        run: >-
          ansible-galaxy collection install
          --pre "-r${{ env.source }}/tests/integration/requirements.yml"
          -p /home/runner/collections/
        shell: bash

      - name: Build and install collection
        id: install-collection
        uses: ansible-network/github_actions/.github/actions/build_install_collection@main
        with:
          install_python_dependencies: true
          source_path: ${{ env.source }}

      - name: Install Terraform binary
        uses: hashicorp/setup-terraform@v3
        with:
          terraform_wrapper: false
          terraform_version: "1.6.3"

      - name: Create AWS/sts session credentials
        uses: ansible-network/github_actions/.github/actions/ansible_aws_test_provider@main
        with:
          collection_path: ${{ steps.install-collection.outputs.collection_path }}
          ansible_core_ci_key: ${{ secrets.ANSIBLE_CORE_CI_KEY }}

      - name: Create AzureRM session credentials
        uses: ansible-network/github_actions/.github/actions/ansible_azure_test_provider@main
        with:
          collection_path: ${{ steps.install-collection.outputs.collection_path }}
          ansible_core_ci_key: ${{ secrets.ANSIBLE_CORE_CI_KEY }}

      # we use raw git to create a repository in the tests
      # this fails if the committer doesn't have a name and an email set
      - name: Set up git
        run: |
          git config --global user.email gha@localhost
          git config --global user.name "Github Actions"
        shell: bash

      - name: Run integration tests
        uses: ansible-network/github_actions/.github/actions/ansible_test_integration@main
        with:
          collection_path: ${{ steps.install-collection.outputs.collection_path }}
          python_version: "3.12"
          ansible_version: milestone
