File size: 4,656 Bytes
8e23875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/db";
import { issues, triageData, repositories } from "@/db/schema";
import { generateId, now } from "@/lib/utils";
import { eq, and, desc } from "drizzle-orm";

// =============================================================================
// GET /api/issues - Get issues for a repository
// =============================================================================
// 
// Python equivalent (from routes/repository.py):
//   repos = await db.issues.find({"repoId": repo_id}, {"_id": 0}).to_list(1000)
//
export async function GET(request: NextRequest) {
    try {
        const { searchParams } = new URL(request.url);
        const repoId = searchParams.get("repoId");
        const state = searchParams.get("state"); // open, closed, all
        const isPR = searchParams.get("isPR"); // true, false

        // Build query conditions
        const conditions = [];
        if (repoId) {
            conditions.push(eq(issues.repoId, repoId));
        }
        if (state && state !== "all") {
            conditions.push(eq(issues.state, state));
        }
        if (isPR !== null && isPR !== undefined) {
            conditions.push(eq(issues.isPR, isPR === "true"));
        }

        // Execute query with Drizzle
        const result = await db
            .select()
            .from(issues)
            .where(conditions.length > 0 ? and(...conditions) : undefined)
            .orderBy(desc(issues.createdAt))
            .limit(100);

        return NextResponse.json(result, { status: 200 });
    } catch (error) {
        console.error("GET /api/issues error:", error);
        return NextResponse.json(
            { error: "Failed to fetch issues" },
            { status: 500 }
        );
    }
}

// =============================================================================
// POST /api/issues - Create a new issue
// =============================================================================
//
// Python equivalent (from routes/repository.py):
//   issue = Issue(
//       githubIssueId=gh_issue['id'],
//       number=gh_issue['number'],
//       title=gh_issue['title'],
//       body=gh_issue.get('body') or '',
//       authorName=gh_issue['user']['login'],
//       repoId=repository.id,
//       repoName=repository.name,
//       ...
//   )
//   issue_dict = issue.model_dump()
//   issue_dict['createdAt'] = issue_dict['createdAt'].isoformat()
//   await db.issues.insert_one(issue_dict)
//
export async function POST(request: NextRequest) {
    try {
        const body = await request.json();

        // Validate required fields
        const { githubIssueId, number, title, authorName, repoId, repoName } = body;
        if (!githubIssueId || !number || !title || !authorName || !repoId || !repoName) {
            return NextResponse.json(
                { error: "Missing required fields" },
                { status: 400 }
            );
        }

        // Check if repository exists
        const repo = await db
            .select()
            .from(repositories)
            .where(eq(repositories.id, repoId))
            .limit(1);

        if (repo.length === 0) {
            return NextResponse.json(
                { error: "Repository not found" },
                { status: 404 }
            );
        }

        // Check if issue already exists
        const existingIssue = await db
            .select()
            .from(issues)
            .where(eq(issues.githubIssueId, githubIssueId))
            .limit(1);

        if (existingIssue.length > 0) {
            return NextResponse.json(
                { error: "Issue already exists", issue: existingIssue[0] },
                { status: 409 }
            );
        }

        // Create new issue
        const newIssue = {
            id: generateId(),
            githubIssueId,
            number,
            title,
            body: body.body || "",
            authorName,
            repoId,
            repoName,
            owner: body.owner || "",
            repo: body.repo || "",
            htmlUrl: body.htmlUrl || "",
            state: body.state || "open",
            isPR: body.isPR || false,
            createdAt: now(),
        };

        await db.insert(issues).values(newIssue);

        return NextResponse.json(
            { message: "Issue created", issue: newIssue },
            { status: 201 }
        );
    } catch (error) {
        console.error("POST /api/issues error:", error);
        return NextResponse.json(
            { error: "Failed to create issue" },
            { status: 500 }
        );
    }
}