name: integration-test

on:
  pull_request_review:
    types: [submitted]


permissions:
  contents: read

jobs:
  build:
    if: github.event.review.state == 'approved' && github.event.pull_request.base.ref == 'main'
    runs-on: ubuntu-latest

    # Specify python/node versions to test against
    strategy:
      matrix:
        python-version: ["3.10", "3.11"]
        node-version: ["16.x"]

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
          node-version: ${{ matrix.node-version }}

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

      # Clone Pynecone Website Repo For integration tests
    - name: Clone Pynecone Website Repo
      uses: actions/checkout@v3
      with:
        repository: pynecone-io/pcweb
        path: pcweb

    # Install poetry
    - name: cache poetry install
      uses: actions/cache@v2
      with:
        path: ~/.local
        key: python-${{ matrix.python-version }}-poetry-1.3.1

    - uses: snok/install-poetry@v1
      with:
        version: 1.3.1
        virtualenvs-create: true
        virtualenvs-in-project: true

    - name: cache deps
      id: cache-deps
      uses: actions/cache@v2
      with:
        path: .venv
        key: python-${{ matrix.python-version }}-pydeps-${{ hashFiles('**/poetry.lock') }}

    - name: Poetry Install
      run: poetry install --no-interaction --no-root
      if: steps.cache-deps.outputs.cache-hit != 'true'
    - name: Install Requirements
      working-directory: ./pcweb
      run: poetry run pip install -r requirements.txt && poetry run pip install pynecone==0.1.19 && poetry run pip install googletrans
    - name: Init Website
      working-directory: ./pcweb
      run: poetry run pc init
    - name: Run Website and Check for errors
      run: |
        chmod +x ./scripts/integration.sh
        ./scripts/integration.sh ./pcweb prod