DevJourney · code I wrote while learning

repo-guard.yml

3.5 KB
name: Repository Guard

# Structural checks that do not depend on installing dependencies:
#   - no committed build output, compiled binaries or dependency trees
#   - no committed R Markdown render output (knitted HTML, *_files, .Rhistory)
#   - only pnpm lockfiles are tracked (no package-lock.json / yarn.lock)
#   - every project with a pnpm lockfile declares packageManager pnpm
#   - no files above the size limit are added

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]
  workflow_dispatch:

permissions:
  contents: read

jobs:
  guard:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v7
        with:
          fetch-depth: 0

      - name: Reject committed build output and binaries
        run: |
          set -euo pipefail
          pattern='(^|/)(node_modules|dist|dist-ssr|build|out|\.next|\.turbo|coverage|\.cache|target|__pycache__|\.venv|venv)(/|$)'
          binary='\.(exe|dll|so|so\.[0-9.]+|dylib|o|obj|a|lib|class|jar|war|pyc|pyo|pdb|tsbuildinfo)$'
          found=$(git ls-files | grep -E "$pattern|$binary" || true)
          if [ -n "$found" ]; then
            echo "Tracked build artifacts or compiled binaries found:"
            echo "$found"
            echo "Remove them with: git rm --cached <path>"
            exit 1
          fi
          echo "No tracked build artifacts."

      - name: Reject generated R Markdown output
        run: |
          set -euo pipefail
          pattern='^R/.*\.html$|^R/.*_files/|^R/.*\.Rhistory$'
          found=$(git ls-files | grep -E "$pattern" || true)
          if [ -n "$found" ]; then
            echo "Generated R Markdown output is tracked:"
            echo "$found"
            echo "Render the .Rmd source instead: git rm --cached <path>"
            exit 1
          fi
          echo "No tracked R Markdown output."

      - name: Reject non-pnpm lockfiles
        run: |
          set -euo pipefail
          found=$(git ls-files | grep -E '(^|/)(package-lock\.json|yarn\.lock|npm-shrinkwrap\.json|bun\.lockb?)$' || true)
          if [ -n "$found" ]; then
            echo "Non-pnpm lockfiles are tracked:"
            echo "$found"
            echo "This repository uses pnpm only."
            exit 1
          fi
          echo "Only pnpm lockfiles are tracked."

      - name: Verify packageManager declaration
        run: |
          set -euo pipefail
          missing=()
          while IFS= read -r manifest; do
            dir=$(dirname "$manifest")
            [ -f "$dir/pnpm-lock.yaml" ] || continue
            if ! grep -q '"packageManager": "pnpm@' "$manifest"; then
              missing+=("$manifest")
            fi
          done < <(git ls-files '*package.json')
          if ((${#missing[@]})); then
            echo "These manifests have a pnpm lockfile but no packageManager field:"
            printf '%s\n' "${missing[@]}"
            exit 1
          fi
          echo "All pnpm projects declare packageManager."

      - name: Reject oversized files
        run: |
          set -euo pipefail
          limit=$((10 * 1024 * 1024))
          media='\.(mp4|mov|avi|mkv|webm|blend|blend1|pdf|zip|psd|wav|mp3|woff2?|ttf|otf)$'
          bad=0
          while IFS= read -r -d '' file; do
            if [[ $file =~ $media ]]; then continue; fi
            size=$(git cat-file -s ":$file")
            if ((size > limit)); then
              printf 'File exceeds 10 MB: %s (%s bytes)\n' "$file" "$size"
              bad=1
            fi
          done < <(git ls-files -z)
          exit $bad