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