1import { createTRPCRouter } from '@/server/api/trpc';
2import z from 'zod';
3import { protectedProcedure } from '../trpc';
4import { Octokit } from 'octokit';
5import { createAppAuth } from '@octokit/auth-app';
6
7export const githubRouter = createTRPCRouter({
8 createIssue: protectedProcedure
9 .input(
10 z.object({
11 title: z.string(),
12 body: z.string(),
13 route: z.string(),
14 label: z.enum(['bug', 'feature', 'enhancement']),
15 })
16 )
17 .mutation(async ({ input, ctx }) => {
18 const octokit = new Octokit({
19 authStrategy: createAppAuth,
20 auth: {
21 appId: process.env.GITHUB_APP_ID ?? '',
22 installationId: process.env.GITHUB_INSTALLATION_ID ?? '',
23 privateKey: (process.env.GITHUB_PRIVATE_KEY ?? '')
24 .split('####')
25 .join('\n'),
26 },
27 });
28
29 const response = await octokit.request(
30 'POST /repos/{owner}/{repo}/issues',
31 {
32 owner: process.env.NEXT_PUBLIC_FEEDBACK_REPO_OWNER ?? '',
33 repo: process.env.NEXT_PUBLIC_FEEDBACK_REPO ?? '',
34 title: input.title,
35 body: `user:${ctx.user.id}\nroute:${input.route}\n\n${input.body}`,
36 labels: [input.label],
37 headers: {
38 'X-GitHub-Api-Version': '2022-11-28',
39 },
40 }
41 );
42
43 return {
44 issueUrl: response.data.html_url,
45 };
46 }),
47});