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@bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1 # v2.1.4
 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              'marcocondrache',
 62              'mchisolm0',
 63              'nairadithya',
 64              'nihalxkumar',
 65              'notJoon',
 66              'polyesterswing',
 67              'prayanshchh',
 68              'razeghi71',
 69              'sarmadgulzar',
 70              'seanstrom',
 71              'th0jensen',
 72              'tommyming',
 73              'virajbhartiya',
 74            ];
 75
 76            const pr = context.payload.pull_request;
 77            const author = pr.user.login;
 78
 79            if (pr.user.type === 'Bot') {
 80              await github.rest.issues.addLabels({
 81                owner: context.repo.owner,
 82                repo: context.repo.repo,
 83                issue_number: pr.number,
 84                labels: [BOT_LABEL]
 85              });
 86              console.log(`PR #${pr.number} by ${author}: labeled '${BOT_LABEL}' (user type: '${pr.user.type}')`);
 87              return;
 88            }
 89
 90            let isStaff = false;
 91            try {
 92              const response = await github.rest.teams.getMembershipForUserInOrg({
 93                org: 'zed-industries',
 94                team_slug: STAFF_TEAM_SLUG,
 95                username: author
 96              });
 97              isStaff = response.data.state === 'active';
 98            } catch (error) {
 99              if (error.status !== 404) {
100                throw error;
101              }
102            }
103
104            if (isStaff) {
105              await github.rest.issues.addLabels({
106                owner: context.repo.owner,
107                repo: context.repo.repo,
108                issue_number: pr.number,
109                labels: [STAFF_LABEL]
110              });
111              console.log(`PR #${pr.number} by ${author}: labeled '${STAFF_LABEL}' (staff team member)`);
112              return;
113            }
114
115            if (GUILD_MEMBERS.includes(author)) {
116              await github.rest.issues.addLabels({
117                owner: context.repo.owner,
118                repo: context.repo.repo,
119                issue_number: pr.number,
120                labels: [GUILD_LABEL]
121              });
122              console.log(`PR #${pr.number} by ${author}: labeled '${GUILD_LABEL}' (guild member)`);
123              // No early return: guild members can also get 'first contribution'
124            }
125
126            // We use inverted logic here due to a suspected GitHub bug where first-time contributors
127            // get 'NONE' instead of 'FIRST_TIME_CONTRIBUTOR' or 'FIRST_TIMER'.
128            // https://github.com/orgs/community/discussions/78038
129            // This will break if GitHub ever adds new associations.
130            const association = pr.author_association;
131            const knownAssociations = ['CONTRIBUTOR', 'COLLABORATOR', 'MEMBER', 'OWNER', 'MANNEQUIN'];
132
133            if (knownAssociations.includes(association)) {
134              console.log(`PR #${pr.number} by ${author}: not a first-time contributor (association: '${association}')`);
135              return;
136            }
137
138            await github.rest.issues.addLabels({
139              owner: context.repo.owner,
140              repo: context.repo.repo,
141              issue_number: pr.number,
142              labels: [FIRST_CONTRIBUTION_LABEL]
143            });
144            console.log(`PR #${pr.number} by ${author}: labeled '${FIRST_CONTRIBUTION_LABEL}' (association: '${association}')`);