|
| 1 | +import { Hono } from "hono"; |
| 2 | +import { |
| 3 | + getPullRequest, |
| 4 | + getPullRequestFiles, |
| 5 | + getPullRequestComments, |
| 6 | + createReviewComment, |
| 7 | + replyToComment, |
| 8 | +} from "./github"; |
| 9 | +import { parseDiffWithHighlighting } from "./diff"; |
| 10 | + |
| 11 | +const api = new Hono().basePath("/api"); |
| 12 | + |
| 13 | +// Get PR details |
| 14 | +api.get("/pr/:owner/:repo/:number", async (c) => { |
| 15 | + const { owner, repo, number } = c.req.param(); |
| 16 | + try { |
| 17 | + const pr = await getPullRequest(owner, repo, parseInt(number, 10)); |
| 18 | + return c.json(pr); |
| 19 | + } catch (error) { |
| 20 | + return c.json( |
| 21 | + { error: error instanceof Error ? error.message : "Failed to fetch PR" }, |
| 22 | + 500 |
| 23 | + ); |
| 24 | + } |
| 25 | +}); |
| 26 | + |
| 27 | +// Get PR files |
| 28 | +api.get("/pr/:owner/:repo/:number/files", async (c) => { |
| 29 | + const { owner, repo, number } = c.req.param(); |
| 30 | + try { |
| 31 | + const files = await getPullRequestFiles(owner, repo, parseInt(number, 10)); |
| 32 | + return c.json(files); |
| 33 | + } catch (error) { |
| 34 | + return c.json( |
| 35 | + { error: error instanceof Error ? error.message : "Failed to fetch files" }, |
| 36 | + 500 |
| 37 | + ); |
| 38 | + } |
| 39 | +}); |
| 40 | + |
| 41 | +// Get PR comments |
| 42 | +api.get("/pr/:owner/:repo/:number/comments", async (c) => { |
| 43 | + const { owner, repo, number } = c.req.param(); |
| 44 | + try { |
| 45 | + const comments = await getPullRequestComments( |
| 46 | + owner, |
| 47 | + repo, |
| 48 | + parseInt(number, 10) |
| 49 | + ); |
| 50 | + return c.json(comments); |
| 51 | + } catch (error) { |
| 52 | + return c.json( |
| 53 | + { error: error instanceof Error ? error.message : "Failed to fetch comments" }, |
| 54 | + 500 |
| 55 | + ); |
| 56 | + } |
| 57 | +}); |
| 58 | + |
| 59 | +// Create PR comment |
| 60 | +api.post("/pr/:owner/:repo/:number/comments", async (c) => { |
| 61 | + const { owner, repo, number } = c.req.param(); |
| 62 | + const body = await c.req.json(); |
| 63 | + |
| 64 | + try { |
| 65 | + let comment; |
| 66 | + |
| 67 | + if (body.reply_to_id) { |
| 68 | + comment = await replyToComment( |
| 69 | + owner, |
| 70 | + repo, |
| 71 | + parseInt(number, 10), |
| 72 | + body.reply_to_id, |
| 73 | + body.body |
| 74 | + ); |
| 75 | + } else { |
| 76 | + comment = await createReviewComment( |
| 77 | + owner, |
| 78 | + repo, |
| 79 | + parseInt(number, 10), |
| 80 | + body.body, |
| 81 | + body.commit_id, |
| 82 | + body.path, |
| 83 | + body.line, |
| 84 | + body.side || "RIGHT" |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + return c.json(comment); |
| 89 | + } catch (error) { |
| 90 | + return c.json( |
| 91 | + { error: error instanceof Error ? error.message : "Failed to create comment" }, |
| 92 | + 500 |
| 93 | + ); |
| 94 | + } |
| 95 | +}); |
| 96 | + |
| 97 | +// Parse and highlight diff (server-side with caching) |
| 98 | +api.post("/parse-diff", async (c) => { |
| 99 | + const { patch, filename, previousFilename, sha } = await c.req.json(); |
| 100 | + |
| 101 | + if (!patch || !filename) { |
| 102 | + return c.json({ error: "Missing patch or filename" }, 400); |
| 103 | + } |
| 104 | + |
| 105 | + try { |
| 106 | + // Use SHA as cache key for immutable caching |
| 107 | + const parsed = parseDiffWithHighlighting(patch, filename, previousFilename, sha); |
| 108 | + return c.json(parsed); |
| 109 | + } catch (error) { |
| 110 | + return c.json( |
| 111 | + { error: error instanceof Error ? error.message : "Failed to parse diff" }, |
| 112 | + 500 |
| 113 | + ); |
| 114 | + } |
| 115 | +}); |
| 116 | + |
| 117 | +export default api; |
0 commit comments