pr_labeler.yml

  1# Labels pull requests by author: 'bot' for bot accounts, 'staff' for
  2# staff team members, 'guild' for guild members, 'first contribution' for
  3# first-time external contributors.
  4name: PR Labeler
  5
  6on:
  7  pull_request_target:
  8    types: [opened]
  9
 10permissions:
 11  contents: read
 12
 13jobs:
 14  check-authorship-and-label:
 15    if: github.repository == 'zed-industries/zed'
 16    runs-on: namespace-profile-2x4-ubuntu-2404
 17    timeout-minutes: 5
 18    steps:
 19      - id: get-app-token
 20        uses: actions/create-github-app-token@f8d387b68d61c58ab83c6c016672934102569859 # v3.0.0
 21        with:
 22          app-id: ${{ secrets.ZED_COMMUNITY_BOT_APP_ID }}
 23          private-key: ${{ secrets.ZED_COMMUNITY_BOT_PRIVATE_KEY }}
 24          owner: zed-industries
 25
 26      - id: apply-authorship-label
 27        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
 28        with:
 29          github-token: ${{ steps.get-app-token.outputs.token }}
 30          script: |
 31            const BOT_LABEL = 'bot';
 32            const STAFF_LABEL = 'staff';
 33            const GUILD_LABEL = 'guild';
 34            const FIRST_CONTRIBUTION_LABEL = 'first contribution';
 35            const STAFF_TEAM_SLUG = 'staff';
 36            const GUILD_MEMBERS = [
 37              '11happy',
 38              'AidanV',
 39              'AmaanBilwar',
 40              'OmChillure',
 41              'Palanikannan1437',
 42              'Shivansh-25',
 43              'SkandaBhat',
 44              'TwistingTwists',
 45              'YEDASAVG',
 46              'Ziqi-Yang',
 47              'alanpjohn',
 48              'arjunkomath',
 49              'austincummings',
 50              'ayushk-1801',
 51              'claiwe',
 52              'criticic',
 53              'dongdong867',
 54              'emamulandalib',
 55              'eureka928',
 56              'feitreim',
 57              'iam-liam',
 58              'iksuddle',
 59              'ishaksebsib',
 60              'lingyaochu',
 61              'loadingalias',
 62              'marcocondrache',
 63              'mchisolm0',
 64              'mostlyKIGuess',
 65              'nairadithya',
 66              'nihalxkumar',
 67              'notJoon',
 68              'polyesterswing',
 69              'prayanshchh',
 70              'razeghi71',
 71              'sarmadgulzar',
 72              'seanstrom',
 73              'th0jensen',
 74              'tommyming',
 75              'virajbhartiya',
 76            ];
 77
 78            const pr = context.payload.pull_request;
 79            const author = pr.user.login;
 80
 81            if (pr.user.type === 'Bot') {
 82              await github.rest.issues.addLabels({
 83                owner: context.repo.owner,
 84                repo: context.repo.repo,
 85                issue_number: pr.number,
 86                labels: [BOT_LABEL]
 87              });
 88              console.log(`PR #${pr.number} by ${author}: labeled '${BOT_LABEL}' (user type: '${pr.user.type}')`);
 89              return;
 90            }
 91
 92            let isStaff = false;
 93            try {
 94              const response = await github.rest.teams.getMembershipForUserInOrg({
 95                org: 'zed-industries',
 96                team_slug: STAFF_TEAM_SLUG,
 97                username: author
 98              });
 99              isStaff = response.data.state === 'active';
100            } catch (error) {
101              if (error.status !== 404) {
102                throw error;
103              }
104            }
105
106            if (isStaff) {
107              await github.rest.issues.addLabels({
108                owner: context.repo.owner,
109                repo: context.repo.repo,
110                issue_number: pr.number,
111                labels: [STAFF_LABEL]
112              });
113              console.log(`PR #${pr.number} by ${author}: labeled '${STAFF_LABEL}' (staff team member)`);
114              return;
115            }
116
117            if (GUILD_MEMBERS.includes(author)) {
118              await github.rest.issues.addLabels({
119                owner: context.repo.owner,
120                repo: context.repo.repo,
121                issue_number: pr.number,
122                labels: [GUILD_LABEL]
123              });
124              console.log(`PR #${pr.number} by ${author}: labeled '${GUILD_LABEL}' (guild member)`);
125              // No early return: guild members can also get 'first contribution'
126            }
127
128            // We use inverted logic here due to a suspected GitHub bug where first-time contributors
129            // get 'NONE' instead of 'FIRST_TIME_CONTRIBUTOR' or 'FIRST_TIMER'.
130            // https://github.com/orgs/community/discussions/78038
131            // This will break if GitHub ever adds new associations.
132            const association = pr.author_association;
133            const knownAssociations = ['CONTRIBUTOR', 'COLLABORATOR', 'MEMBER', 'OWNER', 'MANNEQUIN'];
134
135            if (knownAssociations.includes(association)) {
136              console.log(`PR #${pr.number} by ${author}: not a first-time contributor (association: '${association}')`);
137              return;
138            }
139
140            await github.rest.issues.addLabels({
141              owner: context.repo.owner,
142              repo: context.repo.repo,
143              issue_number: pr.number,
144              labels: [FIRST_CONTRIBUTION_LABEL]
145            });
146            console.log(`PR #${pr.number} by ${author}: labeled '${FIRST_CONTRIBUTION_LABEL}' (association: '${association}')`);