{"record":{"id":"17216c4794b6d4fa","repo":"mastra-ai/mastra","slug":"source-control-connection-not-found-for-this-organ","errorCode":null,"errorMessage":"Source-control connection not found for this organization.","messagePattern":"Source-control connection not found for this organization\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory/src/integrations/github/session-subscriptions.ts","lineNumber":114,"sourceCode":"  );\n}\n\nasync function resolveSessionTarget(requestContext: RequestContext, github: GithubIntegration): Promise<SessionTarget> {\n  const context = requestContext.get('controller') as AgentControllerRequestContext<RepositorySessionState> | undefined;\n  const orgId = sessionOrgId(requestContext);\n  const userId = sessionUserId(requestContext);\n  const projectRepositoryId = context?.getState().projectRepositoryId;\n  if (!context || !context.threadId || !projectRepositoryId || !orgId || !userId) {\n    throw new Error('GitHub subscriptions require an authenticated repository session with an active thread.');\n  }\n\n  const projectRepository = await github.sourceControlStorage.projectRepositories.get({\n    orgId,\n    id: projectRepositoryId,\n  });\n  if (!projectRepository) throw new Error('Project repository not found for this organization.');\n  const connection = await github.sourceControlStorage.connections.get({ orgId, id: projectRepository.connectionId });\n  if (!connection) throw new Error('Source-control connection not found for this organization.');\n  const repository = await github.sourceControlStorage.repositories.get({ orgId, id: projectRepository.repositoryId });\n  if (!repository) throw new Error('Repository not found for this organization.');\n  const installation = await github.sourceControlStorage.installations.get({ orgId, id: connection.installationId });\n  if (!installation) throw new Error('Source-control installation not found for this organization.');\n  return { context, projectRepository, connection, installation, repository, orgId, userId };\n}\n\nasync function verifyPullRequest(target: SessionTarget, pullRequest: number, github: GithubIntegration) {\n  const [owner, repo] = target.repository.slug.split('/');\n  if (!owner || !repo) throw new Error('GitHub repository is invalid.');\n  const octokit = github.getInstallationOctokit(Number(target.installation.externalId));\n  const { data } = await octokit.pulls.get({ owner, repo, pull_number: pullRequest });\n  if (String(data.base.repo.id) !== target.repository.externalId)\n    throw new Error('Pull request repository does not match the active project repository.');\n}\n\nasync function subscriptionInput(target: SessionTarget, pullRequestNumber: number) {\n  return {","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory/src/integrations/github/session-subscriptions.ts#L96-L132","documentation":"resolveSessionTarget loads the source-control connection referenced by the project repository's connectionId. If no connection record exists for the organization, this error is thrown — the project repository points to a deleted or foreign connection.","triggerScenarios":"connections.get({ orgId, id: projectRepository.connectionId }) returns undefined because the connection was deleted or was created under a different org.","commonSituations":"GitHub App connection uninstalled/removed while project repositories still reference it; dangling foreign keys after cleanup scripts; cross-org data access attempts.","solutions":["Reconnect the GitHub installation and create a fresh connection, then re-link the project repository","Verify the connectionId on the project repository matches an existing connections row in the same org","Remove the orphaned project repository and re-add it after reconnecting"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const conn = await storage.connections.get({ orgId, id: projectRepository.connectionId });\nif (!conn) throw new Error('connection missing — reconnect the GitHub installation');","typeGuard":null,"tryCatchPattern":"try {\n  await subscribe(...);\n} catch (e) {\n  if (e.message.includes('connection not found')) {\n    // prompt user to reconnect the GitHub App\n  }\n}","preventionTips":["Handle GitHub App uninstall webhooks by cleaning up dependent project repositories","Check connection existence before linking repositories to it","Cascade-delete project repositories when their connection is removed"],"tags":["storage","not-found","github","connection"],"backgroundTag":"record-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}