name: 'Lint PR'

on:
  pull_request_target:
    types:
      - opened
      - edited
      - synchronize
      - reopened
  workflow_dispatch:

permissions:
  contents: read

jobs:
  main:
    name: Validate PR title
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request_target'
    steps:
      - uses: amannn/action-semantic-pull-request@v5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  lint:
    name: Code Formatting Check
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repository
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Install dependencies
        run: npm ci

      - name: Check Code Formatting
        run: npm run format-check

  test:
    name: Build and Test
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repository
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build

      - name: Check Tests
        run: npm test

  guarddog:
    permissions:
      contents: read
    name: GuardDog Check
    runs-on: ubuntu-latest

    steps:
      - name: Check out the repository
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'

      - name: Install GuardDog
        run: pip install 'guarddog<3.0.0'

      - run: guarddog npm scan src/ --exit-non-zero-on-finding
      # - run: guarddog npm verify package.json --exclude-rules empty_information --output-format sarif --exit-non-zero-on-finding
