name: Check App Quality

on:
  pull_request:
    branches: [main]

jobs:
  preview:
    name: Create deploy preview
    runs-on: ubuntu-latest
    outputs:
      deploy-url: ${{ steps.netlify.outputs.deploy-url }}
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: npm
      - name: Install dependencies
        run: npm ci
      - name: Run Astro build command
        run: npm run build
      - name: Deploy to Netlify
        id: netlify
        uses: nwtgck/actions-netlify@v2
        with:
          publish-dir: dist
          production-branch: main
          github-token: ${{ secrets.GITHUB_TOKEN }}
          alias: deploy-preview-${{ github.event.number }}
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
        timeout-minutes: 1

  lighthouse:
    name: Run Lighthouse tests
    needs: preview
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Lighthouse
        uses: foo-software/lighthouse-check-action@master
        with:
          urls: ${{ needs.preview.outputs.deploy-url }}
          gitAuthor: ${{ github.actor }}
          gitBranch: ${{ github.ref }}
          gitHubAccessToken: ${{ secrets.GITHUB_TOKEN }}
          device: all
          prCommentEnabled: true
          prCommentSaveOld: false
