Spaces:
Sleeping
Sleeping
Commit ·
91d0f07
1
Parent(s): 52c8217
Add simpler /api/issues/reply endpoint
Browse files
src/app/api/issues/reply/route.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Reply to Issue/PR Route
|
| 3 |
+
*
|
| 4 |
+
* POST /api/issues/reply
|
| 5 |
+
* Alternative simpler path for posting comments to GitHub
|
| 6 |
+
*/
|
| 7 |
+
|
| 8 |
+
import { NextRequest, NextResponse } from "next/server";
|
| 9 |
+
import { getCurrentUser } from "@/lib/auth";
|
| 10 |
+
import { createGitHubClient, createIssueComment } from "@/lib/github-client";
|
| 11 |
+
import { db } from "@/db";
|
| 12 |
+
import { issues } from "@/db/schema";
|
| 13 |
+
import { eq } from "drizzle-orm";
|
| 14 |
+
|
| 15 |
+
export async function POST(request: NextRequest) {
|
| 16 |
+
try {
|
| 17 |
+
const user = await getCurrentUser(request);
|
| 18 |
+
if (!user) {
|
| 19 |
+
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
if (!user.githubAccessToken) {
|
| 23 |
+
return NextResponse.json({
|
| 24 |
+
error: "GitHub access token not found"
|
| 25 |
+
}, { status: 401 });
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
const body = await request.json();
|
| 29 |
+
const { issueId, owner: directOwner, repo: directRepo, number: directNumber, message } = body;
|
| 30 |
+
|
| 31 |
+
if (!message) {
|
| 32 |
+
return NextResponse.json({
|
| 33 |
+
error: "message is required"
|
| 34 |
+
}, { status: 400 });
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
let owner: string | null = directOwner;
|
| 38 |
+
let repo: string | null = directRepo;
|
| 39 |
+
let issueNumber: number | null = directNumber;
|
| 40 |
+
|
| 41 |
+
// If direct identifiers not provided, try to get from database
|
| 42 |
+
if (!owner || !repo || !issueNumber) {
|
| 43 |
+
if (!issueId) {
|
| 44 |
+
return NextResponse.json({
|
| 45 |
+
error: "Either issueId or owner/repo/number are required"
|
| 46 |
+
}, { status: 400 });
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
const issue = await db.select()
|
| 50 |
+
.from(issues)
|
| 51 |
+
.where(eq(issues.id, issueId))
|
| 52 |
+
.limit(1);
|
| 53 |
+
|
| 54 |
+
if (!issue[0]) {
|
| 55 |
+
return NextResponse.json({ error: "Issue not found in database" }, { status: 404 });
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
owner = issue[0].owner;
|
| 59 |
+
repo = issue[0].repo;
|
| 60 |
+
issueNumber = issue[0].number;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
if (!owner || !repo || !issueNumber) {
|
| 64 |
+
return NextResponse.json({
|
| 65 |
+
error: "Invalid issue data - missing owner, repo, or number"
|
| 66 |
+
}, { status: 400 });
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
// Post comment to GitHub
|
| 70 |
+
const octokit = createGitHubClient(user.githubAccessToken);
|
| 71 |
+
const comment = await createIssueComment(octokit, owner, repo, issueNumber, message);
|
| 72 |
+
|
| 73 |
+
return NextResponse.json({
|
| 74 |
+
success: true,
|
| 75 |
+
comment,
|
| 76 |
+
commentUrl: comment.html_url,
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
} catch (error: any) {
|
| 80 |
+
console.error("POST /api/issues/reply error:", error);
|
| 81 |
+
|
| 82 |
+
if (error?.status === 404) {
|
| 83 |
+
return NextResponse.json({
|
| 84 |
+
error: "Issue not found on GitHub"
|
| 85 |
+
}, { status: 404 });
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
if (error?.status === 403) {
|
| 89 |
+
return NextResponse.json({
|
| 90 |
+
error: "GitHub API rate limit exceeded or insufficient permissions"
|
| 91 |
+
}, { status: 429 });
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
return NextResponse.json({
|
| 95 |
+
error: "Failed to post comment to GitHub",
|
| 96 |
+
detail: error.message
|
| 97 |
+
}, { status: 500 });
|
| 98 |
+
}
|
| 99 |
+
}
|