name: Auto Label Pull Requests

on:
  workflow_dispatch:
  pull_request_target:
    branches: ["main"]
    types: [opened, synchronize, reopened, edited]

jobs:
  autolabeler:
    if: github.repository == 'community-scripts/ProxmoxVE'
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    env:
      CONFIG_PATH: .github/autolabeler-config.json
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install dependencies
        run: npm install minimatch

      - name: Label PR based on file changes and PR template
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs').promises;
            const path = require('path');
            const { minimatch } = require('minimatch');

            const configPath = path.resolve(process.env.CONFIG_PATH);
            const fileContent = await fs.readFile(configPath, 'utf-8');
            const autolabelerConfig = JSON.parse(fileContent);

            const prNumber = context.payload.pull_request.number;
            const prBody = context.payload.pull_request.body || "";

            let labelsToAdd = new Set();

            const prListFilesResponse = await github.rest.pulls.listFiles({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: prNumber,
            });
            const prFiles = prListFilesResponse.data;

            for (const [label, rules] of Object.entries(autolabelerConfig)) {
              const shouldAddLabel = prFiles.some((prFile) => {
                return rules.some((rule) => {
                  const isFileStatusMatch = rule.fileStatus ? rule.fileStatus === prFile.status : true;
                  const isIncludeGlobMatch = rule.includeGlobs.some((glob) => minimatch(prFile.filename, glob));
                  const isExcludeGlobMatch = rule.excludeGlobs.some((glob) => minimatch(prFile.filename, glob));
                  return isFileStatusMatch && isIncludeGlobMatch && !isExcludeGlobMatch;
                });
              });

              if (shouldAddLabel) {
                labelsToAdd.add(label);
                // Add specific sub-labels for tools
                if (label === "tools") {
                  for (const prFile of prFiles) {
                    const filename = prFile.filename;
                    if (filename.startsWith("tools/addon/")) labelsToAdd.add("addon");
                    if (filename.startsWith("tools/pve/")) labelsToAdd.add("pve-tool");
                  }
                }
              }
            }

            // Always parse template checkboxes to add content-type labels (bugfix, feature, etc.)
            const templateLabelMappings = {
              "🐞 **Bug fix**": "bugfix",
              "✨ **New feature**": "feature",
              "💥 **Breaking change**": "breaking change",
              "🆕 **New script**": "new script",
              "🔧 **Refactoring / Code Cleanup**": "refactor",
              "📝 **Documentation update**": "documentation"
            };

            for (const [checkbox, label] of Object.entries(templateLabelMappings)) {
              const escapedCheckbox = checkbox.replace(/([.*+?^=!:${}()|[\]\/\\])/g, "\\$1");
              const regex = new RegExp(`- \\[(x|X)\\]\\s*${escapedCheckbox}`, "i");

              if (regex.test(prBody)) {
                labelsToAdd.add(label);
              }
            }

            // Handle website checkbox specially - only add if not already an update script with content label
            const websiteCheckbox = "🌍 **Website update**";
            const escapedWebsite = websiteCheckbox.replace(/([.*+?^=!:${}()|[\]\/\\])/g, "\\$1");
            const websiteRegex = new RegExp(`- \\[(x|X)\\]\\s*${escapedWebsite}`, "i");

            if (websiteRegex.test(prBody)) {
              const hasJson = prFiles.some((f) => f.filename.startsWith("frontend/public/json/"));
              const hasUpdateScript = labelsToAdd.has("update script");
              const hasContentLabel = ["bugfix", "feature", "refactor"].some((l) => labelsToAdd.has(l));

              // If it's an update script PR with json changes and a content label, skip adding website/json
              // The PR should be categorized as update script with the content label
              if (!(hasUpdateScript && hasJson && hasContentLabel)) {
                labelsToAdd.add(hasJson ? "json" : "website");
              }
            }

            if (labelsToAdd.size === 0) {
              labelsToAdd.add("needs triage");
            }

            if (labelsToAdd.size > 0) {
              await github.rest.issues.addLabels({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: prNumber,
                labels: Array.from(labelsToAdd),
              });
            }
