name: Build


on:
  push:
    branches: [ main ]

jobs:
  unittests:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.8]

    steps:
    - uses: actions/checkout@v2

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

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pipenv
        pipenv install --dev

    - name: Test with unittest
      run: |
        pipenv run python -m unittest discover -p 'test_*.py'

  commits:
    needs: unittests
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2

    - name: Set up Python 3.8
      uses: actions/setup-python@v2
      with:
        python-version: 3.8

    - name: Update requirements
      run: |
        python -m pip install --upgrade pip
        pip install pipenv-to-requirements
        pipenv_to_requirements
        git add requirements*.txt

        if [[ ! -z $(git status -s requirements*.txt) ]]
        then
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m 'Automatically generated requirements.txt and requirements-dev.txt' requirements*.txt
          git push
        fi

    - name: Check in test outputs
      run: |
        pip install pipenv
        pipenv install --dev
        pipenv run python -m unittest discover -p 'test_*.py'
        find tests -name output -exec git add --force {} \;
        if [[ ! -z $(git status -s tests) ]]
        then
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m 'Automated adding outputs from tests' tests
          git push
        fi

    - name: Check in test updated notebooks
      run: |
        if [[ ! -z $(git status -s notebooks) ]]
        then
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m 'Automated adding updated notebooks' notebooks
          git push
        fi
