diff --git a/drizzle/migrations/0000_living_clint_barton.sql b/drizzle/migrations/0000_bent_warbound.sql similarity index 68% rename from drizzle/migrations/0000_living_clint_barton.sql rename to drizzle/migrations/0000_bent_warbound.sql index 1e6063b..e89f8de 100644 --- a/drizzle/migrations/0000_living_clint_barton.sql +++ b/drizzle/migrations/0000_bent_warbound.sql @@ -1,4 +1,4 @@ CREATE TABLE `rtmAuthor` ( `authorId` text NOT NULL, - `mergerId` text NOT NULL + `mergerId` text ); diff --git a/drizzle/migrations/meta/0000_snapshot.json b/drizzle/migrations/meta/0000_snapshot.json index 1e1c55e..09cd86d 100644 --- a/drizzle/migrations/meta/0000_snapshot.json +++ b/drizzle/migrations/meta/0000_snapshot.json @@ -1,7 +1,7 @@ { "version": "5", "dialect": "sqlite", - "id": "b0d382b3-a73f-4dc3-b10e-dc36aa66506c", + "id": "97b48966-3e7f-4b02-97ef-92ea25d36f18", "prevId": "00000000-0000-0000-0000-000000000000", "tables": { "rtmAuthor": { @@ -18,7 +18,7 @@ "name": "mergerId", "type": "text", "primaryKey": false, - "notNull": true, + "notNull": false, "autoincrement": false } }, diff --git a/drizzle/migrations/meta/_journal.json b/drizzle/migrations/meta/_journal.json index fbd97e6..a396c21 100644 --- a/drizzle/migrations/meta/_journal.json +++ b/drizzle/migrations/meta/_journal.json @@ -5,8 +5,8 @@ { "idx": 0, "version": "5", - "when": 1714320681654, - "tag": "0000_living_clint_barton", + "when": 1714328964046, + "tag": "0000_bent_warbound", "breakpoints": true } ] diff --git a/package.json b/package.json index 9dd06a2..5cca637 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,10 @@ }, "dependencies": { "@libsql/client": "^0.6.0", + "@octokit/webhooks-types": "^7.5.1", "drizzle-orm": "^0.30.9", - "hono": "^4.2.5" + "hono": "^4.2.5", + "octokit": "^3.2.0" }, "type": "module", "devDependencies": { diff --git a/src/db/schema.ts b/src/db/schema.ts index c4147e8..d7edcb6 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -2,5 +2,5 @@ import { text, sqliteTable } from "drizzle-orm/sqlite-core"; export const rtmAuthor = sqliteTable("rtmAuthor", { authorId: text('authorId').notNull(), - mergerId: text('mergerId').notNull(), + mergerId: text('mergerId'), }); diff --git a/src/index.ts b/src/index.ts index 6db5766..2baacea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,16 +2,21 @@ import { Context, Hono } from 'hono' import { drizzle } from "drizzle-orm/libsql"; import { createClient } from "@libsql/client/web"; import * as schema from './db/schema' +import { IssueCommentEvent } from '@octokit/webhooks-types'; +import { Octokit } from 'octokit'; + +const octokit = new Octokit({ auth: `personal-access-token123` }); export type Bindings = { TURSO_URL: string, TURSO_TOKEN: string, + GITHUB_API_TOKEN: string, } const app = new Hono() app.get('/', async (c) => { - const db = importDB(c) + const { db } = importLibs(c) const fetch = await db.insert(schema.rtmAuthor).values({ authorId: '1', mergerId: '2', @@ -19,14 +24,46 @@ app.get('/', async (c) => { return c.json(fetch.toJSON()) }) -export default { - fetch: app.fetch.bind(app), -} +app.post('/ev/readyToMerge', async (c) => { + const body = await c.req.json() as IssueCommentEvent + const bodyUrl = body.issue.html_url.split('/') + const { db, octokit } = importLibs(c) + let isRTM = false -function importDB(c: Context) { + if (bodyUrl[bodyUrl.length - 2] !== 'pull') + return c.json({ ok: false, message: 'not a pull request' }, 418) + if (!body.comment.body.toLowerCase().includes('ready to merge')) { + isRTM = true + return c.json({ ok: false, message: 'does not contain what i\'m looking for' }, 418) + } + if (!body.comment.body.toLowerCase().includes('approve merge')) + return c.json({ ok: false, message: 'does not contain what i\'m looking for' }, 418) + + switch (isRTM ? 'readyToMerge' : 'approval') { + case 'readyToMerge': + await db.insert(schema.rtmAuthor).values({ + authorId: body.comment.user.id.toString(), + }).execute() + await octokit.rest.issues.createComment({ + body: `Hey @${body.sender.login}`, + issue_number: body.issue.number, + owner: body.repository.owner.login, + repo: body.repository.name, + }) + break + case 'approval': + break + } + + return c.json({ ok: true }) +}) + +export default app +function importLibs(c: Context) { const turso = createClient({ url: c.env.TURSO_URL!, authToken: c.env.TURSO_TOKEN!, }) - return drizzle(turso, { schema }); -} \ No newline at end of file + const octokit = new Octokit({ auth: c.env.GITHUB_API_TOKEN! }); + return { db: drizzle(turso, { schema }), octokit: octokit }; +} diff --git a/wrangler.toml b/wrangler.toml index ac881fa..2b77568 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -1,5 +1,6 @@ name = "sern-automata" compatibility_date = "2023-12-01" +compatibility_flags = [ "nodejs_compat" ] # [vars] # MY_VAR = "my-variable"