name: Pull-Request-CI

on:
  pull_request:
    branches:
      - dev
  push:
    branches:
      - dev

env:
  isis-root: /usr/share/miniconda/envs/isis/
  isis-data: test-resources/ISISDATA/
  hirise-pds-url: https://hirise-pds.lpl.arizona.edu/PDS/EDR/PSP/ORB_010500_010599/PSP_010502_2090

jobs:
  Build-and-Test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
       os: [ubuntu-latest]
       python-version: ["3.7"]
    services:
      postgres:
        image: postgis/postgis:10-2.5
        env:
          POSTGRES_PASSWORD: NotTheDefault
          POSTGRES_DB: postgres
        ports:
          - 35432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5   
    defaults:
      run:
        shell: bash -l {0}
    steps:
      - name: ISIS Conda Env
        uses: conda-incubator/setup-miniconda@v2
        with:
          python-version: 3.6
          activate-environment: isis
          channels: usgs-astrogeology, conda-forge
      - name: Install ISIS
        run: |
          conda install -q -y -c usgs-astrogeology isis
      - name: Setup test-resources/
        run: |
          mkdir $GITHUB_WORKSPACE/test-resources/
          mkdir $GITHUB_WORKSPACE/${{ env.isis-data }}
      - name: Download IMG
        run: |
          curl "${{ env.hirise-pds-url }}/PSP_010502_2090_RED5_0.IMG" -o $GITHUB_WORKSPACE/test-resources/PSP_010502_2090_RED5_0.img
          curl "${{ env.hirise-pds-url }}/PSP_010502_2090_RED5_1.IMG" -o $GITHUB_WORKSPACE/test-resources/PSP_010502_2090_RED5_1.img
      - name: Exit isis env
        run: conda deactivate
      - name: Checkout Code
        uses: actions/checkout@v2
      - name: Cache conda
        uses: actions/cache@v2
        env:
          # Increase this value to reset cache if etc/example-environment.yml has not changed
          CACHE_NUMBER: 0
        with:
          path: ~/conda_pkgs_dir
          key:
            ${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-${{
            hashFiles('environment.yml') }}
      - name: Setup ENV
        uses: conda-incubator/setup-miniconda@v2
        with:
          miniconda-version: "latest"
          mamba-version: "*"
          use-mamba: true
          channels: conda-forge
          channel-priority: strict
          activate-environment: autocnet
          environment-file: environment.yml
          use-only-tar-bz2: true
          python-version: ${{ matrix.python-version }}
      - name: config ISIS vars
        run: |
          conda env config vars set ISISROOT=${{ env.isis-root }}
          conda env config vars set ISISDATA=$GITHUB_WORKSPACE/${{ env.isis-data }}
      - name: Check build environment
        run: |
          conda list
      - name: Install Python Package
        run: |
          python setup.py install
      - name: Test Python Package
        run: |
           pytest --cov-report=xml
      - name: Upload Coverage
        uses: codecov/codecov-action@v1
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: true
          verbose: true
        if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.7'
