Skip to content

Commit 9f9fd69

Browse files
committed
feat: various checks before packing
1 parent 52ca3d1 commit 9f9fd69

File tree

2 files changed

+96
-9
lines changed

2 files changed

+96
-9
lines changed

src/app.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}

src/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App } from 'octokit';
1+
import { getApp } from './app.js';
22
import { logger, type Env } from './util.js';
33
import { verifyWebhookSignature } from './verify.js';
44

@@ -13,14 +13,6 @@ const server = {
1313
});
1414
}
1515

16-
const app = new App({
17-
appId: env.APP_ID,
18-
privateKey: env.PRIVATE_KEY.replaceAll('\\n', '\n'),
19-
webhooks: {
20-
secret: env.WEBHOOK_SECRET,
21-
},
22-
});
23-
2416
const id = request.headers.get('x-github-delivery')!;
2517
const name = request.headers.get('x-github-event')!;
2618
const signature = request.headers.get('x-hub-signature-256') ?? '';
@@ -37,6 +29,9 @@ const server = {
3729
});
3830
}
3931

32+
logger.info('signature verified, processing webhook');
33+
const app = getApp(env);
34+
4035
try {
4136
await app.webhooks.receive({
4237
id,

0 commit comments

Comments
 (0)