orca/.github/workflows/track-community-prs.yaml

120 lines
3.5 KiB
YAML

name: Track Community PRs
on:
pull_request_target:
types: [opened, reopened, ready_for_review]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to backfill or retry'
required: true
type: number
permissions:
contents: read
jobs:
track-community-pr:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Generate bufo-bot token
id: app-token
uses: actions/create-github-app-token@v3
with:
app-id: 2590194
private-key: ${{ secrets.BUFO_BOT_PRIVATE_KEY }}
owner: stablyai
- name: Add PR to project
uses: actions/github-script@v8
env:
PROJECT_OWNER: stablyai
PROJECT_NUMBER: '13'
INTERNAL_TEAM_SLUG: stably-eng
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
const projectOwner = process.env.PROJECT_OWNER;
const projectNumber = Number(process.env.PROJECT_NUMBER);
const internalTeamSlug = process.env.INTERNAL_TEAM_SLUG;
const owner = context.repo.owner;
const repo = context.repo.repo;
const prNumber = context.eventName === 'workflow_dispatch'
? Number(context.payload.inputs.pr_number)
: context.payload.pull_request.number;
const { data: pr } = await github.rest.pulls.get({
owner,
repo,
pull_number: prNumber,
});
const author = pr.user.login;
const skippedAuthors = new Set([
'github-actions[bot]',
'dependabot[bot]',
]);
if (skippedAuthors.has(author)) {
core.info(`Skipping bot PR author ${author}.`);
return;
}
let isInternalAuthor = false;
try {
const membership = await github.rest.teams.getMembershipForUserInOrg({
org: projectOwner,
team_slug: internalTeamSlug,
username: author,
});
isInternalAuthor = membership.data.state === 'active';
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
if (isInternalAuthor) {
core.info(`Skipping internal PR author ${author}.`);
return;
}
const projectResult = await github.graphql(
`
query($owner: String!, $number: Int!) {
organization(login: $owner) {
projectV2(number: $number) {
id
}
}
}
`,
{ owner: projectOwner, number: projectNumber },
);
const projectId = projectResult.organization.projectV2.id;
await github.graphql(
`
mutation($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: {
projectId: $projectId,
contentId: $contentId
}) {
item {
id
}
}
}
`,
{
projectId,
contentId: pr.node_id,
},
);
core.info(`Added PR #${pr.number} (${pr.html_url}) to project ${projectOwner}/${projectNumber}.`);