Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pm-stack/projector/src/projector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ function fromIssueOrPr(repo: string, obj: Record<string, any>, kind: "issue" | "
? obj.labels.map((l: any) => (typeof l === "string" ? l : l?.name)).filter(Boolean)
: [];
const assignees = Array.isArray(obj.assignees) ? obj.assignees.map((a: any) => a?.login).filter(Boolean) : [];
// Merge fields (draft / merged / merged_at) exist only on PR-shaped
// payloads. Issue-shaped payloads (the `issue` object of issues and
// issue_comment events, even when it describes a PR) carry none of them:
// emit null so the store preserves the stored values instead of
// resetting them to defaults (#4158).
const carriesMergeFields = "merged" in obj || "merged_at" in obj || "draft" in obj;
return {
ref: itemRef(repo, number),
repo,
Expand All @@ -56,15 +62,15 @@ function fromIssueOrPr(repo: string, obj: Record<string, any>, kind: "issue" | "
title: obj.title ?? "",
state: obj.state === "closed" ? "closed" : "open",
state_reason: iso(obj.state_reason),
draft: obj.draft ? 1 : 0,
merged: obj.merged ? 1 : 0,
draft: carriesMergeFields ? (obj.draft ? 1 : 0) : null,
merged: carriesMergeFields ? (obj.merged ? 1 : 0) : null,
author: iso(obj.user?.login),
labels: jstr(labels),
assignees: jstr(assignees),
created_at: createdAt,
updated_at: updatedAt,
closed_at: iso(obj.closed_at),
merged_at: iso(obj.merged_at),
merged_at: carriesMergeFields ? iso(obj.merged_at) : null,
source_updated_at: updatedAt,
};
}
Expand Down
74 changes: 49 additions & 25 deletions pm-stack/projector/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ export interface ItemRow {
title: string;
state: "open" | "closed";
state_reason: string | null;
draft: number;
merged: number;
// draft / merged are null when the source payload's shape does not carry
// merge fields at all (issue-shaped payloads, #4158): an upsert then
// preserves the stored values instead of resetting them to defaults.
draft: number | null;
merged: number | null;
author: string | null;
labels: string;
assignees: string;
Expand Down Expand Up @@ -104,33 +107,18 @@ const now = () => new Date().toISOString();

/** Upsert an item. Monotonic: a stale source (older updated_at) never overwrites. */
export async function upsertItem(db: Client, i: Omit<ItemRow, "fetched_at">): Promise<void> {
// Two steps, so "the payload shape does not carry merge fields" (null
// draft/merged, #4158) stays distinguishable from an explicit 0 on the
// update path: the INSERT materializes defaults for a brand-new row, the
// UPDATE preserves the stored merge fields whenever the incoming row
// does not carry them. (A single INSERT ... ON CONFLICT cannot do this:
// excluded.draft would already be the COALESCE'd insert value.)
await db.execute({
sql: `INSERT INTO pmp_items (
sql: `INSERT OR IGNORE INTO pmp_items (
ref, repo, number, kind, url, api_url, title, state, state_reason,
draft, merged, author, labels, assignees, created_at, updated_at,
closed_at, merged_at, source_updated_at, fetched_at
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
ON CONFLICT(ref) DO UPDATE SET
repo = excluded.repo,
number = excluded.number,
kind = excluded.kind,
url = excluded.url,
api_url = excluded.api_url,
title = excluded.title,
state = excluded.state,
state_reason = excluded.state_reason,
draft = excluded.draft,
merged = excluded.merged,
author = excluded.author,
labels = excluded.labels,
assignees = excluded.assignees,
created_at = excluded.created_at,
updated_at = excluded.updated_at,
closed_at = excluded.closed_at,
merged_at = excluded.merged_at,
source_updated_at = excluded.source_updated_at,
fetched_at = excluded.fetched_at
WHERE excluded.source_updated_at >= pmp_items.source_updated_at`,
) VALUES (?,?,?,?,?,?,?,?,?,COALESCE(?,0),COALESCE(?,0),?,?,?,?,?,?,?,?,?)`,
args: [
i.ref,
i.repo,
Expand All @@ -154,6 +142,42 @@ export async function upsertItem(db: Client, i: Omit<ItemRow, "fetched_at">): Pr
now(),
],
});
await db.execute({
sql: `UPDATE pmp_items SET
repo = ?, number = ?, kind = ?, url = ?, api_url = ?, title = ?,
state = ?, state_reason = ?,
draft = COALESCE(?, draft),
merged = COALESCE(?, merged),
author = ?, labels = ?, assignees = ?, created_at = ?, updated_at = ?,
closed_at = ?,
merged_at = CASE WHEN ? IS NULL THEN merged_at ELSE ? END,
source_updated_at = ?, fetched_at = ?
WHERE ref = ? AND ? >= source_updated_at`,
args: [
i.repo,
i.number,
i.kind,
i.url,
i.api_url,
i.title,
i.state,
i.state_reason,
i.draft,
i.merged,
i.author,
i.labels,
i.assignees,
i.created_at,
i.updated_at,
i.closed_at,
i.merged,
i.merged_at,
i.source_updated_at,
now(),
i.ref,
i.source_updated_at,
],
});
}

export async function getItems(db: Client, refs: string[]): Promise<ItemRow[]> {
Expand Down
158 changes: 158 additions & 0 deletions pm-stack/projector/test/projector.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { describe, test, expect } from "bun:test";
import { applyEvent, type ProjectorStore, prFromPayload, repoOf } from "../src/projector";
import type { ItemRow, CommentRow } from "../src/store";
import {
migrate,
upsertItem as storeUpsertItem,
upsertComment as storeUpsertComment,
markCommentDeleted as storeMarkCommentDeleted,
recordPush as storeRecordPush,
getItems,
} from "../src/store";
import { createClient, type Client } from "@libsql/client";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

function fakeStore() {
const items = new Map<string, Omit<ItemRow, "fetched_at">>();
Expand All @@ -9,6 +21,10 @@ function fakeStore() {
const deleted: string[] = [];
const store: ProjectorStore = {
async upsertItem(i) {
const prev = items.get(i.ref);
if (prev && i.merged === null) {
i = { ...i, draft: prev.draft, merged: prev.merged, merged_at: prev.merged_at };
}
items.set(i.ref, i);
},
async upsertComment(c) {
Expand Down Expand Up @@ -268,6 +284,148 @@ describe("projector: comment deletion + push", () => {
});
});

describe("projector: issue-shaped payloads never reset merge fields (#4158)", () => {
const repo = { full_name: "developing-today/code" };
// The `issue` object GitHub sends for a PR in issues / issue_comment
// events: it has a pull_request link but NO merged / merged_at / draft.
const issueShapeForPr = (over: Record<string, any>) => ({
number: 400,
title: "Merged feature",
state: "closed",
html_url: "https://github.com/developing-today/code/pull/400",
url: "https://api.github.com/repos/developing-today/code/issues/400",
user: { login: "dezren39" },
labels: [{ name: "bug" }],
pull_request: {
url: "https://api.github.com/repos/developing-today/code/pulls/400",
merged_at: "2026-10-25T03:00:00Z",
},
created_at: "2026-10-25T00:00:00Z",
updated_at: "2026-10-25T03:00:00Z",
closed_at: "2026-10-25T03:00:00Z",
...over,
});
const mergedPrEvent = {
action: "closed",
repository: repo,
pull_request: {
number: 400,
title: "Merged feature",
state: "closed",
merged: true,
draft: false,
html_url: "https://github.com/developing-today/code/pull/400",
url: "https://api.github.com/repos/developing-today/code/pulls/400",
user: { login: "dezren39" },
labels: [],
created_at: "2026-10-25T00:00:00Z",
updated_at: "2026-10-25T03:00:00Z",
merged_at: "2026-10-25T03:00:00Z",
closed_at: "2026-10-25T03:00:00Z",
},
};

test("merged PR survives issues and issue_comment events (fake store)", async () => {
const { store, items } = fakeStore();
await applyEvent(store, "pull_request", mergedPrEvent);
await applyEvent(store, "issues", {
action: "labeled",
repository: repo,
issue: issueShapeForPr({ updated_at: "2026-10-25T04:00:00Z" }),
});
await applyEvent(store, "issue_comment", {
action: "created",
repository: repo,
issue: issueShapeForPr({ updated_at: "2026-10-25T05:00:00Z" }),
comment: {
id: 900,
body: "post-merge comment",
user: { login: "dezren39" },
created_at: "2026-10-25T05:00:00Z",
updated_at: "2026-10-25T05:00:00Z",
html_url: "https://github.com/developing-today/code/pull/400#issuecomment-900",
},
});
const row = items.get("developing-today/code#400");
expect(row?.merged).toBe(1);
expect(row?.merged_at).toBe("2026-10-25T03:00:00Z");
expect(row?.state).toBe("closed");
// The later issue-shaped upserts really did apply (labels moved).
expect(JSON.parse(row!.labels)).toEqual(["bug"]);
});

test("PR-shaped payloads still update merge fields (fake store)", async () => {
const { store, items } = fakeStore();
const pr = {
number: 401,
title: "Draft work",
state: "open",
merged: false,
merged_at: null,
draft: true,
html_url: "https://github.com/developing-today/code/pull/401",
url: "https://api.github.com/repos/developing-today/code/pulls/401",
user: { login: "dezren39" },
created_at: "2026-10-25T00:00:00Z",
updated_at: "2026-10-25T00:00:00Z",
};
await applyEvent(store, "pull_request", { action: "opened", repository: repo, pull_request: pr });
expect(items.get("developing-today/code#401")?.draft).toBe(1);
await applyEvent(store, "pull_request", {
action: "ready_for_review",
repository: repo,
pull_request: { ...pr, draft: false, updated_at: "2026-10-25T01:00:00Z" },
});
const row = items.get("developing-today/code#401");
expect(row?.draft).toBe(0);
expect(row?.merged).toBe(0);
expect(row?.merged_at).toBeNull();
});

test("merged PR survives issues and issue_comment events (real store)", async () => {
const dir = mkdtempSync(join(tmpdir(), "projector-4158-"));
const db: Client = createClient({ url: `file:${join(dir, "test.db")}` });
try {
await migrate(db);
const store: ProjectorStore = {
upsertItem: (i) => storeUpsertItem(db, i),
upsertComment: (c) => storeUpsertComment(db, c),
markCommentDeleted: (c) => storeMarkCommentDeleted(db, c),
recordPush: (p) => storeRecordPush(db, p),
};
await applyEvent(store, "pull_request", mergedPrEvent);
await applyEvent(store, "issues", {
action: "labeled",
repository: repo,
issue: issueShapeForPr({ updated_at: "2026-10-25T04:00:00Z" }),
});
await applyEvent(store, "issue_comment", {
action: "created",
repository: repo,
issue: issueShapeForPr({ updated_at: "2026-10-25T05:00:00Z" }),
comment: {
id: 901,
body: "post-merge comment",
user: { login: "dezren39" },
created_at: "2026-10-25T05:00:00Z",
updated_at: "2026-10-25T05:00:00Z",
html_url: "https://github.com/developing-today/code/pull/400#issuecomment-901",
},
});
const rows = await getItems(db, ["developing-today/code#400"]);
expect(rows.length).toBe(1);
expect(rows[0].merged).toBe(1);
expect(rows[0].merged_at).toBe("2026-10-25T03:00:00Z");
expect(rows[0].draft).toBe(0);
expect(rows[0].state).toBe("closed");
expect(JSON.parse(rows[0].labels)).toEqual(["bug"]);
} finally {
db.close();
rmSync(dir, { recursive: true, force: true });
}
});
});

describe("projector: helpers", () => {
test("repoOf and prFromPayload", () => {
expect(repoOf({ repository: { full_name: "a/b" } })).toBe("a/b");
Expand Down
Loading