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
6 changes: 3 additions & 3 deletions plugins/github-watcher/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const pull = (over: Record<string, unknown> = {}) => ({
headRefName: "feature/widgets",
headRefOid: "0123456789abcdef",
baseRefName: "main",
reviewRequests: { nodes: [{ requestedReviewer: { __typename: "User", login: "carol" } }, { requestedReviewer: { __typename: "Team", name: "backend" } }, { requestedReviewer: null }] },
reviewRequests: { nodes: [{ requestedReviewer: { __typename: "User", login: "carol" } }, { requestedReviewer: { __typename: "Team", combinedSlug: "acme/backend" } }, { requestedReviewer: null }] },
latestReviews: { nodes: [{ author: { login: "bob" }, state: "COMMENTED" }, { author: { login: "jane" }, state: "PENDING" }] },
reviews: {
nodes: [
Expand Down Expand Up @@ -109,7 +109,7 @@ describe("parseTarget", () => {
head: "feature/widgets",
headSha: "0123456789abcdef",
reviewDecision: "review required",
requestedReviewers: ["backend", "carol"],
requestedReviewers: ["acme/backend", "carol"],
reviews: ["bob: commented"],
checks: { state: "failure", failing: ["ci/deploy (https://ci/deploy)", "test (https://ci/test)"] },
});
Expand Down Expand Up @@ -201,7 +201,7 @@ describe("parseSnapshot", () => {
headSha: "0123456789abcdef",
headAuthor: "jane",
reviewDecision: "review required",
requestedReviewers: ["backend", "carol"],
requestedReviewers: ["acme/backend", "carol"],
reviews: [
{ author: "bob", state: "commented", submittedAt: "2026-10-09T08:30:00Z", commit: "aaaa" },
{ author: "carol", state: "approved", submittedAt: "2026-10-09T10:00:00Z", commit: "bbbb" },
Expand Down
9 changes: 6 additions & 3 deletions plugins/github-watcher/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const targetDocument = (issueRelations: string) => `query($owner: String!, $name
mergeCommit { oid }
files(first: 100) { nodes { path } }
timelineItems(last: 1, itemTypes: [BASE_REF_CHANGED_EVENT]) { nodes { ... on BaseRefChangedEvent { createdAt } } }
reviewRequests(first: 20) { nodes { requestedReviewer { __typename ... on User { login } ... on Team { name } ... on Bot { login } ... on Mannequin { login } } } }
reviewRequests(first: 20) { nodes { requestedReviewer { __typename ... on User { login } ... on Team { combinedSlug } ... on Bot { login } ... on Mannequin { login } } } }
latestReviews(first: 50) { nodes { author ${ACTOR} state } }
reviews(last: 50) { nodes { id author ${ACTOR} state body createdAt submittedAt url commit { oid } comments(first: 50) { nodes { ${COMMENT_FIELDS} path } } } }
commits(last: 1) { nodes { commit { oid author { user { login } } statusCheckRollup { state contexts(first: 100) { nodes { __typename ... on CheckRun { name status conclusion detailsUrl } ... on StatusContext { context state targetUrl } } } } } } } } } } }`;
Expand Down Expand Up @@ -239,7 +239,7 @@ const pullNode = z.object({
headRefName: z.string().nullish(),
headRefOid: z.string().nullish(),
baseRefName: z.string().nullish(),
reviewRequests: z.object({ nodes: z.array(z.object({ requestedReviewer: z.object({ __typename: z.string(), login: z.string().nullish(), name: z.string().nullish() }).nullish() })) }).nullish(),
reviewRequests: z.object({ nodes: z.array(z.object({ requestedReviewer: z.object({ __typename: z.string(), login: z.string().nullish(), combinedSlug: z.string().nullish(), name: z.string().nullish() }).nullish() })) }).nullish(),
latestReviews: z.object({ nodes: z.array(z.object({ author: actor, state: z.string() })) }).nullish(),
reviews: z.object({ nodes: z.array(reviewNode) }).nullish(),
commits: z.object({ nodes: z.array(z.object({ commit: z.object({ oid: z.string(), author: z.object({ user: actor }).nullish(), statusCheckRollup: z.object({ state: z.string(), contexts: z.object({ nodes: z.array(contextNode) }).nullish() }).nullish() }) })) }).nullish(),
Expand Down Expand Up @@ -395,7 +395,10 @@ function targetOf({ repo, node }: TargetRead, comments: Comment[]): Target {
head: node.headRefName ?? null,
headSha: commit?.oid ?? node.headRefOid ?? null,
reviewDecision: lower(node.reviewDecision),
requestedReviewers: (node.reviewRequests?.nodes ?? []).map((r) => r.requestedReviewer?.login ?? r.requestedReviewer?.name ?? "").filter((s) => s !== "").sort(),
// A user (and a bot or mannequin) is its login; a team is its
// `combinedSlug` (`org/slug`), which a login-shape test rejects so a team
// request is never read as a delegate (BB-327).
requestedReviewers: (node.reviewRequests?.nodes ?? []).map((r) => r.requestedReviewer?.login ?? r.requestedReviewer?.combinedSlug ?? r.requestedReviewer?.name ?? "").filter((s) => s !== "").sort(),
reviews: (node.latestReviews?.nodes ?? [])
.filter((r) => r.state !== "PENDING")
.map((r) => `${r.author?.login ?? "someone"}: ${lower(r.state)}`)
Expand Down
73 changes: 73 additions & 0 deletions plugins/stewards/approvals-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ export const APPROVAL_MARKER_MIGRATIONS = [
`ALTER TABLE project_approvals ADD COLUMN verdict TEXT NOT NULL DEFAULT 'approved'`,
];

// BB-327: every login seen in a pull request's requested-reviewers list, for
// the binding and URL. GitHub drops a reviewer from the live list once they
// submit a review, and the snapshot carries no request history, so the gate
// remembers the logins it has seen and counts a delegate's later APPROVED
// review against this set as well as the live list. A new table, so a store
// from before this step reads back exactly as before.
export const PULL_REQUESTER_MIGRATIONS = [
`CREATE TABLE IF NOT EXISTS pull_requesters (
binding_id TEXT NOT NULL,
url TEXT NOT NULL,
login TEXT NOT NULL,
created_at INTEGER NOT NULL,
PRIMARY KEY (binding_id, url, login)
)`,
];

// BB-327: the (delegate, head) pairs the plugin has re-requested review from
// after a changes-requested review, so a repeated look does not re-request and
// the status can say the delegate was re-solicited for the current head. A new
// table, so a store from before this step reads back exactly as before.
export const REVIEWER_REQUEST_MIGRATIONS = [
`CREATE TABLE IF NOT EXISTS reviewer_requests (
binding_id TEXT NOT NULL,
url TEXT NOT NULL,
login TEXT NOT NULL,
head_sha TEXT NOT NULL,
created_at INTEGER NOT NULL,
PRIMARY KEY (binding_id, url, login, head_sha)
)`,
];

/**
* A project steward's approval (or decline) of an owner-reviewed pull request,
* bound to its head sha. `commentId` is the GitHub comment id of the
Expand Down Expand Up @@ -69,6 +100,17 @@ export function createApprovalsStore(db: Database.Database, now: () => number) {
const getProjectApproval = db.prepare<[string, string], Record<string, unknown>>("SELECT * FROM project_approvals WHERE binding_id = ? AND head_sha = ?");
const projectApprovalsFor = db.prepare<[string], Record<string, unknown>>("SELECT * FROM project_approvals WHERE binding_id = ? ORDER BY created_at");
const deleteProjectApproval = db.prepare("DELETE FROM project_approvals WHERE binding_id = ? AND head_sha = ?");
// Prepared on first use: the migration tests open stores on partial schemas
// from before this group, and `pull_requesters` is the newest table.
const lazy = <P extends unknown[], R>(sql: string): (() => Database.Statement<P, R>) => {
let prepared: Database.Statement<P, R> | null = null;
return () => (prepared ??= db.prepare<P, R>(sql));
};
const insertPullRequester = lazy<[string, string, string, number], unknown>("INSERT OR IGNORE INTO pull_requesters (binding_id, url, login, created_at) VALUES (?, ?, ?, ?)");
const pullRequestersFor = lazy<[string, string], Record<string, unknown>>("SELECT login FROM pull_requesters WHERE binding_id = ? AND url = ? ORDER BY created_at, login");
const deletePullRequester = lazy<[string, string, string], unknown>("DELETE FROM pull_requesters WHERE binding_id = ? AND url = ? AND login = ?");
const insertReviewerRequest = lazy<[string, string, string, string, number], unknown>("INSERT OR IGNORE INTO reviewer_requests (binding_id, url, login, head_sha, created_at) VALUES (?, ?, ?, ?, ?)");
const reviewerRequestsFor = lazy<[string, string, string], Record<string, unknown>>("SELECT login FROM reviewer_requests WHERE binding_id = ? AND url = ? AND head_sha = ? ORDER BY created_at, login");

const toProjectApproval = (r: Record<string, unknown>): ProjectApproval => ({
bindingId: String(r.binding_id),
Expand Down Expand Up @@ -109,5 +151,36 @@ export function createApprovalsStore(db: Database.Database, now: () => number) {
clearProjectApproval(bindingId: string, headSha: string): boolean {
return deleteProjectApproval.run(bindingId, headSha).changes > 0;
},
// requested reviewers remembered for the delegate rule (BB-327)
/**
* Remember every login seen in a pull request's requested-reviewers list,
* for the binding and URL (BB-327). GitHub drops a reviewer from the live
* list once they submit, so the gate reads this set to count a delegate's
* later APPROVED review.
*/
rememberPullRequesters(bindingId: string, url: string, logins: readonly string[]): void {
const at = now();
for (const raw of logins) {
const login = raw.trim().toLowerCase();
if (login === "") continue;
insertPullRequester().run(bindingId, url, login, at);
}
},
/** The logins remembered as requested reviewers for a binding and URL (BB-327). */
pullRequesters(bindingId: string, url: string): string[] {
return pullRequestersFor().all(bindingId, url).map((r) => String(r.login));
},
/** Forget a remembered requester the owner removed while they had no review, ending that delegation (BB-327). */
forgetPullRequester(bindingId: string, url: string, login: string): void {
deletePullRequester().run(bindingId, url, login.trim().toLowerCase());
},
/** Record that a delegate was re-requested for a head, so a repeated look does not re-request (BB-327). */
recordReviewerRequest(bindingId: string, url: string, login: string, headSha: string): void {
insertReviewerRequest().run(bindingId, url, login.trim().toLowerCase(), headSha, now());
},
/** The delegates already re-requested for a binding, URL and head (BB-327). */
reviewerRequests(bindingId: string, url: string, headSha: string): string[] {
return reviewerRequestsFor().all(bindingId, url, headSha).map((r) => String(r.login));
},
};
}
Loading
Loading