Blame · Line-by-line history
issues.tsx
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 79136bb | 1 | /** |
| 2 | * Issue tracker routes — list, create, view, comment, close/reopen. | |
| 3 | */ | |
| 4 | ||
| 5 | import { Hono } from "hono"; | |
| 6 | import { eq, and, desc, asc, sql } from "drizzle-orm"; | |
| 7 | import { db } from "../db"; | |
| 8 | import { | |
| 9 | issues, | |
| 10 | issueComments, | |
| 11 | repositories, | |
| 12 | users, | |
| 13 | labels, | |
| 14 | issueLabels, | |
| 15 | } from "../db/schema"; | |
| 16 | import { Layout } from "../views/layout"; | |
| 17 | import { RepoHeader, RepoNav } from "../views/components"; | |
| 6fc53bd | 18 | import { ReactionsBar } from "../views/reactions"; |
| 19 | import { summariseReactions } from "../lib/reactions"; | |
| 24cf2ca | 20 | import { loadIssueTemplate } from "../lib/templates"; |
| 79136bb | 21 | import { renderMarkdown } from "../lib/markdown"; |
| 22 | import { softAuth, requireAuth } from "../middleware/auth"; | |
| 23 | import type { AuthEnv } from "../middleware/auth"; | |
| 04f6b7f | 24 | import { requireRepoAccess } from "../middleware/repo-access"; |
| bb0f894 | 25 | import { |
| 26 | Flex, | |
| 27 | Container, | |
| 28 | PageHeader, | |
| 29 | Form, | |
| 30 | FormGroup, | |
| 31 | Input, | |
| 32 | TextArea, | |
| 33 | Button, | |
| 34 | LinkButton, | |
| 35 | Badge, | |
| 36 | EmptyState, | |
| 37 | TabNav, | |
| 38 | FilterTabs, | |
| 39 | List, | |
| 40 | ListItem, | |
| 41 | Alert, | |
| 42 | CommentBox, | |
| 43 | CommentForm, | |
| 44 | formatRelative, | |
| 45 | } from "../views/ui"; | |
| 79136bb | 46 | |
| 47 | const issueRoutes = new Hono<AuthEnv>(); | |
| 48 | ||
| 49 | // Helper to resolve repo from :owner/:repo params | |
| 50 | async function resolveRepo(ownerName: string, repoName: string) { | |
| 51 | const [owner] = await db | |
| 52 | .select() | |
| 53 | .from(users) | |
| 54 | .where(eq(users.username, ownerName)) | |
| 55 | .limit(1); | |
| 56 | if (!owner) return null; | |
| 57 | ||
| 58 | const [repo] = await db | |
| 59 | .select() | |
| 60 | .from(repositories) | |
| 61 | .where( | |
| 62 | and(eq(repositories.ownerId, owner.id), eq(repositories.name, repoName)) | |
| 63 | ) | |
| 64 | .limit(1); | |
| 65 | if (!repo) return null; | |
| 66 | ||
| 67 | return { owner, repo }; | |
| 68 | } | |
| 69 | ||
| 70 | // Issue list | |
| 04f6b7f | 71 | issueRoutes.get("/:owner/:repo/issues", softAuth, requireRepoAccess("read"), async (c) => { |
| 79136bb | 72 | const { owner: ownerName, repo: repoName } = c.req.param(); |
| 73 | const user = c.get("user"); | |
| 74 | const state = c.req.query("state") || "open"; | |
| 75 | ||
| 76 | const resolved = await resolveRepo(ownerName, repoName); | |
| 77 | if (!resolved) { | |
| 78 | return c.html( | |
| 79 | <Layout title="Not Found" user={user}> | |
| bb0f894 | 80 | <EmptyState title="Repository not found" /> |
| 79136bb | 81 | </Layout>, |
| 82 | 404 | |
| 83 | ); | |
| 84 | } | |
| 85 | ||
| 86 | const { repo } = resolved; | |
| 87 | ||
| 88 | const issueList = await db | |
| 89 | .select({ | |
| 90 | issue: issues, | |
| 91 | author: { username: users.username }, | |
| 92 | }) | |
| 93 | .from(issues) | |
| 94 | .innerJoin(users, eq(issues.authorId, users.id)) | |
| 95 | .where( | |
| 96 | and(eq(issues.repositoryId, repo.id), eq(issues.state, state)) | |
| 97 | ) | |
| 98 | .orderBy(desc(issues.createdAt)); | |
| 99 | ||
| 100 | // Count open/closed | |
| 101 | const [counts] = await db | |
| 102 | .select({ | |
| 103 | open: sql<number>`count(*) filter (where ${issues.state} = 'open')`, | |
| 104 | closed: sql<number>`count(*) filter (where ${issues.state} = 'closed')`, | |
| 105 | }) | |
| 106 | .from(issues) | |
| 107 | .where(eq(issues.repositoryId, repo.id)); | |
| 108 | ||
| 109 | return c.html( | |
| 110 | <Layout title={`Issues — ${ownerName}/${repoName}`} user={user}> | |
| 111 | <RepoHeader owner={ownerName} repo={repoName} /> | |
| 112 | <IssueNav owner={ownerName} repo={repoName} active="issues" /> | |
| bb0f894 | 113 | <Flex justify="space-between" align="center" style="margin-bottom:16px"> |
| 114 | <FilterTabs | |
| 115 | tabs={[ | |
| 116 | { | |
| 117 | label: `${counts?.open ?? 0} Open`, | |
| 118 | href: `/${ownerName}/${repoName}/issues?state=open`, | |
| 119 | active: state === "open", | |
| 120 | }, | |
| 121 | { | |
| 122 | label: `${counts?.closed ?? 0} Closed`, | |
| 123 | href: `/${ownerName}/${repoName}/issues?state=closed`, | |
| 124 | active: state === "closed", | |
| 125 | }, | |
| 126 | ]} | |
| 127 | /> | |
| 79136bb | 128 | {user && ( |
| bb0f894 | 129 | <LinkButton |
| 79136bb | 130 | href={`/${ownerName}/${repoName}/issues/new`} |
| bb0f894 | 131 | variant="primary" |
| 79136bb | 132 | > |
| 133 | New issue | |
| bb0f894 | 134 | </LinkButton> |
| 79136bb | 135 | )} |
| bb0f894 | 136 | </Flex> |
| 79136bb | 137 | {issueList.length === 0 ? ( |
| bb0f894 | 138 | <EmptyState> |
| 79136bb | 139 | <p> |
| 140 | No {state} issues. | |
| 141 | {state === "closed" && ( | |
| 142 | <span> | |
| 143 | {" "} | |
| 144 | <a href={`/${ownerName}/${repoName}/issues?state=open`}> | |
| 145 | View open issues | |
| 146 | </a> | |
| 147 | </span> | |
| 148 | )} | |
| 149 | </p> | |
| bb0f894 | 150 | </EmptyState> |
| 79136bb | 151 | ) : ( |
| bb0f894 | 152 | <List> |
| 79136bb | 153 | {issueList.map(({ issue, author }) => ( |
| bb0f894 | 154 | <ListItem> |
| 79136bb | 155 | <div |
| 156 | class={`issue-state-icon ${issue.state === "open" ? "state-open" : "state-closed"}`} | |
| 157 | > | |
| 158 | {issue.state === "open" ? "\u25CB" : "\u2713"} | |
| 159 | </div> | |
| 160 | <div> | |
| 161 | <div class="issue-title"> | |
| 162 | <a href={`/${ownerName}/${repoName}/issues/${issue.number}`}> | |
| 163 | {issue.title} | |
| 164 | </a> | |
| 165 | </div> | |
| 166 | <div class="issue-meta"> | |
| 167 | #{issue.number} opened by {author.username}{" "} | |
| 168 | {formatRelative(issue.createdAt)} | |
| 169 | </div> | |
| 170 | </div> | |
| bb0f894 | 171 | </ListItem> |
| 79136bb | 172 | ))} |
| bb0f894 | 173 | </List> |
| 79136bb | 174 | )} |
| 175 | </Layout> | |
| 176 | ); | |
| 177 | }); | |
| 178 | ||
| 179 | // New issue form | |
| 180 | issueRoutes.get( | |
| 181 | "/:owner/:repo/issues/new", | |
| 182 | softAuth, | |
| 183 | requireAuth, | |
| 04f6b7f | 184 | requireRepoAccess("write"), |
| 79136bb | 185 | async (c) => { |
| 186 | const { owner: ownerName, repo: repoName } = c.req.param(); | |
| 187 | const user = c.get("user")!; | |
| 188 | const error = c.req.query("error"); | |
| 24cf2ca | 189 | const template = await loadIssueTemplate(ownerName, repoName); |
| 79136bb | 190 | |
| 191 | return c.html( | |
| 192 | <Layout title={`New issue — ${ownerName}/${repoName}`} user={user}> | |
| 193 | <RepoHeader owner={ownerName} repo={repoName} /> | |
| 194 | <IssueNav owner={ownerName} repo={repoName} active="issues" /> | |
| bb0f894 | 195 | <Container maxWidth={800}> |
| 196 | <h2 style="margin-bottom:16px">New issue</h2> | |
| 79136bb | 197 | {error && ( |
| bb0f894 | 198 | <Alert variant="error">{decodeURIComponent(error)}</Alert> |
| 79136bb | 199 | )} |
| 0316dbb | 200 | <Form method="post" action={`/${ownerName}/${repoName}/issues/new`}> |
| 201 | <FormGroup> | |
| 202 | <Input | |
| 79136bb | 203 | type="text" |
| 204 | name="title" | |
| 205 | required | |
| 206 | placeholder="Title" | |
| bb0f894 | 207 | style="font-size:16px;padding:10px 14px" |
| 5db1b25 | 208 | aria-label="Issue title" |
| 79136bb | 209 | /> |
| bb0f894 | 210 | </FormGroup> |
| 211 | <FormGroup> | |
| 212 | <TextArea | |
| 79136bb | 213 | name="body" |
| 214 | rows={12} | |
| 215 | placeholder="Leave a comment... (Markdown supported)" | |
| bb0f894 | 216 | mono |
| 79136bb | 217 | /> |
| bb0f894 | 218 | </FormGroup> |
| 219 | <Button type="submit" variant="primary"> | |
| 79136bb | 220 | Submit new issue |
| bb0f894 | 221 | </Button> |
| 222 | </Form> | |
| 223 | </Container> | |
| 79136bb | 224 | </Layout> |
| 225 | ); | |
| 226 | } | |
| 227 | ); | |
| 228 | ||
| 229 | // Create issue | |
| 230 | issueRoutes.post( | |
| 231 | "/:owner/:repo/issues/new", | |
| 232 | softAuth, | |
| 233 | requireAuth, | |
| 04f6b7f | 234 | requireRepoAccess("write"), |
| 79136bb | 235 | async (c) => { |
| 236 | const { owner: ownerName, repo: repoName } = c.req.param(); | |
| 237 | const user = c.get("user")!; | |
| 238 | const body = await c.req.parseBody(); | |
| 239 | const title = String(body.title || "").trim(); | |
| 240 | const issueBody = String(body.body || "").trim(); | |
| 241 | ||
| 242 | if (!title) { | |
| 243 | return c.redirect( | |
| 244 | `/${ownerName}/${repoName}/issues/new?error=Title+is+required` | |
| 245 | ); | |
| 246 | } | |
| 247 | ||
| 248 | const resolved = await resolveRepo(ownerName, repoName); | |
| 249 | if (!resolved) return c.redirect(`/${ownerName}/${repoName}`); | |
| 250 | ||
| 251 | const [issue] = await db | |
| 252 | .insert(issues) | |
| 253 | .values({ | |
| 254 | repositoryId: resolved.repo.id, | |
| 255 | authorId: user.id, | |
| 256 | title, | |
| 257 | body: issueBody || null, | |
| 258 | }) | |
| 259 | .returning(); | |
| 260 | ||
| 261 | // Update issue count | |
| 262 | await db | |
| 263 | .update(repositories) | |
| 264 | .set({ issueCount: resolved.repo.issueCount + 1 }) | |
| 265 | .where(eq(repositories.id, resolved.repo.id)); | |
| 266 | ||
| 267 | return c.redirect( | |
| 268 | `/${ownerName}/${repoName}/issues/${issue.number}` | |
| 269 | ); | |
| 270 | } | |
| 271 | ); | |
| 272 | ||
| 273 | // View single issue | |
| 04f6b7f | 274 | issueRoutes.get("/:owner/:repo/issues/:number", softAuth, requireRepoAccess("read"), async (c) => { |
| 79136bb | 275 | const { owner: ownerName, repo: repoName } = c.req.param(); |
| 276 | const issueNum = parseInt(c.req.param("number"), 10); | |
| 277 | const user = c.get("user"); | |
| 278 | ||
| 279 | const resolved = await resolveRepo(ownerName, repoName); | |
| 280 | if (!resolved) { | |
| 281 | return c.html( | |
| 282 | <Layout title="Not Found" user={user}> | |
| bb0f894 | 283 | <EmptyState title="Not found" /> |
| 79136bb | 284 | </Layout>, |
| 285 | 404 | |
| 286 | ); | |
| 287 | } | |
| 288 | ||
| 289 | const [issue] = await db | |
| 290 | .select() | |
| 291 | .from(issues) | |
| 292 | .where( | |
| 293 | and( | |
| 294 | eq(issues.repositoryId, resolved.repo.id), | |
| 295 | eq(issues.number, issueNum) | |
| 296 | ) | |
| 297 | ) | |
| 298 | .limit(1); | |
| 299 | ||
| 300 | if (!issue) { | |
| 301 | return c.html( | |
| 302 | <Layout title="Not Found" user={user}> | |
| bb0f894 | 303 | <EmptyState title="Issue not found" /> |
| 79136bb | 304 | </Layout>, |
| 305 | 404 | |
| 306 | ); | |
| 307 | } | |
| 308 | ||
| 309 | const [author] = await db | |
| 310 | .select() | |
| 311 | .from(users) | |
| 312 | .where(eq(users.id, issue.authorId)) | |
| 313 | .limit(1); | |
| 314 | ||
| 315 | // Get comments | |
| 316 | const comments = await db | |
| 317 | .select({ | |
| 318 | comment: issueComments, | |
| 319 | author: { username: users.username }, | |
| 320 | }) | |
| 321 | .from(issueComments) | |
| 322 | .innerJoin(users, eq(issueComments.authorId, users.id)) | |
| 323 | .where(eq(issueComments.issueId, issue.id)) | |
| 324 | .orderBy(asc(issueComments.createdAt)); | |
| 325 | ||
| 6fc53bd | 326 | // Load reactions for the issue + each comment in parallel. |
| 327 | const [issueReactions, ...commentReactions] = await Promise.all([ | |
| 328 | summariseReactions("issue", issue.id, user?.id), | |
| 329 | ...comments.map((row) => | |
| 330 | summariseReactions("issue_comment", row.comment.id, user?.id) | |
| 331 | ), | |
| 332 | ]); | |
| 333 | ||
| 79136bb | 334 | const canManage = |
| 335 | user && | |
| 336 | (user.id === resolved.owner.id || user.id === issue.authorId); | |
| 337 | ||
| 338 | return c.html( | |
| 339 | <Layout | |
| 340 | title={`${issue.title} #${issue.number} — ${ownerName}/${repoName}`} | |
| 341 | user={user} | |
| 342 | > | |
| 343 | <RepoHeader owner={ownerName} repo={repoName} /> | |
| 344 | <IssueNav owner={ownerName} repo={repoName} active="issues" /> | |
| 345 | <div class="issue-detail"> | |
| 346 | <h2> | |
| 347 | {issue.title}{" "} | |
| bb0f894 | 348 | <span style="color:var(--text-muted);font-weight:400"> |
| 79136bb | 349 | #{issue.number} |
| 350 | </span> | |
| 351 | </h2> | |
| bb0f894 | 352 | <Flex align="center" gap={8} style="margin:8px 0 20px"> |
| 353 | <Badge variant={issue.state === "open" ? "open" : "closed"}> | |
| 79136bb | 354 | {issue.state === "open" ? "\u25CB Open" : "\u2713 Closed"} |
| bb0f894 | 355 | </Badge> |
| 356 | <span style="color:var(--text-muted);font-size:14px"> | |
| 357 | <strong style="color:var(--text)"> | |
| 79136bb | 358 | {author?.username || "unknown"} |
| 359 | </strong>{" "} | |
| 360 | opened this issue {formatRelative(issue.createdAt)} | |
| 361 | </span> | |
| bb0f894 | 362 | </Flex> |
| 79136bb | 363 | |
| 364 | {issue.body && ( | |
| bb0f894 | 365 | <CommentBox |
| 366 | author={author?.username || "unknown"} | |
| 367 | date={issue.createdAt} | |
| 368 | body={renderMarkdown(issue.body)} | |
| 369 | /> | |
| 79136bb | 370 | )} |
| 371 | ||
| 372 | {comments.map(({ comment, author: commentAuthor }) => ( | |
| bb0f894 | 373 | <CommentBox |
| 374 | author={commentAuthor.username} | |
| 375 | date={comment.createdAt} | |
| 376 | body={renderMarkdown(comment.body)} | |
| 377 | /> | |
| 79136bb | 378 | ))} |
| 379 | ||
| 380 | {user && ( | |
| 381 | <div style="margin-top: 20px"> | |
| 382 | <form | |
| cce7944 | 383 | method="post" |
| 79136bb | 384 | action={`/${ownerName}/${repoName}/issues/${issue.number}/comment`} |
| 385 | > | |
| 386 | <div class="form-group"> | |
| 387 | <textarea | |
| 388 | name="body" | |
| 389 | rows={6} | |
| 390 | required | |
| 391 | placeholder="Leave a comment... (Markdown supported)" | |
| 392 | style="font-family: var(--font-mono); font-size: 13px" | |
| 393 | /> | |
| 394 | </div> | |
| 395 | <div style="display: flex; gap: 8px"> | |
| 396 | <button type="submit" class="btn btn-primary"> | |
| 397 | Comment | |
| 398 | </button> | |
| 399 | {canManage && ( | |
| 400 | <button | |
| 401 | type="submit" | |
| 402 | formaction={`/${ownerName}/${repoName}/issues/${issue.number}/${issue.state === "open" ? "close" : "reopen"}`} | |
| 403 | class={`btn ${issue.state === "open" ? "btn-danger" : ""}`} | |
| 404 | > | |
| 405 | {issue.state === "open" | |
| 406 | ? "Close issue" | |
| 407 | : "Reopen issue"} | |
| 408 | </button> | |
| 409 | )} | |
| 410 | </div> | |
| 411 | </form> | |
| 412 | </div> | |
| 413 | )} | |
| 414 | </div> | |
| 415 | </Layout> | |
| 416 | ); | |
| 417 | }); | |
| 418 | ||
| 419 | // Add comment | |
| 420 | issueRoutes.post( | |
| 421 | "/:owner/:repo/issues/:number/comment", | |
| 422 | softAuth, | |
| 423 | requireAuth, | |
| 04f6b7f | 424 | requireRepoAccess("write"), |
| 79136bb | 425 | async (c) => { |
| 426 | const { owner: ownerName, repo: repoName } = c.req.param(); | |
| 427 | const issueNum = parseInt(c.req.param("number"), 10); | |
| 428 | const user = c.get("user")!; | |
| 429 | const body = await c.req.parseBody(); | |
| 430 | const commentBody = String(body.body || "").trim(); | |
| 431 | ||
| 432 | if (!commentBody) { | |
| 433 | return c.redirect( | |
| 434 | `/${ownerName}/${repoName}/issues/${issueNum}` | |
| 435 | ); | |
| 436 | } | |
| 437 | ||
| 438 | const resolved = await resolveRepo(ownerName, repoName); | |
| 439 | if (!resolved) return c.redirect(`/${ownerName}/${repoName}`); | |
| 440 | ||
| 441 | const [issue] = await db | |
| 442 | .select() | |
| 443 | .from(issues) | |
| 444 | .where( | |
| 445 | and( | |
| 446 | eq(issues.repositoryId, resolved.repo.id), | |
| 447 | eq(issues.number, issueNum) | |
| 448 | ) | |
| 449 | ) | |
| 450 | .limit(1); | |
| 451 | ||
| 452 | if (!issue) return c.redirect(`/${ownerName}/${repoName}/issues`); | |
| 453 | ||
| 454 | await db.insert(issueComments).values({ | |
| 455 | issueId: issue.id, | |
| 456 | authorId: user.id, | |
| 457 | body: commentBody, | |
| 458 | }); | |
| 459 | ||
| 460 | return c.redirect( | |
| 461 | `/${ownerName}/${repoName}/issues/${issueNum}` | |
| 462 | ); | |
| 463 | } | |
| 464 | ); | |
| 465 | ||
| 466 | // Close issue | |
| 467 | issueRoutes.post( | |
| 468 | "/:owner/:repo/issues/:number/close", | |
| 469 | softAuth, | |
| 470 | requireAuth, | |
| 04f6b7f | 471 | requireRepoAccess("write"), |
| 79136bb | 472 | async (c) => { |
| 473 | const { owner: ownerName, repo: repoName } = c.req.param(); | |
| 474 | const issueNum = parseInt(c.req.param("number"), 10); | |
| 475 | ||
| 476 | const resolved = await resolveRepo(ownerName, repoName); | |
| 477 | if (!resolved) return c.redirect(`/${ownerName}/${repoName}`); | |
| 478 | ||
| 479 | await db | |
| 480 | .update(issues) | |
| 481 | .set({ state: "closed", closedAt: new Date(), updatedAt: new Date() }) | |
| 482 | .where( | |
| 483 | and( | |
| 484 | eq(issues.repositoryId, resolved.repo.id), | |
| 485 | eq(issues.number, issueNum) | |
| 486 | ) | |
| 487 | ); | |
| 488 | ||
| 489 | return c.redirect( | |
| 490 | `/${ownerName}/${repoName}/issues/${issueNum}` | |
| 491 | ); | |
| 492 | } | |
| 493 | ); | |
| 494 | ||
| 495 | // Reopen issue | |
| 496 | issueRoutes.post( | |
| 497 | "/:owner/:repo/issues/:number/reopen", | |
| 498 | softAuth, | |
| 499 | requireAuth, | |
| 04f6b7f | 500 | requireRepoAccess("write"), |
| 79136bb | 501 | async (c) => { |
| 502 | const { owner: ownerName, repo: repoName } = c.req.param(); | |
| 503 | const issueNum = parseInt(c.req.param("number"), 10); | |
| 504 | ||
| 505 | const resolved = await resolveRepo(ownerName, repoName); | |
| 506 | if (!resolved) return c.redirect(`/${ownerName}/${repoName}`); | |
| 507 | ||
| 508 | await db | |
| 509 | .update(issues) | |
| 510 | .set({ state: "open", closedAt: null, updatedAt: new Date() }) | |
| 511 | .where( | |
| 512 | and( | |
| 513 | eq(issues.repositoryId, resolved.repo.id), | |
| 514 | eq(issues.number, issueNum) | |
| 515 | ) | |
| 516 | ); | |
| 517 | ||
| 518 | return c.redirect( | |
| 519 | `/${ownerName}/${repoName}/issues/${issueNum}` | |
| 520 | ); | |
| 521 | } | |
| 522 | ); | |
| 523 | ||
| 524 | // Shared nav component with issues tab | |
| 525 | const IssueNav = ({ | |
| 526 | owner, | |
| 527 | repo, | |
| 528 | active, | |
| 529 | }: { | |
| 530 | owner: string; | |
| 531 | repo: string; | |
| 532 | active: "code" | "commits" | "issues"; | |
| 533 | }) => ( | |
| bb0f894 | 534 | <TabNav |
| 535 | tabs={[ | |
| 536 | { label: "Code", href: `/${owner}/${repo}`, active: active === "code" }, | |
| 537 | { label: "Issues", href: `/${owner}/${repo}/issues`, active: active === "issues" }, | |
| 538 | { label: "Commits", href: `/${owner}/${repo}/commits`, active: active === "commits" }, | |
| 539 | ]} | |
| 540 | /> | |
| 79136bb | 541 | ); |
| 542 | ||
| 543 | export default issueRoutes; | |
| 544 | export { IssueNav }; |