pr_labeler.yml

 1# Labels pull requests by author: 'bot' for bot accounts, 'staff' for
 2# staff team members, 'first contribution' for first-time external contributors.
 3name: PR Labeler
 4
 5on:
 6  pull_request_target:
 7    types: [opened]
 8
 9permissions:
10  contents: read
11
12jobs:
13  check-authorship-and-label:
14    if: github.repository == 'zed-industries/zed'
15    runs-on: namespace-profile-2x4-ubuntu-2404
16    timeout-minutes: 5
17    steps:
18      - id: get-app-token
19        uses: actions/create-github-app-token@bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1 # v2.1.4
20        with:
21          app-id: ${{ secrets.ZED_COMMUNITY_BOT_APP_ID }}
22          private-key: ${{ secrets.ZED_COMMUNITY_BOT_PRIVATE_KEY }}
23          owner: zed-industries
24
25      - id: apply-authorship-label
26        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
27        with:
28          github-token: ${{ steps.get-app-token.outputs.token }}
29          script: |
30            const BOT_LABEL = 'bot';
31            const STAFF_LABEL = 'staff';
32            const FIRST_CONTRIBUTION_LABEL = 'first contribution';
33            const STAFF_TEAM_SLUG = 'staff';
34
35            const pr = context.payload.pull_request;
36            const author = pr.user.login;
37
38            if (pr.user.type === 'Bot') {
39              await github.rest.issues.addLabels({
40                owner: context.repo.owner,
41                repo: context.repo.repo,
42                issue_number: pr.number,
43                labels: [BOT_LABEL]
44              });
45              console.log(`PR #${pr.number} by ${author}: labeled '${BOT_LABEL}' (user type: '${pr.user.type}')`);
46              return;
47            }
48
49            let isStaff = false;
50            try {
51              const response = await github.rest.teams.getMembershipForUserInOrg({
52                org: 'zed-industries',
53                team_slug: STAFF_TEAM_SLUG,
54                username: author
55              });
56              isStaff = response.data.state === 'active';
57            } catch (error) {
58              if (error.status !== 404) {
59                throw error;
60              }
61            }
62
63            if (isStaff) {
64              await github.rest.issues.addLabels({
65                owner: context.repo.owner,
66                repo: context.repo.repo,
67                issue_number: pr.number,
68                labels: [STAFF_LABEL]
69              });
70              console.log(`PR #${pr.number} by ${author}: labeled '${STAFF_LABEL}' (staff team member)`);
71              return;
72            }
73
74            // We use inverted logic here due to a suspected GitHub bug where first-time contributors
75            // get 'NONE' instead of 'FIRST_TIME_CONTRIBUTOR' or 'FIRST_TIMER'.
76            // https://github.com/orgs/community/discussions/78038
77            // This will break if GitHub ever adds new associations.
78            const association = pr.author_association;
79            const knownAssociations = ['CONTRIBUTOR', 'COLLABORATOR', 'MEMBER', 'OWNER', 'MANNEQUIN'];
80
81            if (knownAssociations.includes(association)) {
82              console.log(`PR #${pr.number} by ${author}: not a first-time contributor (association: '${association}')`);
83              return;
84            }
85
86            await github.rest.issues.addLabels({
87              owner: context.repo.owner,
88              repo: context.repo.repo,
89              issue_number: pr.number,
90              labels: [FIRST_CONTRIBUTION_LABEL]
91            });
92            console.log(`PR #${pr.number} by ${author}: labeled '${FIRST_CONTRIBUTION_LABEL}' (association: '${association}')`);