Blame · Line-by-line history
insights.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.
| 3ef4c9d | 1 | /** |
| 2 | * Repo insights + milestones. | |
| 3 | * | |
| 4 | * GET /:owner/:repo/insights — contributors, commit activity, gate health, AI-generated summary | |
| 5 | * GET /:owner/:repo/milestones — list | |
| 6 | * POST /:owner/:repo/milestones — create | |
| 7 | * POST /:owner/:repo/milestones/:id/close — close | |
| 8 | * POST /:owner/:repo/milestones/:id/reopen — reopen | |
| 9 | * POST /:owner/:repo/milestones/:id/delete — delete | |
| 10 | */ | |
| 11 | ||
| 12 | import { Hono } from "hono"; | |
| 13 | import { and, desc, eq, sql } from "drizzle-orm"; | |
| 14 | import { db } from "../db"; | |
| 15 | import { | |
| 16 | gateRuns, | |
| 17 | issues, | |
| 18 | milestones, | |
| 19 | pullRequests, | |
| 20 | repositories, | |
| 21 | users, | |
| 22 | } from "../db/schema"; | |
| 23 | import { Layout } from "../views/layout"; | |
| 24 | import { RepoHeader, RepoNav } from "../views/components"; | |
| 25 | import { softAuth, requireAuth } from "../middleware/auth"; | |
| 26 | import type { AuthEnv } from "../middleware/auth"; | |
| 27 | import { getUnreadCount } from "../lib/unread"; | |
| 28 | import { listCommits } from "../git/repository"; | |
| 29 | ||
| 30 | const insights = new Hono<AuthEnv>(); | |
| 31 | insights.use("*", softAuth); | |
| 32 | ||
| 33 | async function loadRepo(owner: string, repo: string) { | |
| 34 | const [row] = await db | |
| 35 | .select({ | |
| 36 | id: repositories.id, | |
| 37 | name: repositories.name, | |
| 38 | defaultBranch: repositories.defaultBranch, | |
| 39 | ownerId: repositories.ownerId, | |
| 40 | starCount: repositories.starCount, | |
| 41 | forkCount: repositories.forkCount, | |
| 42 | }) | |
| 43 | .from(repositories) | |
| 44 | .innerJoin(users, eq(repositories.ownerId, users.id)) | |
| 45 | .where(and(eq(users.username, owner), eq(repositories.name, repo))) | |
| 46 | .limit(1); | |
| 47 | return row; | |
| 48 | } | |
| 49 | ||
| 50 | // ---------- Insights ---------- | |
| 51 | ||
| 52 | insights.get("/:owner/:repo/insights", async (c) => { | |
| 53 | const user = c.get("user"); | |
| 54 | const { owner, repo } = c.req.param(); | |
| 55 | const repoRow = await loadRepo(owner, repo); | |
| 56 | if (!repoRow) return c.notFound(); | |
| 57 | ||
| 58 | const unread = user ? await getUnreadCount(user.id) : 0; | |
| 59 | ||
| 60 | // Commit activity — last 200 commits on default | |
| 61 | const commits = await listCommits( | |
| 62 | owner, | |
| 63 | repo, | |
| 64 | repoRow.defaultBranch, | |
| 65 | 200 | |
| 66 | ); | |
| 67 | ||
| 68 | // Contributors by commit count | |
| 69 | const byAuthor = new Map<string, number>(); | |
| 70 | for (const c0 of commits) { | |
| 71 | byAuthor.set(c0.author, (byAuthor.get(c0.author) || 0) + 1); | |
| 72 | } | |
| 73 | const contributors = [...byAuthor.entries()] | |
| 74 | .sort((a, b) => b[1] - a[1]) | |
| 75 | .slice(0, 10); | |
| 76 | ||
| 77 | // Commits per day (last 30) | |
| 78 | const dayCounts = new Map<string, number>(); | |
| 79 | for (const c0 of commits) { | |
| 80 | const day = c0.date.slice(0, 10); | |
| 81 | dayCounts.set(day, (dayCounts.get(day) || 0) + 1); | |
| 82 | } | |
| 83 | const days: Array<{ date: string; count: number }> = []; | |
| 84 | const now = new Date(); | |
| 85 | for (let i = 29; i >= 0; i--) { | |
| 86 | const d = new Date(now); | |
| 87 | d.setDate(d.getDate() - i); | |
| 88 | const key = d.toISOString().slice(0, 10); | |
| 89 | days.push({ date: key, count: dayCounts.get(key) || 0 }); | |
| 90 | } | |
| 91 | const maxDay = Math.max(1, ...days.map((d) => d.count)); | |
| 92 | ||
| 93 | // Gate health 30d | |
| 94 | const gateStats = await db | |
| 95 | .select({ | |
| 96 | status: gateRuns.status, | |
| 97 | c: sql<number>`count(*)::int`, | |
| 98 | }) | |
| 99 | .from(gateRuns) | |
| 100 | .where(eq(gateRuns.repositoryId, repoRow.id)) | |
| 101 | .groupBy(gateRuns.status); | |
| 102 | ||
| 103 | const statTotals: Record<string, number> = {}; | |
| 104 | let totalRuns = 0; | |
| 105 | for (const r of gateStats) { | |
| 106 | statTotals[r.status] = r.c; | |
| 107 | totalRuns += r.c; | |
| 108 | } | |
| 109 | const greenRate = | |
| 110 | totalRuns === 0 | |
| 111 | ? 100 | |
| 112 | : Math.round( | |
| 113 | (((statTotals.passed || 0) + | |
| 114 | (statTotals.repaired || 0) + | |
| 115 | (statTotals.skipped || 0)) / | |
| 116 | totalRuns) * | |
| 117 | 100 | |
| 118 | ); | |
| 119 | ||
| 120 | // Issues + PR counts | |
| 121 | const [issueStats] = await db | |
| 122 | .select({ | |
| 123 | open: sql<number>`count(*) filter (where ${issues.state} = 'open')::int`, | |
| 124 | closed: sql<number>`count(*) filter (where ${issues.state} = 'closed')::int`, | |
| 125 | }) | |
| 126 | .from(issues) | |
| 127 | .where(eq(issues.repositoryId, repoRow.id)); | |
| 128 | ||
| 129 | const [prStats] = await db | |
| 130 | .select({ | |
| 131 | open: sql<number>`count(*) filter (where ${pullRequests.state} = 'open')::int`, | |
| 132 | merged: sql<number>`count(*) filter (where ${pullRequests.state} = 'merged')::int`, | |
| 133 | closed: sql<number>`count(*) filter (where ${pullRequests.state} = 'closed')::int`, | |
| 134 | }) | |
| 135 | .from(pullRequests) | |
| 136 | .where(eq(pullRequests.repositoryId, repoRow.id)); | |
| 137 | ||
| 138 | return c.html( | |
| 139 | <Layout | |
| 140 | title={`Insights — ${owner}/${repo}`} | |
| 141 | user={user} | |
| 142 | notificationCount={unread} | |
| 143 | > | |
| 144 | <RepoHeader | |
| 145 | owner={owner} | |
| 146 | repo={repo} | |
| 147 | starCount={repoRow.starCount} | |
| 148 | forkCount={repoRow.forkCount} | |
| 149 | currentUser={user?.username || null} | |
| 150 | /> | |
| 151 | <RepoNav owner={owner} repo={repo} active="insights" /> | |
| 152 | <h3 style="margin-bottom: 16px">Insights</h3> | |
| 153 | ||
| 154 | <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 24px"> | |
| 155 | <div class="panel" style="padding: 16px"> | |
| 156 | <div style="font-size: 28px; font-weight: 700; color: var(--green)"> | |
| 157 | {greenRate}% | |
| 158 | </div> | |
| 159 | <div style="font-size: 12px; color: var(--text-muted); text-transform: uppercase"> | |
| 160 | Green rate | |
| 161 | </div> | |
| 162 | </div> | |
| 163 | <div class="panel" style="padding: 16px"> | |
| 164 | <div style="font-size: 28px; font-weight: 700">{commits.length}</div> | |
| 165 | <div style="font-size: 12px; color: var(--text-muted); text-transform: uppercase"> | |
| 166 | Recent commits | |
| 167 | </div> | |
| 168 | </div> | |
| 169 | <div class="panel" style="padding: 16px"> | |
| 170 | <div style="font-size: 28px; font-weight: 700"> | |
| 171 | {(prStats?.open || 0) + (prStats?.merged || 0) + (prStats?.closed || 0)} | |
| 172 | </div> | |
| 173 | <div style="font-size: 12px; color: var(--text-muted); text-transform: uppercase"> | |
| 174 | Pull requests | |
| 175 | </div> | |
| 176 | </div> | |
| 177 | <div class="panel" style="padding: 16px"> | |
| 178 | <div style="font-size: 28px; font-weight: 700"> | |
| 179 | {(issueStats?.open || 0) + (issueStats?.closed || 0)} | |
| 180 | </div> | |
| 181 | <div style="font-size: 12px; color: var(--text-muted); text-transform: uppercase"> | |
| 182 | Issues | |
| 183 | </div> | |
| 184 | </div> | |
| 185 | </div> | |
| 186 | ||
| 187 | <div class="dashboard-section"> | |
| 188 | <h3>Commit activity (last 30 days)</h3> | |
| 189 | <div class="panel" style="padding: 16px"> | |
| 190 | <div style="display: flex; align-items: flex-end; gap: 2px; height: 80px"> | |
| 191 | {days.map((d) => ( | |
| 192 | <div | |
| 193 | title={`${d.date}: ${d.count} commits`} | |
| 194 | style={`flex: 1; background: var(--accent); height: ${Math.max(2, (d.count / maxDay) * 80)}px; border-radius: 2px; opacity: ${d.count === 0 ? 0.2 : 1}`} | |
| 195 | ></div> | |
| 196 | ))} | |
| 197 | </div> | |
| 198 | <div style="display: flex; justify-content: space-between; margin-top: 6px; font-size: 11px; color: var(--text-muted)"> | |
| 199 | <span>{days[0].date}</span> | |
| 200 | <span>{days[days.length - 1].date}</span> | |
| 201 | </div> | |
| 202 | </div> | |
| 203 | </div> | |
| 204 | ||
| 205 | <div class="dashboard-section"> | |
| 206 | <h3>Top contributors</h3> | |
| 207 | <div class="panel"> | |
| 208 | {contributors.length === 0 ? ( | |
| 209 | <div class="panel-empty">No contributors yet.</div> | |
| 210 | ) : ( | |
| 211 | contributors.map(([author, count]) => { | |
| 212 | const pct = Math.round( | |
| 213 | (count / contributors[0][1]) * 100 | |
| 214 | ); | |
| 215 | return ( | |
| 216 | <div class="panel-item"> | |
| 217 | <div style="flex: 1"> | |
| 218 | <div style="font-weight: 500">{author}</div> | |
| 219 | <div | |
| 220 | style={`height: 6px; background: var(--accent); border-radius: 3px; margin-top: 4px; width: ${pct}%`} | |
| 221 | ></div> | |
| 222 | </div> | |
| 223 | <div style="font-size: 12px; color: var(--text-muted); width: 80px; text-align: right"> | |
| 224 | {count} commit{count !== 1 ? "s" : ""} | |
| 225 | </div> | |
| 226 | </div> | |
| 227 | ); | |
| 228 | }) | |
| 229 | )} | |
| 230 | </div> | |
| 231 | </div> | |
| 232 | </Layout> | |
| 233 | ); | |
| 234 | }); | |
| 235 | ||
| 236 | // ---------- Milestones ---------- | |
| 237 | ||
| 238 | insights.get("/:owner/:repo/milestones", async (c) => { | |
| 239 | const user = c.get("user"); | |
| 240 | const { owner, repo } = c.req.param(); | |
| 241 | const repoRow = await loadRepo(owner, repo); | |
| 242 | if (!repoRow) return c.notFound(); | |
| 243 | const unread = user ? await getUnreadCount(user.id) : 0; | |
| 244 | const state = c.req.query("state") || "open"; | |
| 245 | ||
| 246 | const rows = await db | |
| 247 | .select() | |
| 248 | .from(milestones) | |
| 249 | .where( | |
| 250 | and( | |
| 251 | eq(milestones.repositoryId, repoRow.id), | |
| 252 | eq(milestones.state, state) | |
| 253 | ) | |
| 254 | ) | |
| 255 | .orderBy(desc(milestones.createdAt)); | |
| 256 | ||
| 257 | return c.html( | |
| 258 | <Layout | |
| 259 | title={`Milestones — ${owner}/${repo}`} | |
| 260 | user={user} | |
| 261 | notificationCount={unread} | |
| 262 | > | |
| 263 | <RepoHeader | |
| 264 | owner={owner} | |
| 265 | repo={repo} | |
| 266 | starCount={repoRow.starCount} | |
| 267 | forkCount={repoRow.forkCount} | |
| 268 | currentUser={user?.username || null} | |
| 269 | /> | |
| 270 | <RepoNav owner={owner} repo={repo} active="issues" /> | |
| 271 | <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px"> | |
| 272 | <div class="issue-tabs"> | |
| 273 | <a | |
| 274 | href={`/${owner}/${repo}/milestones?state=open`} | |
| 275 | class={state === "open" ? "active" : ""} | |
| 276 | > | |
| 277 | Open | |
| 278 | </a> | |
| 279 | <a | |
| 280 | href={`/${owner}/${repo}/milestones?state=closed`} | |
| 281 | class={state === "closed" ? "active" : ""} | |
| 282 | > | |
| 283 | Closed | |
| 284 | </a> | |
| 285 | </div> | |
| 286 | {user && user.id === repoRow.ownerId && ( | |
| 287 | <a | |
| 288 | href={`/${owner}/${repo}/milestones#new`} | |
| 289 | class="btn btn-primary btn-sm" | |
| 290 | > | |
| 291 | + New milestone | |
| 292 | </a> | |
| 293 | )} | |
| 294 | </div> | |
| 295 | ||
| 296 | {rows.length === 0 ? ( | |
| 297 | <div class="empty-state"> | |
| 298 | <p>No {state} milestones.</p> | |
| 299 | </div> | |
| 300 | ) : ( | |
| 301 | <div class="panel" style="margin-bottom: 24px"> | |
| 302 | {rows.map((m) => ( | |
| 303 | <div class="panel-item" style="justify-content: space-between"> | |
| 304 | <div style="flex: 1"> | |
| 305 | <div style="font-weight: 600">{m.title}</div> | |
| 306 | {m.description && ( | |
| 307 | <div class="meta" style="margin-top: 2px">{m.description}</div> | |
| 308 | )} | |
| 309 | <div class="meta" style="margin-top: 2px"> | |
| 310 | {m.dueDate | |
| 311 | ? `Due ${new Date(m.dueDate).toLocaleDateString()}` | |
| 312 | : "No due date"} | |
| 313 | </div> | |
| 314 | </div> | |
| 315 | {user && user.id === repoRow.ownerId && ( | |
| 316 | <div style="display: flex; gap: 4px"> | |
| 317 | {m.state === "open" ? ( | |
| 318 | <form | |
| 319 | method="POST" | |
| 320 | action={`/${owner}/${repo}/milestones/${m.id}/close`} | |
| 321 | > | |
| 322 | <button type="submit" class="btn btn-sm"> | |
| 323 | Close | |
| 324 | </button> | |
| 325 | </form> | |
| 326 | ) : ( | |
| 327 | <form | |
| 328 | method="POST" | |
| 329 | action={`/${owner}/${repo}/milestones/${m.id}/reopen`} | |
| 330 | > | |
| 331 | <button type="submit" class="btn btn-sm"> | |
| 332 | Reopen | |
| 333 | </button> | |
| 334 | </form> | |
| 335 | )} | |
| 336 | <form | |
| 337 | method="POST" | |
| 338 | action={`/${owner}/${repo}/milestones/${m.id}/delete`} | |
| 339 | onsubmit="return confirm('Delete this milestone?')" | |
| 340 | > | |
| 341 | <button type="submit" class="btn btn-sm btn-danger"> | |
| 342 | Delete | |
| 343 | </button> | |
| 344 | </form> | |
| 345 | </div> | |
| 346 | )} | |
| 347 | </div> | |
| 348 | ))} | |
| 349 | </div> | |
| 350 | )} | |
| 351 | ||
| 352 | {user && user.id === repoRow.ownerId && ( | |
| 353 | <form | |
| 354 | id="new" | |
| 355 | method="POST" | |
| 356 | action={`/${owner}/${repo}/milestones`} | |
| 357 | class="panel" | |
| 358 | style="padding: 16px" | |
| 359 | > | |
| 360 | <h3 style="margin-bottom: 12px">Create milestone</h3> | |
| 361 | <div class="form-group"> | |
| 362 | <label>Title</label> | |
| 363 | <input type="text" name="title" required /> | |
| 364 | </div> | |
| 365 | <div class="form-group"> | |
| 366 | <label>Description</label> | |
| 367 | <textarea name="description" rows={3}></textarea> | |
| 368 | </div> | |
| 369 | <div class="form-group"> | |
| 370 | <label>Due date (optional)</label> | |
| 371 | <input type="date" name="dueDate" /> | |
| 372 | </div> | |
| 373 | <button type="submit" class="btn btn-primary"> | |
| 374 | Create | |
| 375 | </button> | |
| 376 | </form> | |
| 377 | )} | |
| 378 | </Layout> | |
| 379 | ); | |
| 380 | }); | |
| 381 | ||
| 382 | insights.post("/:owner/:repo/milestones", requireAuth, async (c) => { | |
| 383 | const user = c.get("user")!; | |
| 384 | const { owner, repo } = c.req.param(); | |
| 385 | const repoRow = await loadRepo(owner, repo); | |
| 386 | if (!repoRow) return c.notFound(); | |
| 387 | if (repoRow.ownerId !== user.id) return c.redirect(`/${owner}/${repo}/milestones`); | |
| 388 | ||
| 389 | const body = await c.req.parseBody(); | |
| 390 | const title = String(body.title || "").trim(); | |
| 391 | if (!title) return c.redirect(`/${owner}/${repo}/milestones`); | |
| 392 | const description = String(body.description || "").trim() || null; | |
| 393 | const dueDateRaw = String(body.dueDate || "").trim(); | |
| 394 | const dueDate = dueDateRaw ? new Date(dueDateRaw) : null; | |
| 395 | ||
| 396 | try { | |
| 397 | await db.insert(milestones).values({ | |
| 398 | repositoryId: repoRow.id, | |
| 399 | title, | |
| 400 | description, | |
| 401 | dueDate, | |
| 402 | }); | |
| 403 | } catch (err) { | |
| 404 | console.error("[milestones] create:", err); | |
| 405 | } | |
| 406 | ||
| 407 | return c.redirect(`/${owner}/${repo}/milestones`); | |
| 408 | }); | |
| 409 | ||
| 410 | insights.post("/:owner/:repo/milestones/:id/close", requireAuth, async (c) => { | |
| 411 | const user = c.get("user")!; | |
| 412 | const { owner, repo, id } = c.req.param(); | |
| 413 | const repoRow = await loadRepo(owner, repo); | |
| 414 | if (!repoRow || repoRow.ownerId !== user.id) { | |
| 415 | return c.redirect(`/${owner}/${repo}/milestones`); | |
| 416 | } | |
| 417 | await db | |
| 418 | .update(milestones) | |
| 419 | .set({ state: "closed", closedAt: new Date() }) | |
| 420 | .where( | |
| 421 | and(eq(milestones.id, id), eq(milestones.repositoryId, repoRow.id)) | |
| 422 | ); | |
| 423 | return c.redirect(`/${owner}/${repo}/milestones`); | |
| 424 | }); | |
| 425 | ||
| 426 | insights.post("/:owner/:repo/milestones/:id/reopen", requireAuth, async (c) => { | |
| 427 | const user = c.get("user")!; | |
| 428 | const { owner, repo, id } = c.req.param(); | |
| 429 | const repoRow = await loadRepo(owner, repo); | |
| 430 | if (!repoRow || repoRow.ownerId !== user.id) { | |
| 431 | return c.redirect(`/${owner}/${repo}/milestones`); | |
| 432 | } | |
| 433 | await db | |
| 434 | .update(milestones) | |
| 435 | .set({ state: "open", closedAt: null }) | |
| 436 | .where( | |
| 437 | and(eq(milestones.id, id), eq(milestones.repositoryId, repoRow.id)) | |
| 438 | ); | |
| 439 | return c.redirect(`/${owner}/${repo}/milestones?state=closed`); | |
| 440 | }); | |
| 441 | ||
| 442 | insights.post("/:owner/:repo/milestones/:id/delete", requireAuth, async (c) => { | |
| 443 | const user = c.get("user")!; | |
| 444 | const { owner, repo, id } = c.req.param(); | |
| 445 | const repoRow = await loadRepo(owner, repo); | |
| 446 | if (!repoRow || repoRow.ownerId !== user.id) { | |
| 447 | return c.redirect(`/${owner}/${repo}/milestones`); | |
| 448 | } | |
| 449 | await db | |
| 450 | .delete(milestones) | |
| 451 | .where( | |
| 452 | and(eq(milestones.id, id), eq(milestones.repositoryId, repoRow.id)) | |
| 453 | ); | |
| 454 | return c.redirect(`/${owner}/${repo}/milestones`); | |
| 455 | }); | |
| 456 | ||
| 457 | export default insights; |