first_contribution_labeler.yml

 1name: First Contribution Labeler
 2
 3on:
 4  pull_request_target:
 5    types: [opened]
 6
 7permissions:
 8  contents: read
 9
10jobs:
11  label_first_contribution:
12    if: github.repository == 'zed-industries/zed'
13    runs-on: ubuntu-latest
14    timeout-minutes: 5
15    steps:
16      - id: get-app-token
17        uses: actions/create-github-app-token@bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1 # v2.1.4
18        with:
19          app-id: ${{ secrets.ZED_COMMUNITY_BOT_APP_ID }}
20          private-key: ${{ secrets.ZED_COMMUNITY_BOT_PRIVATE_KEY }}
21          owner: zed-industries
22
23      - id: check-and-label
24        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
25        with:
26          github-token: ${{ steps.get-app-token.outputs.token }}
27          script: |
28            const LABEL_NAME = 'first contribution';
29
30            const pr = context.payload.pull_request;
31            const author = pr.user.login;
32
33            // Skip bots (they shouldn't have FIRST_TIME* association anyway, but just in case)
34            if (author.endsWith('[bot]')) {
35              console.log(`Skipping bot: ${author}`);
36              return;
37            }
38
39            // Check if this is a first-time contributor.
40            // We use inverted logic here due to a suspected GitHub bug where first-time contributors
41            // get 'NONE' instead of 'FIRST_TIME_CONTRIBUTOR' or 'FIRST_TIMER'.
42            // https://github.com/orgs/community/discussions/78038
43            // This will break if GitHub ever adds new associations.
44            const association = pr.author_association;
45            const knownAssociations = ['CONTRIBUTOR', 'COLLABORATOR', 'MEMBER', 'OWNER', 'MANNEQUIN'];
46
47            if (knownAssociations.includes(association)) {
48              console.log(`Author ${author} has association '${association}', not a first-time contributor`);
49              return;
50            }
51
52            // Skip staff members
53            try {
54              const response = await github.rest.teams.getMembershipForUserInOrg({
55                org: 'zed-industries',
56                team_slug: 'staff',
57                username: author
58              });
59              if (response.data.state === 'active') {
60                console.log(`Skipping staff member: ${author}`);
61                return;
62              }
63            } catch (error) {
64              if (error.status !== 404) {
65                throw error;
66              }
67              // 404 means user is not a staff member, continue
68            }
69
70            await github.rest.issues.addLabels({
71              owner: context.repo.owner,
72              repo: context.repo.repo,
73              issue_number: pr.number,
74              labels: [LABEL_NAME]
75            });
76
77            console.log(`Applied '${LABEL_NAME}' label to PR #${pr.number} by ${author}`);