name: Build

on:
  push:
    branches: [ main ]

jobs:
  unittests-n-commits:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

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

    - name: Update requirements files and test
      run: |
        python -m pip install --upgrade pip
        pip install pipenv-to-requirements
        pipenv_to_requirements
        pip install -r requirements.txt
        pip install -r requirements-dev.txt

    - name: Run make file
      run: |
        make

    - name: Run unit tests over output
      run: |
        pip install pipenv
        pipenv install --dev
        pipenv run python -m unittest discover -p 'test_*.py'

    - name: Check in changes to requirements files
      run: |
        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 updated content
      run: |
        find linkml_model -name __pycache__ -type d -prune -exec rm -rf '{}' '+'
        git add --force docs linkml_model
        if [[ ! -z $(git status -s .) ]]
        then
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m 'Updating generated outputs' .
          git push
        fi
