name: test-build

on:
  push:
    branches:
      - main
    paths-ignore:
      - '.gitea/workflows/**'
      - '**.md'

  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-js
    steps:
      - name: Checkout
        uses: actions/checkout@v6.0.2

      - name: Set up Node
        uses: actions/setup-node@v6.4.0
        with:
          node-version: 24.15.0

      - name: Set up Bun
        uses: oven-sh/setup-bun@v2.2.0
        with:
          bun-version: 1.3.13

      - name: Install Dependencies
        run: bun install --frozen-lockfile

      - name: Cache Astro Build Cache
        uses: actions/cache@v5.0.5
        with:
          path: |
            .astro
            node_modules/.vite
          key: ${{ runner.os }}-astro-${{ hashFiles('**/*.astro', 'astro.config.mjs') }}
          restore-keys: |
            ${{ runner.os }}-astro-

      - name: Lint Code
        run: bun run lint

      - name: Build Project
        run: bun run build

      - name: ntfy Failed
        uses: niniyas/ntfy-action@master
        if: failure()
        with:
          url: '${{ secrets.NTFY_URL }}'
          topic: '${{ secrets.NTFY_TOPIC }}'
          title: 'Test Failure - Site Profile'
          priority: 4
          headers: '{"Authorization": "Bearer ${{ secrets.NTFY_CRED }}"}'
          tags: action,failed
          details: 'Tests have failed for building Site Profile'
          icon: 'https://cdn.jsdelivr.net/gh/selfhst/icons/png/gitea.png'
          actions: '[{"action": "view", "label": "Open Gitea", "url": "https://gitea.alexlebens.dev/alexlebens/site-saralebens/actions?workflow=test-build.yaml", "clear": true}]'
          image: true

  guarddog:
    runs-on: ubuntu-js
    steps:
      - name: Checkout
        uses: actions/checkout@v6.0.2

      - name: Set up Python
        uses: actions/setup-python@v6.2.0
        with:
          python-version: '3.12.13'

      - name: Install GuardDog
        run: |
          python3 -m pip install --upgrade pip
          python3 -m pip install guarddog

      - name: Run GuardDog
        run: |
          guarddog npm scan ./

      - name: ntfy Failed
        uses: niniyas/ntfy-action@master
        if: failure()
        with:
          url: '${{ secrets.NTFY_URL }}'
          topic: '${{ secrets.NTFY_TOPIC }}'
          title: 'Security Failure - Site Profile'
          priority: 4
          headers: '{"Authorization": "Bearer ${{ secrets.NTFY_CRED }}"}'
          tags: action,failed
          details: 'Guarddog scan failed for Site Profile'
          icon: 'https://cdn.jsdelivr.net/gh/selfhst/icons/png/gitea.png'
          actions: '[{"action": "view", "label": "Open Gitea", "url": "https://gitea.alexlebens.dev/alexlebens/site-saralebens/actions?workflow=test-build.yaml", "clear": true}]'
          image: true
