name: Auto response

on:
  issues:
    types: [labeled]
  pull_request_target:
    types: [labeled]

permissions:
  issues: write
  pull-requests: write

jobs:
  auto-response:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/create-github-app-token@v1
        id: app-token
        with:
          app-id: "2729701"
          private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
      - name: Handle labeled items
        uses: actions/github-script@v7
        with:
          github-token: ${{ steps.app-token.outputs.token }}
          script: |
            const rules = [
              {
                label: "skill-clawdhub",
                close: true,
                message:
                  "Thanks for the contribution! New skills should be published to Clawdhub for everyone to use. We’re keeping the core lean on skills, so I’m closing this out.",
              },
            ];

            const labelName = context.payload.label?.name;
            if (!labelName) {
              return;
            }

            const rule = rules.find((item) => item.label === labelName);
            if (!rule) {
              return;
            }

            const issueNumber = context.payload.issue?.number ?? context.payload.pull_request?.number;
            if (!issueNumber) {
              return;
            }

            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: issueNumber,
              body: rule.message,
            });

            if (rule.close) {
              await github.rest.issues.update({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: issueNumber,
                state: "closed",
              });
            }
