name: Community Champion Auto Labeler

on:
  issues:
    types: [opened]
  pull_request_target:
    types: [opened]

jobs:
  label_community_champion:
    if: github.repository_owner == 'zed-industries'
    runs-on: ubuntu-latest
    steps:
      - name: Check if author is a community champion and apply label
        uses: actions/github-script@v7
        env:
          COMMUNITY_CHAMPIONS: |
            0x2CA
            5brian
            5herlocked
            abdelq
            afgomez
            AidanV
            akbxr
            AlvaroParker
            amtoaer
            artemevsevev
            bajrangCoder
            bcomnes
            Be-ing
            blopker
            bnjjj
            bobbymannino
            CharlesChen0823
            chbk
            davewa
            davidbarsky
            ddoemonn
            djsauble
            errmayank
            fantacell
            fdncred
            findrakecil
            FloppyDisco
            gko
            huacnlee
            imumesh18
            injust
            jacobtread
            jansol
            jeffreyguenther
            jenslys
            jongretar
            lemorage
            lnay
            marcocondrache
            marius851000
            mikebronner
            ognevny
            PKief
            playdohface
            RemcoSmitsDev
            romaninsh
            rxptr
            Simek
            someone13574
            sourcefrog
            suxiaoshao
            Takk8IS
            tartarughina
            thedadams
            tidely
            timvermeulen
            valentinegb
            versecafe
            vitallium
            WhySoBad
            ya7010
            Zertsov
        with:
          script: |
            const communityChampions = process.env.COMMUNITY_CHAMPIONS
              .split('\n')
              .map(handle => handle.trim().toLowerCase())
              .filter(handle => handle.length > 0);

            let author;
            if (context.eventName === 'issues') {
              author = context.payload.issue.user.login;
            } else if (context.eventName === 'pull_request_target') {
              author = context.payload.pull_request.user.login;
            }

            if (!author || !communityChampions.includes(author.toLowerCase())) {
              return;
            }

            const issueNumber = context.payload.issue?.number || context.payload.pull_request?.number;

            try {
              await github.rest.issues.addLabels({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: issueNumber,
                labels: ['community champion']
              });

              console.log(`Applied 'community champion' label to #${issueNumber} by ${author}`);
            } catch (error) {
              console.error(`Failed to apply label: ${error.message}`);
            }
