name: Docling compatibility

# Runs daily and on manual trigger.
# Installs the LATEST docling + docling-core (ignoring version pins)
# and runs the backend tests. Opens an issue if something breaks.

on:
  schedule:
    - cron: "30 6 * * *" # Every day at 06:30 UTC
  workflow_dispatch: # Manual trigger from GitHub UI

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  compat:
    name: Test against latest Docling
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: document-parser

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip
          cache-dependency-path: document-parser/requirements.txt

      - name: Install system dependencies
        run: sudo apt-get update && sudo apt-get install -y --no-install-recommends poppler-utils

      - name: Install pinned dependencies
        run: |
          pip install --upgrade pip
          pip install -r requirements.txt
          pip install pytest pytest-asyncio httpx

      - name: Upgrade docling to latest
        id: versions
        run: |
          pip install --upgrade docling docling-core
          echo "docling=$(pip show docling | grep Version | cut -d' ' -f2)" >> "$GITHUB_OUTPUT"
          echo "docling_core=$(pip show docling-core | grep Version | cut -d' ' -f2)" >> "$GITHUB_OUTPUT"

      - name: Run tests
        run: pytest tests/ -v

      - name: Open issue on failure
        if: failure()
        uses: actions/github-script@v7
        with:
          script: |
            const docling = '${{ steps.versions.outputs.docling }}';
            const doclingCore = '${{ steps.versions.outputs.docling_core }}';
            const run = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;

            // Don't open duplicate issues
            const { data: issues } = await github.rest.issues.listForRepo({
              owner: context.repo.owner,
              repo: context.repo.repo,
              state: 'open',
              labels: 'docling-compat',
            });

            const title = `Docling compatibility break: docling ${docling} / docling-core ${doclingCore}`;

            if (issues.some(i => i.title === title)) {
              console.log('Issue already exists, skipping.');
              return;
            }

            await github.rest.issues.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              title,
              labels: ['docling-compat', 'bug'],
              body: [
                `## Docling compatibility failure`,
                ``,
                `The daily compatibility check failed with the latest Docling releases:`,
                ``,
                `| Package | Version |`,
                `|---------|---------|`,
                `| \`docling\` | \`${docling}\` |`,
                `| \`docling-core\` | \`${doclingCore}\` |`,
                ``,
                `**Workflow run:** [View logs](${run})`,
                ``,
                `### Next steps`,
                `1. Check the failing tests in the workflow logs`,
                `2. Identify breaking changes in the Docling changelog`,
                `3. Update the code and version pins in \`requirements.txt\``,
                `4. Close this issue once fixed`,
              ].join('\n'),
            });
