name: Security

on:
  pull_request:
    types:
      - opened
      - synchronize
      - reopened
      - ready_for_review
  push:
    branches:
      - main
  schedule:
    - cron: '23 3 * * 1'
  workflow_dispatch:

permissions:
  contents: read

jobs:
  dependency-review:
    name: Dependency Review
    if: ${{ github.event_name == 'pull_request' && !github.event.pull_request.draft }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
    steps:
      - name: Check out repository
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Review dependency changes
        uses: actions/dependency-review-action@v4

  codeql:
    name: CodeQL (${{ matrix.language }})
    if: ${{ github.event_name != 'pull_request' }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
      security-events: write

    strategy:
      fail-fast: false
      matrix:
        include:
          - language: java-kotlin
            build-mode: manual
          - language: javascript-typescript
            build-mode: none

    steps:
      - name: Check out repository
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Set up Java
        if: matrix.language == 'java-kotlin'
        uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 21
          cache: maven

      - name: Ensure Maven wrapper is executable
        if: matrix.language == 'java-kotlin'
        run: chmod +x server/mvnw

      - name: Initialize CodeQL
        uses: github/codeql-action/init@v3
        with:
          languages: ${{ matrix.language }}
          build-mode: ${{ matrix.build-mode }}

      - name: Build Java for CodeQL
        if: matrix.language == 'java-kotlin'
        run: cd server && ./mvnw -q -DskipTests package

      - name: Analyze
        uses: github/codeql-action/analyze@v3
        with:
          category: /language:${{ matrix.language }}
