fix(github): recover existing apps and keep member linking separate

Explicit manual recovery switches to the existing-App identity path only after vault storage succeeds. Preserve the incomplete registration for local webhook repair. Account-only linking avoids manager setup APIs and retains personal GitHub connection onboarding.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-07 07:36:53 -05:00
1 parent e3d8a0a47b
commit 01206ab4a5
4 files changed
+139 -24

No files matched your search

@@ -2108,6 +2108,7 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
webhookSecret,
providerFetch,
webhookSyncRequests,
privateKey,
setSupplementalProviderFetch(value: typeof supplementalProviderFetch) {
supplementalProviderFetch = value;
},
@@ -2370,7 +2371,7 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
.where(eq(chatGitHubRegistrations.endpointId, bot.id));
expect(session.status).toBe("failed");
});
it("wizard retains recovery when a manifest vault write saved App credentials but lost OAuth credentials", async () => {
it("wizard retains partial manifest recovery until explicit existing-App recovery succeeds", async () => {
const f = await reviewBotFixture();
const returnState = randomUUID();
const [connection] = await db
@@ -2420,7 +2421,10 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
fetch: f.providerFetch,
connector: () =>
({
githubApp: async () => ({ id: "partial-oauth" }),
githubApp: async () => ({
id: "partial-oauth",
webhookUrl: "https://gateway.example.test/github-apps/partial-oauth/webhook",
}),
claimGitHubApp,
}) as unknown as PaperclipCloudConnector,
startDirect: f.service.startGitHubRegistration,
@@ -2442,6 +2446,29 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
.from(chatGitHubRegistrations)
.where(eq(chatGitHubRegistrations.endpointId, f.endpoint.id));
expect(session.status).toBe("failed");
const resourcesBefore = await f.service.listResources(f.endpoint.id);
await f.service.storeGitHubApp(f.endpoint.id, "owner-user", {
appId: f.endpoint.botExternalId!,
privateKey: f.privateKey,
webhookSecret: f.webhookSecret,
});
const [recovered] = await db.select().from(chatGitHubRegistrations)
.where(eq(chatGitHubRegistrations.id, session.id));
expect(recovered.handoff).toMatchObject({ cloudId: "partial-oauth", returnState });
expect(recovered.handoff?.manifestClaimId).toBeUndefined();
expect(recovered.status).toBe("failed");
expect(await wizard.advance(f.endpoint.id, "owner-user")).toMatchObject({ state: "verify" });
expect(claimGitHubApp).not.toHaveBeenCalled();
expect(f.webhookSyncRequests).toEqual([
expect.objectContaining({
url: "https://gateway.example.test/github-apps/partial-oauth/webhook",
secret: f.webhookSecret,
}),
]);
expect((await f.service.listResources(f.endpoint.id)).map(({ id, enabled }) => ({ id, enabled })))
.toEqual(resourcesBefore.map(({ id, enabled }) => ({ id, enabled })));
expect((await f.service.get(f.endpoint.id)).botExternalId).toBe(f.endpoint.botExternalId);
expect(finish).not.toHaveBeenCalled();
});
it("wizard recovers a renamed enrolled origin only after checking bound state and Cloud authority", async () => {
const f = await seedCompany();
+18 -4
View File
@@ -16,7 +16,7 @@ import { githubChatManagementService } from "./chat-github-management.js";
import { githubReviewCheckService } from "./chat-github-checks.js";
import { githubAutomaticReviewEvent, githubAutomaticAdmission, githubPreviousAssessment } from "./chat-github-events.js";
import { githubReviewPrompt } from "./chat-github-review-policy.js";
import { chatGitHubConfigurations, chatGitHubReviews } from "@paperclipai/db";
import { chatGitHubConfigurations, chatGitHubRegistrations, chatGitHubReviews } from "@paperclipai/db";
import type { GitHubReviewEventContext, GitHubReviewPolicy } from "@paperclipai/shared";
import { githubChatReviewService } from "./chat-github-reviews.js";
import { githubChatWizardService } from "./chat-github-wizard.js";
@@ -38213,7 +38213,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) {
: null;
}
async function storeGitHubApp(endpointId: string, userId: string, credentials: Record<string, string>) {
async function storeGitHubApp(endpointId: string, userId: string, credentials: Record<string, string>, source: "manifest" | "existing_app" = "existing_app") {
const initial = await endpointRecord(endpointId);
if (!initial || initial.endpoint.provider !== "github") throw notFound("GitHub bot not found");
await withCredentialMutationLease(initial.endpoint, async credentialLease => {
@@ -38257,6 +38257,20 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) {
setup: { ...current.setup, github: { ...current.setup.github, stage: "install", appSlug: slug, installationUrl: `https://github.com/apps/${slug}/installations/new`, registrationStatus: "completed" } },
healthMessage: "Install the App on GitHub to continue", updatedAt: new Date(),
}).where(eq(chatEndpoints.id, endpointId));
if (source === "existing_app") {
// Explicit recovery uses the existing-App identity path. Only drop the
// manifest requirement after the same App's credentials are vaulted;
// leave registration incomplete so advance repairs and verifies delivery.
await tx.update(chatGitHubRegistrations).set({
handoff: sql`${chatGitHubRegistrations.handoff} - 'manifestClaimId'`,
}).where(and(
eq(chatGitHubRegistrations.companyId, record.endpoint.companyId),
eq(chatGitHubRegistrations.endpointId, endpointId),
eq(chatGitHubRegistrations.userId, userId),
inArray(chatGitHubRegistrations.status, ["pending", "exchanging", "failed"]),
sql`${chatGitHubRegistrations.handoff} ? 'manifestClaimId'`,
));
}
await logActivity(tx as unknown as Db, { companyId: record.endpoint.companyId, actorType: "user", actorId: userId, action: "chat_github.app_connected", entityType: "tool_connection", entityId: record.endpoint.connectionId, details: { endpointId, appId: identity.botExternalId } });
});
});
@@ -38278,7 +38292,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) {
const githubRegistration = githubChatRegistrationService(db, {
publicOrigin: () => getPublicBaseUrl(), webhookOrigin: () => getWebhookPublicBaseUrl(), fetch: fetchImpl,
storeApp: async (endpointId, userId, app) => { await storeGitHubApp(endpointId, userId, { appId: app.appId, privateKey: app.privateKey, webhookSecret: app.webhookSecret, ...(app.clientId ? { clientId: app.clientId } : {}), ...(app.clientSecret ? { clientSecret: app.clientSecret } : {}) }); },
storeApp: async (endpointId, userId, app) => { await storeGitHubApp(endpointId, userId, { appId: app.appId, privateKey: app.privateKey, webhookSecret: app.webhookSecret, ...(app.clientId ? { clientId: app.clientId } : {}), ...(app.clientSecret ? { clientSecret: app.clientSecret } : {}) }, "manifest"); },
});
async function refreshGitHubRepositories(endpointId: string, userId: string) {
@@ -38299,7 +38313,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) {
const githubWizard = githubChatWizardService(db, {
origin: () => getPublicBaseUrl() ?? options.githubWizardOrigin ?? null, fetch: fetchImpl, startDirect: githubRegistration.start, completeDirect: githubRegistration.complete, resumeDirect: githubRegistration.resumeRegistration,
storeApp: storeGitHubApp,
storeApp: (id, userId, credentials) => storeGitHubApp(id, userId, credentials, "manifest"),
storeCredentials: async (id, userId, additional) => {
const record = await endpointRecord(id);
if (!record || record.endpoint.provider !== "github") throw notFound("GitHub bot not found");