name: PR Checks

on:
  pull_request:
    branches:
      - main
      - release/*

env:
  GOPROXY: https://proxy.golang.org,direct
  GOPRIVATE: git.marketally.com
  GONOSUMDB: git.marketally.com
  GO_VERSION: "1.25"
  NODE_VERSION: "22"

jobs:
  # Quick lint checks - must pass
  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}
          cache: false

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Install pnpm
        run: npm install -g pnpm

      - name: Install Go dependencies
        run: go mod download

      - name: Check Go formatting
        run: |
          if [ -n "$(gofmt -l .)" ]; then
            echo "Go code is not formatted. Please run 'gofmt -w .'"
            gofmt -l .
            exit 1
          fi

      - name: Go vet
        run: go vet ./...

      - name: Go linter
        run: |
          go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.7.2 run

      - name: Check for build errors
        run: go build -v ./...

  # Unit tests
  test-unit:
    name: Unit Tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}
          cache: false

      - name: Install dependencies
        run: go mod download

      - name: Run unit tests
        run: |
          # Skip tests that require external services (Redis, Elasticsearch, Meilisearch, Azure, SHA256 git)
          go test -tags="sqlite sqlite_unlock_notify" -race \
            -skip "TestRepoStatsIndex|TestRenderHelper|Sha256|SHA256|Redis|redis|Elasticsearch|Meilisearch|AzureBlob|TestLockAndDo|TestLocker|TestBaseRedis" \
            ./modules/... \
            ./services/...
        env:
          GITEA_I_AM_BEING_UNSAFE_RUNNING_AS_ROOT: true

  # Frontend checks
  frontend:
    name: Frontend Checks
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Install pnpm
        run: npm install -g pnpm

      - name: Install dependencies
        run: pnpm install --frozen-lockfile
        continue-on-error: true

      - name: TypeScript check
        run: pnpm run tsc
        continue-on-error: true

      - name: ESLint
        run: pnpm run eslint
        continue-on-error: true
