Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

refactor(integrations): use the integrations SDK in the remaining products (#125029)

Tom Owers2e3d8b2d259b
diff --git a/posthog/integrations/model_access_baseline.txt b/posthog/integrations/model_access_baseline.txt
index 4639e7b5dc04..3f6019eb152d 100644
--- a/posthog/integrations/model_access_baseline.txt
+++ b/posthog/integrations/model_access_baseline.txt
@@ -25,75 +25,32 @@ posthog/session_recordings/session_recording_api.py 1
posthog/session_recordings/session_recording_external_reference_api.py 1
posthog/tasks/comment_slack_sync.py 3
posthog/tasks/integrations.py 11
-products/ai_observability/backend/api/evaluation_reports.py 2
-products/alerts/backend/presentation/views/alert.py 2
-products/alerts_platform/backend/delivery/slack.py 2
products/batch_exports/backend/debug/debugger.py 1
products/batch_exports/backend/debug/start_session.py 1
products/batch_exports/backend/models/batch_export.py 1
-products/billing_alerts/backend/facade/api.py 2
-products/business_knowledge/backend/github_repos.py 2
-products/canvas/backend/welcome.py 2
-products/cdp/backend/validation.py 3
-products/commands/backend/channels/slack/mentions.py 3
-products/commands/backend/logic/github.py 2
-products/commands/backend/logic/subjects.py 2
-products/conversations/backend/api/github_setup.py 4
-products/conversations/backend/facade/api.py 2
-products/conversations/backend/services/gmail_sync.py 7
-products/conversations/backend/tasks/github.py 2
-products/customer_analytics/backend/facade/api.py 5
-products/customer_analytics/backend/logic/calendar_sync.py 5
-products/customer_analytics/backend/logic/event_stream_destination.py 2
-products/customer_analytics/backend/logic/feature_request_github.py 2
+products/commands/backend/channels/slack/mentions.py 2
products/customer_analytics/backend/logic/feature_requests.py 2
products/customer_analytics/backend/models/event_stream.py 1
products/customer_analytics/backend/models/feature_request.py 1
products/customer_analytics/backend/temporal/calendar_sync.py 2
products/data_modeling/backend/models/github_sync_config.py 1
-products/engineering_analytics/backend/logic/job_log_insights.py 2
-products/engineering_analytics/backend/logic/job_logs/activity.py 2
-products/engineering_analytics/backend/logic/ownership_files.py 2
-products/engineering_analytics/backend/logic/quarantine.py 2
products/error_tracking/backend/logic/__init__.py 1
-products/error_tracking/backend/logic/alerts.py 2
-products/error_tracking/backend/logic/external_references.py 4
-products/error_tracking/backend/logic/github_external_references.py 2
+products/error_tracking/backend/logic/external_references.py 1
products/error_tracking/backend/models.py 3
-products/error_tracking/backend/presentation/views/git_provider_file_link_resolver.py 3
-products/error_tracking/backend/temporal/alerts/delivery.py 2
+products/error_tracking/backend/temporal/alerts/delivery.py 1
products/growth/backend/product_push/surfaces.py 2
products/messaging/backend/models/optout_sync_config.py 3
products/messaging/backend/models/provider_import_run.py 1
-products/messaging/backend/presentation/views/customerio_webhook.py 2
-products/messaging/backend/presentation/views/message_templates.py 2
-products/messaging/backend/remote_config.py 2
-products/messaging/backend/services/customerio_config.py 13
+products/messaging/backend/services/customerio_config.py 1
products/messaging/backend/services/customerio_sync_service.py 1
products/messaging/backend/services/customerio_webhook.py 1
-products/messaging/backend/services/transactional_email.py 2
-products/review_hog/backend/activity_logging.py 2
-products/review_hog/backend/automatic_reviews.py 3
products/review_hog/backend/ownership.py 2
-products/review_hog/backend/repository_settings.py 2
-products/review_hog/backend/reviewer/status_comment.py 2
-products/review_hog/backend/temporal/activities.py 2
-products/review_hog/backend/temporal/resolution.py 2
-products/signals/backend/github_optional.py 2
products/signals/backend/inbox_summary.py 1
products/signals/backend/models.py 4
-products/signals/backend/onboarding_completion.py 2
products/signals/backend/report_generation/resolve_reviewers.py 3
-products/signals/backend/scout_harness/profile/builders.py 2
-products/signals/backend/scout_harness/serializers.py 2
-products/signals/backend/scout_harness/slack_delivery.py 2
-products/signals/backend/serializers.py 2
products/signals/backend/slack_inbox_notifications.py 2
-products/signals/backend/slack_notification_targets.py 2
-products/signals/backend/temporal/agentic/select_repository.py 2
-products/signals/backend/tracker_issues.py 4
-products/signals/backend/views.py 2
-products/skills/backend/api/community_publish_services.py 1
+products/signals/backend/slack_notification_targets.py 1
+products/signals/backend/tracker_issues.py 3
products/slack_app/backend/api.py 1
products/slack_app/backend/facade/api.py 3
products/slack_app/backend/models.py 3
@@ -105,14 +62,8 @@ products/slack_app/backend/slack_thread.py 2
products/slack_app/backend/temporal/activities/classifiers.py 1
products/slack_app/backend/temporal/activities/messaging.py 1
products/slack_app/backend/temporal/activities/onboarding.py 2
-products/stamphog/backend/logic/channel_resolution.py 2
-products/stamphog/backend/logic/slack_digest.py 3
-products/subscriptions/backend/logic/slack_subscriptions.py 2
-products/subscriptions/backend/logic/subscription_writes.py 2
products/subscriptions/backend/models/subscription.py 1
products/subscriptions/backend/temporal/activities.py 2
-products/subscriptions/backend/temporal/ai_subscription/delivery.py 1
-products/subscriptions/backend/temporal/delivery_common.py 1
products/tasks/backend/facade/api.py 15
products/tasks/backend/github_repository_access.py 2
products/tasks/backend/logic/pull_request_titles.py 2
@@ -142,16 +93,9 @@ products/tasks/backend/temporal/process_task/activities/start_agent_server.py 2
products/tasks/backend/temporal/process_task/sandbox_credentials.py 1
products/tasks/backend/temporal/process_task/utils.py 3
products/tasks/scripts/run_agent_in_docker.py 2
-products/visual_review/backend/logic/github_api.py 2
-products/visual_review/backend/logic/team_channels.py 2
products/warehouse_sources/backend/models/external_data_destination.py 1
products/warehouse_sources/backend/temporal/data_imports/sources/custom/source.py 1
-products/wizard/backend/logic/workers/repository_publisher.py 3
products/workflows/backend/management/commands/delete_stale_ses_identity.py 2
products/workflows/backend/management/commands/disable_ses_feedback_forwarding.py 2
-products/workflows/backend/services/customerio_import/content.py 3
-products/workflows/backend/services/data_suggestions/team_context.py 5
-products/workflows/backend/services/email_health.py 3
-products/workflows/backend/services/setup_status.py 2
-products/workflows/backend/services/transactional_email_workflows.py 2
+products/workflows/backend/services/email_health.py 2
products/workflows/backend/tasks/ses_tenant_state.py 2
diff --git a/posthog/models/integration/external_issues.py b/posthog/models/integration/external_issues.py
index adcf9f3488a5..b94fcc79dc36 100644
--- a/posthog/models/integration/external_issues.py
+++ b/posthog/models/integration/external_issues.py
@@ -3,6 +3,7 @@
from typing import Any
from posthog.egress.limiter.policies import Priority
+from posthog.integrations import IntegrationRecord
from . import model
from .github import GitHubIntegration
@@ -24,7 +25,7 @@ def is_supported_external_issue_provider(kind: str) -> bool:
return kind in SUPPORTED_EXTERNAL_ISSUE_PROVIDERS
-def external_issue_url(integration: model.Integration, external_context: dict[str, Any] | None) -> str:
+def external_issue_url(integration: IntegrationRecord, external_context: dict[str, Any] | None) -> str:
"""Link to an external issue from the identifier its integration client stored.
Returns an empty string when the context does not carry the provider's identifier, so a
diff --git a/products/ai_observability/backend/api/evaluation_reports.py b/products/ai_observability/backend/api/evaluation_reports.py
index 95728c4aa684..55ee983d8913 100644
--- a/products/ai_observability/backend/api/evaluation_reports.py
+++ b/products/ai_observability/backend/api/evaluation_reports.py
@@ -22,7 +22,7 @@ from posthog.api.forbid_destroy_model import ForbidDestroyModel
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.auth import InternalAPIAuthentication
from posthog.event_usage import report_user_action
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager
from posthog.permissions import (
AccessControlPermission,
APIScopePermission,
@@ -330,11 +330,7 @@ class EvaluationReportSerializer(serializers.ModelSerializer):
# Enforce tenant + kind boundary: only integrations that belong to this team AND
# are Slack integrations are valid. Prevents cross-team reuse of integration ids
# and rejects non-Slack kinds that happen to share an id.
- owned = set(
- Integration.objects.filter(
- team_id=team.id, kind=Integration.IntegrationKind.SLACK, id__in=slack_ids_to_verify
- ).values_list("id", flat=True)
- )
+ owned = set(IntegrationManager.slack.oauth.get_many(team.id, slack_ids_to_verify))
missing = slack_ids_to_verify - owned
if missing:
raise serializers.ValidationError(f"Slack integration(s) not found for this team: {sorted(missing)}.")
diff --git a/products/alerts/backend/presentation/views/alert.py b/products/alerts/backend/presentation/views/alert.py
index 389a970defe2..752eff65f270 100644
--- a/products/alerts/backend/presentation/views/alert.py
+++ b/products/alerts/backend/presentation/views/alert.py
@@ -51,8 +51,8 @@ from posthog.helpers.trigram_search import (
apply_trigram_search,
drop_similar_when_exact_exists,
)
+from posthog.integrations import IntegrationManager
from posthog.models import User
-from posthog.models.integration import Integration
from posthog.models.tag import tagify
from posthog.models.tagged_item import TaggedItem
from posthog.permissions import get_authenticator_scopes
@@ -1443,7 +1443,7 @@ class AlertCreateDestinationSerializer(serializers.Serializer):
# The runtime refuses another team's integration anyway, so without this the destination
# is created and then silently never posts.
team = self.context["get_team"]()
- if not Integration.objects.filter(team=team, id=attrs["slack_workspace_id"], kind="slack").exists():
+ if not IntegrationManager.slack.oauth.get_many(team.id, [attrs["slack_workspace_id"]]):
raise ValidationError({"slack_workspace_id": "Connect this Slack workspace to PostHog first."})
return attrs
diff --git a/products/alerts_platform/backend/delivery/slack.py b/products/alerts_platform/backend/delivery/slack.py
index 716cbc095a12..ed40f3973eba 100644
--- a/products/alerts_platform/backend/delivery/slack.py
+++ b/products/alerts_platform/backend/delivery/slack.py
@@ -10,7 +10,8 @@ from typing import Any, Final
import structlog
from slack_sdk.errors import SlackApiError
-from posthog.models.integration import SLACK_INTEGRATION_KINDS, Integration, SlackIntegration
+from posthog.integrations import IntegrationManager, IntegrationNotFound, IntegrationRecord
+from posthog.models.integration import SlackIntegration
from posthog.slack.channels import (
MAX_SECTION_CHARS,
SlackButton,
@@ -294,12 +295,10 @@ class SlackTransport:
except SlackApiError as error:
raise DeliveryError(f"Slack refused the edit: {error.response.get('error')}") from error
- def _integration(self, *, team_id: int, workspace_id: int) -> Integration:
+ def _integration(self, *, team_id: int, workspace_id: int) -> IntegrationRecord:
# Scoped by team as well as by id. The destination names an integration, and nothing
# between the destination and here checks that it still belongs to the sending team.
- integration = Integration.objects.filter(
- id=workspace_id, team_id=team_id, kind__in=SLACK_INTEGRATION_KINDS
- ).first()
- if integration is None:
- raise DeliveryError("The Slack workspace for this alert is not connected.")
- return integration
+ try:
+ return IntegrationManager.slack.oauth.get(team_id, workspace_id)
+ except IntegrationNotFound as error:
+ raise DeliveryError("The Slack workspace for this alert is not connected.") from error
diff --git a/products/billing_alerts/backend/facade/api.py b/products/billing_alerts/backend/facade/api.py
index 7e7415b55c90..7c9dc7c2a52b 100644
--- a/products/billing_alerts/backend/facade/api.py
+++ b/products/billing_alerts/backend/facade/api.py
@@ -11,7 +11,7 @@ from django.utils import timezone
from rest_framework.exceptions import ValidationError as DRFValidationError
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager
from posthog.models.team.team import Team
from products.alerts.backend.facade.destinations import (
@@ -183,11 +183,7 @@ def destinations_for_alerts(alerts: list[BillingAlertConfiguration]) -> dict[str
def slack_integration_belongs_to_team(*, integration_id: int, team_id: int) -> bool:
- return Integration.objects.filter(
- id=integration_id,
- team_id=team_id,
- kind=Integration.IntegrationKind.SLACK,
- ).exists()
+ return bool(IntegrationManager.slack.oauth.get_many(team_id, [integration_id]))
def _raise_if_billing_alert_already_has_this_destination_type(
diff --git a/products/business_knowledge/backend/github_repos.py b/products/business_knowledge/backend/github_repos.py
index 650835813bb2..3fb7ee7d4a7e 100644
--- a/products/business_knowledge/backend/github_repos.py
+++ b/products/business_knowledge/backend/github_repos.py
@@ -26,8 +26,9 @@ from posthog.egress.github.transport import GitHubRateLimitError
from posthog.egress.limiter.policies import Priority
from posthog.egress.transport.transport import EgressBudgetExhausted
from posthog.event_usage import report_user_action
+from posthog.integrations import IntegrationManager, IntegrationNotFound, IntegrationRecord
from posthog.models.github_integration_base import GitHubIntegrationError, _is_safe_github_repo_path
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.models.integration import GitHubIntegration
from posthog.models.integration_repository_cache import (
GITHUB_REPOSITORY_FULL_CACHE_TTL_SECONDS,
GitHubRepositoryFullCache,
@@ -251,7 +252,7 @@ def read_repository_file(
return loaded
-def _fetch_file(team: Team, integration: Integration, full_name: str, path: str, sha: str) -> RepositoryFile:
+def _fetch_file(team: Team, integration: IntegrationRecord, full_name: str, path: str, sha: str) -> RepositoryFile:
try:
payload = GitHubIntegration(
integration, source="business_knowledge", priority=Priority.NORMAL
@@ -339,7 +340,7 @@ def search_terms(query: str) -> list[str]:
return terms
-def _require_allowlist(team: Team) -> tuple[Integration, list[str]]:
+def _require_allowlist(team: Team) -> tuple[IntegrationRecord, list[str]]:
integration_id, allowed = selected_repositories(team)
if integration_id is None or not allowed:
raise GithubReposError("No GitHub repositories are selected for business knowledge.")
@@ -353,7 +354,7 @@ def _require_allowlist(team: Team) -> tuple[Integration, list[str]]:
return integration, allowed
-def _installation_repo_names(integration: Integration) -> set[str]:
+def _installation_repo_names(integration: IntegrationRecord) -> set[str]:
# A pure read of the stored repository list, so a repository removed from the installation
# stops being searchable as soon as that list refreshes, even while its cache row remains.
listed = GitHubIntegration(
@@ -596,10 +597,13 @@ def _ilike_pattern(term: str) -> str:
return f"%{escaped}%"
-def _integration_for(team_id: int, integration_id: int | None) -> Integration | None:
+def _integration_for(team_id: int, integration_id: int | None) -> IntegrationRecord | None:
if integration_id is None:
return None
- return Integration.objects.filter(id=integration_id, team_id=team_id, kind="github").first()
+ try:
+ return IntegrationManager.github.oauth.get(team_id, integration_id)
+ except IntegrationNotFound:
+ return None
def _repo_names(value: object) -> list[str]:
diff --git a/products/canvas/backend/welcome.py b/products/canvas/backend/welcome.py
index 6892d80fc26e..7541ddc95e57 100644
--- a/products/canvas/backend/welcome.py
+++ b/products/canvas/backend/welcome.py
@@ -13,7 +13,7 @@ from uuid import UUID
import structlog
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager
from posthog.models.user import User
from products.canvas.backend import build_service
@@ -234,9 +234,7 @@ def seed_home_canvas(canvas: Canvas, *, user: User, channel_id: UUID) -> None:
value={
# They are reading the checklist inside the desktop app.
"download-desktop": True,
- "connect-github": Integration.objects.filter(
- team_id=canvas.team_id, kind=Integration.IntegrationKind.GITHUB
- ).exists(),
+ "connect-github": IntegrationManager.github.oauth.exists(canvas.team_id),
},
)
layout = default_layout()
diff --git a/products/cdp/backend/validation.py b/products/cdp/backend/validation.py
index 8dbabe865a62..25953db69c7a 100644
--- a/products/cdp/backend/validation.py
+++ b/products/cdp/backend/validation.py
@@ -18,7 +18,7 @@ from posthog.hogql.context import HogQLContext
from posthog.hogql.parser import parse_program, parse_string_template
from posthog.hogql.visitor import TraversingVisitor
-from posthog.models.integration import POSTHOG_CONNECT_KIND, Integration
+from posthog.integrations import IntegrationManager
from posthog.models.team.team import Team
from posthog.models.user import User
@@ -95,7 +95,7 @@ def _validate_not_posthog_connection(integration_ids: list[int], context: dict)
get_team = context.get("get_team")
if get_team is None or not integration_ids:
return
- if Integration.objects.filter(team_id=get_team().id, id__in=integration_ids, kind=POSTHOG_CONNECT_KIND).exists():
+ if IntegrationManager.posthog.any.get_many(get_team().id, integration_ids):
raise serializers.ValidationError(
{"input": "A PostHog connection can't be used as a function input. Choose a different integration."}
)
@@ -153,7 +153,7 @@ def _validate_email_sender_override(from_value: dict, context: dict) -> None:
domain_cache: dict[int, str] = shared_cache if isinstance(shared_cache, dict) else {}
missing_ids = [integration_id for integration_id in integration_ids if integration_id not in domain_cache]
if missing_ids:
- for integration in Integration.objects.filter(team_id=get_team().id, id__in=missing_ids, kind="email"):
+ for integration in IntegrationManager.email.credentials.get_many(get_team().id, missing_ids).values():
config = integration.config or {}
domain_cache[integration.id] = (config.get("domain") or (config.get("email") or "").split("@")[-1]).lower()
for integration_id in missing_ids:
diff --git a/products/commands/backend/channels/slack/mentions.py b/products/commands/backend/channels/slack/mentions.py
index 511443f8007a..34328bdd3bfb 100644
--- a/products/commands/backend/channels/slack/mentions.py
+++ b/products/commands/backend/channels/slack/mentions.py
@@ -20,6 +20,7 @@ from typing import assert_never
import structlog
from posthog.dataclasses import frozen
+from posthog.integrations import IntegrationManager, IntegrationNotFound
from posthog.models.integration import Integration, SlackIntegration
from posthog.models.user import User
from posthog.slack.channels import post_message
@@ -162,10 +163,9 @@ class SlackThread:
def post_in_thread(thread: SlackThread, message: str) -> None:
- integration = Integration.objects.filter(
- id=thread.integration_id, team_id=thread.integration_team_id, kind="slack"
- ).first()
- if integration is None:
+ try:
+ integration = IntegrationManager.slack.oauth.get(thread.integration_team_id, thread.integration_id)
+ except IntegrationNotFound:
logger.warning("slack_command_reply_integration_missing", integration_id=thread.integration_id)
return
try:
diff --git a/products/commands/backend/logic/github.py b/products/commands/backend/logic/github.py
index 94eca898a580..3d077af664ec 100644
--- a/products/commands/backend/logic/github.py
+++ b/products/commands/backend/logic/github.py
@@ -13,7 +13,8 @@ import structlog
from posthog.egress.github.transport import GitHubRateLimitError
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, GitHubIntegrationError, Integration
+from posthog.integrations import IntegrationManager
+from posthog.models.integration import GitHubIntegration, GitHubIntegrationError
from .commands import REPOSITORY_NAME_RE, PullRequestFacts
@@ -60,13 +61,10 @@ class InstallationGitHub:
those writes off projects that never did. The project a command runs in is picked later.
"""
# nosemgrep: github-installation-first-team -- any of the given projects serves, see the docstring
- integration = (
- Integration.objects.filter(kind="github", integration_id=installation_id, team_id__in=team_ids)
- .order_by("id")
- .first()
- )
- if integration is None:
+ rows = IntegrationManager.github.oauth.list_by_external_id(installation_id, team_ids=team_ids)
+ if not rows:
return None
+ integration = min(rows, key=lambda row: row.id)
return cls(GitHubIntegration(integration, source=_SOURCE, priority=Priority.CRITICAL))
def collaborator_permission(self, repository: str, login: str, github_user_id: int) -> str:
diff --git a/products/commands/backend/logic/subjects.py b/products/commands/backend/logic/subjects.py
index 91b0779484f6..a498f8bb05f5 100644
--- a/products/commands/backend/logic/subjects.py
+++ b/products/commands/backend/logic/subjects.py
@@ -10,7 +10,7 @@ follows ``Subject``, a case in each function below, and the kind to the commands
from typing import Any, Protocol, assert_never
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager
from posthog.utils import absolute_uri
from products.error_tracking.backend.facade import api as error_tracking
@@ -104,13 +104,8 @@ def candidate_team_ids(ref: SubjectRef) -> tuple[int, ...]:
"""
match ref:
case PullRequestRef():
- return tuple(
- dict.fromkeys(
- Integration.objects.filter(kind="github", integration_id=ref.installation_id)
- .order_by("id")
- .values_list("team_id", flat=True)
- )
- )
+ rows = IntegrationManager.github.oauth.list_by_external_id(ref.installation_id)
+ return tuple(dict.fromkeys(row.team_id for row in sorted(rows, key=lambda row: row.id)))
case ErrorTrackingIssueRef():
return (ref.team_id,)
case _:
diff --git a/products/conversations/backend/api/github_setup.py b/products/conversations/backend/api/github_setup.py
index dea08bfa42ad..6cba0858113e 100644
--- a/products/conversations/backend/api/github_setup.py
+++ b/products/conversations/backend/api/github_setup.py
@@ -10,7 +10,8 @@ from rest_framework.response import Response
from rest_framework.views import APIView
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager, IntegrationNotFound
+from posthog.models.integration import GitHubIntegration
from posthog.models.user import User
from products.conversations.backend.permissions import IsConversationsAdmin
@@ -33,7 +34,10 @@ class GithubStatusView(APIView):
integration_id = settings_dict.get("github_integration_id")
integration_info = None
if integration_id:
- integration = Integration.objects.filter(id=integration_id, team=team, kind="github").first()
+ try:
+ integration = IntegrationManager.github.oauth.get(team.id, integration_id)
+ except IntegrationNotFound:
+ integration = None
if integration:
account = integration.config.get("account", {})
integration_info = {
@@ -64,8 +68,8 @@ class GithubConnectView(APIView):
return Response({"error": "integration_id is required"}, status=400)
try:
- integration = Integration.objects.get(id=integration_id, team=team, kind="github")
- except Integration.DoesNotExist:
+ integration = IntegrationManager.github.oauth.get(team.id, integration_id)
+ except IntegrationNotFound:
return Response({"error": "GitHub integration not found"}, status=404)
settings_dict = team.conversations_settings or {}
@@ -113,8 +117,8 @@ class GithubReposView(APIView):
return Response({"error": "GitHub not connected"}, status=400)
try:
- integration = Integration.objects.get(id=integration_id, team=team, kind="github")
- except Integration.DoesNotExist:
+ integration = IntegrationManager.github.oauth.get(team.id, integration_id)
+ except IntegrationNotFound:
return Response({"error": "GitHub integration not found"}, status=404)
github = GitHubIntegration(integration, source="conversations", priority=Priority.CRITICAL)
diff --git a/products/conversations/backend/facade/api.py b/products/conversations/backend/facade/api.py
index e27e96977511..4284106fd2a7 100644
--- a/products/conversations/backend/facade/api.py
+++ b/products/conversations/backend/facade/api.py
@@ -24,8 +24,8 @@ from temporalio.service import RPCError
from posthog.dataclasses import frozen
from posthog.ingress.contracts import DeliveryOwnership, WebhookDelivery
+from posthog.integrations import IntegrationManager
from posthog.models.comment import Comment
-from posthog.models.integration import Integration
from posthog.models.team import Team
from posthog.slack.identity import resolve_slack_profile_by_email
from posthog.temporal.common.client import sync_connect
@@ -891,13 +891,12 @@ def list_email_threads_for_account_matching(
continue
integration_ids.add(int(integration_id))
- integration_owner_ids = dict(
- Integration.objects.filter(
- team_id=team_id,
- id__in=integration_ids,
- kind=Integration.IntegrationKind.GOOGLE_CALENDAR,
- ).values_list("id", "created_by_id")
- )
+ integration_owner_ids = {
+ integration_id: integration.created_by_id
+ for integration_id, integration in IntegrationManager.google_calendar.oauth.get_many(
+ team_id, integration_ids
+ ).items()
+ }
matching_threads: list[EmailThreadForAccountMatching] = []
for thread in thread_page:
normalized_thread_id = str(thread.id)
diff --git a/products/conversations/backend/services/gmail_sync.py b/products/conversations/backend/services/gmail_sync.py
index c307e29fdc12..a38b9eb4db3e 100644
--- a/products/conversations/backend/services/gmail_sync.py
+++ b/products/conversations/backend/services/gmail_sync.py
@@ -16,8 +16,11 @@ from bs4 import BeautifulSoup, NavigableString, PageElement, Tag
from posthog.dataclasses import frozen
from posthog.egress.google_workspace import google_workspace_request
-from posthog.models.integration import ERROR_TOKEN_REFRESH_FAILED, Integration, OauthIntegration
+from posthog.integrations import IntegrationManager, IntegrationNotFound, IntegrationRecord
+from posthog.models.integration import ERROR_TOKEN_REFRESH_FAILED, OauthIntegration
from posthog.models.organization import OrganizationMembership
+from posthog.models.team import Team
+from posthog.models.user import User
from products.conversations.backend.models import EmailChannel, EmailChannelKind, EmailThreadMessageDirection
from products.conversations.backend.services.email_thread_ingestion import (
@@ -63,29 +66,24 @@ class _EmailBodies:
html: str
-def integration_has_gmail_scope(integration: Integration) -> bool:
+def integration_has_gmail_scope(integration: IntegrationRecord) -> bool:
scopes = integration.config.get("scope") or ""
return GMAIL_READONLY_SCOPE in scopes.split()
def can_sync_gmail_integration(integration_id: int, team_id: int) -> bool:
- integration = (
- Integration.objects.select_related("created_by")
- .filter(id=integration_id, team_id=team_id, kind="google-calendar")
- .first()
- )
- if integration is None or not integration_has_gmail_scope(integration) or not _has_active_owner(integration):
+ try:
+ integration = IntegrationManager.google_calendar.oauth.get(team_id, integration_id)
+ except IntegrationNotFound:
+ return False
+ if not integration_has_gmail_scope(integration) or not _has_active_owner(integration):
return False
email = str(integration.config.get("email") or "").strip()
return "@" in email
def sync_gmail_integration(integration_id: int, team_id: int) -> None:
- integration = Integration.objects.select_related("team", "created_by").get(
- id=integration_id,
- team_id=team_id,
- kind="google-calendar",
- )
+ integration = IntegrationManager.google_calendar.oauth.get(team_id, integration_id)
if not integration_has_gmail_scope(integration) or not _has_active_owner(integration):
return
@@ -113,10 +111,14 @@ def sync_gmail_integration(integration_id: int, team_id: int) -> None:
if next_history_id is None:
return
- integration.refresh_from_db(fields=["config"])
- integration.config[GMAIL_HISTORY_ID_CONFIG_KEY] = next_history_id
- integration.config[GMAIL_LAST_SYNCED_AT_CONFIG_KEY] = timezone.now().isoformat()
- integration.save(update_fields=["config"])
+ IntegrationManager.google_calendar.oauth.update(
+ integration.team_id,
+ integration.id,
+ config_patch={
+ GMAIL_HISTORY_ID_CONFIG_KEY: next_history_id,
+ GMAIL_LAST_SYNCED_AT_CONFIG_KEY: timezone.now().isoformat(),
+ },
+ )
def sync_gmail_backfill_batch(
@@ -127,11 +129,7 @@ def sync_gmail_backfill_batch(
end_at: datetime,
page_token: str | None = None,
) -> tuple[str | None, int]:
- integration = Integration.objects.select_related("team", "created_by").get(
- id=integration_id,
- team_id=team_id,
- kind="google-calendar",
- )
+ integration = IntegrationManager.google_calendar.oauth.get(team_id, integration_id)
if not integration_has_gmail_scope(integration) or not _has_active_owner(integration):
raise GmailSyncError(f"Integration {integration.id} cannot sync Gmail")
@@ -162,12 +160,18 @@ def sync_gmail_backfill_batch(
return next_page_token, len(message_ids)
-def _has_active_owner(integration: Integration) -> bool:
- owner = integration.created_by
+def _owner(integration: IntegrationRecord) -> User | None:
+ if integration.created_by_id is None:
+ return None
+ return User.objects.filter(id=integration.created_by_id).first()
+
+
+def _has_active_owner(integration: IntegrationRecord) -> bool:
+ owner = _owner(integration)
return bool(owner and owner.is_active and owner.teams.filter(id=integration.team_id).exists())
-def _get_fresh_access_token(integration: Integration) -> str:
+def _get_fresh_access_token(integration: IntegrationRecord) -> str:
oauth = OauthIntegration(integration)
if integration.errors != ERROR_TOKEN_REFRESH_FAILED and oauth.access_token_expired():
oauth.refresh_access_token()
@@ -179,15 +183,15 @@ def _get_fresh_access_token(integration: Integration) -> str:
return token
-def _email_channel(integration: Integration) -> EmailChannel:
- owner = integration.created_by
+def _email_channel(integration: IntegrationRecord) -> EmailChannel:
+ owner = _owner(integration)
email = str(integration.config.get("email") or "").strip().lower()
if owner is None or not email or "@" not in email:
raise GmailSyncError(f"Integration {integration.id} has no mailbox owner")
name = " ".join(part for part in (owner.first_name, owner.last_name) if part).strip() or email
return EmailChannel(
- team=integration.team,
+ team=Team.objects.get(id=integration.team_id),
kind=EmailChannelKind.CUSTOMER_COMMUNICATION,
owner=owner,
inbound_token="",
@@ -200,11 +204,11 @@ def _email_channel(integration: Integration) -> EmailChannel:
)
-def _organization_member_emails(integration: Integration) -> set[str]:
+def _organization_member_emails(integration: IntegrationRecord) -> set[str]:
return {
email.lower()
for email in OrganizationMembership.objects.filter(
- organization_id=integration.team.organization_id,
+ organization_id=Team.objects.values_list("organization_id", flat=True).get(id=integration.team_id),
user__is_active=True,
).values_list("user__email", flat=True)
}
@@ -212,7 +216,7 @@ def _organization_member_emails(integration: Integration) -> set[str]:
def _initial_sync(
*,
- integration: Integration,
+ integration: IntegrationRecord,
channel: EmailChannel,
access_token: str,
internal_emails: set[str],
@@ -246,7 +250,7 @@ def _initial_sync(
def _sync_history(
*,
- integration: Integration,
+ integration: IntegrationRecord,
channel: EmailChannel,
access_token: str,
start_history_id: str,
@@ -254,8 +258,7 @@ def _sync_history(
) -> str | None:
processed_messages = 0
while processed_messages < HISTORY_MESSAGE_BATCH_SIZE:
- integration.refresh_from_db(fields=["config"])
- config = integration.config or {}
+ config = IntegrationManager.google_calendar.oauth.get(integration.team_id, integration.id).config or {}
progress_start_id = str(config.get(GMAIL_HISTORY_START_ID_CONFIG_KEY) or start_history_id)
page_token = str(config.get(GMAIL_HISTORY_PAGE_TOKEN_CONFIG_KEY) or "") or None
target_history_id = str(config.get(GMAIL_HISTORY_TARGET_ID_CONFIG_KEY) or "") or None
@@ -326,36 +329,41 @@ def _sync_history(
def _save_history_progress(
- integration: Integration,
+ integration: IntegrationRecord,
*,
start_history_id: str,
page_token: str | None,
target_history_id: str | None,
pending_message_ids: list[str],
) -> None:
- integration.refresh_from_db(fields=["config"])
- integration.config[GMAIL_HISTORY_START_ID_CONFIG_KEY] = start_history_id
- integration.config[GMAIL_HISTORY_PAGE_TOKEN_CONFIG_KEY] = page_token
- integration.config[GMAIL_HISTORY_TARGET_ID_CONFIG_KEY] = target_history_id
- integration.config[GMAIL_PENDING_MESSAGE_IDS_CONFIG_KEY] = pending_message_ids
- integration.save(update_fields=["config"])
-
-
-def _clear_history_progress(integration: Integration) -> None:
- integration.refresh_from_db(fields=["config"])
- for key in (
- GMAIL_HISTORY_START_ID_CONFIG_KEY,
- GMAIL_HISTORY_PAGE_TOKEN_CONFIG_KEY,
- GMAIL_HISTORY_TARGET_ID_CONFIG_KEY,
- GMAIL_PENDING_MESSAGE_IDS_CONFIG_KEY,
- ):
- integration.config.pop(key, None)
- integration.save(update_fields=["config"])
+ IntegrationManager.google_calendar.oauth.update(
+ integration.team_id,
+ integration.id,
+ config_patch={
+ GMAIL_HISTORY_START_ID_CONFIG_KEY: start_history_id,
+ GMAIL_HISTORY_PAGE_TOKEN_CONFIG_KEY: page_token,
+ GMAIL_HISTORY_TARGET_ID_CONFIG_KEY: target_history_id,
+ GMAIL_PENDING_MESSAGE_IDS_CONFIG_KEY: pending_message_ids,
+ },
+ )
+
+
+def _clear_history_progress(integration: IntegrationRecord) -> None:
+ IntegrationManager.google_calendar.oauth.update(
+ integration.team_id,
+ integration.id,
+ config_remove=(
+ GMAIL_HISTORY_START_ID_CONFIG_KEY,
+ GMAIL_HISTORY_PAGE_TOKEN_CONFIG_KEY,
+ GMAIL_HISTORY_TARGET_ID_CONFIG_KEY,
+ GMAIL_PENDING_MESSAGE_IDS_CONFIG_KEY,
+ ),
+ )
def _ingest_message_ids(
*,
- integration: Integration,
+ integration: IntegrationRecord,
channel: EmailChannel,
access_token: str,
message_ids: list[str],
@@ -373,7 +381,7 @@ def _ingest_message_ids(
def _ingest_message_id(
*,
- integration: Integration,
+ integration: IntegrationRecord,
channel: EmailChannel,
access_token: str,
gmail_message_id: str,
@@ -635,7 +643,7 @@ def _has_external_participant(email: ParsedEmail, internal_emails: set[str]) ->
def _request(
*,
- integration: Integration,
+ integration: IntegrationRecord,
access_token: str,
url: str,
endpoint: str,
@@ -655,7 +663,7 @@ def _request(
def _get_json(
*,
- integration: Integration,
+ integration: IntegrationRecord,
access_token: str,
url: str,
endpoint: str,
@@ -673,7 +681,7 @@ def _get_json(
def _get_json_allowing_missing(
*,
- integration: Integration,
+ integration: IntegrationRecord,
access_token: str,
url: str,
endpoint: str,
diff --git a/products/conversations/backend/tasks/github.py b/products/conversations/backend/tasks/github.py
index 58f420718288..0186cb895c07 100644
--- a/products/conversations/backend/tasks/github.py
+++ b/products/conversations/backend/tasks/github.py
@@ -12,6 +12,7 @@ from celery import shared_task
from posthog.comment.formatting import rich_content_to_markdown
from posthog.egress.github.transport import GitHubRateLimitError
from posthog.egress.limiter.policies import Priority
+from posthog.integrations import IntegrationManager, IntegrationNotFound
from posthog.models.comment import Comment as CommentModel
from posthog.models.github_integration_base import GitHubIntegrationError
from posthog.models.team import Team
@@ -394,7 +395,7 @@ def create_github_issue(
labels: list[str] | None = None,
) -> dict[str, Any] | None:
"""Create a GitHub issue and a linked Ticket."""
- from posthog.models.integration import GitHubIntegration, Integration
+ from posthog.models.integration import GitHubIntegration
try:
team = Team.objects.get(id=team_id)
@@ -403,8 +404,8 @@ def create_github_issue(
return None
try:
- integration = Integration.objects.get(id=integration_id, team=team, kind="github")
- except Integration.DoesNotExist:
+ integration = IntegrationManager.github.oauth.get(team.id, integration_id)
+ except IntegrationNotFound:
logger.warning("github_create_issue_integration_not_found", integration_id=integration_id)
return None
diff --git a/products/customer_analytics/backend/facade/api.py b/products/customer_analytics/backend/facade/api.py
index 94b80feb4274..090e7d82b7f3 100644
--- a/products/customer_analytics/backend/facade/api.py
+++ b/products/customer_analytics/backend/facade/api.py
@@ -64,7 +64,8 @@ from temporalio.exceptions import WorkflowAlreadyStartedError
from posthog.dataclasses import frozen
from posthog.exceptions_capture import capture_exception
-from posthog.models import Integration, OrganizationMembership, Tag
+from posthog.integrations import IntegrationManager, IntegrationNotFound
+from posthog.models import OrganizationMembership, Tag
from posthog.models.activity_logging.activity_log import AuditableScope, Detail, Trigger, changes_between, log_activity
from posthog.models.group.util import get_group_by_key
from posthog.models.group_type_mapping import get_group_types_for_project
@@ -4712,7 +4713,7 @@ def list_calendar_sync_statuses(team_id: int) -> list[contracts.CalendarSyncStat
)
statuses = []
- for integration in Integration.objects.filter(team_id=team_id, kind="google-calendar").order_by("id"):
+ for integration in sorted(IntegrationManager.google_calendar.oauth.list(team_id), key=lambda row: row.id):
config = integration.config or {}
last_synced_at = _parse_datetime(config.get(LAST_SYNCED_AT_CONFIG_KEY))
started_at = _parse_datetime(config.get(SYNC_STARTED_AT_CONFIG_KEY))
@@ -4745,7 +4746,7 @@ def update_calendar_sync_interval(team_id: int, integration_id: int, interval_mi
try:
update_calendar_sync_config(integration_id, team_id, {SYNC_INTERVAL_CONFIG_KEY: interval_minutes})
- except Integration.DoesNotExist:
+ except IntegrationNotFound:
return False
return True
@@ -4770,12 +4771,9 @@ def trigger_calendar_sync(
schedule. Returns 'started', 'already_running' (a sync for this calendar is in
flight; the workflow id is deterministic per integration), or None when the
integration doesn't exist for this team (→ 404)."""
- integration = (
- Integration.objects.only("id", "kind", "created_by_id")
- .filter(id=integration_id, team_id=team_id, kind=Integration.IntegrationKind.GOOGLE_CALENDAR)
- .first()
- )
- if integration is None:
+ try:
+ integration = IntegrationManager.google_calendar.oauth.get(team_id, integration_id)
+ except IntegrationNotFound:
return None
if not has_management_access and not integration.can_be_managed_by_creator(user_id):
raise ResourceForbiddenError
@@ -4812,12 +4810,9 @@ def trigger_google_account_backfill(
end_date: date,
has_management_access: bool,
) -> str | None:
- integration = (
- Integration.objects.only("id")
- .filter(id=integration_id, team_id=team_id, kind=Integration.IntegrationKind.GOOGLE_CALENDAR)
- .first()
- )
- if integration is None:
+ try:
+ IntegrationManager.google_calendar.oauth.get(team_id, integration_id)
+ except IntegrationNotFound:
return None
if not has_management_access:
raise ResourceForbiddenError
@@ -5739,8 +5734,10 @@ def _to_event_stream_view(stream: EventStream) -> contracts.EventStreamView:
def _validate_slack_integration(team_id: int, integration_id: int | None) -> None:
if integration_id is None:
return
- if not Integration.objects.filter(team_id=team_id, id=integration_id, kind="slack").exists():
- raise EventStreamValidationError("Slack integration not found for this team.")
+ try:
+ IntegrationManager.slack.oauth.get(team_id, integration_id)
+ except IntegrationNotFound:
+ raise EventStreamValidationError("Slack integration not found for this team.") from None
def _normalize_event_names(event_names: Iterable[str]) -> list[str]:
diff --git a/products/customer_analytics/backend/logic/calendar_sync.py b/products/customer_analytics/backend/logic/calendar_sync.py
index 62ddfe0df3bf..a9e41f0e5a6d 100644
--- a/products/customer_analytics/backend/logic/calendar_sync.py
+++ b/products/customer_analytics/backend/logic/calendar_sync.py
@@ -1,9 +1,9 @@
+from collections.abc import Mapping
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from email.utils import parseaddr
from typing import Any
-from django.db import transaction
from django.db.models import Prefetch, Q
from django.utils import timezone
@@ -12,7 +12,8 @@ import structlog
from posthog.clickhouse.query_tagging import Feature, Product, tags_context
from posthog.dataclasses import frozen
from posthog.egress.google_workspace import google_workspace_request
-from posthog.models.integration import ERROR_TOKEN_REFRESH_FAILED, Integration, OauthIntegration
+from posthog.integrations import IntegrationManager, IntegrationRecord
+from posthog.models.integration import ERROR_TOKEN_REFRESH_FAILED, OauthIntegration
from posthog.models.team import Team
from products.customer_analytics.backend.logic.email_account_matching import match_accounts_for_emails
@@ -65,7 +66,7 @@ class CalendarBackfillPage:
counts: CalendarSyncCounts
-def get_calendar_sync_interval(config: dict) -> int:
+def get_calendar_sync_interval(config: Mapping[str, Any]) -> int:
value = config.get(SYNC_INTERVAL_CONFIG_KEY)
return value if type(value) is int and value in ALLOWED_SYNC_INTERVALS else 60
@@ -73,16 +74,9 @@ def get_calendar_sync_interval(config: dict) -> int:
def update_calendar_sync_config(
integration_id: int, team_id: int, updates: dict[str, object], removals: tuple[str, ...] = ()
) -> None:
- with transaction.atomic():
- integration = Integration.objects.select_for_update().get(
- id=integration_id, team_id=team_id, kind="google-calendar"
- )
- config = dict(integration.config or {})
- config.update(updates)
- for key in removals:
- config.pop(key, None)
- integration.config = config
- integration.save(update_fields=["config"])
+ IntegrationManager.google_calendar.oauth.update(
+ team_id, integration_id, config_patch=updates, config_remove=removals
+ )
def mark_calendar_sync_started(integration_id: int, team_id: int) -> None:
@@ -122,8 +116,8 @@ def sync_calendar_integration(integration_id: int, team_id: int) -> CalendarSync
incremental via the stored syncToken. A stale token (HTTP 410) drops the cursor
and re-lists the whole window in the same run.
"""
- integration = Integration.objects.get(id=integration_id, team_id=team_id, kind="google-calendar")
- team = integration.team
+ integration = IntegrationManager.google_calendar.oauth.get(team_id, integration_id)
+ team = Team.objects.get(id=integration.team_id)
counts = CalendarSyncCounts()
try:
access_token = _get_fresh_access_token(integration)
@@ -131,7 +125,7 @@ def sync_calendar_integration(integration_id: int, team_id: int) -> CalendarSync
internal_domain = _domain_of(connected_email)
mark_calendar_sync_started(integration_id, team_id)
- integration.refresh_from_db(fields=["config"])
+ integration = IntegrationManager.google_calendar.oauth.get(team_id, integration_id)
sync_token = (integration.config or {}).get(SYNC_TOKEN_CONFIG_KEY)
try:
@@ -162,7 +156,7 @@ def sync_calendar_integration_backfill_page(
if start_at >= end_at:
raise CalendarSyncError("Calendar backfill start must be before its end")
- integration = Integration.objects.get(id=integration_id, team_id=team_id, kind="google-calendar")
+ integration = IntegrationManager.google_calendar.oauth.get(team_id, integration_id)
access_token = _get_fresh_access_token(integration)
mark_calendar_sync_started(integration_id, team_id)
params: dict[str, Any] = {
@@ -177,7 +171,7 @@ def sync_calendar_integration_backfill_page(
payload = _get_events_page(access_token, str(integration.integration_id), params)
counts = CalendarSyncCounts(fetched=len(payload.get("items", [])))
_process_events(
- integration.team,
+ Team.objects.get(id=integration.team_id),
payload.get("items", []),
_domain_of((integration.config or {}).get("email", "")),
counts,
@@ -457,7 +451,7 @@ def _person_uuids_by_email(team: Team, emails: list[str]) -> dict[str, str]:
return email_to_uuid
-def _get_fresh_access_token(integration: Integration) -> str:
+def _get_fresh_access_token(integration: IntegrationRecord) -> str:
oauth = OauthIntegration(integration)
if integration.errors != ERROR_TOKEN_REFRESH_FAILED and oauth.access_token_expired():
oauth.refresh_access_token()
diff --git a/products/customer_analytics/backend/logic/event_stream_destination.py b/products/customer_analytics/backend/logic/event_stream_destination.py
index 71eaaa07659b..0c274912b2b4 100644
--- a/products/customer_analytics/backend/logic/event_stream_destination.py
+++ b/products/customer_analytics/backend/logic/event_stream_destination.py
@@ -13,7 +13,8 @@ from typing import TYPE_CHECKING, Any
import structlog
from slack_sdk.errors import SlackApiError
-from posthog.models.integration import Integration, SlackIntegration
+from posthog.integrations import IntegrationManager, IntegrationNotFound
+from posthog.models.integration import SlackIntegration
from products.cdp.backend.facade.api import HogFunctionSerializer
from products.cdp.backend.facade.models import HogFunction
@@ -180,9 +181,10 @@ def send_test_slack_message(*, team_id: int, stream_id: str, user: "User") -> st
return None
if not (stream.slack_integration_id and stream.slack_channel_id):
raise EventStreamTestMessageError("Save a Slack workspace and channel before sending a test message.")
- integration = Integration.objects.filter(team_id=team_id, id=stream.slack_integration_id, kind="slack").first()
- if integration is None:
- raise EventStreamTestMessageError("The stream's Slack workspace is no longer connected.")
+ try:
+ integration = IntegrationManager.slack.oauth.get(team_id, stream.slack_integration_id)
+ except IntegrationNotFound:
+ raise EventStreamTestMessageError("The stream's Slack workspace is no longer connected.") from None
try:
SlackIntegration(integration, source="customer_analytics_event_stream").client.chat_postMessage(
diff --git a/products/customer_analytics/backend/logic/feature_request_github.py b/products/customer_analytics/backend/logic/feature_request_github.py
index 8f09e4bc1ce2..b6011f902e2b 100644
--- a/products/customer_analytics/backend/logic/feature_request_github.py
+++ b/products/customer_analytics/backend/logic/feature_request_github.py
@@ -1,11 +1,12 @@
import re
from datetime import datetime
from functools import partial
-from typing import TYPE_CHECKING, Literal
+from typing import TYPE_CHECKING, Literal, cast
from urllib.parse import urlparse
from uuid import UUID
from django.db import transaction
+from django.db.models import Model
from django.utils import timezone
import requests
@@ -15,7 +16,7 @@ from structlog.contextvars import bound_contextvars, get_contextvars
from posthog.dataclasses import frozen
from posthog.egress.github.transport import GitHubEgressBudgetExhausted, GitHubRateLimitError
from posthog.egress.limiter.policies import Priority
-from posthog.models import Integration
+from posthog.integrations import IntegrationManager, IntegrationNotFound, IntegrationRecord
from posthog.models.github_integration_base import GitHubIntegrationError
from posthog.models.integration.github import GitHubIntegration
from posthog.permissions import posthog_feature_flag_enabled
@@ -82,7 +83,7 @@ def _log_manual_action_after_commit(
def _log_issue_fetch_failure(
- integration: Integration, *, stage: str, error_type: str, status_code: int | None = None
+ integration: IntegrationRecord, *, stage: str, error_type: str, status_code: int | None = None
) -> None:
logger.warning(
"feature_request_github_issue_fetch_failed",
@@ -179,10 +180,13 @@ def _integration(
integration_id: int | None,
installation_id: str | None = None,
user_access_control: "UserAccessControl | None" = None,
-) -> Integration:
+) -> IntegrationRecord:
if integration_id is None:
raise FeatureRequestValidationError("integration_id", "Reconnect the GitHub integration before resuming sync.")
- integration = Integration.objects.filter(id=integration_id, team_id=team_id, kind="github").first()
+ try:
+ integration: IntegrationRecord | None = IntegrationManager.github.oauth.get(team_id, integration_id)
+ except IntegrationNotFound:
+ integration = None
if (
integration is None
or not integration.integration_id
@@ -195,14 +199,14 @@ def _integration(
"integration_id", "Select an available GitHub integration for this project."
)
if user_access_control is not None and not user_access_control.check_access_level_for_object(
- integration, required_level="viewer"
+ cast(Model, integration), required_level="viewer"
):
raise FeatureRequestValidationError("integration_id", "Select a GitHub integration you can access.")
return integration
def _fetch_issue(
- integration: Integration, repository: str, issue_number: int, *, priority: Priority = Priority.NORMAL
+ integration: IntegrationRecord, repository: str, issue_number: int, *, priority: Priority = Priority.NORMAL
) -> GitHubIssueState:
try:
response = GitHubIntegration(integration, source="customer_analytics", priority=priority).api_request(
@@ -321,7 +325,7 @@ def link_feature_request_github(
"issue_url", "This request already has a GitHub issue. Unlink it before adding another."
)
integration = _integration(team_id, input.integration_id, user_access_control=user_access_control)
- installation_id = integration.integration_id
+ installation_id = integration.integration_id or ""
with bound_contextvars(
team_id=team_id,
feature_request_id=str(feature_request_id),
@@ -342,7 +346,7 @@ def link_feature_request_github(
link = FeatureRequestGitHubLink.objects.for_team(team_id).create(
team_id=team_id,
feature_request=request,
- integration=integration,
+ integration_id=integration.id,
installation_id=installation_id,
repository=repository,
issue_number=issue_number,
@@ -475,7 +479,7 @@ def unlink_feature_request_github(
)
-def _eligible_integration(link: FeatureRequestGitHubLink) -> tuple[Integration | None, str | None]:
+def _eligible_integration(link: FeatureRequestGitHubLink) -> tuple[IntegrationRecord | None, str | None]:
if not link.sync_enabled:
return None, "paused"
if link.feature_request.archived_at is not None:
diff --git a/products/engineering_analytics/backend/logic/job_log_insights.py b/products/engineering_analytics/backend/logic/job_log_insights.py
index 7de98b08fdb8..e0682fd3dd9f 100644
--- a/products/engineering_analytics/backend/logic/job_log_insights.py
+++ b/products/engineering_analytics/backend/logic/job_log_insights.py
@@ -16,7 +16,8 @@ import structlog
from posthog.dataclasses import frozen
from posthog.egress.github.limiter import consume_github_installation_sync
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager, IntegrationNotFound
+from posthog.models.integration import GitHubIntegration
from posthog.models.team import Team
from products.engineering_analytics.backend.facade.contracts import CIEngine, JobLogInsights
@@ -127,8 +128,9 @@ def _log_credential(team: Team, source_id: str) -> _LogCredential | None:
return _LogCredential(token=credential.personal_access_token, installation_id=None)
if credential.integration_id is None:
return None
- integration = Integration.objects.filter(team_id=team.pk, id=credential.integration_id, kind="github").first()
- if integration is None:
+ try:
+ integration = IntegrationManager.github.oauth.get(team.pk, credential.integration_id)
+ except IntegrationNotFound:
return None
github = GitHubIntegration(integration, source=_EGRESS_SOURCE, priority=Priority.NORMAL)
if not github.github_installation_id:
diff --git a/products/engineering_analytics/backend/logic/job_logs/activity.py b/products/engineering_analytics/backend/logic/job_logs/activity.py
index 3eb2b273076c..a13e2b8cddd0 100644
--- a/products/engineering_analytics/backend/logic/job_logs/activity.py
+++ b/products/engineering_analytics/backend/logic/job_logs/activity.py
@@ -22,7 +22,8 @@ from temporalio.exceptions import ApplicationError
from posthog.dataclasses import frozen
from posthog.egress.github.limiter import acquire_github_installation
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager
+from posthog.models.integration import GitHubIntegration
from posthog.models.team import Team
from posthog.sync import database_sync_to_async
from posthog.temporal.common.base import PostHogWorkflow
@@ -93,13 +94,17 @@ def _resolve_credentials(team_id: int, integration_id: int) -> tuple[str, str, s
"""Return ``(github_access_token, installation_id, log_ingest_token)``. ``log_ingest_token`` is
the owning team's project API token — the OTLP Bearer that routes emitted logs to that team's Logs.
"""
- integration = Integration.objects.get(id=integration_id, team_id=team_id, kind="github")
+ integration = IntegrationManager.github.oauth.get(team_id, integration_id)
github = GitHubIntegration(integration, source="job_logs", priority=Priority.BATCH)
if github.access_token_expired():
github.refresh_access_token()
if not integration.access_token:
raise ValueError(f"No GitHub access token for integration {integration_id}")
- return integration.access_token, str(github.github_installation_id), integration.team.api_token
+ return (
+ integration.access_token,
+ str(github.github_installation_id),
+ Team.objects.values_list("api_token", flat=True).get(id=team_id),
+ )
def _resolve_depot_credentials(team_id: int, source_id: str) -> _DepotCredentials:
diff --git a/products/engineering_analytics/backend/logic/ownership_files.py b/products/engineering_analytics/backend/logic/ownership_files.py
index f5e1fc5b23e3..edb62b248d47 100644
--- a/products/engineering_analytics/backend/logic/ownership_files.py
+++ b/products/engineering_analytics/backend/logic/ownership_files.py
@@ -10,7 +10,8 @@ GitHub integration, and then to the anonymous public reader.
"""
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager, IntegrationNotFound
+from posthog.models.integration import GitHubIntegration
from posthog.models.team import Team
from posthog.ownership.github_files import EGRESS_SOURCE, AuthenticatedRepoFiles, GitHubFilesFetcher, fetcher_for_team
@@ -29,8 +30,9 @@ def _fetcher_for_credential(
return GitHubFilesFetcher.from_token(credential.personal_access_token, priority=priority)
if credential.integration_id is None:
return None
- integration = Integration.objects.filter(team_id=team_id, id=credential.integration_id, kind="github").first()
- if integration is None:
+ try:
+ integration = IntegrationManager.github.oauth.get(team_id, credential.integration_id)
+ except IntegrationNotFound:
return None
github = GitHubIntegration(integration, source=EGRESS_SOURCE, priority=priority)
return GitHubFilesFetcher.from_integration(github, priority=priority)
diff --git a/products/engineering_analytics/backend/logic/quarantine.py b/products/engineering_analytics/backend/logic/quarantine.py
index a21cd2d54eb5..092fd47c8218 100644
--- a/products/engineering_analytics/backend/logic/quarantine.py
+++ b/products/engineering_analytics/backend/logic/quarantine.py
@@ -37,7 +37,8 @@ import structlog
from posthog.hogql import ast
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager
+from posthog.models.integration import GitHubIntegration
from posthog.models.team import Team
from products.engineering_analytics.backend.facade.contracts import (
@@ -510,7 +511,7 @@ def _resolve_write_target(
"connect it as a GitHub source to quarantine there."
)
- integration_row = Integration.objects.filter(team=team, kind="github").first()
+ integration_row = IntegrationManager.github.oauth.find(team.id)
if integration_row is None:
raise QuarantineWriteError("Connect a GitHub integration with write access to quarantine tests from here.")
github = GitHubIntegration(integration_row, source="ci_quarantine", priority=Priority.CRITICAL)
diff --git a/products/engineering_analytics/backend/tests/test_access_control.py b/products/engineering_analytics/backend/tests/test_access_control.py
index 835e2f2485fc..dd653bb70b0f 100644
--- a/products/engineering_analytics/backend/tests/test_access_control.py
+++ b/products/engineering_analytics/backend/tests/test_access_control.py
@@ -137,9 +137,9 @@ class TestEngineeringAnalyticsAccessControl(WarehouseAccessControlTestMixin):
mock.patch(
"products.engineering_analytics.backend.logic.quarantine.GitHubIntegration", return_value=github
),
- mock.patch("products.engineering_analytics.backend.logic.quarantine.Integration") as integration_cls,
+ mock.patch("products.engineering_analytics.backend.logic.quarantine.IntegrationManager") as manager,
):
- integration_cls.objects.filter.return_value.first.return_value = object()
+ manager.github.oauth.find.return_value = object()
response = self.client.post(
self._url("quarantine/request"),
{
diff --git a/products/engineering_analytics/backend/tests/test_quarantine.py b/products/engineering_analytics/backend/tests/test_quarantine.py
index 667a292a3605..0b7699a97d05 100644
--- a/products/engineering_analytics/backend/tests/test_quarantine.py
+++ b/products/engineering_analytics/backend/tests/test_quarantine.py
@@ -526,18 +526,18 @@ class TestQuarantineRender(TestCase):
class TestQuarantineRequest(BaseTest):
def _install(self, github: mock.Mock, *, has_integration: bool = True, connected: bool = True) -> mock.Mock:
gh_patch = mock.patch(f"{_Q}.GitHubIntegration", return_value=github)
- integration_patch = mock.patch(f"{_Q}.Integration")
+ integration_patch = mock.patch(f"{_Q}.IntegrationManager")
# The explicit repo override is authorized against the team's connected GitHub sources;
# mock that list so the real _repo_is_connected runs against a matching source (or none).
sources = [SimpleNamespace(repo="PostHog/posthog")] if connected else []
sources_patch = mock.patch(f"{_Q}.list_github_sources", return_value=sources)
gh_patch.start()
- integration_cls = integration_patch.start()
+ manager = integration_patch.start()
sources_patch.start()
self.addCleanup(gh_patch.stop)
self.addCleanup(integration_patch.stop)
self.addCleanup(sources_patch.stop)
- integration_cls.objects.filter.return_value.first.return_value = object() if has_integration else None
+ manager.github.oauth.find.return_value = object() if has_integration else None
return github
@time_machine.travel("2026-06-12", tick=False)
diff --git a/products/error_tracking/backend/logic/alerts.py b/products/error_tracking/backend/logic/alerts.py
index ee02b4fda947..85b763fbe1ce 100644
--- a/products/error_tracking/backend/logic/alerts.py
+++ b/products/error_tracking/backend/logic/alerts.py
@@ -8,7 +8,7 @@ from django.db.models import QuerySet
import structlog
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager
from posthog.models.scoping.manager import resolve_effective_team_id
from posthog.models.team.team import Team
from posthog.models.user import User
@@ -278,9 +278,11 @@ def _validate_destination(team_id: int, destination: dict[str, Any]) -> None:
# canonical team, so accept any integration in the same project.
project_id = Team.objects.values_list("project_id", flat=True).get(id=team_id)
# nosemgrep: environment-model-scoped-by-project -- alert rows live on the canonical team, so a Slack integration from any environment of the project is valid
- if not Integration.objects.filter(
- team__project_id=project_id, id=integration_id, kind=Integration.IntegrationKind.SLACK
- ).exists():
+ project_team_ids = Team.objects.filter(project_id=project_id).values_list("id", flat=True)
+ if not any(
+ IntegrationManager.slack.oauth.get_many(project_team_id, [integration_id])
+ for project_team_id in project_team_ids
+ ):
raise AlertValidationError("Slack integration not found for this project.")
channel = config.get("channel")
if not isinstance(channel, str) or not channel:
diff --git a/products/error_tracking/backend/logic/external_references.py b/products/error_tracking/backend/logic/external_references.py
index d7fc3eba5a95..9ea68172ccbf 100644
--- a/products/error_tracking/backend/logic/external_references.py
+++ b/products/error_tracking/backend/logic/external_references.py
@@ -7,12 +7,12 @@ from django.db.models import QuerySet
import requests
from posthog.egress.limiter.policies import Priority
+from posthog.integrations import IntegrationKind, IntegrationManager, IntegrationNotFound, IntegrationRecord
from posthog.models.integration import (
GitHubIntegration,
GitHubIntegrationError,
GitLabIntegration,
GitLabIntegrationError,
- Integration,
JiraIntegration,
LinearIntegration,
external_issue_url,
@@ -22,23 +22,27 @@ from posthog.models.integration import (
from products.error_tracking.backend.logic import get_issue_permalink_by_fingerprint
from products.error_tracking.backend.models import ErrorTrackingExternalReference, ErrorTrackingIssue
+_EXTERNAL_ISSUE_INTEGRATIONS = IntegrationManager.kinds(
+ IntegrationKind.GITHUB, IntegrationKind.GITLAB, IntegrationKind.LINEAR, IntegrationKind.JIRA
+)
+
class ErrorTrackingExternalReferenceValidationError(Exception):
pass
EXTERNAL_REFERENCE_REQUIRED_CONFIG_FIELDS = {
- Integration.IntegrationKind.GITHUB.value: ("repository", "title", "body"),
- Integration.IntegrationKind.GITLAB.value: ("title", "body"),
- Integration.IntegrationKind.LINEAR.value: ("team_id", "title", "description"),
- Integration.IntegrationKind.JIRA.value: ("project_key", "title", "description"),
+ IntegrationKind.GITHUB.value: ("repository", "title", "body"),
+ IntegrationKind.GITLAB.value: ("title", "body"),
+ IntegrationKind.LINEAR.value: ("team_id", "title", "description"),
+ IntegrationKind.JIRA.value: ("project_key", "title", "description"),
}
EXTERNAL_REFERENCE_NON_BLANK_CONFIG_FIELDS = {
- Integration.IntegrationKind.GITHUB.value: ("repository", "title"),
- Integration.IntegrationKind.GITLAB.value: ("title",),
- Integration.IntegrationKind.LINEAR.value: ("team_id", "title"),
- Integration.IntegrationKind.JIRA.value: ("project_key", "title"),
+ IntegrationKind.GITHUB.value: ("repository", "title"),
+ IntegrationKind.GITLAB.value: ("title",),
+ IntegrationKind.LINEAR.value: ("team_id", "title"),
+ IntegrationKind.JIRA.value: ("project_key", "title"),
}
# Keys build_external_issue_url reads per provider, with the identifier type each expects —
@@ -46,14 +50,14 @@ EXTERNAL_REFERENCE_NON_BLANK_CONFIG_FIELDS = {
# rather than creating a new one. References cannot be deleted, so a malformed identifier
# would persist a permanently broken link.
LINK_EXISTING_REQUIRED_CONTEXT_FIELDS: dict[str, dict[str, type]] = {
- Integration.IntegrationKind.GITHUB.value: {"repository": str, "number": int},
- Integration.IntegrationKind.GITLAB.value: {"issue_id": int},
- Integration.IntegrationKind.LINEAR.value: {"id": str},
- Integration.IntegrationKind.JIRA.value: {"key": str},
+ IntegrationKind.GITHUB.value: {"repository": str, "number": int},
+ IntegrationKind.GITLAB.value: {"issue_id": int},
+ IntegrationKind.LINEAR.value: {"id": str},
+ IntegrationKind.JIRA.value: {"key": str},
}
-def _validate_github_assignee(integration: Integration, repository: str, login: str) -> None:
+def _validate_github_assignee(integration: IntegrationRecord, repository: str, login: str) -> None:
# GitHub creates the issue and silently drops a login it cannot assign, so check before creating.
# Enterprise Managed User logins add an underscore and a shortcode, so allow "_" and a longer login.
if not re.fullmatch(r"[A-Za-z0-9_-]{1,100}", login):
@@ -72,7 +76,7 @@ def _validate_github_assignee(integration: Integration, repository: str, login:
)
-def _validate_external_reference_config(integration: Integration, config: Any) -> None:
+def _validate_external_reference_config(integration: IntegrationRecord, config: Any) -> None:
if not isinstance(config, dict):
raise ErrorTrackingExternalReferenceValidationError("External reference config must be an object.")
@@ -108,15 +112,15 @@ def _validate_external_reference_config(integration: Integration, config: Any) -
if (
assignee
and assignee.strip()
- and integration.kind == Integration.IntegrationKind.GITLAB
+ and integration.kind == IntegrationKind.GITLAB
and not re.fullmatch(r"[0-9]{1,20}", assignee.strip())
):
raise ErrorTrackingExternalReferenceValidationError("GitLab assignee must be a numeric user ID.")
- if assignee and assignee.strip() and integration.kind == Integration.IntegrationKind.GITHUB:
+ if assignee and assignee.strip() and integration.kind == IntegrationKind.GITHUB:
_validate_github_assignee(integration, config["repository"], assignee.strip())
- if integration.kind == Integration.IntegrationKind.LINEAR:
+ if integration.kind == IntegrationKind.LINEAR:
team_id = config["team_id"]
teams = LinearIntegration(integration).list_teams() or []
valid_team_ids = {str(team["id"]) for team in teams if isinstance(team, dict) and team.get("id")}
@@ -126,7 +130,7 @@ def _validate_external_reference_config(integration: Integration, config: Any) -
)
-def _clean_existing_external_context(integration: Integration, external_context: Any) -> dict[str, Any]:
+def _clean_existing_external_context(integration: IntegrationRecord, external_context: Any) -> dict[str, Any]:
"""Validate and normalize the external_context for linking an already-existing issue.
Keeps the provider identifier fields and an optional title, so we never persist arbitrary
@@ -173,7 +177,7 @@ def _clean_existing_external_context(integration: Integration, external_context:
)
cleaned["title"] = title.strip()
- if integration.kind == Integration.IntegrationKind.GITHUB:
+ if integration.kind == IntegrationKind.GITHUB:
resource_type = external_context.get("resource_type")
if resource_type is not None:
if resource_type not in {"issue", "pull_request"}:
@@ -193,18 +197,30 @@ def get_external_reference(reference_id: UUID, team_id: int) -> ErrorTrackingExt
return list_external_references(team_id=team_id).filter(id=reference_id).first()
+def _get_integration(team_id: int, integration_id: int) -> IntegrationRecord:
+ try:
+ return _EXTERNAL_ISSUE_INTEGRATIONS.get(team_id, integration_id)
+ except IntegrationNotFound:
+ pass
+ # A row of another kind gets the provider error below, as before. A supported kind with a
+ # non-default auth type stays "not found", because the provider code cannot use it.
+ try:
+ integration = IntegrationManager.any.get(team_id, integration_id)
+ except IntegrationNotFound:
+ raise ErrorTrackingExternalReferenceValidationError("Integration does not belong to this team.")
+ if is_supported_external_issue_provider(integration.kind):
+ raise ErrorTrackingExternalReferenceValidationError("Integration does not belong to this team.")
+ return integration
+
+
def _get_issue_and_integration(
team_id: int, issue_id: UUID, integration_id: int
-) -> tuple[ErrorTrackingIssue, Integration]:
+) -> tuple[ErrorTrackingIssue, IntegrationRecord]:
issue = ErrorTrackingIssue.objects.filter(id=issue_id, team_id=team_id).first()
if issue is None:
raise ErrorTrackingExternalReferenceValidationError("Issue does not belong to this team.")
- integration = Integration.objects.filter(id=integration_id, team_id=team_id).first()
- if integration is None:
- raise ErrorTrackingExternalReferenceValidationError("Integration does not belong to this team.")
-
- return issue, integration
+ return issue, _get_integration(team_id, integration_id)
def create_external_reference(
@@ -240,7 +256,7 @@ def create_external_reference(
# Containment (not equality) also matches references the create flow stored
# with extra provider keys alongside the identifier.
existing = ErrorTrackingExternalReference.objects.filter(
- issue=issue, integration=integration, external_context__contains=identifier_context
+ issue=issue, integration_id=integration.id, external_context__contains=identifier_context
).first()
if existing is not None:
updated_context = {
@@ -251,13 +267,13 @@ def create_external_reference(
existing.external_context = updated_context
existing.save(update_fields=["external_context"])
return existing, False
- if integration.kind == Integration.IntegrationKind.LINEAR:
+ if integration.kind == IntegrationKind.LINEAR:
# Linked issues get the same PostHog back-link attachment as created ones.
attachment_url = get_issue_permalink_by_fingerprint(team_id=team_id, issue_id=issue.id)
LinearIntegration(integration).create_attachment(stored_context["id"], attachment_url)
return ErrorTrackingExternalReference.objects.create(
issue=issue,
- integration=integration,
+ integration_id=integration.id,
external_context=stored_context,
), True
@@ -269,16 +285,16 @@ def create_external_reference(
if assignee:
provider_config["assignee"] = assignee
- if integration.kind == Integration.IntegrationKind.GITHUB:
+ if integration.kind == IntegrationKind.GITHUB:
created_context = GitHubIntegration(
integration, source="error_tracking", priority=Priority.CRITICAL
).create_issue(provider_config)
- elif integration.kind == Integration.IntegrationKind.GITLAB:
+ elif integration.kind == IntegrationKind.GITLAB:
created_context = GitLabIntegration(integration).create_issue(provider_config)
- elif integration.kind == Integration.IntegrationKind.LINEAR:
+ elif integration.kind == IntegrationKind.LINEAR:
attachment_url = get_issue_permalink_by_fingerprint(team_id=team_id, issue_id=issue.id)
created_context = LinearIntegration(integration).create_issue(attachment_url, provider_config)
- elif integration.kind == Integration.IntegrationKind.JIRA:
+ elif integration.kind == IntegrationKind.JIRA:
created_context = JiraIntegration(integration).create_issue(provider_config)
else:
raise ErrorTrackingExternalReferenceValidationError("Provider not supported")
@@ -286,7 +302,7 @@ def create_external_reference(
created_context["title"] = title
return ErrorTrackingExternalReference.objects.create(
issue=issue,
- integration=integration,
+ integration_id=integration.id,
external_context=created_context,
), True
@@ -303,14 +319,12 @@ def search_external_issues(
Each result is ``{id, title, url, external_context}`` where ``external_context`` is the
exact payload to persist when the user selects it.
"""
- integration = Integration.objects.filter(id=integration_id, team_id=team_id).first()
- if integration is None:
- raise ErrorTrackingExternalReferenceValidationError("Integration does not belong to this team.")
+ integration = _get_integration(team_id, integration_id)
# Provider failures (expired tokens, rate limits) surface as validation errors so the
# endpoint returns an actionable 400 instead of a 500.
try:
- if integration.kind == Integration.IntegrationKind.GITHUB:
+ if integration.kind == IntegrationKind.GITHUB:
if not repository:
raise ErrorTrackingExternalReferenceValidationError("A repository is required to search GitHub issues.")
# Bare names only, matching what create/link store: an owner-qualified path can
@@ -322,11 +336,11 @@ def search_external_issues(
return GitHubIntegration(integration, source="error_tracking", priority=Priority.CRITICAL).search_issues(
repository, search
)
- elif integration.kind == Integration.IntegrationKind.GITLAB:
+ elif integration.kind == IntegrationKind.GITLAB:
return GitLabIntegration(integration).search_issues(search)
- elif integration.kind == Integration.IntegrationKind.LINEAR:
+ elif integration.kind == IntegrationKind.LINEAR:
return LinearIntegration(integration).search_issues(search)
- elif integration.kind == Integration.IntegrationKind.JIRA:
+ elif integration.kind == IntegrationKind.JIRA:
return JiraIntegration(integration).search_issues(search)
except (GitHubIntegrationError, GitLabIntegrationError, requests.RequestException, ValueError) as error:
# RequestException and ValueError (JSON decoding) cover provider timeouts,
@@ -343,18 +357,18 @@ def build_external_issue_url(reference: ErrorTrackingExternalReference) -> str:
def external_issue_id(reference: ErrorTrackingExternalReference) -> str:
external_context = reference.external_context or {}
id_field = {
- Integration.IntegrationKind.GITHUB.value: "number",
- Integration.IntegrationKind.GITLAB.value: "issue_id",
- Integration.IntegrationKind.LINEAR.value: "id",
- Integration.IntegrationKind.JIRA.value: "key",
+ IntegrationKind.GITHUB.value: "number",
+ IntegrationKind.GITLAB.value: "issue_id",
+ IntegrationKind.LINEAR.value: "id",
+ IntegrationKind.JIRA.value: "key",
}.get(reference.integration.kind)
value = external_context.get(id_field) if id_field else reference.external_id
if value is None:
return ""
external_id = str(value)
if reference.integration.kind in {
- Integration.IntegrationKind.GITHUB,
- Integration.IntegrationKind.GITLAB,
+ IntegrationKind.GITHUB,
+ IntegrationKind.GITLAB,
} and not external_id.startswith("#"):
return f"#{external_id}"
return external_id
diff --git a/products/error_tracking/backend/logic/github_external_references.py b/products/error_tracking/backend/logic/github_external_references.py
index 944d1c9a76e2..6a44449731f7 100644
--- a/products/error_tracking/backend/logic/github_external_references.py
+++ b/products/error_tracking/backend/logic/github_external_references.py
@@ -9,7 +9,8 @@ from django.db import transaction
from posthog.dataclasses import frozen
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager, IntegrationRecord
+from posthog.models.integration import GitHubIntegration
from products.error_tracking.backend.facade import contracts
from products.error_tracking.backend.logic import external_references
@@ -149,7 +150,7 @@ def _resolve_issue(job: contracts.GitHubExternalReferenceJob) -> ErrorTrackingIs
return fingerprint.issue if fingerprint is not None else None
-def _actor_lacks_write_permission(integration: Integration, job: contracts.GitHubExternalReferenceJob) -> bool:
+def _actor_lacks_write_permission(integration: IntegrationRecord, job: contracts.GitHubExternalReferenceJob) -> bool:
"""Whether the actor's effective permission on the repo is below write.
This is the only gate that proves repo access, because the webhook's author_association
@@ -178,15 +179,7 @@ def _actor_lacks_write_permission(integration: Integration, job: contracts.GitHu
def link_reference(job: contracts.GitHubExternalReferenceJob) -> bool:
owner, repository = job.repository_full_name.split("/", 1)
- integration = (
- Integration.objects.filter(
- team_id=job.team_id,
- kind=Integration.IntegrationKind.GITHUB.value,
- integration_id=job.installation_id,
- )
- .order_by("id")
- .first()
- )
+ integration = IntegrationManager.github.oauth.find(job.team_id, external_id=job.installation_id)
if integration is None:
return False
try:
diff --git a/products/error_tracking/backend/presentation/views/git_provider_file_link_resolver.py b/products/error_tracking/backend/presentation/views/git_provider_file_link_resolver.py
index 41647a878804..a86040c17c7d 100644
--- a/products/error_tracking/backend/presentation/views/git_provider_file_link_resolver.py
+++ b/products/error_tracking/backend/presentation/views/git_provider_file_link_resolver.py
@@ -15,7 +15,8 @@ from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.utils import action
from posthog.egress.github.transport import GitHubEgressBudgetExhausted, github_request
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, GitLabIntegration, GitLabIntegrationError, Integration
+from posthog.integrations import IntegrationManager, IntegrationRecord
+from posthog.models.integration import GitHubIntegration, GitLabIntegration, GitLabIntegrationError
logger = structlog.get_logger(__name__)
@@ -303,7 +304,7 @@ class GitProviderFileLinksViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):
return Response({"found": True, "url": outcome.url})
# Try with assigned GitHub integration (private repos).
- integration = Integration.objects.filter(team_id=self.team.id, kind="github").first()
+ integration = IntegrationManager.github.oauth.find(self.team.id)
if integration:
github = GitHubIntegration(integration, source="error_tracking", priority=Priority.NORMAL)
@@ -357,12 +358,12 @@ class GitProviderFileLinksViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):
return Response({"found": True, "url": url})
# Try with team GitLab integrations (private repos and self-hosted).
- integrations = Integration.objects.filter(team_id=self.team.id, kind="gitlab")
+ integrations = IntegrationManager.gitlab.token.list(self.team.id)
if not integrations:
return Response({"found": False})
- def try_integration(integration: Integration) -> str | None:
+ def try_integration(integration: IntegrationRecord) -> str | None:
try:
gitlab = GitLabIntegration(integration)
hostname = gitlab.hostname
diff --git a/products/error_tracking/backend/temporal/alerts/delivery.py b/products/error_tracking/backend/temporal/alerts/delivery.py
index 66b6db527517..213af46c0f88 100644
--- a/products/error_tracking/backend/temporal/alerts/delivery.py
+++ b/products/error_tracking/backend/temporal/alerts/delivery.py
@@ -27,7 +27,8 @@ from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from temporalio.exceptions import ApplicationError
-from posthog.models.integration import Integration, SlackIntegration
+from posthog.integrations import IntegrationKind
+from posthog.models.integration import SlackIntegration
from posthog.redis import get_client
from products.error_tracking.backend.logic.alerts import MAX_THROTTLE_SECONDS
@@ -621,6 +622,6 @@ def _reconcile_root_status(
def _slack_client(destination: ErrorTrackingAlertDestination) -> WebClient | None:
integration = destination.integration
- if integration is None or integration.kind != Integration.IntegrationKind.SLACK:
+ if integration is None or integration.kind != IntegrationKind.SLACK:
return None
return SlackIntegration(integration, source="error_tracking_alerts").client
diff --git a/products/error_tracking/backend/test/test_facade_api.py b/products/error_tracking/backend/test/test_facade_api.py
index 4c0143aa1571..757b9ae9a5ef 100644
--- a/products/error_tracking/backend/test/test_facade_api.py
+++ b/products/error_tracking/backend/test/test_facade_api.py
@@ -11,6 +11,7 @@ import requests
from parameterized import parameterized
from rest_framework.exceptions import ValidationError as DRFValidationError
+from posthog.integrations import SOURCE_MARKER_CONFIG_KEY, IntegrationManager
from posthog.models import Team
from posthog.models.event_filter_config import EventFilterPipelineOverride
from posthog.models.event_ingestion_restriction_config import IngestionPipeline
@@ -256,6 +257,33 @@ class TestErrorTrackingFacadeAPI(BaseTest):
assert not ErrorTrackingExternalReference.objects.exists()
+ @parameterized.expand(
+ [
+ ("unsupported_kind", "slack", {}, "Provider not supported"),
+ ("source_row", "github", {SOURCE_MARKER_CONFIG_KEY: "Github"}, "Integration does not belong to this team."),
+ ("other_auth_type", "github", None, "Integration does not belong to this team."),
+ ]
+ )
+ def test_link_external_reference_integration_lookup_errors(self, _name, kind, config, expected_message):
+ issue = self._create_issue(team=self.team, name="Checkout TypeError")
+ if config is None:
+ integration, _ = IntegrationManager.github.token.upsert(
+ self.team.id, "token-row", config={}, sensitive_config={"access_token": "token"}
+ )
+ else:
+ integration = Integration.objects.create(team=self.team, kind=kind, config=config)
+
+ with self.assertRaises(api.ExternalReferenceValidationError) as raised:
+ api.create_external_reference(
+ team_id=self.team.id,
+ issue_id=issue.id,
+ integration_id=integration.id,
+ external_context={"key": "ENG-1"},
+ distinct_id=self.user.id,
+ )
+
+ assert str(raised.exception) == expected_message
+
@parameterized.expand(
[
("both", {"title": "x"}, {"key": "ENG-1"}),
diff --git a/products/messaging/backend/presentation/views/customerio_webhook.py b/products/messaging/backend/presentation/views/customerio_webhook.py
index 83a91e01687d..119e87db2f16 100644
--- a/products/messaging/backend/presentation/views/customerio_webhook.py
+++ b/products/messaging/backend/presentation/views/customerio_webhook.py
@@ -6,7 +6,7 @@ from rest_framework.response import Response
from rest_framework.views import APIView
from posthog.auth import WebhookSignatureAuthentication
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager, IntegrationNotFound
from products.messaging.backend.facade.customerio import (
record_global_resubscribe,
@@ -45,7 +45,10 @@ class CustomerIOWebhookAuthentication(WebhookSignatureAuthentication):
def get_auth_context(self, request: Request) -> Any:
if self._team_id is None or self._integration_id is None:
return None
- return Integration.objects.filter(team_id=self._team_id, pk=self._integration_id).first()
+ try:
+ return IntegrationManager.customerio_webhook.token.get(self._team_id, self._integration_id)
+ except IntegrationNotFound:
+ return None
class CustomerIOWebhookView(APIView):
diff --git a/products/messaging/backend/presentation/views/message_templates.py b/products/messaging/backend/presentation/views/message_templates.py
index 3dce0c0caef7..948efaa172c7 100644
--- a/products/messaging/backend/presentation/views/message_templates.py
+++ b/products/messaging/backend/presentation/views/message_templates.py
@@ -31,8 +31,8 @@ from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.shared import UserBasicSerializer
from posthog.auth import ProjectSecretAPIKeyAuthentication
from posthog.event_usage import groups, report_user_action
+from posthog.integrations import IntegrationManager, IntegrationNotFound
from posthog.models import User
-from posthog.models.integration import Integration
from posthog.models.tag import tagify
from posthog.permissions import is_authenticated_via_project_secret_api_key
from posthog.rate_limit import PersonalOrProjectSecretApiKeyRateThrottle, ProjectSecretApiKeyTeamRateThrottle
@@ -146,8 +146,12 @@ class EmailTemplateSenderSerializer(serializers.Serializer):
integration_id = data.get("integrationId")
if integration_id is None:
raise serializers.ValidationError({"integrationId": "Choose an email sender."})
- if not Integration.objects.filter(team_id=self.context["team_id"], kind="email", id=integration_id).exists():
- raise serializers.ValidationError({"integrationId": "No email sender with this ID in this project."})
+ try:
+ IntegrationManager.email.credentials.get(self.context["team_id"], integration_id)
+ except IntegrationNotFound:
+ raise serializers.ValidationError(
+ {"integrationId": "No email sender with this ID in this project."}
+ ) from None
return data
diff --git a/products/messaging/backend/remote_config.py b/products/messaging/backend/remote_config.py
index 914336c9e271..46b6dd276e4d 100644
--- a/products/messaging/backend/remote_config.py
+++ b/products/messaging/backend/remote_config.py
@@ -1,4 +1,4 @@
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager
# Firebase identifies an app by its project_id, APNs by its bundle_id. Kept in step with
# nodejs/src/messaging/push-subscriptions/push-subscriptions.service.ts, which resolves the same
@@ -16,9 +16,7 @@ def build_push_config(team_id: int) -> dict[str, list[str]]:
Always present, including as an empty list. An SDK cannot tell an unconfigured project from an
older server that never sends the key, so absent has to keep meaning "attempt the registration".
"""
- integrations = Integration.objects.filter(team_id=team_id, kind__in=list(PUSH_APP_ID_CONFIG_KEYS)).only(
- "kind", "config"
- )
+ integrations = IntegrationManager.kinds(*PUSH_APP_ID_CONFIG_KEYS).list(team_id)
# config is a JSONField, so an identifier can be any JSON value. Anything but a non-empty string is
# unusable as an app_id, and letting one through would raise inside the set or the sort — which
# aborts build_config and leaves the team's whole remote config stale, not just its push key.
diff --git a/products/messaging/backend/services/customerio_config.py b/products/messaging/backend/services/customerio_config.py
index 0a82f72cfbe7..b970ee3c1054 100644
--- a/products/messaging/backend/services/customerio_config.py
+++ b/products/messaging/backend/services/customerio_config.py
@@ -3,7 +3,7 @@ from typing import IO, Any
from django.utils import timezone
from posthog.dataclasses import frozen
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager, IntegrationNotFound, IntegrationReader, IntegrationRecord
from products.messaging.backend.models.optout_sync_config import OptOutSyncConfig
from products.messaging.backend.services.customerio_import_service import CustomerIOImportService
@@ -25,7 +25,7 @@ class ConfigIncomplete(Exception):
def resolve_app_api_key(team_id: int, app_api_key: str | None) -> str:
"""The App API key to import with: the one sent, or the stored one when none is sent."""
- integration = Integration.objects.filter(team_id=team_id, kind="customerio-app").first()
+ integration = IntegrationManager.customerio_app.token.find(team_id)
api_key = app_api_key or (integration.sensitive_config.get("app_api_key") if integration else None)
if not api_key:
@@ -37,27 +37,30 @@ def resolve_app_api_key(team_id: int, app_api_key: str | None) -> str:
return api_key
-def save_app_integration(team_id: int, api_key: str, created_by_id: int | None) -> Integration:
+def save_app_integration(team_id: int, api_key: str, created_by_id: int | None) -> IntegrationRecord:
"""Store the App API key on first use and link it to the sync config. Returns the stored integration."""
- integration = Integration.objects.filter(team_id=team_id, kind="customerio-app").first()
+ integration = IntegrationManager.customerio_app.token.find(team_id)
if not integration:
- integration = Integration.objects.create(
- team_id=team_id,
- kind="customerio-app",
+ integration = IntegrationManager.customerio_app.token.create(
+ team_id,
+ external_id=None,
+ config={},
sensitive_config={"app_api_key": api_key},
created_by_id=created_by_id,
- errors="",
)
config, _ = OptOutSyncConfig.objects.get_or_create(team_id=team_id)
- config.app_integration = integration
+ config.app_integration_id = integration.id
config.save(update_fields=["app_integration"])
return integration
def stored_app_api_key(team_id: int, integration_id: int) -> str | None:
- integration = Integration.objects.filter(team_id=team_id, kind="customerio-app", id=integration_id).first()
- return integration.sensitive_config.get("app_api_key") if integration else None
+ try:
+ integration = IntegrationManager.customerio_app.token.get(team_id, integration_id)
+ except IntegrationNotFound:
+ return None
+ return integration.sensitive_config.get("app_api_key")
def import_from_customerio(team_id: int, app_api_key: str | None, created_by_id: int | None) -> dict[str, Any]:
@@ -124,8 +127,13 @@ def sync_config_state(team_id: int) -> dict[str, Any]:
}
+def _delete_integrations(accessor: IntegrationReader, team_id: int) -> None:
+ for integration in accessor.list(team_id):
+ accessor.delete(team_id, integration.id, ignore_usages=True)
+
+
def remove_app_config(team_id: int) -> None:
- Integration.objects.filter(team_id=team_id, kind="customerio-app").delete()
+ _delete_integrations(IntegrationManager.customerio_app.any, team_id)
try:
config = OptOutSyncConfig.objects.get(team_id=team_id)
config.app_integration = None
@@ -138,7 +146,7 @@ def remove_app_config(team_id: int) -> None:
def save_webhook_config(
team_id: int, signing_secret: str | None, enabled: bool, created_by_id: int | None
) -> SavedConfig:
- integration = Integration.objects.filter(team_id=team_id, kind="customerio-webhook").first()
+ integration = IntegrationManager.customerio_webhook.token.find(team_id)
if integration and signing_secret:
raise ConfigConflict("Integration already exists. Delete it first to use a different secret.")
@@ -147,16 +155,16 @@ def save_webhook_config(
raise ConfigIncomplete("Webhook signing secret is required to enable sync.")
if not integration and signing_secret:
- integration = Integration.objects.create(
- team_id=team_id,
- kind="customerio-webhook",
+ integration = IntegrationManager.customerio_webhook.token.create(
+ team_id,
+ external_id=None,
+ config={},
sensitive_config={"webhook_signing_secret": signing_secret},
created_by_id=created_by_id,
- errors="",
)
config, _ = OptOutSyncConfig.objects.get_or_create(team_id=team_id)
- config.webhook_integration = integration
+ config.webhook_integration_id = integration.id if integration else None
config.webhook_enabled = enabled
config.save(update_fields=["webhook_integration", "webhook_enabled"])
@@ -167,7 +175,7 @@ def save_webhook_config(
def remove_webhook_config(team_id: int) -> None:
- Integration.objects.filter(team_id=team_id, kind="customerio-webhook").delete()
+ _delete_integrations(IntegrationManager.customerio_webhook.any, team_id)
try:
config = OptOutSyncConfig.objects.get(team_id=team_id)
config.webhook_integration = None
@@ -181,7 +189,7 @@ def save_track_config(
team_id: int, site_id: str | None, api_key: str | None, region: str, enabled: bool, created_by_id: int | None
) -> SavedConfig:
has_new_creds = bool(site_id and api_key)
- integration = Integration.objects.filter(team_id=team_id, kind="customerio-track").first()
+ integration = IntegrationManager.customerio_track.token.find(team_id)
if integration and has_new_creds:
raise ConfigConflict("Integration already exists. Delete it first to use different credentials.")
@@ -190,17 +198,16 @@ def save_track_config(
raise ConfigIncomplete("Site ID and API key are required to enable outbound sync.")
if not integration and has_new_creds:
- integration = Integration.objects.create(
- team_id=team_id,
- kind="customerio-track",
- sensitive_config={"site_id": site_id, "api_key": api_key},
+ integration = IntegrationManager.customerio_track.token.create(
+ team_id,
+ external_id=None,
config={"region": region},
+ sensitive_config={"site_id": site_id, "api_key": api_key},
created_by_id=created_by_id,
- errors="",
)
config, _ = OptOutSyncConfig.objects.get_or_create(team_id=team_id)
- config.track_integration = integration
+ config.track_integration_id = integration.id if integration else None
config.track_enabled = enabled
config.save(update_fields=["track_integration", "track_enabled"])
@@ -213,7 +220,7 @@ def save_track_config(
def remove_track_config(team_id: int) -> None:
- Integration.objects.filter(team_id=team_id, kind="customerio-track").delete()
+ _delete_integrations(IntegrationManager.customerio_track.any, team_id)
try:
config = OptOutSyncConfig.objects.get(team_id=team_id)
config.track_integration = None
diff --git a/products/messaging/backend/services/transactional_email.py b/products/messaging/backend/services/transactional_email.py
index b07ad141b48a..cba570de5b16 100644
--- a/products/messaging/backend/services/transactional_email.py
+++ b/products/messaging/backend/services/transactional_email.py
@@ -14,7 +14,7 @@ import requests
import structlog
from prometheus_client import Counter, Histogram
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager, IntegrationNotFound
from posthog.models.team import Team
from posthog.plugins.plugin_server_api import (
reload_hog_flows_on_workers,
@@ -197,12 +197,10 @@ def sender_address_error(team_id: int, sender: dict[str, Any]) -> str | None:
# Without a sender there is no domain to check against. The sender field rejects that case on its own.
if integration_id is None:
return None
- config = (
- Integration.objects.filter(team_id=team_id, kind="email", id=integration_id)
- .values_list("config", flat=True)
- .first()
- or {}
- )
+ try:
+ config = IntegrationManager.email.credentials.get(team_id, integration_id).config or {}
+ except IntegrationNotFound:
+ config = {}
domain = (config.get("domain") or (config.get("email") or "").partition("@")[2]).lower()
if domain and address.partition("@")[2].lower() != domain:
return f"Use an address on {domain}, the domain this sender is verified for."
diff --git a/products/review_hog/backend/activity_logging.py b/products/review_hog/backend/activity_logging.py
index e1742b742209..dfe0e9e934b3 100644
--- a/products/review_hog/backend/activity_logging.py
+++ b/products/review_hog/backend/activity_logging.py
@@ -10,8 +10,8 @@ from uuid import UUID
from django.db import models
+from posthog.integrations import IntegrationManager
from posthog.models.activity_logging.activity_log import AuditableScope, Change, Detail, changes_between, log_activity
-from posthog.models.integration import Integration
from posthog.models.signals import model_activity_signal, mutable_receiver
from posthog.models.team import Team
from posthog.models.user import User
@@ -32,11 +32,9 @@ def _organization_id_for_team(team_id: int) -> UUID | None:
def installation_account_name(installation_id: str) -> str:
"""The GitHub account of an installation, as the core GitHub integration stores it."""
- name = (
- Integration.objects.filter(kind="github", integration_id=installation_id)
- .values_list("config__account__name", flat=True)
- .first()
- )
+ rows = IntegrationManager.github.oauth.list_by_external_id(installation_id)
+ # nosemgrep: github-installation-first-team -- only the account name is read, and every project of one installation stores the same name
+ name = ((rows[0].config or {}).get("account") or {}).get("name") if rows else None
return name if isinstance(name, str) and name else installation_id
diff --git a/products/review_hog/backend/automatic_reviews.py b/products/review_hog/backend/automatic_reviews.py
index 18b10caf0d43..aba160d5e526 100644
--- a/products/review_hog/backend/automatic_reviews.py
+++ b/products/review_hog/backend/automatic_reviews.py
@@ -4,7 +4,7 @@ from typing import Literal
from prometheus_client import Counter
from posthog.dataclasses import frozen
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager
from posthog.otel_metrics import OtelInstrumentFactory
from products.review_hog.backend.automatic_review_rules import AutomaticReviewReason, decide_automatic_review
@@ -46,10 +46,8 @@ def observe_dispatch(outcome: AuthoredPRReviewOutcome) -> None:
def connector_user_id(*, team_id: int, installation_id: str) -> int | None:
"""The active member who connected the installation to the project. Bot pull requests run as them."""
- creator_ids = Integration.objects.filter(
- team_id=team_id, kind="github", integration_id=installation_id, created_by__isnull=False
- ).values_list("created_by_id", flat=True)
- for creator_id in creator_ids:
+ rows = IntegrationManager.github.oauth.list(team_id, external_id=installation_id)
+ for creator_id in (row.created_by_id for row in rows if row.created_by_id is not None):
if is_active_member(team_id=team_id, user_id=creator_id):
return creator_id
return None
@@ -75,9 +73,7 @@ def plan_automatic_review(
if owner is None:
return AutomaticDispatch(outcome="repository_not_added")
team_id = owner.team_id
- has_integration = Integration.objects.filter(
- team_id=team_id, kind="github", integration_id=owner.installation_id
- ).exists()
+ has_integration = IntegrationManager.github.oauth.exists(team_id, external_id=owner.installation_id)
if not has_integration:
logger.warning(
"Team %s has no GitHub integration for installation %s; skipping automatic review of %s",
diff --git a/products/review_hog/backend/repository_settings.py b/products/review_hog/backend/repository_settings.py
index 66cc4d61d4c8..2dbf8f0580d0 100644
--- a/products/review_hog/backend/repository_settings.py
+++ b/products/review_hog/backend/repository_settings.py
@@ -17,7 +17,8 @@ from django.db import IntegrityError, transaction
from posthog.dataclasses import frozen
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager, IntegrationRecord
+from posthog.models.integration import GitHubIntegration
from posthog.models.organization import OrganizationMembership
from posthog.models.team import Team
from posthog.models.user import User
@@ -96,12 +97,10 @@ class ProjectRepositories:
def _project(self, team_id: int) -> ProjectRef:
return load_project_ref(team_id, viewer_organization_id=self.team.organization_id)
- def integrations(self) -> list[Integration]:
- return list(
- Integration.objects.filter(team_id=self.team_id, kind="github").select_related("created_by").order_by("id")
- )
+ def integrations(self) -> list[IntegrationRecord]:
+ return IntegrationManager.github.oauth.list(self.team_id)
- def integration_for(self, installation_id: str) -> Integration:
+ def integration_for(self, installation_id: str) -> IntegrationRecord:
integration = next((row for row in self.integrations() if row.integration_id == installation_id), None)
if integration is None:
raise RepositorySettingsError("This project has no GitHub connection for that installation.")
@@ -199,7 +198,9 @@ class ProjectRepositories:
def installations(self) -> list[InstallationSummary]:
claims = {claim.installation_id: claim for claim in ReviewInstallationClaim.objects.for_team(self.team_id)}
summaries: list[InstallationSummary] = []
- for integration in self.integrations():
+ integrations = self.integrations()
+ creators = User.objects.in_bulk({row.created_by_id for row in integrations if row.created_by_id})
+ for integration in integrations:
installation_id = integration.integration_id or ""
all_claim = RepositoryOwnership.all_claim(installation_id)
account = (integration.config or {}).get("account") or {}
@@ -207,7 +208,7 @@ class ProjectRepositories:
InstallationSummary(
installation_id=installation_id,
account_name=account.get("name") or installation_id,
- connected_by=integration.created_by,
+ connected_by=creators.get(integration.created_by_id) if integration.created_by_id else None,
claim=claims.get(installation_id),
all_taken_by=(
self._project(all_claim.team_id)
diff --git a/products/review_hog/backend/reviewer/status_comment.py b/products/review_hog/backend/reviewer/status_comment.py
index 5808705c29cd..8374940c1fcf 100644
--- a/products/review_hog/backend/reviewer/status_comment.py
+++ b/products/review_hog/backend/reviewer/status_comment.py
@@ -28,7 +28,8 @@ from django.utils import timezone
from posthog.dataclasses import frozen
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager
+from posthog.models.integration import GitHubIntegration
from posthog.utils import pluralize
from products.review_hog.backend.models import ReviewReport
@@ -489,7 +490,9 @@ def _auth_from_row(team_id: int, integration_row_id: int) -> tuple[str, str | No
revocation surfaces there and is swallowed like any other status-comment failure.
"""
github = GitHubIntegration(
- Integration.objects.get(id=integration_row_id, team_id=team_id), source="review_hog", priority=Priority.CRITICAL
+ IntegrationManager.github.oauth.get(team_id, integration_row_id),
+ source="review_hog",
+ priority=Priority.CRITICAL,
)
return github.get_access_token(), github.github_installation_id
diff --git a/products/review_hog/backend/temporal/activities.py b/products/review_hog/backend/temporal/activities.py
index 3561f690b421..41c2bf462cba 100644
--- a/products/review_hog/backend/temporal/activities.py
+++ b/products/review_hog/backend/temporal/activities.py
@@ -30,7 +30,8 @@ from posthog.dataclasses import frozen
from posthog.egress.github.transport import GitHubEgressBudgetExhausted, GitHubRateLimitError
from posthog.egress.limiter.policies import Priority
from posthog.event_usage import groups
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager
+from posthog.models.integration import GitHubIntegration
from posthog.models.team.team import Team
from posthog.sync import database_sync_to_async
from posthog.temporal.common.scoped import scoped_temporal
@@ -774,7 +775,7 @@ async def _refresh_status_comment(
def _github_integration_exists(team_id: int) -> bool:
- return Integration.objects.filter(team_id=team_id, kind="github").exists()
+ return IntegrationManager.github.oauth.exists(team_id)
def _installation_auth(team_id: int, repository: str) -> tuple[str, str | None]:
@@ -799,7 +800,7 @@ def _installation_for(team_id: int, repository: str) -> GitHubIntegration:
"""The team's GitHub App installation that can access `repository` — a live API probe.
Callers that make many writes in one run should select once and re-mint tokens from the
- returned integration row (`GitHubIntegration(Integration.objects.get(...)).get_access_token()`)
+ returned integration row (`GitHubIntegration(IntegrationManager.github.oauth.get(...)).get_access_token()`)
instead of re-probing per write: the probe answers *which* installation, not *may we write* —
GitHub enforces access server-side on every call anyway.
"""
diff --git a/products/review_hog/backend/temporal/resolution.py b/products/review_hog/backend/temporal/resolution.py
index 301caaeaa715..086fb04a677a 100644
--- a/products/review_hog/backend/temporal/resolution.py
+++ b/products/review_hog/backend/temporal/resolution.py
@@ -32,7 +32,8 @@ from temporalio.exceptions import ApplicationError
from posthog.dataclasses import frozen
from posthog.egress.limiter.policies import Priority
from posthog.github.merge_queue import MergeQueueState
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager
+from posthog.models.integration import GitHubIntegration
from posthog.sync import database_sync_to_async
from posthog.temporal.common.heartbeat import Heartbeater
from posthog.temporal.common.scoped import scoped_temporal
@@ -211,7 +212,7 @@ def _fetch_pr_metadata(input: ResolveThreadsInput, token: str, installation_id:
def _run_github(team_id: int, integration_row_id: int) -> GitHubIntegration:
"""The run-pinned installation, rebuilt from its row so every call mints a fresh token."""
return GitHubIntegration(
- Integration.objects.get(id=integration_row_id, team_id=team_id), source="review_hog", priority=Priority.NORMAL
+ IntegrationManager.github.oauth.get(team_id, integration_row_id), source="review_hog", priority=Priority.NORMAL
)
diff --git a/products/review_hog/backend/tests/test_repositories_api.py b/products/review_hog/backend/tests/test_repositories_api.py
index a2d31875147f..841a595cb66f 100644
--- a/products/review_hog/backend/tests/test_repositories_api.py
+++ b/products/review_hog/backend/tests/test_repositories_api.py
@@ -1,6 +1,9 @@
from posthog.test.base import APIBaseTest
from unittest.mock import MagicMock, patch
+from django.db import connection
+from django.test.utils import CaptureQueriesContext
+
from parameterized import parameterized
from posthog.constants import AvailableFeature
@@ -20,6 +23,7 @@ from products.review_hog.backend.models import (
ReviewUserRepositoryChoice,
ReviewUserSettings,
)
+from products.review_hog.backend.repository_settings import ProjectRepositories
from products.review_hog.backend.temporal.client import WorkflowProbe
INSTALLATION = "1001"
@@ -144,6 +148,30 @@ class TestReviewRepositorySettingsAPI(APIBaseTest):
choices.status_code,
) == expected
+ def test_installations_load_creators_in_one_query(self) -> None:
+ extra_creators = [User.objects.create(email=f"creator{i}@example.com", first_name=f"C{i}") for i in range(3)]
+ for index, creator in enumerate(extra_creators):
+ Integration.objects.create(
+ team=self.team,
+ kind="github",
+ integration_id=f"200{index}",
+ config={"account": {"name": f"org-{index}"}},
+ created_by=creator,
+ )
+ Integration.objects.create(team=self.team, kind="github", integration_id="3000", config={})
+
+ with CaptureQueriesContext(connection) as queries:
+ summaries = ProjectRepositories(self.team, self.user).installations()
+
+ assert len([query for query in queries if 'FROM "posthog_user"' in query["sql"]]) == 1
+ assert {summary.installation_id: summary.connected_by for summary in summaries} == {
+ INSTALLATION: self.user,
+ "2000": extra_creators[0],
+ "2001": extra_creators[1],
+ "2002": extra_creators[2],
+ "3000": None,
+ }
+
def test_project_rule_stores_only_what_differs_and_is_logged(self) -> None:
res = self.client.patch(
self._url("project_settings/"),
diff --git a/products/review_hog/backend/tests/test_status_comment.py b/products/review_hog/backend/tests/test_status_comment.py
index c9a884bf4ad5..33ee088c305d 100644
--- a/products/review_hog/backend/tests/test_status_comment.py
+++ b/products/review_hog/backend/tests/test_status_comment.py
@@ -721,7 +721,7 @@ class TestUpdateResolutionStatusComment(BaseTest):
patched = mock_request.call_args_list[1].kwargs["json"]["body"]
assert status_marker(str(report.id)) in patched
- @patch(f"{_MODULE}.Integration")
+ @patch(f"{_MODULE}.IntegrationManager")
def test_pinned_integration_row_skips_the_selection_probe(
self,
mock_integration_model: MagicMock,
@@ -749,7 +749,7 @@ class TestUpdateResolutionStatusComment(BaseTest):
)
mock_integration.first_for_team_repository.assert_not_called()
- mock_integration_model.objects.get.assert_called_once_with(id=42, team_id=self.team.id)
+ mock_integration_model.github.oauth.get.assert_called_once_with(self.team.id, 42)
assert _patches(mock_request) == ["/repos/o/r/issues/comments/777"]
diff --git a/products/signals/backend/github_optional.py b/products/signals/backend/github_optional.py
index e13d46672b01..eb14fae12ec6 100644
--- a/products/signals/backend/github_optional.py
+++ b/products/signals/backend/github_optional.py
@@ -10,8 +10,9 @@ from rest_framework.exceptions import APIException
from posthog.egress.github.transport import GitHubRateLimitError
from posthog.egress.limiter.policies import Priority
from posthog.egress.transport.transport import EgressBudgetExhausted
+from posthog.integrations import IntegrationManager
from posthog.models.github_integration_base import _is_safe_github_repo_path
-from posthog.models.integration import GitHubIntegration, GitHubIntegrationError, Integration
+from posthog.models.integration import GitHubIntegration, GitHubIntegrationError
from posthog.models.team.team import Team
from posthog.ph_client import feature_enabled_or_false
@@ -39,7 +40,7 @@ class ResearchAccessPolicy:
if not _is_safe_github_repo_path(repository):
return None
transient_error: Exception | None = None
- for integration in Integration.objects.filter(team_id=team_id, kind="github"):
+ for integration in IntegrationManager.github.oauth.list(team_id):
try:
github = GitHubIntegration(integration, source="signals_github_optional", priority=priority)
if github.installation_can_access_repository(repository, raise_on_transient=True):
diff --git a/products/signals/backend/onboarding_completion.py b/products/signals/backend/onboarding_completion.py
index fdaa3119179f..2b25da09d0c1 100644
--- a/products/signals/backend/onboarding_completion.py
+++ b/products/signals/backend/onboarding_completion.py
@@ -6,8 +6,9 @@ from rest_framework.exceptions import APIException
from posthog.egress.github.transport import GitHubRateLimitError
from posthog.egress.limiter.policies import Priority
from posthog.egress.transport.transport import EgressBudgetExhausted
+from posthog.integrations import IntegrationManager
from posthog.models.github_integration_base import GitHubIntegrationError
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.models.integration import GitHubIntegration
from products.signals.backend.models import SignalSourceConfig
@@ -30,7 +31,7 @@ class InboxOnboardingReconciler:
def has_repository(self) -> bool:
failure: Exception | None = None
- for integration in Integration.objects.filter(team_id=self.team_id, kind="github").order_by("id"):
+ for integration in sorted(IntegrationManager.github.oauth.list(self.team_id), key=lambda row: row.id):
github = GitHubIntegration(integration, source="signals_onboarding", priority=Priority.NORMAL)
try:
repositories, _ = github.list_repositories(per_page=1)
diff --git a/products/signals/backend/scout_harness/profile/builders.py b/products/signals/backend/scout_harness/profile/builders.py
index 1f37546dd124..ba9a4279f1a0 100644
--- a/products/signals/backend/scout_harness/profile/builders.py
+++ b/products/signals/backend/scout_harness/profile/builders.py
@@ -43,8 +43,8 @@ from posthog.hogql.constants import HogQLGlobalSettings
from posthog.hogql.parser import parse_select
from posthog.hogql.query import execute_hogql_query
+from posthog.integrations import IntegrationManager
from posthog.models.activity_logging.activity_log import ActivityLog
-from posthog.models.integration import Integration
from posthog.models.product_intent.product_intent import ProductIntent
from posthog.models.team.team import Team
from posthog.models.user import User
@@ -242,7 +242,7 @@ def _integrations(team: Team) -> list[dict[str, Any]]:
Only the kind + creation timestamp — `Integration.config` and `Integration.sensitive_config`
can hold tokens or workspace details we don't want to surface to the agent.
"""
- rows = Integration.objects.filter(team=team).order_by("kind", "id")
+ rows = sorted(IntegrationManager.any.list(team.id), key=lambda row: (row.kind, row.id))
return [
{
"kind": row.kind,
diff --git a/products/signals/backend/scout_harness/serializers.py b/products/signals/backend/scout_harness/serializers.py
index dd747aa5f65a..a223c40d97a1 100644
--- a/products/signals/backend/scout_harness/serializers.py
+++ b/products/signals/backend/scout_harness/serializers.py
@@ -28,7 +28,6 @@ from rest_framework.fields import empty
from posthog.api.shared import UserBasicSerializer
from posthog.event_usage import groups
-from posthog.models.integration import Integration
from posthog.models.team.team import Team
from posthog.models.user import User
from posthog.permissions import get_authenticator_scopes
@@ -76,7 +75,10 @@ from products.signals.backend.scout_harness.precheck import (
)
from products.signals.backend.scout_harness.scout_costs import SCOUT_COST_WINDOW_DAYS
from products.signals.backend.scout_harness.skill_loader import reserved_scout_name_error
-from products.signals.backend.scout_harness.slack_delivery import MAX_SCOUT_SLACK_DM_TARGETS
+from products.signals.backend.scout_harness.slack_delivery import (
+ MAX_SCOUT_SLACK_DM_TARGETS,
+ find_project_slack_integration,
+)
from products.signals.backend.scout_harness.tags import slugify_tag
from products.signals.backend.scout_harness.tool_catalogue import SCOUT_RUN_CONTEXT_TOOLS, get_scout_tool_catalogue
from products.signals.backend.scout_harness.tools.checks import MAX_CHECK_EXPLANATION_LENGTH
@@ -3109,12 +3111,7 @@ def _validate_output_destinations(value: dict, context: dict) -> dict:
raise RuntimeError("Scout config output destination validation requires project_id in its context")
integration_id = slack["integration_id"]
- # nosemgrep: environment-model-scoped-by-project -- scout configs are project-level, so a Slack integration from any environment of the project is valid
- integration = Integration.objects.filter(
- id=integration_id,
- team__project_id=project_id,
- kind=Integration.IntegrationKind.SLACK,
- ).first()
+ integration = find_project_slack_integration(integration_id=integration_id, project_id=project_id)
if integration is None:
raise serializers.ValidationError(
{"slack": {"integration_id": "No Slack integration with this ID exists on this project."}}
diff --git a/products/signals/backend/scout_harness/slack_delivery.py b/products/signals/backend/scout_harness/slack_delivery.py
index ed8b2b46c3f9..1c2285259cec 100644
--- a/products/signals/backend/scout_harness/slack_delivery.py
+++ b/products/signals/backend/scout_harness/slack_delivery.py
@@ -15,7 +15,9 @@ from slack_sdk.errors import SlackApiError
from slack_sdk.web import SlackResponse, WebClient
from posthog.dataclasses import frozen
-from posthog.models.integration import Integration, SlackIntegration
+from posthog.integrations import IntegrationManager, IntegrationNotFound, IntegrationRecord
+from posthog.models.integration import SlackIntegration
+from posthog.models.team.team import Team
from posthog.redis import get_client
from posthog.slack.formatting import channel_id_from_target, escape_slack_mrkdwn
from posthog.slack.markdown import SLACK_MARKDOWN_TEXT_MAX_LEN, slack_markdown_block
@@ -170,7 +172,7 @@ def _slack_retry_after_seconds(exc: Exception) -> int | None:
return min(retry_after, 3600) if retry_after is not None and retry_after > 0 else None
-def _scout_invite_footer(integration: Integration, *, scout_team_id: int, channel_id: str) -> list[dict]:
+def _scout_invite_footer(integration: IntegrationRecord, *, scout_team_id: int, channel_id: str) -> list[dict]:
"""The follow-up invite footer, when the Slack connection uses the scout's environment.
A mention is answered from the connected project, so a connection pointing somewhere else would
@@ -198,13 +200,18 @@ def _prettify_scout_name(skill_name: str) -> str:
return cleaned[:1].upper() + cleaned[1:] if cleaned else "Scout"
-def _slack_integration_for_project(*, integration_id: int, project_id: int) -> Integration:
- # nosemgrep: environment-model-scoped-by-project -- scout configs are project-level, so a Slack integration from any environment of the project is valid
- integration = Integration.objects.filter(
- id=integration_id,
- team__project_id=project_id,
- kind=Integration.IntegrationKind.SLACK,
- ).first()
+def find_project_slack_integration(*, integration_id: int, project_id: int) -> IntegrationRecord | None:
+ # Scout configs are project-level, so a Slack integration from any environment of the project is valid.
+ for team_id in Team.objects.filter(project_id=project_id).values_list("id", flat=True):
+ try:
+ return IntegrationManager.slack.oauth.get(team_id, integration_id)
+ except IntegrationNotFound:
+ continue
+ return None
+
+
+def _slack_integration_for_project(*, integration_id: int, project_id: int) -> IntegrationRecord:
+ integration = find_project_slack_integration(integration_id=integration_id, project_id=project_id)
if integration is None:
raise ScoutSlackPermanentDeliveryError(
"The configured Slack integration no longer exists on this project",
@@ -841,7 +848,7 @@ def post_scout_report_to_slack(
scout_report_slack_message(
team_id=report.team_id,
report_id=str(report.id),
- slack_workspace_id=integration.integration_id,
+ slack_workspace_id=integration.integration_id or "",
target=channel_id,
)
if edit_note is None
@@ -905,7 +912,7 @@ def post_scout_report_to_slack(
reply_ts = []
if thread_ts:
record_report_slack_thread(
- slack_workspace_id=integration.integration_id,
+ slack_workspace_id=integration.integration_id or "",
team_id=report.team_id,
report_id=str(report.id),
integration_id=integration.id,
@@ -922,7 +929,7 @@ def post_scout_report_to_slack(
def _record_reply_ts(posted: list[str]) -> None:
record_scout_report_slack_replies(
team_id=report.team_id,
- slack_workspace_id=integration.integration_id,
+ slack_workspace_id=integration.integration_id or "",
channel=conversation,
thread_ts=str(thread_ts),
reply_ts=posted,
diff --git a/products/signals/backend/serializers.py b/products/signals/backend/serializers.py
index aa9462a2d0c2..08836e32e701 100644
--- a/products/signals/backend/serializers.py
+++ b/products/signals/backend/serializers.py
@@ -16,14 +16,13 @@ from rest_framework import serializers
from rest_framework.request import Request
from posthog.api.fields import RepeatedOrCommaSeparatedListField
-from posthog.api.scoped_related_fields import TeamScopedPrimaryKeyRelatedField
from posthog.auth import is_mcp_request
from posthog.dataclasses import frozen
from posthog.exceptions_capture import capture_exception
-from posthog.integrations import IntegrationRecord
+from posthog.integrations import IntegrationKind, IntegrationRecord
from posthog.integrations.fields import IntegrationReferenceField
from posthog.models import Team, User
-from posthog.models.integration import Integration, is_supported_external_issue_provider
+from posthog.models.integration import SUPPORTED_EXTERNAL_ISSUE_PROVIDERS
from products.signals.backend import contracts
from products.signals.backend.agent_instructions import AGENT_STEERING_MAX_LENGTH, STEERING_FIELDS
@@ -434,8 +433,8 @@ class SignalTeamConfigSerializer(serializers.ModelSerializer):
max_length=AGENT_STEERING_MAX_LENGTH,
help_text="Project-level instructions for agents implementing signal reports. Other agent roles do not receive them. Blank uses project instructions only.",
)
- issue_tracking_integration = TeamScopedPrimaryKeyRelatedField(
- queryset=Integration.objects.all(),
+ issue_tracking_integration = IntegrationReferenceField(
+ kinds=sorted(SUPPORTED_EXTERNAL_ISSUE_PROVIDERS),
required=False,
allow_null=True,
help_text=(
@@ -671,15 +670,6 @@ class SignalTeamConfigSerializer(serializers.ModelSerializer):
def validate_default_slack_notification_integration(self, value: IntegrationRecord | None) -> int | None:
return value.id if value is not None else None
- def validate_issue_tracking_integration(self, value: Integration | None) -> Integration | None:
- if value is None:
- return None
- if not is_supported_external_issue_provider(value.kind):
- raise serializers.ValidationError(
- f"'{value.kind}' cannot track issues. Connect GitHub, GitLab, Linear, or Jira."
- )
- return value
-
def validate(self, attrs: dict) -> dict:
attrs = super().validate(attrs)
# A new channel without its workspace must not keep the old binding, which may name a
@@ -713,7 +703,7 @@ class SignalTeamConfigSerializer(serializers.ModelSerializer):
)
# The repository reaches a GitHub path, so a name it cannot hold fails here rather than on
# every run.
- if integration.kind == Integration.IntegrationKind.GITHUB:
+ if integration.kind == IntegrationKind.GITHUB:
try:
validated_github_repository(config["repository"])
except serializers.ValidationError as error:
diff --git a/products/signals/backend/slack_notification_targets.py b/products/signals/backend/slack_notification_targets.py
index eacb265311b7..4ea829f7d7e7 100644
--- a/products/signals/backend/slack_notification_targets.py
+++ b/products/signals/backend/slack_notification_targets.py
@@ -20,7 +20,8 @@ from django.core.cache import cache
from rest_framework import serializers
from slack_sdk.errors import SlackApiError
-from posthog.models.integration import Integration, SlackIntegration
+from posthog.integrations import IntegrationRecord
+from posthog.models.integration import SlackIntegration
from posthog.models.user import User
from posthog.models.user_integration import UserIntegration
from posthog.slack.formatting import channel_id_from_target
@@ -50,14 +51,14 @@ def is_slack_member_target(target: str) -> bool:
return bool(_MEMBER_ID_RE.fullmatch(channel_id_from_target(target)))
-def saved_notification_integration(user: User) -> Integration | None:
+def saved_notification_integration(user: User) -> IntegrationRecord | None:
"""The Slack workspace this user's notifications already go through, if any."""
config = SignalUserAutonomyConfig.objects.filter(user=user).select_related("slack_notification_integration").first()
return config.slack_notification_integration if config else None
def lookup_slack_user_id_by_email(
- slack: SlackIntegration, email: str, integration: Integration | None = None
+ slack: SlackIntegration, email: str, integration: IntegrationRecord | None = None
) -> str | None:
integration = integration or slack.integration
normalized_email = email.strip().lower()
@@ -91,7 +92,7 @@ def lookup_slack_user_id_by_email(
return member_id
-def _take_member_lookup_budget(integration: Integration) -> None:
+def _take_member_lookup_budget(integration: IntegrationRecord) -> None:
budget_key = f"signals/slack/{integration.id}/member_lookup_budget"
try:
lookups = 1 if cache.add(budget_key, 1, 60) else cache.incr(budget_key)
@@ -101,7 +102,7 @@ def _take_member_lookup_budget(integration: Integration) -> None:
raise serializers.ValidationError({"slack_notification_direct_message": _SLACK_UNAVAILABLE_ERROR})
-def _cached_slack_member(slack: SlackIntegration, integration: Integration, member_id: str) -> dict | None:
+def _cached_slack_member(slack: SlackIntegration, integration: IntegrationRecord, member_id: str) -> dict | None:
lookup_key = f"slack/{integration.id}/users/{member_id}"
cached_lookup = cache.get(lookup_key)
if cached_lookup is not None:
@@ -118,7 +119,7 @@ def _cached_slack_member(slack: SlackIntegration, integration: Integration, memb
return serialized_lookup[0] if serialized_lookup else None
-def resolve_own_direct_message_target(user: User, integration: Integration | None) -> str:
+def resolve_own_direct_message_target(user: User, integration: IntegrationRecord | None) -> str:
"""Resolve a user's own Slack account in this workspace to a `U…|@name` target.
The Slack account they linked to PostHog answers first, then their PostHog email against the
@@ -143,7 +144,7 @@ def resolve_own_direct_message_target(user: User, integration: Integration | Non
return f"{member['id']}|@{_member_display_name(member)}"
-def validate_slack_notification_target(user: User, target: str, integration: Integration | None) -> None:
+def validate_slack_notification_target(user: User, target: str, integration: IntegrationRecord | None) -> None:
"""Resolve a member target a caller sent itself, or raise.
A channel target needs no lookup: the picker already warns when the app is missing from the
@@ -160,7 +161,7 @@ def validate_slack_notification_target(user: User, target: str, integration: Int
raise serializers.ValidationError({"slack_notification_channel": _UNRESOLVED_MEMBER_ERROR})
-def _linked_slack_member_id(user: User, integration: Integration) -> str | None:
+def _linked_slack_member_id(user: User, integration: IntegrationRecord) -> str | None:
link = (
UserIntegration.objects.filter(
user=user,
diff --git a/products/signals/backend/tasks.py b/products/signals/backend/tasks.py
index f4dad19aae21..ee57940875b7 100644
--- a/products/signals/backend/tasks.py
+++ b/products/signals/backend/tasks.py
@@ -508,7 +508,7 @@ def deliver_scout_slack_thread_replies(
scout_report_slack_message(
team_id=team_id,
report_id=report_id,
- slack_workspace_id=integration.integration_id,
+ slack_workspace_id=integration.integration_id or "",
thread_ts=thread_ts,
)
if report_id is not None
@@ -519,7 +519,7 @@ def deliver_scout_slack_thread_replies(
if message is not None:
record_scout_report_slack_replies(
team_id=team_id,
- slack_workspace_id=integration.integration_id,
+ slack_workspace_id=integration.integration_id or "",
channel=message.channel,
thread_ts=thread_ts,
reply_ts=posted,
diff --git a/products/signals/backend/temporal/agentic/select_repository.py b/products/signals/backend/temporal/agentic/select_repository.py
index fb287ca0fa74..387a4c96e20e 100644
--- a/products/signals/backend/temporal/agentic/select_repository.py
+++ b/products/signals/backend/temporal/agentic/select_repository.py
@@ -5,10 +5,12 @@ from requests import RequestException
from posthog.dataclasses import frozen
from posthog.egress.github.transport import GitHubRateLimitError
+from posthog.egress.limiter.policies import Priority
from posthog.egress.transport.transport import EgressBudgetExhausted
from posthog.event_usage import groups
+from posthog.integrations import IntegrationManager
from posthog.models import Organization
-from posthog.models.integration import GitHubIntegrationError, Integration
+from posthog.models.integration import GitHubIntegration, GitHubIntegrationError
from posthog.models.team.team import Team
from posthog.sync import database_sync_to_async
from posthog.temporal.common.heartbeat import Heartbeater
@@ -63,10 +65,16 @@ async def github_optional_pilot_activity(team_id: int) -> bool:
def _pilot_code_availability(team_id: int) -> ReportResearchContext:
- integrations = list(Integration.objects.filter(team_id=team_id, kind="github"))
+ integrations = IntegrationManager.github.oauth.list(team_id)
if not integrations:
return ReportResearchContext(mode="data_only", repository_status="no_integration")
- if all(item.repository_cache_updated_at and item.repository_cache == [] for item in integrations):
+ adapters = [
+ GitHubIntegration(item, source="signals_report_research", priority=Priority.NORMAL) for item in integrations
+ ]
+ if all(
+ adapter.integration.repository_cache_updated_at and adapter.integration.repository_cache == []
+ for adapter in adapters
+ ):
return ReportResearchContext(mode="data_only", repository_status="no_repositories")
if resolve_team_github_integration(team_id, team_only=True) is None:
return ReportResearchContext(mode="data_only", repository_status="github_unavailable")
diff --git a/products/signals/backend/test/test_scout_harness_profile.py b/products/signals/backend/test/test_scout_harness_profile.py
index b330c8a3cd0a..48b329e4fbad 100644
--- a/products/signals/backend/test/test_scout_harness_profile.py
+++ b/products/signals/backend/test/test_scout_harness_profile.py
@@ -21,6 +21,7 @@ from django.utils import timezone
from parameterized import parameterized
+from posthog.integrations import SOURCE_MARKER_CONFIG_KEY
from posthog.models.activity_logging.activity_log import ActivityLog
from posthog.models.integration import Integration
from posthog.models.product_intent.product_intent import ProductIntent
@@ -171,6 +172,12 @@ class TestIntegrations(BaseTest):
result = _integrations(self.team)
assert sorted(r["kind"] for r in result) == ["github", "slack"]
+ def test_skips_warehouse_source_rows(self) -> None:
+ Integration.objects.create(team=self.team, kind="slack", config={})
+ Integration.objects.create(team=self.team, kind="github", config={SOURCE_MARKER_CONFIG_KEY: "Github"})
+ result = _integrations(self.team)
+ assert [r["kind"] for r in result] == ["slack"]
+
def test_does_not_leak_config_or_sensitive_config(self) -> None:
# Defensive: integration config/sensitive_config can hold tokens; only kind + created_at
# should reach the agent.
diff --git a/products/signals/backend/test/test_scout_slack_delivery.py b/products/signals/backend/test/test_scout_slack_delivery.py
index 59d9275d0fc1..a7c2c422da2d 100644
--- a/products/signals/backend/test/test_scout_slack_delivery.py
+++ b/products/signals/backend/test/test_scout_slack_delivery.py
@@ -31,6 +31,7 @@ from products.signals.backend.scout_harness.slack_delivery import (
ScoutSlackPermanentDeliveryError,
_latest_report_delivery_key,
_slack_retry_after_seconds,
+ find_project_slack_integration,
get_scout_slack_destination,
mark_latest_scout_report_delivery,
post_scout_emission_to_slack,
@@ -214,6 +215,27 @@ class TestScoutSlackDelivery(BaseTest):
source_id=f"run:{run.id}:finding:checkout-500s",
)
+ @parameterized.expand(
+ [
+ ("same_environment", "same", True),
+ ("sibling_environment", "sibling", True),
+ ("other_project", "other", False),
+ ]
+ )
+ def test_find_project_slack_integration_scope(self, _name: str, owner: str, expected_found: bool) -> None:
+ teams = {
+ "same": self.team,
+ "sibling": Team.objects.create(
+ organization=self.organization, project=self.team.project, parent_team=self.team, name="Sibling"
+ ),
+ "other": Team.objects.create(organization=self.organization, name="Other project"),
+ }
+ integration = Integration.objects.create(team=teams[owner], kind=Integration.IntegrationKind.SLACK)
+
+ found = find_project_slack_integration(integration_id=integration.id, project_id=self.team.project_id)
+
+ assert (found.id if found else None) == (integration.id if expected_found else None)
+
def test_posts_safe_markdown_but_does_not_invite_followup_for_child_environment(self) -> None:
emission = self._make_emission(
"**Checkout** failures [trace](https://example.com/trace) <!channel> [ping](!here)"
diff --git a/products/signals/backend/test/test_tracker_issues.py b/products/signals/backend/test/test_tracker_issues.py
index bebfefe9f9f3..6ccb2eed5774 100644
--- a/products/signals/backend/test/test_tracker_issues.py
+++ b/products/signals/backend/test/test_tracker_issues.py
@@ -15,6 +15,7 @@ from rest_framework.test import APIClient
from posthog.egress.github.transport import GitHubEgressBudgetExhausted, GitHubRateLimitError
from posthog.egress.limiter.policies import Priority
+from posthog.integrations import SOURCE_MARKER_CONFIG_KEY
from posthog.models import Team, User
from posthog.models.integration import (
GitHubIntegration,
@@ -783,3 +784,21 @@ def test_serializer_rejects_a_provider_that_cannot_hold_issues(team):
assert not serializer.is_valid()
assert "issue_tracking_integration" in serializer.errors
+
+
+def test_serializer_rejects_a_warehouse_source_row(team):
+ integration = Integration.objects.create(
+ team=team,
+ kind="github",
+ integration_id="github-source",
+ config={"account": {"name": "acme"}, SOURCE_MARKER_CONFIG_KEY: "Github"},
+ )
+ serializer = SignalTeamConfigSerializer(
+ data={"issue_tracking_integration": integration.id, "issue_tracking_config": {"repository": "web"}},
+ partial=True,
+ context={"team_id": team.id},
+ )
+
+ assert not serializer.is_valid()
+ assert "issue_tracking_integration" in serializer.errors
diff --git a/products/signals/backend/tracker_issues.py b/products/signals/backend/tracker_issues.py
index f2203adb7839..b9e13b9b64ef 100644
--- a/products/signals/backend/tracker_issues.py
+++ b/products/signals/backend/tracker_issues.py
@@ -26,11 +26,11 @@ from rest_framework.exceptions import ValidationError as DRFValidationError
from posthog.dataclasses import frozen
from posthog.egress.github.transport import GitHubEgressBudgetExhausted, GitHubRateLimitError
from posthog.egress.limiter.policies import Priority
+from posthog.integrations import IntegrationKind, IntegrationRecord
from posthog.models.integration import (
SUPPORTED_EXTERNAL_ISSUE_PROVIDERS,
GitHubIntegration,
GitLabIntegration,
- Integration,
JiraIntegration,
LinearIntegration,
external_issue_url,
@@ -53,10 +53,10 @@ logger = structlog.get_logger(__name__)
# Target fields the team must supply per provider, on top of what the integration itself carries.
# GitLab needs none: its integration is already bound to one project.
TRACKER_TARGET_REQUIRED_FIELDS: dict[str, tuple[str, ...]] = {
- Integration.IntegrationKind.GITHUB.value: ("repository",),
- Integration.IntegrationKind.GITLAB.value: (),
- Integration.IntegrationKind.LINEAR.value: ("team_id",),
- Integration.IntegrationKind.JIRA.value: ("project_key",),
+ IntegrationKind.GITHUB.value: ("repository",),
+ IntegrationKind.GITLAB.value: (),
+ IntegrationKind.LINEAR.value: ("team_id",),
+ IntegrationKind.JIRA.value: ("project_key",),
}
# A GitHub repository name is interpolated into the issues path, so anything holding a path or
@@ -79,7 +79,7 @@ ABANDONED_CLAIM_REASON = "Tracker issue creation stopped before it returned a re
class TrackerIssueTarget:
"""Where a team wants its self-driving tracker issues to land."""
- integration: Integration
+ integration: IntegrationRecord
config: dict[str, Any]
@property
@@ -124,7 +124,7 @@ def _create_provider_issue(target: TrackerIssueTarget, *, title: str, body: str,
kind = target.integration.kind
label = target.label
- if kind == Integration.IntegrationKind.GITHUB:
+ if kind == IntegrationKind.GITHUB:
config: dict[str, Any] = {
"repository": validated_github_repository(target.config["repository"]),
"title": title,
@@ -138,10 +138,10 @@ def _create_provider_issue(target: TrackerIssueTarget, *, title: str, body: str,
context["repository"] = repository if "/" in repository else f"{github.organization()}/{repository}"
return context
- if kind == Integration.IntegrationKind.GITLAB:
+ if kind == IntegrationKind.GITLAB:
return GitLabIntegration(target.integration).create_issue({"title": title, "body": body})
- if kind == Integration.IntegrationKind.LINEAR:
+ if kind == IntegrationKind.LINEAR:
# Linear's client attaches the report link to the new issue itself, so the auditor can
# walk issue → report without the description.
return LinearIntegration(target.integration).create_issue(
@@ -149,7 +149,7 @@ def _create_provider_issue(target: TrackerIssueTarget, *, title: str, body: str,
{"team_id": target.config["team_id"], "title": title, "description": body},
)
- if kind == Integration.IntegrationKind.JIRA:
+ if kind == IntegrationKind.JIRA:
return JiraIntegration(target.integration).create_issue(
{"project_key": target.config["project_key"], "title": title, "description": body}
)
@@ -185,7 +185,7 @@ def _claim_tracker_issue(
report_id=report_id,
defaults={
"team_id": team_id,
- "integration": target.integration,
+ "integration_id": target.integration.id,
"provider": target.integration.kind,
"status": SignalReportTrackerIssue.Status.PENDING,
},
@@ -202,7 +202,7 @@ def _claim_tracker_issue(
if claim.failure_reason == ABANDONED_CLAIM_REASON:
return None
claim.status = SignalReportTrackerIssue.Status.PENDING
- claim.integration = target.integration
+ claim.integration_id = target.integration.id
claim.provider = target.integration.kind
claim.save(update_fields=["status", "integration", "provider", "updated_at"])
return claim
@@ -278,7 +278,7 @@ def _pr_body_reference(tracker: SignalReportTrackerIssue, *, pr_repository: str)
"""The line appended to the pull request body so the pull request names its tracker issue."""
context = tracker.external_context or {}
- if tracker.provider == Integration.IntegrationKind.GITHUB:
+ if tracker.provider == IntegrationKind.GITHUB:
repository, number = context.get("repository"), context.get("number")
if not repository or not number:
return None
@@ -332,7 +332,7 @@ def link_pull_request_to_tracker_issue(*, team_id: int, report_id: str, pr_url:
if outcome != BodyEditOutcome.WRITTEN:
return False
- if tracker.provider == Integration.IntegrationKind.LINEAR and tracker.integration is not None:
+ if tracker.provider == IntegrationKind.LINEAR and tracker.integration is not None:
# Best-effort: the description reference already links the two, and this attachment
# needs a Linear scope the team may not have granted.
try:
@@ -373,7 +373,7 @@ def _comment_before_close(
while a missing comment costs only the attribution.
"""
if (
- tracker.provider != Integration.IntegrationKind.GITHUB
+ tracker.provider != IntegrationKind.GITHUB
or tracker.integration is None
or tracker.close_comment_posted_at is not None
):
@@ -403,15 +403,15 @@ def _close_provider_issue(tracker: SignalReportTrackerIssue, *, completed: bool,
raise ValueError("Tracker issue has no integration left to close it through")
context = tracker.external_context or {}
- if tracker.provider == Integration.IntegrationKind.GITHUB:
+ if tracker.provider == IntegrationKind.GITHUB:
GitHubIntegration(integration, source="signals_tracker_issues", priority=priority).close_issue(
context["repository"], int(context["number"]), completed=completed
)
- elif tracker.provider == Integration.IntegrationKind.GITLAB:
+ elif tracker.provider == IntegrationKind.GITLAB:
GitLabIntegration(integration).close_issue(int(context["issue_id"]))
- elif tracker.provider == Integration.IntegrationKind.LINEAR:
+ elif tracker.provider == IntegrationKind.LINEAR:
LinearIntegration(integration).close_issue(str(context["id"]), completed=completed)
- elif tracker.provider == Integration.IntegrationKind.JIRA:
+ elif tracker.provider == IntegrationKind.JIRA:
JiraIntegration(integration).close_issue(str(context["key"]))
else:
raise ValueError(f"Unsupported tracker provider {tracker.provider}")
diff --git a/products/signals/backend/views.py b/products/signals/backend/views.py
index 8a54952cab84..88694815598c 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -69,11 +69,12 @@ from posthog.dataclasses import frozen
from posthog.egress.github.transport import GitHubEgressBudgetExhausted, GitHubRateLimitError
from posthog.egress.limiter.policies import Priority
from posthog.event_usage import report_user_action
+from posthog.integrations import IntegrationManager, IntegrationNotFound
from posthog.models import Team, User
from posthog.models.activity_logging.activity_log import Change, Detail, log_activity
from posthog.models.activity_logging.model_activity import is_impersonated_session
from posthog.models.github_integration_base import GitHubIntegrationBase, PullRequestRef
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.models.integration import GitHubIntegration
from posthog.models.team.extensions import get_or_create_team_extension
from posthog.models.user_integration import ReauthorizationRequired, UserGitHubIntegration, UserIntegration
from posthog.oauth_provenance import is_sandbox_oauth_request, is_sandbox_origin_request
@@ -6917,16 +6918,12 @@ class SignalUserAutonomyConfigView(APIView):
raise serializers.ValidationError(
{"slack_notification_integration_id": "Unknown Slack integration for this user."}
)
- candidate = Integration.objects.filter(
- pk=integration_id,
- kind="slack",
- team_id=current_team_id,
- ).first()
- if candidate is None:
+ try:
+ integration = IntegrationManager.slack.oauth.get(current_team_id, integration_id)
+ except IntegrationNotFound:
raise serializers.ValidationError(
{"slack_notification_integration_id": "Unknown Slack integration for this user."}
)
- integration = candidate
defaults["slack_notification_integration"] = integration
wants_direct_message = bool(validated.get("slack_notification_direct_message"))
existing_config = SignalUserAutonomyConfig.objects.filter(user=user).first() if integration_in_request else None
diff --git a/products/skills/backend/api/community_publish_services.py b/products/skills/backend/api/community_publish_services.py
index f57def2ea472..44e2f5c2736a 100644
--- a/products/skills/backend/api/community_publish_services.py
+++ b/products/skills/backend/api/community_publish_services.py
@@ -26,6 +26,8 @@ from posthog.dataclasses import frozen
from posthog.egress.github.transport import GitHubRateLimitError, github_request
from posthog.egress.limiter.policies import Priority
from posthog.models.github_integration_base import GitHubIntegrationError
+
+# nosemgrep: integration-model-through-sdk (an unsaved row for the publisher adapter, never stored)
from posthog.models.integration import GitHubIntegration, Integration
from ..marketplace.packaging import SPEC_DESCRIPTION_MAX_LENGTH
diff --git a/products/stamphog/backend/logic/channel_resolution.py b/products/stamphog/backend/logic/channel_resolution.py
index 3ea21b981dd2..b07d22d3ea5d 100644
--- a/products/stamphog/backend/logic/channel_resolution.py
+++ b/products/stamphog/backend/logic/channel_resolution.py
@@ -37,7 +37,7 @@ from owners_yaml.schema import Producer, TeamEntry
from posthog.dataclasses import frozen
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager
from posthog.ownership.github_files import AuthenticatedRepoFiles, GitHubFilesFetcher
from posthog.slack.channels import SlackChannel, fetch_channel_map, find_channel
@@ -188,7 +188,7 @@ def build_routing_context(team_id: int) -> RoutingContext | None:
Raises ``RoutingUnavailable`` when a registry could not be read.
"""
- integration = Integration.objects.filter(team_id=team_id, kind="slack").first()
+ integration = IntegrationManager.slack.oauth.find(team_id)
if integration is None:
logger.info("stamphog_routing_no_slack_integration", team_id=team_id)
return None
diff --git a/products/stamphog/backend/logic/slack_digest.py b/products/stamphog/backend/logic/slack_digest.py
index 265f5a229a0d..95660180584a 100644
--- a/products/stamphog/backend/logic/slack_digest.py
+++ b/products/stamphog/backend/logic/slack_digest.py
@@ -19,7 +19,8 @@ from typing import TYPE_CHECKING
import structlog
-from posthog.models.integration import Integration, SlackIntegration
+from posthog.integrations import IntegrationManager, IntegrationNotFound
+from posthog.models.integration import SlackIntegration
from posthog.slack.channels import SlackPostRefused, post_message, post_with_join
from posthog.slack.formatting import escape_slack_mrkdwn
@@ -184,8 +185,9 @@ def post_digest_details(team_id: int, destination: Destination, summary: DigestS
"""
if not thread_ts or not summary.prs:
return
- integration = Integration.objects.filter(id=destination.slack_integration_id, team_id=team_id, kind="slack").first()
- if integration is None:
+ try:
+ integration = IntegrationManager.slack.oauth.get(team_id, destination.slack_integration_id)
+ except IntegrationNotFound:
return
try:
post_message(
@@ -209,8 +211,9 @@ def post_digest_lead(team_id: int, destination: Destination, summary: DigestSumm
sweeper finalizes. Without that order, a Slack-accepted digest reads as unposted, and the next
run releases its merges and sends the same lead again.
"""
- integration = Integration.objects.filter(id=destination.slack_integration_id, team_id=team_id, kind="slack").first()
- if integration is None:
+ try:
+ integration = IntegrationManager.slack.oauth.get(team_id, destination.slack_integration_id)
+ except IntegrationNotFound:
raise DigestSlackError(f"No slack integration {destination.slack_integration_id} for team {team_id}")
try:
diff --git a/products/subscriptions/backend/logic/slack_subscriptions.py b/products/subscriptions/backend/logic/slack_subscriptions.py
index 3f7ae83e1210..4bc573a08a25 100644
--- a/products/subscriptions/backend/logic/slack_subscriptions.py
+++ b/products/subscriptions/backend/logic/slack_subscriptions.py
@@ -12,7 +12,8 @@ from slack_sdk.web.async_client import AsyncWebClient
from slack_sdk.web.async_slack_response import AsyncSlackResponse
from posthog.dataclasses import frozen
-from posthog.models.integration import Integration, SlackIntegration
+from posthog.integrations import IntegrationManager, IntegrationRecord
+from posthog.models.integration import SlackIntegration
from posthog.slack.formatting import channel_id_from_target
from posthog.slack.markdown import slack_rich_text
from posthog.storage import object_storage
@@ -86,6 +87,10 @@ class SlackGallery:
blocks: list[dict[str, Any]] = field(default_factory=list)
+def _ai_data_processing_approved(subscription: Subscription, integration: IntegrationRecord | None) -> bool:
+ return integration is not None and bool(subscription.team.organization.is_ai_data_processing_approved)
+
+
def _asset_image_bytes(asset: ExportedAsset) -> bytes | None:
if asset.content:
return bytes(asset.content)
@@ -182,7 +187,7 @@ def _ai_summary_block(change_summary: str) -> dict[str, Any]:
def _subscription_footer_blocks(
- subscription: Subscription, resource_info: SubscriptionResource, integration: Integration | None
+ subscription: Subscription, resource_info: SubscriptionResource, integration: IntegrationRecord | None
) -> list[dict[str, Any]]:
utm_tags = f"{UTM_TAGS_BASE}&utm_medium=slack"
action_elements: list[dict] = [
@@ -202,7 +207,7 @@ def _subscription_footer_blocks(
{"type": "divider"},
{"type": "actions", "elements": action_elements},
]
- ai_enabled = bool(integration and integration.team.organization.is_ai_data_processing_approved)
+ ai_enabled = _ai_data_processing_approved(subscription, integration)
if explore_hint := slack_followup_invite(integration, utm_tags=utm_tags, ai_enabled=ai_enabled):
blocks.append(explore_hint)
@@ -216,7 +221,7 @@ def _prepare_slack_gallery(
is_new_subscription: bool = False,
change_summary: str | None = None,
summary_skipped_over_budget: bool = False,
- integration: Integration | None = None,
+ integration: IntegrationRecord | None = None,
) -> SlackGallery:
utm_tags = f"{UTM_TAGS_BASE}&utm_medium=slack"
resource_info = subscription.resource_info
@@ -274,7 +279,7 @@ def _prepare_slack_gallery(
f"<{resource_info.url}?{utm_tags}|View in PostHog> · <{subscription.url}?{utm_tags}|Manage subscription>"
)
- ai_enabled = bool(integration and integration.team.organization.is_ai_data_processing_approved)
+ ai_enabled = _ai_data_processing_approved(subscription, integration)
if explore_hint := slack_followup_invite_text(integration, utm_tags=utm_tags, ai_enabled=ai_enabled):
lines.append(explore_hint)
@@ -391,9 +396,9 @@ def _blocks_for_asset(asset: ExportedAsset, resource_url: str, *, include_insigh
return blocks
-def get_slack_integration_for_team(team_id: int) -> Integration | None:
+def get_slack_integration_for_team(team_id: int) -> IntegrationRecord | None:
"""Get Slack integration for a team. Returns None if not found."""
- return Integration.objects.filter(team_id=team_id, kind="slack").first()
+ return IntegrationManager.slack.oauth.find(team_id)
def send_slack_subscription_report(
@@ -420,7 +425,7 @@ def _prepare_slack_message(
is_new_subscription: bool = False,
change_summary: str | None = None,
summary_skipped_over_budget: bool = False,
- integration: Integration | None = None,
+ integration: IntegrationRecord | None = None,
) -> SlackMessage:
"""Prepare Slack message content. Pure function with no side effects."""
utm_tags = f"{UTM_TAGS_BASE}&utm_medium=slack"
@@ -497,7 +502,7 @@ def _prepare_slack_message(
def send_slack_message_with_integration(
- integration: Integration,
+ integration: IntegrationRecord,
subscription: Subscription,
assets: list[ExportedAsset],
total_asset_count: int,
@@ -573,7 +578,7 @@ async def _send_slack_message_with_retry(
async def deliver_slack_message_data(
- integration: Integration,
+ integration: IntegrationRecord,
subscription: Subscription,
message_data: SlackMessage,
) -> SlackDeliveryResult:
@@ -629,7 +634,7 @@ async def deliver_slack_message_data(
async def deliver_slack_gallery(
- integration: Integration, subscription: Subscription, gallery: SlackGallery
+ integration: IntegrationRecord, subscription: Subscription, gallery: SlackGallery
) -> SlackDeliveryResult:
slack_integration = SlackIntegration(integration, source="subscriptions")
async with aiohttp.ClientSession(trust_env=True) as slack_session:
@@ -677,7 +682,7 @@ async def deliver_slack_gallery(
async def send_slack_message_with_integration_async(
- integration: Integration,
+ integration: IntegrationRecord,
subscription: Subscription,
assets: list[ExportedAsset],
total_asset_count: int,
diff --git a/products/subscriptions/backend/logic/subscription_writes.py b/products/subscriptions/backend/logic/subscription_writes.py
index 7e257e662559..f9337b372c36 100644
--- a/products/subscriptions/backend/logic/subscription_writes.py
+++ b/products/subscriptions/backend/logic/subscription_writes.py
@@ -19,7 +19,8 @@ from posthog.cloud_utils import is_cloud
from posthog.constants import SUBSCRIPTION_AI_PROMPT_FEATURE_FLAG_KEY
from posthog.event_usage import AnalyticsProps, groups
from posthog.exceptions_capture import capture_exception
-from posthog.models.integration import Integration, SlackIntegration
+from posthog.integrations import IntegrationManager, IntegrationNotFound
+from posthog.models.integration import SlackIntegration
from posthog.models.organization import Organization
from posthog.models.team import Team
from posthog.models.user import User
@@ -469,15 +470,11 @@ class _SubscriptionWrite:
{"integration_id": ["A Slack integration is required for Slack subscriptions."]}
)
try:
- integration = Integration.objects.get(id=integration_id, team_id=self.team_id)
- except Integration.DoesNotExist:
+ integration = IntegrationManager.slack.oauth.get(self.team_id, integration_id)
+ except IntegrationNotFound:
raise SubscriptionRequestInvalidError(
{"integration_id": ["This integration does not exist or does not belong to your team."]}
)
- if integration.kind != "slack":
- raise SubscriptionRequestInvalidError(
- {"integration_id": ["Slack subscriptions require a Slack integration."]}
- )
if gallery_enabled and SlackIntegration(integration).missing_scopes({"files:write"}):
raise SubscriptionRequestInvalidError(
{
diff --git a/products/subscriptions/backend/temporal/ai_subscription/delivery.py b/products/subscriptions/backend/temporal/ai_subscription/delivery.py
index 7344a03cc6a1..706771a93802 100644
--- a/products/subscriptions/backend/temporal/ai_subscription/delivery.py
+++ b/products/subscriptions/backend/temporal/ai_subscription/delivery.py
@@ -16,8 +16,8 @@ from slack_sdk.errors import SlackApiError
from posthog.email import EmailMessage, raise_if_delivery_rejected
from posthog.exceptions_capture import capture_exception
from posthog.helpers.markdown_safety import is_allowed_link_url, strip_external_links_markdown
+from posthog.integrations import IntegrationRecord
from posthog.models import Team, User
-from posthog.models.integration import Integration
from posthog.slack.formatting import channel_id_from_target
from posthog.sync import database_sync_to_async
from posthog.utils import absolute_uri
@@ -589,7 +589,7 @@ def _build_ai_slack_message(
markdown: str,
*,
delivery_id: uuid.UUID,
- integration: Integration | None = None,
+ integration: IntegrationRecord | None = None,
charts: list[dict] | None = None,
) -> SlackMessage:
utm_tags = f"{UTM_TAGS_BASE}&utm_medium=slack"
@@ -691,7 +691,7 @@ async def send_slack_ai_subscription_report(
*,
subscription: Subscription,
markdown: str,
- integration: Integration,
+ integration: IntegrationRecord,
delivery_id: uuid.UUID,
charts: list[dict] | None = None,
) -> SlackDeliveryResult:
diff --git a/products/subscriptions/backend/temporal/delivery_common.py b/products/subscriptions/backend/temporal/delivery_common.py
index e55ea7894900..9824e3988648 100644
--- a/products/subscriptions/backend/temporal/delivery_common.py
+++ b/products/subscriptions/backend/temporal/delivery_common.py
@@ -7,7 +7,7 @@ from temporalio.exceptions import ApplicationError
from posthog.email import EmailDeliveryError
from posthog.exceptions_capture import capture_exception
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationRecord
from posthog.sync import database_sync_to_async
from products.subscriptions.backend.logic import SLACK_USER_CONFIG_ERRORS, _capture_delivery_failed_event
@@ -151,8 +151,8 @@ async def deliver_email(
return DeliverSubscriptionResult(recipient_results=recipient_results)
-def _resolve_slack_integration(subscription: Subscription) -> Integration | None:
- integration = subscription.integration
+def _resolve_slack_integration(subscription: Subscription) -> IntegrationRecord | None:
+ integration: IntegrationRecord | None = subscription.integration
if integration is not None and integration.kind != "slack":
LOGGER.warning(
"deliver_subscription.invalid_integration_kind",
@@ -169,7 +169,7 @@ def _resolve_slack_integration(subscription: Subscription) -> Integration | None
async def deliver_slack(
subscription: Subscription,
recipient_results: list[RecipientResult],
- send: Callable[[Integration], Awaitable[SlackDeliveryResult]],
+ send: Callable[[IntegrationRecord], Awaitable[SlackDeliveryResult]],
) -> DeliverSubscriptionResult:
"""A missing integration or a permanent Slack config error auto-disables the subscription;
transient Slack errors raise so Temporal retries."""
diff --git a/products/subscriptions/backend/tests/test_subscription.py b/products/subscriptions/backend/tests/test_subscription.py
index a2a31737340a..cc8ac711e71f 100644
--- a/products/subscriptions/backend/tests/test_subscription.py
+++ b/products/subscriptions/backend/tests/test_subscription.py
@@ -18,6 +18,7 @@ from rest_framework import status
from temporalio.exceptions import WorkflowAlreadyStartedError
from posthog.constants import SUBSCRIPTION_AI_PROMPT_FEATURE_FLAG_KEY, AvailableFeature
+from posthog.integrations import SOURCE_MARKER_CONFIG_KEY
from posthog.models import Team
from posthog.models.integration import Integration
from posthog.models.organization import OrganizationMembership
@@ -957,8 +958,14 @@ class TestSubscriptionTemporal(APILicensedTest):
)
assert res.status_code == status.HTTP_201_CREATED
- def test_cannot_create_slack_subscription_with_non_slack_integration(self):
- integration = Integration.objects.create(team=self.team, kind="hubspot", config={})
+ @parameterized.expand(
+ [
+ ("non_slack_kind", "hubspot", {}),
+ ("warehouse_source_row", "slack", {SOURCE_MARKER_CONFIG_KEY: "Slack"}),
+ ]
+ )
+ def test_cannot_create_slack_subscription_with_unusable_integration(self, _name, kind, config):
+ integration = Integration.objects.create(team=self.team, kind=kind, config=config)
response = self._create_subscription(
target_type="slack",
@@ -966,7 +973,7 @@ class TestSubscriptionTemporal(APILicensedTest):
integration_id=integration.id,
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
- assert "require a Slack integration" in response.json()["detail"]
+ assert "does not exist or does not belong to your team" in response.json()["detail"]
def test_can_create_teams_subscription_with_a_webhook_url(self):
response = self._create_subscription(target_type="teams", target_value=VALID_TEAMS_WEBHOOK_URL)
diff --git a/products/visual_review/backend/logic/github_api.py b/products/visual_review/backend/logic/github_api.py
index 7250617ee230..f615b5bb009d 100644
--- a/products/visual_review/backend/logic/github_api.py
+++ b/products/visual_review/backend/logic/github_api.py
@@ -18,6 +18,7 @@ if TYPE_CHECKING:
from posthog.dataclasses import frozen
from posthog.egress.github.transport import GitHubRateLimitError
+from posthog.integrations import IntegrationManager
from posthog.models.github_integration_base import GitHubIntegrationError
from ..models import Repo
@@ -205,9 +206,9 @@ def _verified_merge_queue_source_pr(
def get_github_integration_for_repo(repo: Repo):
"""Get GitHub integration for the repo's team."""
- from posthog.models.integration import GitHubIntegration, Integration
+ from posthog.models.integration import GitHubIntegration
- integration = Integration.objects.filter(team_id=repo.team_id, kind="github").first()
+ integration = IntegrationManager.github.oauth.find(repo.team_id)
if not integration:
raise errors.GitHubIntegrationNotFoundError(f"No GitHub integration found for team {repo.team_id}")
diff --git a/products/visual_review/backend/logic/team_channels.py b/products/visual_review/backend/logic/team_channels.py
index 9a65fdfd0163..4cddbdf19b52 100644
--- a/products/visual_review/backend/logic/team_channels.py
+++ b/products/visual_review/backend/logic/team_channels.py
@@ -12,7 +12,8 @@ from owners_yaml.resolver import Purpose, team_channel
from owners_yaml.schema import Producer, TeamEntry
from posthog.dataclasses import frozen
-from posthog.models.integration import Integration, SlackIntegration
+from posthog.integrations import IntegrationManager, IntegrationRecord
+from posthog.models.integration import SlackIntegration
from posthog.slack.channels import (
SlackChannel,
SlackPostRefused,
@@ -57,13 +58,13 @@ class Delivery:
class Workspace:
"""The project's Slack connection, and its channels by name. Read once and shared across teams."""
- integration: Integration
+ integration: IntegrationRecord
channels_by_name: Mapping[str, SlackChannel]
def open_workspace(team_id: int) -> Workspace | None:
"""The project's Slack workspace, or None when the project has no Slack integration."""
- integration = Integration.objects.filter(team_id=team_id, kind="slack").first()
+ integration = IntegrationManager.slack.oauth.find(team_id)
if integration is None:
logger.info("visual_review.team_post_no_slack_integration", team_id=team_id)
return None
diff --git a/products/visual_review/backend/tests/conftest.py b/products/visual_review/backend/tests/conftest.py
index befd4e433aef..51ba9767b278 100644
--- a/products/visual_review/backend/tests/conftest.py
+++ b/products/visual_review/backend/tests/conftest.py
@@ -366,6 +366,7 @@ def mock_github_integration(team, mocker):
"""
Create a mock GitHub integration for the team.
"""
+ from posthog.integrations import IntegrationManager
from posthog.models.integration import GitHubIntegration, Integration
mock_integration = MagicMock(spec=Integration)
@@ -380,16 +381,14 @@ def mock_github_integration(team, mocker):
"access_token": "ghs_fake_token_for_testing",
}
- original_filter = Integration.objects.filter
+ original_find = IntegrationManager.github.oauth.find
- def patched_filter(*args, **kwargs):
- if kwargs.get("kind") == "github" and kwargs.get("team_id") == team.id:
- mock_qs = MagicMock()
- mock_qs.first.return_value = mock_integration
- return mock_qs
- return original_filter(*args, **kwargs)
+ def patched_find(team_id, *args, **kwargs):
+ if team_id == team.id:
+ return mock_integration
+ return original_find(team_id, *args, **kwargs)
- mocker.patch.object(Integration.objects, "filter", side_effect=patched_filter)
+ mocker.patch.object(IntegrationManager.github.oauth, "find", side_effect=patched_find)
def mock_access_token_expired(self):
return False
diff --git a/products/visual_review/backend/tests/logic/test_debt_digest.py b/products/visual_review/backend/tests/logic/test_debt_digest.py
index 541577d81526..e37816c102ee 100644
--- a/products/visual_review/backend/tests/logic/test_debt_digest.py
+++ b/products/visual_review/backend/tests/logic/test_debt_digest.py
@@ -709,7 +709,7 @@ class TestCollectAndSend:
"products.visual_review.backend.logic.debt_digest.resolve_path_owners",
return_value=_ownership({_SOURCE_PATH: "team-one", _OTHER_PATH: "team-two", _PRODUCT_PATH: "team-two"}),
),
- patch("products.visual_review.backend.logic.team_channels.Integration") as integration,
+ patch("products.visual_review.backend.logic.team_channels.IntegrationManager") as manager,
patch("products.visual_review.backend.logic.team_channels.SlackIntegration") as slack,
patch(
"products.visual_review.backend.logic.team_channels.fetch_channel_map",
@@ -724,7 +724,7 @@ class TestCollectAndSend:
) as post,
patch("products.visual_review.backend.logic.team_channels.post_message") as thread_post,
):
- integration.objects.filter.return_value.first.return_value = MagicMock()
+ manager.slack.oauth.find.return_value = MagicMock()
slack.return_value.client.conversations_info.return_value = {"channel": {"id": "C1"}}
debt_digest.send_debt_digest(repo, mode=debt_digest.MODE_LIVE)
diff --git a/products/visual_review/backend/tests/logic/test_quarantine_notice.py b/products/visual_review/backend/tests/logic/test_quarantine_notice.py
index b53792fe5d74..6ccc9151722a 100644
--- a/products/visual_review/backend/tests/logic/test_quarantine_notice.py
+++ b/products/visual_review/backend/tests/logic/test_quarantine_notice.py
@@ -33,7 +33,7 @@ class TestSendQuarantineNotice:
"products.visual_review.backend.logic.owners.resolve_path_owners",
return_value=PathOwnership(team_by_path={_SOURCE_PATH: "team-devex"}, registry={}, resolved=True),
) as owners,
- patch(f"{channels}.Integration") as integration,
+ patch(f"{channels}.IntegrationManager") as manager,
patch(f"{channels}.SlackIntegration") as slack,
patch(
f"{channels}.fetch_channel_map",
@@ -41,7 +41,7 @@ class TestSendQuarantineNotice:
) as channel_map,
patch(f"{channels}.post_with_join", return_value="1700000000.1") as post,
):
- integration.objects.filter.return_value.first.return_value = MagicMock()
+ manager.slack.oauth.find.return_value = MagicMock()
slack.return_value.client.conversations_info.return_value = {"channel": {"is_shared": False}}
post.slack = slack
post.owners = owners
diff --git a/products/visual_review/backend/tests/test_github_integration.py b/products/visual_review/backend/tests/test_github_integration.py
index d02f4cfb4136..556d47189f71 100644
--- a/products/visual_review/backend/tests/test_github_integration.py
+++ b/products/visual_review/backend/tests/test_github_integration.py
@@ -238,6 +238,7 @@ def mock_github_integration(team, mocker):
This patches the Integration lookup to return a fake integration
with valid credentials structure.
"""
+ from posthog.integrations import IntegrationManager
from posthog.models.integration import GitHubIntegration, Integration
# Create a mock integration object
@@ -253,17 +254,15 @@ def mock_github_integration(team, mocker):
"access_token": "ghs_fake_token_for_testing",
}
- # Patch the Integration.objects.filter to return our mock
- original_filter = Integration.objects.filter
+ # Patch the SDK lookup to return our mock
+ original_find = IntegrationManager.github.oauth.find
- def patched_filter(*args, **kwargs):
- if kwargs.get("kind") == "github" and kwargs.get("team_id") == team.id:
- mock_qs = MagicMock()
- mock_qs.first.return_value = mock_integration
- return mock_qs
- return original_filter(*args, **kwargs)
+ def patched_find(team_id, *args, **kwargs):
+ if team_id == team.id:
+ return mock_integration
+ return original_find(team_id, *args, **kwargs)
- mocker.patch.object(Integration.objects, "filter", side_effect=patched_filter)
+ mocker.patch.object(IntegrationManager.github.oauth, "find", side_effect=patched_find)
# Also patch GitHubIntegration methods we need
def mock_access_token_expired(self):
diff --git a/products/wizard/backend/logic/workers/repository_publisher.py b/products/wizard/backend/logic/workers/repository_publisher.py
index 9319be1fc766..90dffc6682f1 100644
--- a/products/wizard/backend/logic/workers/repository_publisher.py
+++ b/products/wizard/backend/logic/workers/repository_publisher.py
@@ -4,7 +4,8 @@ from typing import Any
from posthog.dataclasses import frozen
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.integrations import IntegrationManager, IntegrationNotFound
+from posthog.models.integration import GitHubIntegration
from products.tasks.backend.facade.sandbox import SandboxBase, sandbox_repo_path
from products.wizard.backend.logic.workers.commands import bound_command_output
@@ -217,8 +218,9 @@ def _commit_staged_changes(
def _github_integration(team_id: int, integration_id: int, source: str) -> GitHubIntegration:
- integration = Integration.objects.filter(team_id=team_id, id=integration_id, kind="github").first()
- if integration is None:
+ try:
+ integration = IntegrationManager.github.oauth.get(team_id, integration_id)
+ except IntegrationNotFound:
raise RepositoryPublishingError("GitHub integration is unavailable.")
return GitHubIntegration(integration, source=source, priority=Priority.CRITICAL)
@@ -302,11 +304,7 @@ def create_pull_request(
base_branch: str | None = None,
) -> RepositoryPullRequest:
repository_name = _repository_parts(repository)
- integration = Integration.objects.filter(team_id=team_id, id=integration_id, kind="github").first()
- if integration is None:
- raise RepositoryPublishingError("GitHub integration is unavailable.")
-
- github = GitHubIntegration(integration, source=source, priority=Priority.CRITICAL)
+ github = _github_integration(team_id, integration_id, source)
if github.organization().casefold() != repository_name.owner.casefold():
raise RepositoryPublishingError("GitHub integration does not own the repository.")
diff --git a/products/wizard/backend/tests/runs/test_repository_publisher.py b/products/wizard/backend/tests/runs/test_repository_publisher.py
index 37dc2e6f2f2b..3c5bfbfd1662 100644
--- a/products/wizard/backend/tests/runs/test_repository_publisher.py
+++ b/products/wizard/backend/tests/runs/test_repository_publisher.py
@@ -142,12 +142,12 @@ def test_stage_publishable_changes_replaces_existing_staging(tmp_path: Path) ->
@patch("products.wizard.backend.logic.workers.repository_publisher.GitHubIntegration")
-@patch("products.wizard.backend.logic.workers.repository_publisher.Integration.objects.filter")
+@patch("products.wizard.backend.logic.workers.repository_publisher.IntegrationManager")
def test_create_signed_commit_strips_head_sha_and_supports_binary_files(
- filter_integrations: MagicMock,
+ integration_manager: MagicMock,
github_integration_class: MagicMock,
) -> None:
- filter_integrations.return_value.first.return_value = MagicMock()
+ integration_manager.github.oauth.get_many.return_value = {13: MagicMock()}
github = github_integration_class.return_value
github.api_request.side_effect = [
_graphql_response({"data": {"repository": {"id": "R_1", "ref": None}}}),
diff --git a/products/workflows/backend/services/customerio_import/content.py b/products/workflows/backend/services/customerio_import/content.py
index ceb97e231fc8..404925318cc7 100644
--- a/products/workflows/backend/services/customerio_import/content.py
+++ b/products/workflows/backend/services/customerio_import/content.py
@@ -9,7 +9,7 @@ from typing import Any
import structlog
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager
from products.messaging.backend.facade.categories import list_categories
from products.workflows.backend.facade.contracts import (
@@ -150,14 +150,14 @@ def _template_from_app_action(app_action: dict[str, Any]) -> dict[str, Any]:
def _stored_app_api_key(team_id: int) -> str | None:
- integration = Integration.objects.filter(team_id=team_id, kind=Integration.IntegrationKind.CUSTOMERIO_APP).first()
+ integration = IntegrationManager.customerio_app.token.find(team_id)
key = (integration.sensitive_config or {}).get("app_api_key") if integration else None
return str(key) if key else None
def _team_senders(team_id: int) -> dict[str, ImportSender]:
senders: dict[str, ImportSender] = {}
- for integration in Integration.objects.filter(team_id=team_id, kind=Integration.IntegrationKind.EMAIL):
+ for integration in IntegrationManager.email.credentials.list(team_id):
config = integration.config or {}
address = str(config.get("email") or integration.integration_id or "").lower()
if address:
diff --git a/products/workflows/backend/services/data_suggestions/team_context.py b/products/workflows/backend/services/data_suggestions/team_context.py
index b04e098576c8..b7edc1cf37e5 100644
--- a/products/workflows/backend/services/data_suggestions/team_context.py
+++ b/products/workflows/backend/services/data_suggestions/team_context.py
@@ -6,8 +6,8 @@ from typing import Any
from django.db.models import BigIntegerField, F, IntegerField
from django.db.models.functions import Coalesce
+from posthog.integrations import IntegrationManager
from posthog.models import EventProperty, PropertyDefinition, Team
-from posthog.models.integration import Integration
from products.access_control.backend.facade.api import person_property_names_with_access_rules
from products.messaging.backend.facade.api import EmailTemplateSummary, list_email_templates
@@ -62,7 +62,7 @@ def _with_filled_secrets(action: dict[str, Any], keys: set[str]) -> dict[str, An
def channels(team: Team) -> frozenset[Channel]:
- kinds = set(Integration.objects.filter(team_id=team.id).values_list("kind", flat=True))
+ kinds = {row.kind for row in IntegrationManager.kinds("twilio", "apns", "firebase").list(team.id)}
connected: set[Channel] = set()
if "twilio" in kinds:
connected.add("sms")
@@ -72,20 +72,12 @@ def channels(team: Team) -> frozenset[Channel]:
def push_integration_ids(team: Team) -> tuple[int, ...]:
- return tuple(
- Integration.objects.filter(team_id=team.id, kind__in=["apns", "firebase"])
- .order_by("created_at")
- .values_list("id", flat=True)
- )
+ return tuple(row.id for row in IntegrationManager.kinds("apns", "firebase").list(team.id))
def slack_integration_id(team: Team) -> int | None:
- return (
- Integration.objects.filter(team_id=team.id, kind="slack")
- .order_by("created_at")
- .values_list("id", flat=True)
- .first()
- )
+ row = IntegrationManager.slack.oauth.find(team.id)
+ return row.id if row is not None else None
def email_sender(team: Team) -> EmailSender | None:
@@ -110,7 +102,7 @@ def has_verified_email_sender(team: Team) -> bool:
def _verified_senders(team: Team) -> list[EmailSender]:
return [
EmailSender(integration_id=integration.id, email=str(config["email"]), name=str(config.get("name") or ""))
- for integration in Integration.objects.filter(team_id=team.id, kind="email").order_by("created_at")
+ for integration in IntegrationManager.email.credentials.list(team.id)
if (config := integration.config or {}).get("verified") is True and config.get("email")
]
diff --git a/products/workflows/backend/services/email_health.py b/products/workflows/backend/services/email_health.py
index 5be6acff5ff5..3b83c79e9875 100644
--- a/products/workflows/backend/services/email_health.py
+++ b/products/workflows/backend/services/email_health.py
@@ -12,6 +12,7 @@ from django.utils import timezone
import structlog
from posthog.api.app_metrics2 import fetch_app_metric_totals_by_source, fetch_app_metric_totals_by_team_and_source
+from posthog.integrations import IntegrationManager
from posthog.models.integration import Integration
from products.workflows.backend.facade.contracts import (
@@ -215,9 +216,10 @@ def verified_email_domains(*, team_id: int) -> list[str]:
return list(
dict.fromkeys(
domain
- for domain in Integration.objects.filter(team_id=team_id, kind="email", config__verified=True)
- .order_by("id")
- .values_list("config__domain", flat=True)
+ for domain in (
+ (row.config or {}).get("domain")
+ for row in IntegrationManager.email.credentials.list(team_id, where={"verified": True})
+ )
if domain
)
)
diff --git a/products/workflows/backend/services/setup_status.py b/products/workflows/backend/services/setup_status.py
index 64200b08626e..85258ad3051d 100644
--- a/products/workflows/backend/services/setup_status.py
+++ b/products/workflows/backend/services/setup_status.py
@@ -1,7 +1,7 @@
"""Which messaging setup steps a project has done, in the order the setup wizard asks for them."""
+from posthog.integrations import IntegrationManager
from posthog.models import Team
-from posthog.models.integration import Integration
from products.messaging.backend.facade.api import list_email_templates
from products.messaging.backend.facade.categories import list_categories
@@ -33,7 +33,7 @@ def email_sending_paused(team: Team) -> bool:
def get_setup_steps(team: Team) -> tuple[SetupStep, ...]:
done = {
- SetupStepKey.CHANNEL: Integration.objects.filter(team_id=team.id, kind="email").exists(),
+ SetupStepKey.CHANNEL: IntegrationManager.email.credentials.exists(team.id),
SetupStepKey.DOMAIN: has_verified_email_sender(team),
SetupStepKey.OPT_OUTS: len(list_categories(team.id)) > 0,
SetupStepKey.SENDING_RULES: _frequency_cap_set(team),
diff --git a/products/workflows/backend/services/transactional_email_workflows.py b/products/workflows/backend/services/transactional_email_workflows.py
index 681435bdf089..5204bfbb220c 100644
--- a/products/workflows/backend/services/transactional_email_workflows.py
+++ b/products/workflows/backend/services/transactional_email_workflows.py
@@ -7,7 +7,7 @@ from uuid import UUID
from django.db import transaction
-from posthog.models.integration import Integration
+from posthog.integrations import IntegrationManager, IntegrationNotFound
from posthog.models.scoping import with_team_scope
from products.workflows.backend.facade.contracts import (
@@ -110,7 +110,12 @@ def resolve_transactional_email_sender(team_id: int, requested_id: int | None) -
.values_list("default_email_integration_id", flat=True)
.first()
)
- sender = Integration.objects.filter(team_id=team_id, kind="email", id=sender_id).first() if sender_id else None
+ sender = None
+ if sender_id:
+ try:
+ sender = IntegrationManager.email.credentials.get(team_id, sender_id)
+ except IntegrationNotFound:
+ pass
if sender is None or not sender.config.get("verified"):
raise TransactionalEmailSenderUnavailable()
return sender.id