name: Nightly Acceptance Tests

permissions:
  contents: read

on:
  schedule:
    # Will run at 00:00 every day
    - cron: "0 0 * * *"

jobs:
  nightly:
    strategy:
      fail-fast: false
      matrix:
        products:
          - account
          - applesilicon
          - baremetal
          - billing
          - block
          - cockpit
          - container
          - dedibox
          - domain
          - edge_services
          - flexibleip
          - function
          - iam
          - inference
          - init
          - instance
          - iot
          - ipam
          - jobs
          - k8s
          - key_manager
          - lb
          - login
          - marketplace
          - mnq
          - mongodb
          - object
          - rdb
          - redis
          - registry
          - serverless_sqldb
          - secret
          - tem
          - vpc
          - vpcgw
          - webhosting
    runs-on: "${{ vars.RUNS_ON || 'ubuntu-latest' }}"
    steps:
      # Checkout should always be before setup-go to ensure caching is working
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Install Go
        uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
        with:
          go-version: stable
      - name: Run Acceptance Tests
        run: go tool gotestsum --format github-actions -- -v ./internal/namespaces/${{ matrix.products }}/... -timeout=4h
        env:
          CLI_UPDATE_CASSETTES: true
          CLI_UPDATE_GOLDENS: true
          SCW_DEBUG: 1
          SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY }}
          SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY }}
          SCW_DEFAULT_ORGANIZATION_ID: ${{ secrets.SCW_DEFAULT_ORGANIZATION_ID }}
          SCW_DEFAULT_PROJECT_ID: ${{ secrets.SCW_DEFAULT_PROJECT_ID }}
      - name: Ping on failure
        if: ${{ failure() }}
        run: |
          curl -X POST -H 'Content-type: application/json' \
          --data '{
          	"blocks": [
              {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "'"Scaleway CLI Nightly workflow failed: <https://github.com/scaleway/scaleway-cli/actions/runs/${GITHUB_RUN_ID}|${FAILED_PRODUCT}>"'"
                  }
              }
            ]
          }' \
          ${SLACK_WEBHOOK_NIGHTLY};
        env:
          SLACK_WEBHOOK_NIGHTLY: ${{ secrets.SLACK_WEBHOOK_NIGHTLY }}
          FAILED_PRODUCT: ${{ matrix.products }}

  sweepers:
    runs-on: "${{ vars.RUNS_ON || 'ubuntu-latest' }}"
    needs: nightly
    if: always()
    steps:
      # Checkout should always be before setup-go to ensure caching is working
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Install Go
        uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
        with:
          go-version: stable
      - name: Run Sweepers
        run: go run -v ./cmd/scw-sweeper
        env:
          SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY }}
          SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY }}
          SCW_DEFAULT_ORGANIZATION_ID: ${{ secrets.SCW_DEFAULT_ORGANIZATION_ID }}
          SCW_DEFAULT_PROJECT_ID: ${{ secrets.SCW_DEFAULT_PROJECT_ID }}
      - name: Ping on failure
        if: ${{ failure() }}
        run: |
          curl -X POST -H 'Content-type: application/json' \
          --data '{
          	"blocks": [
              {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "'"Scaleway CLI sweepers in Nightly workflow failed: <https://github.com/scaleway/scaleway-cli/actions/runs/${GITHUB_RUN_ID}>"'"
                  }
              }
            ]
          }' \
          ${SLACK_WEBHOOK_NIGHTLY};
        env:
          SLACK_WEBHOOK_NIGHTLY: ${{ secrets.SLACK_WEBHOOK_NIGHTLY }}
