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

name: CI Build

on:
  push:
    branches: [ main ]
  pull_request:
    # all branches
    # h/t: https://stackoverflow.com/questions/64635032/github-actions-run-on-push-to-all-branches
    #branches: [ main ]

permissions:
  contents: read

jobs:
  build:

    runs-on: ubuntu-latest

    #
    # PG DATABASE
    # https://docs.github.com/en/actions/using-containerized-services/creating-postgresql-service-containers
    # https://remarkablemark.org/blog/2021/03/14/setup-postgresql-in-github-actions/
    services:
      postgres:
        # Docker Hub image (postgres, postgres:latest, postgres:14.3, etc.)
        # see for env var options: https://hub.docker.com/_/postgres
        # the heroku pg version is currently 13.6, so let's mimic that
        image: postgres:13.6
        env:
          POSTGRES_DB: my_db_test
          # password is necessary, and can't be empty string,
          # otherwise you will see "Error: Failed to initialize, postgres service is unhealthy." if not set
          # or "sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at '127.0.0.1', port 5432 failed: fe_sendauth: no password supplied" if blank
          POSTGRES_USER: ci_user
          POSTGRES_PASSWORD: ci_rocks
        # port mapping is required
        # fixes "sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at '127.0.0.1', port 5432 failed: Connection refused"
        ports:
          - 5432:5432
        # health checks ensure database is running, and password auth is being used
        # https://www.postgresql.org/docs/current/app-pg-isready.html
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
    - uses: actions/checkout@v3

    #
    # SETUP A PYTHON DEVELOPMENT ENVIRONMENT
    #
    - name: Set up Python 3.10
      uses: actions/setup-python@v3
      with:
        python-version: "3.10"

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 pytest
        pip install -r requirements.txt
        pip install -r requirements-test.txt

    - name: Lint with flake8
      run: |
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

    #
    # RUN TESTS
    #
    - name: Test with pytest
      env:
        # the postgres service requires a password, so use the credentials set by earlier postgres service step:
        TEST_DATABASE_URL: 'postgresql://ci_user:ci_rocks@localhost:5432/my_db_test'
      run: |
        APP_ENV="test" CI="true" pytest
