---

name: CI

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]
  schedule:
    # * is a special character in YAML so you have to quote this string
    # Run at 1:00 every day
    - cron: '0 1 * * *'

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.9"]
        ci_pattern:
          - test_query.py::TestContentType
          - test_query.py::TestSuccess
          - test_query.py::TestIncorrectFields
          - test_query.py::TestMaxNumResults
          - test_query.py::TestIncludeTargetData
          - test_query.py::TestAcceptHeader
          - test_query.py::TestActiveFlag
          - test_query.py::TestBadImage
          - test_query.py::TestMaximumImageFileSize
          - test_query.py::TestMaximumImageDimensions
          - test_query.py::TestImageFormats
          - test_query.py::TestProcessing
          - test_query.py::TestUpdate
          - test_query.py::TestDeleted
          - test_query.py::TestTargetStatusFailed
          - test_query.py::TestDateFormats
          - test_query.py::TestInactiveProject
          - test_add_target.py
          - test_authorization_header.py::TestAuthorizationHeader
          - test_authorization_header.py::TestMalformed::test_one_part_no_space
          - test_authorization_header.py::TestMalformed::test_one_part_with_space
          - test_authorization_header.py::TestMalformed::test_missing_signature
          - test_authorization_header.py::TestBadKey
          - test_content_length.py::TestIncorrect::test_not_integer
          - test_content_length.py::TestIncorrect::test_too_large
          - test_content_length.py::TestIncorrect::test_too_small
          - test_database_summary.py
          - test_date_header.py::TestFormat
          - test_date_header.py::TestMissing
          - test_date_header.py::TestSkewedTime::test_date_out_of_range
          - test_date_header.py::TestSkewedTime::test_date_in_range
          - test_delete_target.py
          - test_get_duplicates.py
          - test_get_target.py
          - test_invalid_given_id.py
          - test_invalid_json.py
          - test_target_list.py
          - test_target_summary.py
          - test_unexpected_json.py
          - test_update_target.py::TestActiveFlag
          - test_update_target.py::TestApplicationMetadata
          - test_update_target.py::TestImage::test_image_valid
          - test_update_target.py::TestImage::test_bad_image_format_or_color_space
          - test_update_target.py::TestImage::test_corrupted
          - test_update_target.py::TestImage::test_image_too_large
          - test_update_target.py::TestImage::test_not_base64_encoded_processable
          - test_update_target.py::TestImage::test_not_base64_encoded_not_processable
          - test_update_target.py::TestImage::test_not_image
          - test_update_target.py::TestImage::test_invalid_type
          - test_update_target.py::TestImage::test_rating_can_change
          - test_update_target.py::TestTargetName
          - test_update_target.py::TestUnexpectedData
          - test_update_target.py::TestUpdate
          - test_update_target.py::TestWidth
          - test_update_target.py::TestInactiveProject
          - test_requests_mock_usage.py
          - test_flask_app_usage.py
          - test_docker.py

    steps:
      # We share Vuforia credentials and therefore Vuforia databases across
      # workflows.
      # We therefore want to run only one workflow at a time.
      - name: Wait for other GitHub Workflows to finish
        uses: softprops/turnstyle@v1
        with:
          same-branch-only: false
          # By default this is 60.
          # We have a lot of jobs so this is set higher - we hit API timeouts.
          poll-interval-seconds: 300
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/checkout@v2
        with:
          # See https://github.com/codecov/codecov-action/issues/190.
          fetch-depth: 2

      - name: "Set up Python"
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      - uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          # This is like the example but we use ``*requirements.txt`` rather
          # than ``requirements.txt`` because we have multiple requirements
          # files.
          key: ${{ runner.os }}-pip-${{ hashFiles('**/*requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-

      - name: "Install dependencies"
        run: |
          python -m pip install --upgrade pip setuptools wheel
          # We use '--ignore-installed' to avoid GitHub's cache which can cause
          # issues - we have seen packages from this cache be cause trouble with
          # pip-extra-reqs.
          python -m pip install --ignore-installed --upgrade --editable .[dev]

      - name: "Set secrets file"
        run: |
          # See the "CI Setup" document for details of how this was set up.
          ci/decrypt_secret.sh
          tar xvf "${HOME}"/secrets/secrets.tar
          python ci/set_secrets_file.py
        env:
          CI_PATTERN: ${{ matrix.ci_pattern }}
          ENCRYPTED_FILE: secrets.tar.gpg
          LARGE_SECRET_PASSPHRASE: ${{ secrets.PASSPHRASE_FOR_VUFORIA_SECRETS }}

      - name: "Run tests"
        run: |
          pytest -s -vvv --exitfirst --cov=src/ --cov=tests --cov-report=xml tests/mock_vws/${{ matrix.ci_pattern }}

      - name: "Show coverage file"
        run: |
          # Sometimes we have been sure that we have 100% coverage, but codecov
          # says otherwise.
          #
          # We show the coverage file here to help with debugging.
          # https://github.com/VWS-Python/vws-python-mock/issues/708
          cat ./coverage.xml

      # We run this job on every PR, on every merge to master, and nightly.
      # This causes us to hit an issue with Codecov.
      #
      # We see "Too many uploads to this commit.".
      # See https://community.codecov.io/t/too-many-uploads-to-this-commit/2574.
      #
      # To work around this, we do not upload coverage data on scheduled runs.
      # We print the event name here to help with debugging.
      - name: "Show event name"
        run: |
          echo ${{ github.event_name }}

      - name: "Upload coverage to Codecov"
        uses: "codecov/codecov-action@v1"
        with:
          fail_ci_if_error: true
        if: ${{ github.event_name == 'pull_request' || github.event_name == 'push' }}
