|
| 1 | +name: Track Community PRs |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, reopened, ready_for_review] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + pr_number: |
| 9 | + description: 'PR number to backfill or retry' |
| 10 | + required: true |
| 11 | + type: number |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: read |
| 15 | + |
| 16 | +jobs: |
| 17 | + track-community-pr: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + timeout-minutes: 5 |
| 20 | + steps: |
| 21 | + - name: Generate bufo-bot token |
| 22 | + id: app-token |
| 23 | + uses: actions/create-github-app-token@v2 |
| 24 | + with: |
| 25 | + app-id: 2590194 |
| 26 | + private-key: ${{ secrets.BUFO_BOT_PRIVATE_KEY }} |
| 27 | + owner: stablyai |
| 28 | + |
| 29 | + - name: Create tracking issue |
| 30 | + uses: actions/github-script@v7 |
| 31 | + env: |
| 32 | + PROJECT_OWNER: stablyai |
| 33 | + PROJECT_NUMBER: '13' |
| 34 | + INTERNAL_TEAM_SLUG: stably-eng |
| 35 | + TRACKING_LABEL: community-pr |
| 36 | + with: |
| 37 | + github-token: ${{ steps.app-token.outputs.token }} |
| 38 | + script: | |
| 39 | + const projectOwner = process.env.PROJECT_OWNER; |
| 40 | + const projectNumber = Number(process.env.PROJECT_NUMBER); |
| 41 | + const internalTeamSlug = process.env.INTERNAL_TEAM_SLUG; |
| 42 | + const trackingLabel = process.env.TRACKING_LABEL; |
| 43 | + const owner = context.repo.owner; |
| 44 | + const repo = context.repo.repo; |
| 45 | +
|
| 46 | + const prNumber = context.eventName === 'workflow_dispatch' |
| 47 | + ? Number(context.payload.inputs.pr_number) |
| 48 | + : context.payload.pull_request.number; |
| 49 | +
|
| 50 | + const { data: pr } = await github.rest.pulls.get({ |
| 51 | + owner, |
| 52 | + repo, |
| 53 | + pull_number: prNumber, |
| 54 | + }); |
| 55 | +
|
| 56 | + const author = pr.user.login; |
| 57 | + const markerText = `community-pr-tracker-${owner}-${repo}-${pr.number}`; |
| 58 | + const marker = `<!-- ${markerText} -->`; |
| 59 | +
|
| 60 | + const skippedAuthors = new Set([ |
| 61 | + 'github-actions[bot]', |
| 62 | + 'dependabot[bot]', |
| 63 | + ]); |
| 64 | +
|
| 65 | + if (skippedAuthors.has(author)) { |
| 66 | + core.info(`Skipping bot PR author ${author}.`); |
| 67 | + return; |
| 68 | + } |
| 69 | +
|
| 70 | + let isInternalAuthor = false; |
| 71 | + try { |
| 72 | + const membership = await github.rest.teams.getMembershipForUserInOrg({ |
| 73 | + org: projectOwner, |
| 74 | + team_slug: internalTeamSlug, |
| 75 | + username: author, |
| 76 | + }); |
| 77 | + isInternalAuthor = membership.data.state === 'active'; |
| 78 | + } catch (error) { |
| 79 | + if (error.status !== 404) { |
| 80 | + throw error; |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | + if (isInternalAuthor) { |
| 85 | + core.info(`Skipping internal PR author ${author}.`); |
| 86 | + return; |
| 87 | + } |
| 88 | +
|
| 89 | + const existingTrackingIssues = await github.paginate( |
| 90 | + github.rest.search.issuesAndPullRequests, |
| 91 | + { |
| 92 | + q: `repo:${owner}/${repo} is:issue in:body ${markerText}`, |
| 93 | + per_page: 100, |
| 94 | + }, |
| 95 | + ); |
| 96 | +
|
| 97 | + if (existingTrackingIssues.length > 0) { |
| 98 | + core.info(`Tracking issue already exists for PR #${pr.number}: ${existingTrackingIssues[0].html_url}`); |
| 99 | + return; |
| 100 | + } |
| 101 | +
|
| 102 | + try { |
| 103 | + await github.rest.issues.getLabel({ |
| 104 | + owner, |
| 105 | + repo, |
| 106 | + name: trackingLabel, |
| 107 | + }); |
| 108 | + } catch (error) { |
| 109 | + if (error.status !== 404) { |
| 110 | + throw error; |
| 111 | + } |
| 112 | +
|
| 113 | + await github.rest.issues.createLabel({ |
| 114 | + owner, |
| 115 | + repo, |
| 116 | + name: trackingLabel, |
| 117 | + color: '0E8A16', |
| 118 | + description: 'Tracks external community pull requests.', |
| 119 | + }); |
| 120 | + } |
| 121 | +
|
| 122 | + const issue = await github.rest.issues.create({ |
| 123 | + owner, |
| 124 | + repo, |
| 125 | + title: `Review community PR #${pr.number}: ${pr.title}`, |
| 126 | + body: [ |
| 127 | + marker, |
| 128 | + '', |
| 129 | + `Community PR opened by @${author}:`, |
| 130 | + '', |
| 131 | + pr.html_url, |
| 132 | + '', |
| 133 | + 'Use this issue for triage, ownership, priority, and review tracking.', |
| 134 | + ].join('\n'), |
| 135 | + labels: [trackingLabel], |
| 136 | + }); |
| 137 | +
|
| 138 | + const projectResult = await github.graphql( |
| 139 | + ` |
| 140 | + query($owner: String!, $number: Int!) { |
| 141 | + organization(login: $owner) { |
| 142 | + projectV2(number: $number) { |
| 143 | + id |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + `, |
| 148 | + { owner: projectOwner, number: projectNumber }, |
| 149 | + ); |
| 150 | +
|
| 151 | + const projectId = projectResult.organization.projectV2.id; |
| 152 | +
|
| 153 | + await github.graphql( |
| 154 | + ` |
| 155 | + mutation($projectId: ID!, $contentId: ID!) { |
| 156 | + addProjectV2ItemById(input: { |
| 157 | + projectId: $projectId, |
| 158 | + contentId: $contentId |
| 159 | + }) { |
| 160 | + item { |
| 161 | + id |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + `, |
| 166 | + { |
| 167 | + projectId, |
| 168 | + contentId: issue.data.node_id, |
| 169 | + }, |
| 170 | + ); |
| 171 | +
|
| 172 | + core.info(`Created ${issue.data.html_url} and added it to project ${projectOwner}/${projectNumber}.`); |
0 commit comments