Remove selected_repository_ids that are no longer accessible via the GitHub installation. * Unlike pruneStaleReposFromConfig (which prunes by repo name after sync), this handles * repos that silently vanished from the installation and were never synced at all.
( db: WorkerDb, owner: SecurityReviewOwner, accessibleRepoIds: Set<number> )
| 1547 | } |
| 1548 | |
| 1549 | async function insertStagedNewFindingNotifications( |
| 1550 | db: Pick<WorkerDb, 'insert'>, |
| 1551 | params: { |
| 1552 | findingId: string; |
| 1553 | recipientUserIds: string[]; |
| 1554 | } |
| 1555 | ): Promise<number> { |
| 1556 | if (params.recipientUserIds.length === 0) return 0; |
| 1557 | |
| 1558 | const inserted = await db |
| 1559 | .insert(security_finding_notifications) |
| 1560 | .values( |
| 1561 | params.recipientUserIds.map(recipientUserId => ({ |
| 1562 | finding_id: params.findingId, |
| 1563 | recipient_user_id: recipientUserId, |
| 1564 | kind: 'new_finding' as const, |
| 1565 | status: 'staged' as const, |
| 1566 | })) |
| 1567 | ) |
| 1568 | .onConflictDoNothing() |
| 1569 | .returning({ id: security_finding_notifications.id }); |
| 1570 | |
| 1571 | return inserted.length; |
| 1572 | } |
| 1573 | |
| 1574 | async function finalizeStagedNewFindingNotifications( |
| 1575 | db: Pick<WorkerDb, 'execute'>, |
| 1576 | params: { owner: SecurityReviewOwner; repoFullName: string } |
| 1577 | ): Promise<{ pending: number; cancelled: number }> { |
| 1578 | const result = await db.execute<{ status: 'pending' | 'cancelled'; count: number }>(sql` |
| 1579 | WITH finalized AS ( |
| 1580 | UPDATE ${security_finding_notifications} |
| 1581 | SET |
| 1582 | status = CASE |
| 1583 | WHEN ${security_findings.status} = 'open' |
| 1584 | AND COALESCE(${security_findings.ignored_reason}, '') NOT LIKE 'superseded:%' |
| 1585 | THEN 'pending' |
| 1586 | ELSE 'cancelled' |
| 1587 | END, |
| 1588 | updated_at = now() |
| 1589 | FROM ${security_findings} |
| 1590 | WHERE ${security_finding_notifications.finding_id} = ${security_findings.id} |
| 1591 | AND ${security_finding_notifications.kind} = 'new_finding' |
| 1592 | AND ${security_finding_notifications.status} = 'staged' |
| 1593 | AND ${security_findings.repo_full_name} = ${params.repoFullName} |
| 1594 | AND ${findingOwnerPredicate(params.owner)} |
| 1595 | RETURNING ${security_finding_notifications.status} |
| 1596 | ) |
| 1597 | SELECT status, count(*)::int AS count |