# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package

on:
  push:
    branches: [ '**' ]
  pull_request:
    branches:
      - master

jobs:
  diffs:
    name: Look for Specific Changes
    outputs:
      found_one: ${{ steps.check_files.outputs.found_one }}
      workflow_change: ${{ steps.check_files.outputs.workflow_change }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 2

      - name: check modified files
        id: check_files
        run: |
          echo "::set-output name=found_one::false"
          echo "::set-output name=workflow_change::false"
          git diff --name-only HEAD^ HEAD > diff_files.txt
          while IFS= read -r file; do
            if [[ $file == *docker* ]]; then
              echo "::set-output name=found_one::true"
            fi
            if [[ $file == */workflows/*package*yml ]]; then
              echo "::set-output name=workflow_change::true"
            fi
            if [[ $file == */workflows/*integration*yml ]]; then
              echo "::set-output name=workflow_change::true"
            fi
          done < diff_files.txt

  build:
    runs-on: ubuntu-latest
    needs: diffs
    strategy:
      matrix:
        python-version: ['3.6', '3.8']
    env:
      zoneId: xWbV4
      image_ctrl: "public.ecr.aws/neotys/neoload-controller:7.7.0"
      image_lg: "public.ecr.aws/neotys/neoload-loadgenerator:7.7.0"
      merge_to_trunk: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
      integration_moment: ${{ github.event_name == 'pull_request' || needs.diffs.outputs.workflow_change == 'true' }}
      docker_files_changed: ${{ needs.diffs.outputs.found_one == 'true' }}
    steps:
    - name: Echo Job State Variables
      run: |
        echo "github.event_name: ${{ github.event_name }}"
        echo "github.ref: ${{ github.ref }}"
        echo "workflow_change: ${{ needs.diffs.outputs.workflow_change }}"
        echo "found_one: ${{ needs.diffs.outputs.found_one }}"
        echo "merge_to_trunk: ${{ env.merge_to_trunk }}"
        echo "integration_moment: ${{ env.integration_moment }}"
        echo "docker_files_changed: ${{ env.docker_files_changed }}"
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: "If-FullTesting: Configure AWS Credentials" # https://docs.amazonaws.cn/en_us/AmazonECR/latest/public/public-service-quotas.html
      if: ${{ env.merge_to_trunk == 'true' || env.integration_moment == 'true' || env.docker_files_changed == 'true' }}
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1
    - name: "If-FullTesting: Pre-cache Images"
      if: ${{ env.merge_to_trunk == 'true' || env.integration_moment == 'true' || env.docker_files_changed == 'true' }}
      run: |
        aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
        docker pull ${{ env.image_ctrl }}
        docker pull ${{ env.image_lg }}
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install .
        python -m pip install coverage
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Test with pytest (unit / mocks)
      run: |
        PYTHONPATH="neoload" COVERAGE_FILE=.coverage.unit coverage3 run -m pytest
    - name: "DISABLE FULL_TESTING_SINCE NLW UNAVAILABLE If-FullTesting: Test with pytest (live calls / integration)"
      if: ${{ env.merge_to_trunk == 'true' || env.integration_moment == 'true' || env.docker_files_changed == 'true' }}
      run: |
        neoload config set docker.controller.image=${{ env.image_ctrl }}
        neoload config set docker.lg.image=${{ env.image_lg }}
        neoload config set docker.zone=${{ env.zoneId }}
#        PYTHONPATH="neoload" COVERAGE_FILE=.coverage.live coverage3 run -m pytest -v -x -m "makelivecalls" --makelivecalls --token ${{ secrets.NLWEB_TOKEN }} --url ${{ secrets.NLWEB_API_URL }} --workspace CLI
    - name: Combine and Create Coverage Report
      run: |
        coverage3 combine
        coverage3 xml
    - name: SonarCloud Scan
      uses: sonarsource/sonarcloud-github-action@master
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
    - name: Zip Test Folder on failure
      if: failure()
      run: zip -r tests.zip tests/
    - name: Upload Test Folder on failure
      uses: actions/upload-artifact@v2
      if: failure()
      with:
        name: test-folder ${{ matrix.node-version }}
        path: tests.zip
        retention-days: 5
