# Consolidated CI workflow for Astroglobe Nextcloud app
#
# Runs on PRs that modify the astroglobe directory
# Based on Nextcloud app skeleton workflows
#
# SPDX-FileCopyrightText: 2025 Nextcloud MCP Server contributors
# SPDX-License-Identifier: MIT

name: Astroglobe CI

on:
  pull_request:
    paths:
      - 'third_party/astroglobe/**'
      - '.github/workflows/astroglobe-ci.yml'

permissions:
  contents: read

concurrency:
  group: astroglobe-ci-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  changes:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
    outputs:
      frontend: ${{ steps.changes.outputs.frontend }}
      php: ${{ steps.changes.outputs.php }}
    steps:
      - uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
        id: changes
        continue-on-error: true
        with:
          filters: |
            frontend:
              - 'third_party/astroglobe/src/**'
              - 'third_party/astroglobe/package.json'
              - 'third_party/astroglobe/package-lock.json'
              - 'third_party/astroglobe/vite.config.js'
              - 'third_party/astroglobe/**/*.js'
              - 'third_party/astroglobe/**/*.ts'
              - 'third_party/astroglobe/**/*.vue'
            php:
              - 'third_party/astroglobe/lib/**'
              - 'third_party/astroglobe/appinfo/**'
              - 'third_party/astroglobe/composer.json'
              - 'third_party/astroglobe/psalm.xml'

  # Node.js build and lint
  node-build:
    runs-on: ubuntu-latest
    needs: changes
    if: needs.changes.outputs.frontend != 'false'
    name: Node.js build
    defaults:
      run:
        working-directory: third_party/astroglobe
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Read package.json node and npm engines version
        uses: skjnldsv/read-package-engines-version-actions@06d6baf7d8f41934ab630e97d9e6c0bc9c9ac5e4 # v3
        id: versions
        with:
          path: third_party/astroglobe
          fallbackNode: '^20'
          fallbackNpm: '^10'

      - name: Set up node ${{ steps.versions.outputs.nodeVersion }}
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: ${{ steps.versions.outputs.nodeVersion }}

      - name: Set up npm ${{ steps.versions.outputs.npmVersion }}
        run: npm i -g 'npm@${{ steps.versions.outputs.npmVersion }}'

      - name: Install dependencies & build
        env:
          CYPRESS_INSTALL_BINARY: 0
          PUPPETEER_SKIP_DOWNLOAD: true
        run: |
          npm ci
          npm run build --if-present

      - name: Check webpack build changes
        run: |
          bash -c "[[ ! \"`git status --porcelain `\" ]] || (echo 'Please recompile and commit the assets' && exit 1)"

  # ESLint
  eslint:
    runs-on: ubuntu-latest
    needs: changes
    if: needs.changes.outputs.frontend != 'false'
    name: ESLint
    defaults:
      run:
        working-directory: third_party/astroglobe
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Read package.json node and npm engines version
        uses: skjnldsv/read-package-engines-version-actions@06d6baf7d8f41934ab630e97d9e6c0bc9c9ac5e4 # v3
        id: versions
        with:
          path: third_party/astroglobe
          fallbackNode: '^20'
          fallbackNpm: '^10'

      - name: Set up node ${{ steps.versions.outputs.nodeVersion }}
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: ${{ steps.versions.outputs.nodeVersion }}

      - name: Set up npm ${{ steps.versions.outputs.npmVersion }}
        run: npm i -g 'npm@${{ steps.versions.outputs.npmVersion }}'

      - name: Install dependencies
        env:
          CYPRESS_INSTALL_BINARY: 0
          PUPPETEER_SKIP_DOWNLOAD: true
        run: npm ci

      - name: Lint
        run: npm run lint

  # Stylelint
  stylelint:
    runs-on: ubuntu-latest
    needs: changes
    if: needs.changes.outputs.frontend != 'false'
    name: Stylelint
    defaults:
      run:
        working-directory: third_party/astroglobe
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Read package.json node and npm engines version
        uses: skjnldsv/read-package-engines-version-actions@06d6baf7d8f41934ab630e97d9e6c0bc9c9ac5e4 # v3
        id: versions
        with:
          path: third_party/astroglobe
          fallbackNode: '^20'
          fallbackNpm: '^10'

      - name: Set up node ${{ steps.versions.outputs.nodeVersion }}
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: ${{ steps.versions.outputs.nodeVersion }}

      - name: Set up npm ${{ steps.versions.outputs.npmVersion }}
        run: npm i -g 'npm@${{ steps.versions.outputs.npmVersion }}'

      - name: Install dependencies
        env:
          CYPRESS_INSTALL_BINARY: 0
          PUPPETEER_SKIP_DOWNLOAD: true
        run: npm ci

      - name: Lint
        run: npm run stylelint

  # PHP Code Style
  php-cs:
    runs-on: ubuntu-latest
    needs: changes
    if: needs.changes.outputs.php != 'false'
    name: PHP CS Fixer
    defaults:
      run:
        working-directory: third_party/astroglobe
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Get php version
        id: versions
        uses: icewind1991/nextcloud-version-matrix@58becf3b4bb6dc6cef677b15e2fd8e7d48c0908f # v1.3.1
        with:
          filename: third_party/astroglobe/appinfo/info.xml

      - name: Set up php${{ steps.versions.outputs.php-min }}
        uses: shivammathur/setup-php@cf4cade2721270509d5b1c766ab3549210a39a2a # v2.33.0
        with:
          php-version: ${{ steps.versions.outputs.php-min }}
          extensions: bz2, ctype, curl, dom, fileinfo, gd, iconv, intl, json, libxml, mbstring, openssl, pcntl, posix, session, simplexml, xmlreader, xmlwriter, zip, zlib, sqlite, pdo_sqlite
          coverage: none
          ini-file: development
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Install dependencies
        run: |
          composer remove nextcloud/ocp --dev || true
          composer i

      - name: Lint
        run: composer run cs:check || ( echo 'Please run `composer run cs:fix` to format your code' && exit 1 )

  # Psalm Static Analysis
  psalm:
    runs-on: ubuntu-latest
    needs: changes
    if: needs.changes.outputs.php != 'false'
    name: Psalm
    defaults:
      run:
        working-directory: third_party/astroglobe
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Get php version
        id: versions
        uses: icewind1991/nextcloud-version-matrix@58becf3b4bb6dc6cef677b15e2fd8e7d48c0908f # v1.3.1
        with:
          filename: third_party/astroglobe/appinfo/info.xml

      - name: Set up php${{ steps.versions.outputs.php-min }}
        uses: shivammathur/setup-php@cf4cade2721270509d5b1c766ab3549210a39a2a # v2.33.0
        with:
          php-version: ${{ steps.versions.outputs.php-min }}
          extensions: bz2, ctype, curl, dom, fileinfo, gd, iconv, intl, json, libxml, mbstring, openssl, pcntl, posix, session, simplexml, xmlreader, xmlwriter, zip, zlib, sqlite, pdo_sqlite
          coverage: none
          ini-file: development
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Install dependencies
        run: |
          composer remove nextcloud/ocp --dev || true
          composer i

      - name: Get OCP version matrix
        id: ocp-versions
        uses: icewind1991/nextcloud-version-matrix@58becf3b4bb6dc6cef677b15e2fd8e7d48c0908f # v1.3.1
        with:
          filename: third_party/astroglobe/appinfo/info.xml

      - name: Install OCP for static analysis
        run: |
          # Get first OCP version from matrix
          OCP_VERSION=$(echo '${{ steps.ocp-versions.outputs.ocp-matrix }}' | jq -r '.include[0]."ocp-version"')
          composer require --dev "nextcloud/ocp:$OCP_VERSION" --ignore-platform-reqs --with-dependencies

      - name: Run Psalm
        run: composer run psalm -- --threads=1 --monochrome --no-progress --output-format=github

  # Summary job
  summary:
    permissions:
      contents: none
    runs-on: ubuntu-latest
    needs: [changes, node-build, eslint, stylelint, php-cs, psalm]
    if: always()
    name: astroglobe-ci-summary
    steps:
      - name: Summary status
        run: |
          if ${{ needs.changes.outputs.frontend != 'false' && (needs.node-build.result != 'success' || needs.eslint.result != 'success' || needs.stylelint.result != 'success') }}; then
            echo "Frontend checks failed"
            exit 1
          fi
          if ${{ needs.changes.outputs.php != 'false' && (needs.php-cs.result != 'success' || needs.psalm.result != 'success') }}; then
            echo "PHP checks failed"
            exit 1
          fi
          echo "All checks passed"
