name: Continuous Integration

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

env:
  DJANGO_SETTINGS_MODULE: "bugsink.settings.development"

jobs:
  flake8:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: "Set up Python 3.11"
      uses: actions/setup-python@v5
      with:
        python-version: "3.11"  # below 3.12 to avoid false positives inside f-string
    - name: Install Flake8
      run: |
        python -m pip install --upgrade flake8
    - name: Run Flake8
      run: |
        # We ignore 2 classes of whitespace errors (which are useful in the local context,
        # but not worth breaking the build).
        # https://github.com/PyCQA/flake8/issues/515 shows a dead end of doing this "properly"
        # so we just specify it on the command line
        flake8 --extend-ignore=E127,E741,E501 `git ls-files | grep py$`

  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install build
      run: |
        python -m pip install --upgrade pip
        pip install build
    - name: Build wheel
      run: |
        python -m build --wheel
    - name: Install from wheel
      run: |
        python -m pip install dist/*.whl
    - name: Install development dependencies
      run: |
        pip install -r requirements.development.txt
    - name: Check out event-samples
      uses: actions/checkout@master
      with:
        repository: bugsink/event-samples
        path: "event-samples"
    - name: Create separate dir to avoid accidentally using non-packaged code
      run: |
        mkdir separate_dir
    - name: Run Makemigrations --check
      working-directory: separate_dir
      run: |
        bugsink-manage makemigrations --check
    - name: Run Tests
      working-directory: separate_dir
      env:
        SAMPLES_DIR: "${{ github.workspace }}/event-samples"
        PYTHONWARNINGS: all
      run: |
        # because we're outside the project directory, the test discovery won't find our packages. We simply enumerate
        # them using some shell-magic. Note that the only non-app that we still care about is 'bugsink' (project, not app)
        # which we mention separately
        bugsink-manage test `bugsink-manage shell -c 'from django.conf import settings; print(" ".join(settings.BUGSINK_APPS))'` bugsink -v2
        # bugsink-manage test ${GITHUB_WORKSPACE}  -v2  # fails with the following, which I don't understand:
        # ImportError: 'tests' module incorrectly imported from '/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/alerts'. Expected '/home/runner/work/bugsink-private/bugsink-private/alerts'. Is this module globally installed? 
