name: Pull Request check

on:
  pull_request:
    types:
      - edited
      - opened
      - synchronize
    paths:
      - src/**/*.*
jobs:
  test:
    permissions:
      pull-requests: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout PR
        uses: actions/checkout@v3
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          token: ${{ secrets.GITHUB_TOKEN }}
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16.x
      - name: Clone the main repo
        run: cd .. && sudo /usr/bin/git clone https://github.com/${{ github.repository }} main && cd -
      - name: Check if the PR is acceptable
        run: |
          diff -q ./tests/ ../main/tests 
          diff -q ./.github/ ../main/.github
      - name: Copy test suites from main repo to PR
        run: cp ../main/tests/*.* tests/
      - name: Get rid of the main repo's clone
        run: sudo rm -rf ../main
      - name: Install dependencies
        run: yarn
      - name: Build code
        run: yarn build
      - name: Run main test suites
        run: yarn test
      - name: Report build failed (if any)
        if: failure()
        uses: ./.github/workflows/pr-build-failed.yml
        with:
          pr-author: ${{ github.event.pull_request.user.login }}
          number: ${{ github.event.pull_request.number }}
