name: Label Component Issues

on:
  issues:
    types:
      - opened

jobs:
  add-component-label:
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - name: Add component labels
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const body = context.payload.issue.body;
            if (!body) return;

            // Define component mappings with regex patterns that handle flexible whitespace
            const components = [
              {
                pattern: /What part of LiteLLM is this about\?\s*SDK \(litellm Python package\)/,
                label: 'sdk',
                color: '0E7C86',
                description: 'Issues related to the litellm Python SDK'
              },
              {
                pattern: /What part of LiteLLM is this about\?\s*Proxy/,
                label: 'proxy',
                color: '5319E7',
                description: 'Issues related to the LiteLLM Proxy'
              },
              {
                pattern: /What part of LiteLLM is this about\?\s*UI Dashboard/,
                label: 'ui-dashboard',
                color: 'D876E3',
                description: 'Issues related to the LiteLLM UI Dashboard'
              },
              {
                pattern: /What part of LiteLLM is this about\?\s*Docs/,
                label: 'docs',
                color: 'FBCA04',
                description: 'Issues related to LiteLLM documentation'
              }
            ];

            // Find matching component
            for (const component of components) {
              if (component.pattern.test(body)) {
                // Ensure label exists
                try {
                  await github.rest.issues.getLabel({
                    owner: context.repo.owner,
                    repo: context.repo.repo,
                    name: component.label
                  });
                } catch (error) {
                  if (error.status === 404) {
                    await github.rest.issues.createLabel({
                      owner: context.repo.owner,
                      repo: context.repo.repo,
                      name: component.label,
                      color: component.color,
                      description: component.description
                    });
                  }
                }

                // Add label to issue
                await github.rest.issues.addLabels({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  issue_number: context.issue.number,
                  labels: [component.label]
                });

                break;
              }
            }

            // Check for 'claude code' keyword (can be applied alongside component labels)
            if (/claude code/i.test(body)) {
              const claudeLabel = {
                name: 'claude code',
                color: '7c3aed',
                description: 'Issues related to Claude Code usage'
              };

              try {
                await github.rest.issues.getLabel({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  name: claudeLabel.name
                });
              } catch (error) {
                if (error.status === 404) {
                  await github.rest.issues.createLabel({
                    owner: context.repo.owner,
                    repo: context.repo.repo,
                    name: claudeLabel.name,
                    color: claudeLabel.color,
                    description: claudeLabel.description
                  });
                }
              }

              await github.rest.issues.addLabels({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.issue.number,
                labels: [claudeLabel.name]
              });
            }
