name: Lint

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  run-linters:
    name: Run linters
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: 14
          registry-url: https://registry.npmjs.org
          cache: 'yarn'

      - uses: actions/cache@v3
        id: install-cache
        with:
          path: node_modules/
          key: ${{ runner.os }}-install-${{ hashFiles('**/yarn.lock') }}

      - if: steps.install-cache.outputs.cache-hit != 'true'
        run: yarn install --frozen-lockfile --ignore-scripts

      - name: Run eslint w/ autofix
        if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.owner.login == github.repository_owner }}
        uses: wearerequired/lint-action@36c7e6689e80d785d27a22f71d970f3a3b4fcb70
        with:
          github_token: ${{ secrets.github_token }}
          eslint: true
          eslint_args: "-c .eslintrc.json"
          auto_fix: true

      - name: Run eslint
        if: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.owner.login != github.repository_owner }}
        run: yarn eslint .
