co-maintainer 0.6.8 → 0.6.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/dist/package.json +1 -1
  2. package/dist/src/ai/fake.d.ts +10 -1
  3. package/dist/src/ai/fake.js +39 -4
  4. package/dist/src/cli/args.js +2 -0
  5. package/dist/src/cli/command_json.d.ts +6 -0
  6. package/dist/src/cli/command_json.js +1 -0
  7. package/dist/src/cli/commands/probe.js +25 -0
  8. package/dist/src/cli/commands/registry.js +11 -0
  9. package/dist/src/cli/commands/review.js +5 -1
  10. package/dist/src/cli/commands/serve.js +10 -0
  11. package/dist/src/cli/commands/set.js +23 -2
  12. package/dist/src/cli/review_args.d.ts +9 -0
  13. package/dist/src/cli/review_args.js +36 -1
  14. package/dist/src/cli/review_github.js +6 -2
  15. package/dist/src/cli/review_result.js +6 -5
  16. package/dist/src/github/app.d.ts +4 -0
  17. package/dist/src/github/app.js +10 -1
  18. package/dist/src/github/permissions.js +40 -0
  19. package/dist/src/local/git_ops.js +3 -2
  20. package/dist/src/local/git_revision.d.ts +5 -2
  21. package/dist/src/local/git_revision.js +17 -10
  22. package/dist/src/pr/checkout.js +14 -5
  23. package/dist/src/pr/codegraph_tools.js +5 -3
  24. package/dist/src/pr/reviewer.d.ts +2 -0
  25. package/dist/src/pr/reviewer.js +1 -1
  26. package/dist/src/remote/client.js +14 -4
  27. package/dist/src/remote/push_client.d.ts +7 -0
  28. package/dist/src/remote/push_client.js +287 -0
  29. package/dist/src/remote/server/body.d.ts +11 -0
  30. package/dist/src/remote/server/body.js +14 -5
  31. package/dist/src/remote/server/routes.d.ts +1 -0
  32. package/dist/src/remote/server/routes.js +61 -4
  33. package/dist/src/remote/server/sync.js +11 -3
  34. package/dist/src/remote/validate.js +17 -0
  35. package/dist/src/server/api/repos.js +13 -2
  36. package/dist/src/server/app.js +5 -6
  37. package/dist/src/server/http.js +5 -0
  38. package/dist/src/server/pages/layout.js +1 -0
  39. package/dist/src/server/pages/repo.d.ts +3 -1
  40. package/dist/src/server/pages/repo.js +4 -1
  41. package/dist/src/server/pages/repo_pushes.d.ts +7 -0
  42. package/dist/src/server/pages/repo_pushes.js +144 -0
  43. package/dist/src/server/pages/repo_settings.js +45 -1
  44. package/dist/src/server/pages/router.js +34 -5
  45. package/dist/src/server/pages/settings.js +1 -1
  46. package/dist/src/server/webhook/index.js +24 -7
  47. package/dist/src/services/dashboard.d.ts +46 -0
  48. package/dist/src/services/dashboard.js +24 -0
  49. package/dist/src/services/jobs.js +18 -3
  50. package/dist/src/services/push_review.d.ts +51 -0
  51. package/dist/src/services/push_review.js +878 -0
  52. package/dist/src/services/setup.js +21 -0
  53. package/dist/src/services/watch_comments.d.ts +10 -0
  54. package/dist/src/services/watch_comments.js +179 -0
  55. package/dist/src/services/watch_issues.d.ts +70 -0
  56. package/dist/src/services/watch_issues.js +266 -0
  57. package/dist/src/services/watch_notes.d.ts +8 -0
  58. package/dist/src/services/watch_notes.js +80 -0
  59. package/dist/src/services/watch_status.d.ts +25 -0
  60. package/dist/src/services/watch_status.js +101 -0
  61. package/dist/src/services/webhook.js +107 -3
  62. package/dist/src/store/migrations.js +63 -0
  63. package/dist/src/store/repos.d.ts +1 -1
  64. package/dist/src/store/rows.d.ts +6 -0
  65. package/dist/src/store/sessions.d.ts +3 -0
  66. package/dist/src/store/sessions.js +5 -0
  67. package/dist/src/store/watch.d.ts +108 -0
  68. package/dist/src/store/watch.js +186 -0
  69. package/dist/src/types.d.ts +2 -0
  70. package/dist/src/util/repo_name.d.ts +2 -0
  71. package/dist/src/util/repo_name.js +56 -0
  72. package/dist/src/util/runtime.js +7 -0
  73. package/dist/src/util/sanitize.d.ts +6 -0
  74. package/dist/src/util/sanitize.js +10 -0
  75. package/package.json +1 -1
@@ -2,17 +2,28 @@ import { readConfig } from "../../config.js";
2
2
  import { errorResponse } from "../errors.js";
3
3
  import { hasDelivery, recordDelivery } from "../../store/deliveries.js";
4
4
  import { dispatchGithubEvent } from "../../services/webhook.js";
5
+ import { readBoundedBytes } from "../../remote/server/body.js";
5
6
  import { verifySignature } from "./signature.js";
7
+ import { canonicalRepoName } from "../../util/repo_name.js";
8
+ /** GitHub caps a webhook payload at 25 MB; nothing larger is genuine. */
9
+ const WEBHOOK_MAX_BODY_BYTES = 25 * 1024 * 1024;
6
10
  function json(status, body) {
7
11
  return Response.json(body, { status });
8
12
  }
9
13
  export async function handleWebhookRequest(request, secret) {
10
- const raw = new Uint8Array(await request.arrayBuffer());
11
- if (secret) {
12
- const header = request.headers.get("x-hub-signature-256");
13
- if (!(await verifySignature(secret, raw, header))) {
14
- return errorResponse(401, "unauthorized", "bad webhook signature");
15
- }
14
+ const bounded = await readBoundedBytes(request, WEBHOOK_MAX_BODY_BYTES);
15
+ if (!bounded.ok) {
16
+ return errorResponse(413, "payload_too_large", "request body too large");
17
+ }
18
+ const raw = bounded.bytes;
19
+ // Fail closed: with no secret configured there is no way to tell a real
20
+ // delivery from a forged one, so reject rather than trust the payload.
21
+ if (!secret) {
22
+ return errorResponse(401, "unauthorized", "webhook secret is not configured, so deliveries cannot be verified");
23
+ }
24
+ const header = request.headers.get("x-hub-signature-256");
25
+ if (!(await verifySignature(secret, raw, header))) {
26
+ return errorResponse(401, "unauthorized", "bad webhook signature");
16
27
  }
17
28
  const deliveryId = request.headers.get("x-github-delivery");
18
29
  const event = request.headers.get("x-github-event");
@@ -34,8 +45,14 @@ export async function handleWebhookRequest(request, secret) {
34
45
  Array.isArray(payload)) {
35
46
  return errorResponse(400, "bad_request", "body is not a JSON object");
36
47
  }
48
+ // GitHub sends the owner's own spelling, which may not be the one this
49
+ // server stored the repository under (issue #67).
50
+ const repository = payload.repository;
51
+ if (repository && typeof repository.full_name === "string") {
52
+ repository.full_name = canonicalRepoName(repository.full_name);
53
+ }
37
54
  const config = readConfig();
38
- const repoFullName = payload.repository?.full_name;
55
+ const repoFullName = repository?.full_name;
39
56
  const repoName = typeof repoFullName === "string" ? repoFullName : undefined;
40
57
  const repoConfig = repoName ? config.repos?.[repoName] : undefined;
41
58
  const maxDiffLines = repoConfig?.maxPullRequestChangeLines ??
@@ -105,6 +105,52 @@ export declare function repoRemoteReviews(fullName: string, limit?: number): {
105
105
  items: ReviewRow[];
106
106
  stats: import("../store/reviews.ts").ReviewStats;
107
107
  };
108
+ /** The Pushes tab: each commit watch review with its findings by severity,
109
+ * how the push arrived, and the issues it opened. */
110
+ export declare function repoPushes(fullName: string, limit?: number): {
111
+ items: {
112
+ via: import("../store/watch.ts").PushSource[];
113
+ p0: number;
114
+ p1: number;
115
+ p2: number;
116
+ p3: number;
117
+ issuesOpened: number;
118
+ id: string;
119
+ kind: string;
120
+ subject_id: string | null;
121
+ repo: string;
122
+ pr_number: number | null;
123
+ branch: string | null;
124
+ token_id: string | null;
125
+ token_name: string | null;
126
+ job_id: string;
127
+ head_sha: string | null;
128
+ base_sha: string | null;
129
+ scope: string;
130
+ model: string;
131
+ findings_count: number;
132
+ open_count: number;
133
+ closed_count: number;
134
+ tokens_in: number;
135
+ tokens_out: number;
136
+ cost: number | null;
137
+ cost_status: "known" | "unknown" | null;
138
+ cost_note: string | null;
139
+ billed_to: "server" | "byok" | null;
140
+ duration_ms: number | null;
141
+ posted_review_id: string | null;
142
+ status: string;
143
+ created_at: string;
144
+ round: number;
145
+ trigger: string | null;
146
+ check_run_id: string | null;
147
+ posted_fallback: number;
148
+ guide_built_at: string | null;
149
+ codegraph: string | null;
150
+ note_comment_id: string | null;
151
+ }[];
152
+ openIssues: number;
153
+ };
108
154
  export declare function repoPulls(fullName: string, page: number, per: number): {
109
155
  items: import("../store/reviews.ts").PullSummary[];
110
156
  page: number;
@@ -8,6 +8,8 @@ import { getRepo, listActiveRepos } from "../store/repos.js";
8
8
  import { listLatestReviewsForRepo, listPullSummaries, listRecentReviews, listReviewsForPr, reviewStats, reviewStatsByDay, reviewStatsByKind, reviewStatsByModel, reviewStatsByRemoteToken, listRemoteReviewsForRepo, reviewStatsByRepo, } from "../store/reviews.js";
9
9
  import { findingStatsBySeverity, listFindingsForReview, } from "../store/findings.js";
10
10
  import { daysAgoIso } from "../util/time.js";
11
+ import { listPushReviewsForRepo, listWatchIssues } from "../store/watch.js";
12
+ import { pushVia } from "./watch_status.js";
11
13
  import { isNotFound, readTextFile } from "../util/runtime.js";
12
14
  export function requireActiveRepo(fullName) {
13
15
  const row = getRepo(fullName);
@@ -116,6 +118,28 @@ export function repoRemoteReviews(fullName, limit = 30) {
116
118
  stats: reviewStats(since, fullName),
117
119
  };
118
120
  }
121
+ /** The Pushes tab: each commit watch review with its findings by severity,
122
+ * how the push arrived, and the issues it opened. */
123
+ export function repoPushes(fullName, limit = 50) {
124
+ requireActiveRepo(fullName);
125
+ const issues = listWatchIssues(fullName);
126
+ return {
127
+ items: listPushReviewsForRepo(fullName, limit).map((review) => {
128
+ const severities = listFindingsForReview(review.id).map((f) => f.severity);
129
+ const count = (severity) => severities.filter((s) => s === severity).length;
130
+ return {
131
+ ...review,
132
+ via: pushVia(fullName, review),
133
+ p0: count("P0"),
134
+ p1: count("P1"),
135
+ p2: count("P2"),
136
+ p3: count("P3"),
137
+ issuesOpened: issues.filter((issue) => issue.opened_review_id === review.id).length,
138
+ };
139
+ }),
140
+ openIssues: issues.filter((issue) => issue.state === "open").length,
141
+ };
142
+ }
119
143
  export function repoPulls(fullName, page, per) {
120
144
  requireActiveRepo(fullName);
121
145
  const safePage = Number.isFinite(page) && page > 0 ? Math.floor(page) : 1;
@@ -6,6 +6,10 @@ import { abortQueuedRemoteReviewRow } from "./remote_review_abort.js";
6
6
  import { getReviewByJobId } from "../store/reviews.js";
7
7
  import { withRateLimitJob } from "./rate_limit.js";
8
8
  const handlers = new Map();
9
+ // Default caps so an unconfigured server is not a free-for-all. An operator can
10
+ // still set 0 in config to opt back into unlimited.
11
+ const DEFAULT_MAX_CONCURRENT_JOBS = 4;
12
+ const DEFAULT_MAX_REMOTE_REVIEWS_PER_TOKEN = 2;
9
13
  export function registerHandler(type, handler) {
10
14
  handlers.set(type, handler);
11
15
  }
@@ -112,11 +116,17 @@ function runningRemoteReviewsPerToken() {
112
116
  }
113
117
  function claimNextEligible() {
114
118
  const config = readConfig();
115
- const perTokenLimit = config.maxConcurrentRemoteReviewsPerToken;
119
+ const perTokenLimit = config.maxConcurrentRemoteReviewsPerToken ??
120
+ DEFAULT_MAX_REMOTE_REVIEWS_PER_TOKEN;
116
121
  const remoteRunning = perTokenLimit
117
122
  ? runningRemoteReviewsPerToken()
118
123
  : undefined;
119
124
  const oldestFirst = listJobs({ status: "queued" }).slice().reverse();
125
+ // Push reviews of one repository run one after another, oldest first, so a
126
+ // later push sees the issues an earlier one opened.
127
+ const pushRepos = new Set(listJobs({ status: "running" })
128
+ .filter((job) => job.type === "push_review")
129
+ .map((job) => job.repo));
120
130
  for (const candidate of oldestFirst) {
121
131
  if (!handlers.has(candidate.type))
122
132
  continue;
@@ -125,6 +135,11 @@ function claimNextEligible() {
125
135
  if (busy)
126
136
  continue;
127
137
  }
138
+ if (candidate.type === "push_review") {
139
+ if (pushRepos.has(candidate.repo))
140
+ continue;
141
+ pushRepos.add(candidate.repo);
142
+ }
128
143
  if (perTokenLimit && candidate.type === "remote_review") {
129
144
  const review = getReviewByJobId(candidate.id);
130
145
  const tokenId = review?.token_id;
@@ -145,7 +160,7 @@ function claimNextEligible() {
145
160
  return undefined;
146
161
  }
147
162
  export async function claimAndRun() {
148
- const limit = readConfig().maxConcurrentJobs;
163
+ const limit = readConfig().maxConcurrentJobs ?? DEFAULT_MAX_CONCURRENT_JOBS;
149
164
  if (limit && inFlight.size >= limit)
150
165
  return false;
151
166
  const job = claimNextEligible();
@@ -159,7 +174,7 @@ export async function claimAndRun() {
159
174
  const inFlight = new Set();
160
175
  let workerTimer;
161
176
  function tick() {
162
- const limit = readConfig().maxConcurrentJobs;
177
+ const limit = readConfig().maxConcurrentJobs ?? DEFAULT_MAX_CONCURRENT_JOBS;
163
178
  while (!limit || inFlight.size < limit) {
164
179
  const job = claimNextEligible();
165
180
  if (!job)
@@ -0,0 +1,51 @@
1
+ import { type Revision } from "../review/revision.ts";
2
+ import { type PushSource } from "../store/watch.ts";
3
+ import type { JobRow } from "../store/rows.ts";
4
+ export declare const PUSH_JOB = "push_review";
5
+ export declare const PUSH_CHECK_JOB = "push_check";
6
+ export declare const CHECK_NAME = "co-maintainer commit watch";
7
+ export declare function isSha(value: unknown): value is string;
8
+ export declare function isZeroSha(value: string): boolean;
9
+ export type PushRequest = {
10
+ repo: string;
11
+ before: string;
12
+ after: string;
13
+ /** `refs/heads/<branch>`. */
14
+ ref: string;
15
+ source: PushSource;
16
+ /** The diff a CI step built. The App path builds its own from GitHub. */
17
+ revision?: Revision;
18
+ deliveryId?: string;
19
+ };
20
+ export type PushOutcome = {
21
+ kind: "enqueued" | "attached";
22
+ jobId: string;
23
+ reviewId: string;
24
+ } | {
25
+ kind: "no_changes";
26
+ } | {
27
+ kind: "skipped";
28
+ reason: string;
29
+ message: string;
30
+ checkRun: boolean;
31
+ }
32
+ /** cancel-and-merge folded this push into a waiting one, and only the
33
+ * client has the diff: it must send `before..after` instead. */
34
+ | {
35
+ kind: "extend";
36
+ before: string;
37
+ };
38
+ export declare const NOT_INITIALIZED = "This repository is not initialized.";
39
+ /** Records a push and decides what happens to it. Synchronous, so the
40
+ * webhook and the remote submit route can call it as they are. */
41
+ export declare function queuePush(req: PushRequest): PushOutcome;
42
+ export type IssueActivity = {
43
+ opened: number[];
44
+ stillFound: number[];
45
+ closed: number[];
46
+ closeDenied: number[];
47
+ closedStillFound: number[];
48
+ };
49
+ export declare function registerPushReviewHandlers(): void;
50
+ /** For the sync route: the stored result of a finished push review. */
51
+ export declare function pushJobStatus(jobId: string): JobRow | undefined;