# see https://github.com/karlicoss/pymplate for up-to-date reference

name: CI
on:
  push:
    branches: '*'
    tags: 'v[0-9]+.*' # only trigger on 'release' tags for PyPi
    # Ideally I would put this in the pypi job... but github syntax doesn't allow for regexes there :shrug:
  pull_request: # needed to trigger on others' PRs
  # Note that people who fork it need to go to "Actions" tab on their fork and click "I understand my workflows, go ahead and enable them".
  workflow_dispatch: # needed to trigger workflows manually


jobs:
  build:
    strategy:
      # useful to quickly debug all versions, otherwise a bit wasteful
      # fail-fast: false
      matrix:
        platform: [ubuntu-latest, macos-latest, windows-latest]
        python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
        exclude: [
            # windows runners are pretty scarce, so let's only run one of them..
            # also 3.7 on windows is a bit broken due to the WindowsPath bug
            {platform: windows-latest, python-version: '3.7'},
            {platform: windows-latest, python-version: '3.9'},
            # 3.11 on windows has this bug, lxml setup fails
            #https://bugs.launchpad.net/lxml/+bug/1977998
            {platform: windows-latest, python-version: '3.11'},
        ]

    runs-on: ${{ matrix.platform }}

    #continue-on-error: ${{ matrix.platform == 'windows-latest' }}

    steps:
    # ugh https://github.com/actions/toolkit/blob/main/docs/commands.md#path-manipulation
    - run: echo "$HOME/.local/bin" >> $GITHUB_PATH
    - if: ${{ matrix.platform == 'macos-latest' && matrix.python-version == '3.11' }}
      # hmm somehow only seems necessary for 3.11 on osx??
      run: echo "$HOME/Library/Python/${{ matrix.python-version }}/bin" >> $GITHUB_PATH

    - uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}

    - uses: actions/checkout@v3
      with:
        submodules: recursive
   
    # uncomment for SSH debugging
    # - uses: mxschmitt/action-tmate@v3

    # explicit bash command is necessary for Windows CI runner, otherwise it thinks it's cmd...
    - run: bash .ci/run

    - if: matrix.platform == 'ubuntu-latest'  # no need to compute coverage for other platforms
      uses: actions/upload-artifact@v3
      with:
        name: .coverage.mypy-core_${{ matrix.platform }}_${{ matrix.python-version }}
        path: .coverage.mypy-core/
    - if: matrix.platform == 'ubuntu-latest'  # no need to compute coverage for other platforms
      uses: actions/upload-artifact@v3
      with:
        name: .coverage.mypy-misc_${{ matrix.platform }}_${{ matrix.python-version }}
        path: .coverage.mypy-misc/

  # split in two pipelines to speedup running
  end2end_tests_chrome:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive
    # NOTE: dev/shm mount to prevent crashes during headless chrome
    - run: |
        docker build -f .ci/end2end_tests.Dockerfile . -t promnesia_end2end_tests
        docker run -v /dev/shm:/dev/shm -e CI promnesia_end2end_tests -- -k chrome

  end2end_tests_firefox:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive
    # NOTE: dev/shm mount to prevent crashes during headless chrome
    - run: |
        docker build -f .ci/end2end_tests.Dockerfile . -t promnesia_end2end_tests
        docker run -v /dev/shm:/dev/shm -e CI promnesia_end2end_tests -- -k firefox

  install_and_run_test:
    # todo use setup-python thing?
    # todo run on macos too?
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive

    - run: .ci/github-ci-compat

    - run: |
        # work around for some breaking change in setuptools 66.0.0
        # see https://github.com/pypa/setuptools/issues/3772#issuecomment-1384671296
        python3 -m pip install --force setuptools==65.7.0
        python3 -m pip install .
        export PATH=.ci/fake-systemd:$PATH
        tests/install_and_run

  pypi:
    runs-on: ubuntu-latest
    needs: [build, end2end_tests_chrome, end2end_tests_firefox, install_and_run_test]

    steps:
    # ugh https://github.com/actions/toolkit/blob/main/docs/commands.md#path-manipulation
    - run: echo "$HOME/.local/bin" >> $GITHUB_PATH

    - uses: actions/setup-python@v4
      with:
        python-version: '3.8'

    - uses: actions/checkout@v3
      with:
        submodules: recursive

    - name: 'release to test pypi'
      # always deploy merged master to test pypi
      if: github.event_name != 'pull_request' && github.event.ref == 'refs/heads/master'
      env:
        TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD_TEST }}
      run: pip3 install --user wheel twine && .ci/release --test
      # TODO run pip install just to test?

    - name: 'release to pypi'
      # always deploy tags to release pypi
      # NOTE: release tags are guarded by on: push: tags on the top
      if: github.event_name != 'pull_request' && startsWith(github.event.ref, 'refs/tags')
      env:
        TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }}
      run: pip3 install --user wheel twine && .ci/release

  ###
  build_extension:
    env:
      name: 'promnesia'
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive
    - uses: actions/setup-node@v3
      with:
        node-version: '18'
    - run: extension/.ci/build --lint # debug version
    - run: extension/.ci/build --lint --release

    # TODO ugh. can't share github actions artifacts publicly...
    # TODO for fuck's sake... why does it end up named as .zip.zip ????
    - uses: actions/upload-artifact@v2
      with:
        name: '${{ env.name }}-chrome-debug-latest.zip'
        path: 'extension/dist/artifacts/chrome/${{ env.name }}_dev_-*.zip'
    - uses: actions/upload-artifact@v2
      with:
        name : '${{ env.name }}-chrome-release-latest.zip'
        path: 'extension/dist/artifacts/chrome/${{ env.name }}-*.zip'
    - uses: actions/upload-artifact@v2
      with:
        name : '${{ env.name }}-firefox-debug-latest.zip'
        path: 'extension/dist/artifacts/firefox/${{ env.name }}_dev_-*.zip'
    - uses: actions/upload-artifact@v2
      with:
        name : '${{ env.name }}-firefox-release-latest.zip'
        path: 'extension/dist/artifacts/firefox/${{ env.name }}-*.zip'
