|
| 1 | +import type { Octokit } from 'octokit'; |
| 2 | +import { App } from 'octokit'; |
| 3 | +import { logger, type Env } from './util.js'; |
| 4 | + |
| 5 | +async function isPRGreen(octokit: Octokit, owner: string, repo: string, pullNumber: number): Promise<boolean> { |
| 6 | + // First, get the PR data to get the head SHA |
| 7 | + const { data: pullRequest } = await octokit.rest.pulls.get({ |
| 8 | + owner, |
| 9 | + repo, |
| 10 | + pull_number: pullNumber, |
| 11 | + }); |
| 12 | + |
| 13 | + // Get the status of the head commit |
| 14 | + const { data: statusData } = await octokit.rest.repos.getCombinedStatusForRef({ |
| 15 | + owner, |
| 16 | + repo, |
| 17 | + ref: pullRequest.head.sha, |
| 18 | + }); |
| 19 | + |
| 20 | + // Get the check runs for the head commit |
| 21 | + const { data: checkRunsData } = await octokit.rest.checks.listForRef({ |
| 22 | + owner, |
| 23 | + repo, |
| 24 | + ref: pullRequest.head.sha, |
| 25 | + }); |
| 26 | + |
| 27 | + const failedChecks = checkRunsData.check_runs |
| 28 | + .filter((run) => run.status === 'completed' && run.conclusion !== 'success') |
| 29 | + .map((run) => run.name); |
| 30 | + |
| 31 | + return statusData.state === 'success' && failedChecks.length === 0; |
| 32 | +} |
| 33 | + |
| 34 | +export function getApp(env: Env) { |
| 35 | + const app = new App({ |
| 36 | + appId: env.APP_ID, |
| 37 | + privateKey: env.PRIVATE_KEY.replaceAll('\\n', '\n'), |
| 38 | + webhooks: { |
| 39 | + secret: env.WEBHOOK_SECRET, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + app.webhooks.on('issue_comment.created', async (data) => { |
| 44 | + logger.debug('in comment create'); |
| 45 | + |
| 46 | + if (!data.payload.comment.body.startsWith('pack this')) { |
| 47 | + logger.debug(`Comment does not start with 'pack this': ${data.payload.comment.body}`); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (!data.payload.comment.user) { |
| 52 | + logger.debug('Comment does not have a user associated with it'); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (!data.payload.issue.pull_request || data.payload.issue.state !== 'open') { |
| 57 | + logger.debug('Comment is not on a pull request or pull request is not open'); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const { |
| 62 | + data: { permission }, |
| 63 | + } = await data.octokit.rest.repos.getCollaboratorPermissionLevel({ |
| 64 | + owner: data.payload.repository.owner.login, |
| 65 | + repo: data.payload.repository.name, |
| 66 | + username: data.payload.comment.user.login, |
| 67 | + }); |
| 68 | + |
| 69 | + if (permission !== 'admin' && permission !== 'write') { |
| 70 | + logger.debug(`User ${data.payload.comment.user.login} does not have sufficient permissions to pack.`); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if ( |
| 75 | + !(await isPRGreen( |
| 76 | + data.octokit, |
| 77 | + data.payload.repository.owner.login, |
| 78 | + data.payload.repository.name, |
| 79 | + data.payload.issue.number, |
| 80 | + )) |
| 81 | + ) { |
| 82 | + logger.debug('Pull request is not green'); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + logger.debug('Beginning the pack process...'); |
| 87 | + |
| 88 | + // TODO: Invoke workflow |
| 89 | + }); |
| 90 | + |
| 91 | + return app; |
| 92 | +} |
0 commit comments