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.

fix(tasks): clear space repositories on GitHub disconnect (#81149)

Jonathan Mieloocfc1ac2c2e5f
diff --git a/posthog/test/setup_receivers_baseline.txt b/posthog/test/setup_receivers_baseline.txt
index 7339d9f65653..8fc7a03f0e0e 100644
--- a/posthog/test/setup_receivers_baseline.txt
+++ b/posthog/test/setup_receivers_baseline.txt
@@ -221,6 +221,7 @@ pre_delete:posthog.storage.team_llm_gateway_policy_signal_handlers._clear_cache_
pre_delete:products.alerts.backend.activity_logging.cleanup_alert_hog_functions
pre_delete:products.alerts.backend.activity_logging.handle_alert_subscription_delete
pre_delete:products.managed_migrations.backend.activity_logging.handle_batch_import_delete
+pre_delete:products.tasks.backend.models.clear_channel_repositories_on_github_integration_delete
pre_delete:products.tasks.backend.team_deletion.cancel_task_workflows_on_team_delete
pre_save:posthog.models.event_ingestion_restriction_config.capture_old_restriction_type
pre_save:posthog.models.organization.organization_about_to_be_created
diff --git a/products/tasks/backend/migrations/0086_clear_disconnected_channel_repositories.py b/products/tasks/backend/migrations/0086_clear_disconnected_channel_repositories.py
new file mode 100644
index 000000000000..cafc19f5d1f8
--- /dev/null
+++ b/products/tasks/backend/migrations/0086_clear_disconnected_channel_repositories.py
@@ -0,0 +1,16 @@
+from django.apps.registry import Apps
+from django.db import migrations
+from django.db.backends.base.schema import BaseDatabaseSchemaEditor
+
+
+def clear_disconnected_channel_repositories(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
+ Channel = apps.get_model("tasks", "Channel")
+ Channel.objects.filter(github_integration__isnull=True).exclude(repositories=[]).update(repositories=[])
+
+
+class Migration(migrations.Migration):
+ dependencies = [("tasks", "0085_sandboxsession_cpu_attribution_usage")]
+
+ operations = [
+ migrations.RunPython(clear_disconnected_channel_repositories, migrations.RunPython.noop),
+ ]
diff --git a/products/tasks/backend/migrations/max_migration.txt b/products/tasks/backend/migrations/max_migration.txt
index a2754182002d..619fb492710d 100644
--- a/products/tasks/backend/migrations/max_migration.txt
+++ b/products/tasks/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0085_sandboxsession_cpu_attribution_usage
+0086_clear_disconnected_channel_repositories
diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.py
index 52462935433f..d19ec1df5e45 100644
--- a/products/tasks/backend/models.py
+++ b/products/tasks/backend/models.py
@@ -7,7 +7,7 @@ from collections.abc import Callable, Iterable
from datetime import datetime
from typing import TYPE_CHECKING, Any, Literal, Optional
-from django.db.models.signals import post_delete, post_save
+from django.db.models.signals import post_delete, post_save, pre_delete
from django.dispatch import receiver
from pydantic import BaseModel
@@ -137,6 +137,19 @@ class Channel(TeamScopedRootMixin):
return f"#{self.name}"
+@receiver(pre_delete, sender=Integration)
+def clear_channel_repositories_on_github_integration_delete(
+ sender: type[Integration], instance: Integration, **kwargs: Any
+) -> None:
+ if instance.kind != Integration.IntegrationKind.GITHUB:
+ return
+
+ Channel.objects.for_team(instance.team_id).filter(github_integration_id=instance.id).update(
+ github_integration=None,
+ repositories=[],
+ )
+
+
SLACK_NOTIFIED_PR_URL_STATE_KEY = "slack_notified_pr_url"
PR_READY_EMAIL_QUEUED_AT_STATE_KEY = "pr_ready_email_queued_at"
PR_READY_EMAIL_SENT_AT_STATE_KEY = "pr_ready_email_sent_at"
diff --git a/products/tasks/backend/tests/test_channels_api.py b/products/tasks/backend/tests/test_channels_api.py
index f7cbce16db5f..afdf06da2af4 100644
--- a/products/tasks/backend/tests/test_channels_api.py
+++ b/products/tasks/backend/tests/test_channels_api.py
@@ -125,6 +125,21 @@ class ChannelsAPITestCase(TestCase):
listed = other_client.get(self._tasks_url(), {"channel": channel_id}).json()["results"]
self.assertEqual([t["id"] for t in listed], [created.json()["id"]])
+ def test_deleting_github_integration_clears_channel_repositories(self):
+ integration = Integration.objects.create(team=self.team, kind="github", integration_id="1", config={})
+ channel = Channel.objects.for_team(self.team.id).create(
+ team_id=self.team.id,
+ name="growth",
+ github_integration=integration,
+ repositories=["posthog/posthog"],
+ )
+
+ Integration.objects.filter(id=integration.id).delete()
+
+ channel.refresh_from_db()
+ self.assertIsNone(channel.github_integration_id)
+ self.assertEqual(channel.repositories, [])
+
@patch("posthog.models.integration.GitHubIntegration.list_all_cached_repositories")
def test_new_tasks_inherit_channel_repositories(self, list_repositories):
list_repositories.return_value = [