name: Seahub Test CI

on:
  pull_request:

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: "3.8"

      - name: apt install
        run: |
          sudo apt-get update --fix-missing
          sudo apt-get install -y libevent-dev libcurl4-openssl-dev libglib2.0-dev
          sudo apt-get install -y uuid-dev intltool libsqlite3-dev build-essential
          sudo apt-get install -y libarchive-dev libtool libjansson-dev valac
          sudo apt-get install -y libfuse-dev cmake re2c flex sqlite3
          sudo apt-get install -y libssl-dev libsasl2-dev libldap2-dev libonig-dev
          sudo apt-get install -y libxml2 libxml2-dev libjwt-dev

      - name: clone and build
        run: |
          git clone --depth=1 --branch=master https://github.com/haiwen/seafile-test-deploy /tmp/seafile-test-deploy
          cd /tmp/seafile-test-deploy && git fetch origin master:master && git checkout master
          export JWT_PRIVATE_KEY=qwertyuiop SITE_ROOT=/ SEAFILE_MYSQL_DB_CCNET_DB_NAME=ccnet SEAFILE_MYSQL_DB_SEAFILE_DB_NAME=seafile SEAFILE_MYSQL_DB_SEAHUB_DB_NAME=seahub
          ./bootstrap.sh

      - name: pip install
        run: |
          cd $GITHUB_WORKSPACE
          pip install -r test-requirements.txt
          sudo rm -rf /usr/lib/python3/dist-packages/pytz/

      - name: run pytest
        run: |
          cd $GITHUB_WORKSPACE
          rm -r tests/seahub/repo_metadata
          export CCNET_CONF_DIR=/tmp/ccnet SEAFILE_CONF_DIR=/tmp/seafile-data TRAVIS=1 SEAFILE_MYSQL_DB_CCNET_DB_NAME=ccnet SEAFILE_MYSQL_DB_SEAFILE_DB_NAME=seafile SEAFILE_MYSQL_DB_SEAHUB_DB_NAME=seahub
          if ./tests/test_seahub_changes.sh; then ./tests/seahubtests.sh init && ./tests/seahubtests.sh runserver && ./tests/seahubtests.sh test; else true; fi

      - uses: actions/setup-node@v2
        with:
          node-version: "20.x"

      - name: run npm lint and npm test
        run: |
          cd $GITHUB_WORKSPACE/tests/
          if chmod +x test_frontend_changes.sh && ./test_frontend_changes.sh; then chmod +x github_actions_npm_lint.sh && ./github_actions_npm_lint.sh; else true; fi
