name: nf-core cloud full size tests
# This workflow is triggered on PRs opened against the master branch.
# It can be additionally triggered manually with GitHub actions workflow dispatch button.
# It runs the -profile 'test_full' on cloud

on:
  release:
    types: [created]
  pull_request_review:
    types: [submitted]
  workflow_dispatch:
    inputs:
      test:
        description: "-profile test (smaller)"
        type: boolean
        default: true
      somatic:
        description: "Somatic full test"
        type: boolean
        default: false
      germline:
        description: "Germline full test"
        type: boolean
        default: false
      germline_ncbench_agilent:
        description: "Germline Agilent NCBench test"
        type: boolean
        default: false
      aws:
        description: "AWS Batch"
        type: boolean
        default: true
      azure:
        description: "Azure Batch"
        type: boolean
        default: true

jobs:
  run-platform:
    strategy:
      fail-fast: false
      matrix:
        include:
          - profile: test_aws
            test: test
            cloud: aws
            compute_env: TOWER_COMPUTE_ENV
            workdir: TOWER_BUCKET_AWS
          - profile: test_azure
            test: test
            cloud: azure
            compute_env: TOWER_CE_AZURE_CPU
            workdir: TOWER_BUCKET_AZURE
          - profile: test_full_aws
            test: somatic
            cloud: aws
            compute_env: TOWER_COMPUTE_ENV
            workdir: TOWER_BUCKET_AWS
          - profile: test_full_azure
            test: somatic
            cloud: azure
            compute_env: TOWER_CE_AZURE_CPU
            workdir: TOWER_BUCKET_AZURE
          - profile: test_full_germline_aws
            test: germline
            cloud: aws
            compute_env: TOWER_COMPUTE_ENV
            workdir: TOWER_BUCKET_AWS
          - profile: test_full_germline_azure
            test: germline
            cloud: azure
            compute_env: TOWER_CE_AZURE_CPU
            workdir: TOWER_BUCKET_AZURE
          - profile: test_full_germline_ncbench_agilent
            test: germline_ncbench_agilent
            cloud: aws
            compute_env: TOWER_COMPUTE_ENV
            workdir: TOWER_BUCKET_AWS
    name: Run AWS full tests
    # run only if the PR is approved by at least 2 reviewers and against the master branch or manually triggered
    if: github.repository == 'nf-core/sarek' && github.event.review.state == 'approved' && github.event.pull_request.base.ref == 'master' || github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest
    steps:
      - uses: octokit/request-action@v2.x
        id: check_approvals
        with:
          route: GET /repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/reviews
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - id: test_variables
        if: github.event_name != 'workflow_dispatch'
        run: |
          JSON_RESPONSE='${{ steps.check_approvals.outputs.data }}'
          CURRENT_APPROVALS_COUNT=$(echo $JSON_RESPONSE | jq -c '[.[] | select(.state | contains("APPROVED")) ] | length')
          test $CURRENT_APPROVALS_COUNT -ge 2 || exit 1 # At least 2 approvals are required

      # Launch workflow on AWS Batch
      - name: Launch workflow via Seqera Platform
        uses: seqeralabs/action-tower-launch@v2
        # If inputs item exists (i.e. workflow_dispatch), then we find matrix.test and check it is false
        # If is false, we negate and run the job
        if: ( !contains(inputs[matrix.test], 'false') && !contains(inputs[matrix.cloud], 'false') )
        with:
          run_name: sarek_${{ matrix.profile }}
          workspace_id: ${{ secrets.TOWER_WORKSPACE_ID }}
          access_token: ${{ secrets.TOWER_ACCESS_TOKEN }}
          compute_env: ${{ secrets[matrix.compute_env] }}
          revision: ${{ github.sha }}
          workdir: ${{ secrets[matrix.workdir] }}/work/sarek/work-${{ github.sha }}/${{ matrix.profile }}
          parameters: |
            {
              "hook_url": "${{ secrets.MEGATESTS_ALERTS_SLACK_HOOK_URL }}",
              "outdir": "${{ secrets[matrix.workdir] }}/sarek/results-${{ github.sha }}/${{ matrix.profile }}/"
            }
          profiles: ${{ matrix.profile }}

      - uses: actions/upload-artifact@v3
        name: Save Logs
        if: success() || failure()
        with:
          name: tower-${{ matrix.profile }}-log
          path: |
            tower_action_*.log
            tower_action_*.json
