-
Notifications
You must be signed in to change notification settings - Fork 1
feat(ecosystem): Implement cross-system issue synchronization #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: ecosystem-sync-integration-before
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import asdict, dataclass | ||
| from datetime import datetime | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from django.utils import timezone | ||
|
|
||
| if TYPE_CHECKING: | ||
| from sentry.integrations.models import Integration | ||
| from sentry.integrations.services.integration import RpcIntegration | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AssignmentSource: | ||
| source_name: str | ||
| integration_id: int | ||
| queued: datetime = timezone.now() | ||
|
|
||
| @classmethod | ||
| def from_integration(cls, integration: Integration | RpcIntegration) -> AssignmentSource: | ||
| return AssignmentSource( | ||
| source_name=integration.name, | ||
| integration_id=integration.id, | ||
| ) | ||
|
|
||
| def to_dict(self) -> dict[str, Any]: | ||
| return asdict(self) | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, input_dict: dict[str, Any]) -> AssignmentSource | None: | ||
| try: | ||
| return cls(**input_dict) | ||
| except (ValueError, TypeError): | ||
| return None | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,9 @@ | ||||||||||||||
| from typing import Any | ||||||||||||||
|
|
||||||||||||||
| from sentry import analytics, features | ||||||||||||||
| from sentry.integrations.models.external_issue import ExternalIssue | ||||||||||||||
| from sentry.integrations.models.integration import Integration | ||||||||||||||
| from sentry.integrations.services.assignment_source import AssignmentSource | ||||||||||||||
| from sentry.integrations.services.integration import integration_service | ||||||||||||||
| from sentry.models.organization import Organization | ||||||||||||||
| from sentry.silo.base import SiloMode | ||||||||||||||
|
|
@@ -24,7 +27,12 @@ | |||||||||||||
| Organization.DoesNotExist, | ||||||||||||||
| ) | ||||||||||||||
| ) | ||||||||||||||
| def sync_assignee_outbound(external_issue_id: int, user_id: int | None, assign: bool) -> None: | ||||||||||||||
| def sync_assignee_outbound( | ||||||||||||||
| external_issue_id: int, | ||||||||||||||
| user_id: int | None, | ||||||||||||||
| assign: bool, | ||||||||||||||
| assignment_source_dict: dict[str, Any] | None = None, | ||||||||||||||
| ) -> None: | ||||||||||||||
| # Sync Sentry assignee to an external issue. | ||||||||||||||
| external_issue = ExternalIssue.objects.get(id=external_issue_id) | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -42,10 +50,15 @@ def sync_assignee_outbound(external_issue_id: int, user_id: int | None, assign: | |||||||||||||
| ): | ||||||||||||||
| return | ||||||||||||||
|
|
||||||||||||||
| if installation.should_sync("outbound_assignee"): | ||||||||||||||
| parsed_assignment_source = ( | ||||||||||||||
| AssignmentSource.from_dict(assignment_source_dict) if assignment_source_dict else None | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| ) | ||||||||||||||
| if installation.should_sync("outbound_assignee", parsed_assignment_source): | ||||||||||||||
| # Assume unassign if None. | ||||||||||||||
| user = user_service.get_user(user_id) if user_id else None | ||||||||||||||
| installation.sync_assignee_outbound(external_issue, user, assign=assign) | ||||||||||||||
| installation.sync_assignee_outbound( | ||||||||||||||
| external_issue, user, assign=assign, assignment_source=parsed_assignment_source | ||||||||||||||
| ) | ||||||||||||||
| analytics.record( | ||||||||||||||
| "integration.issue.assignee.synced", | ||||||||||||||
| provider=integration.provider, | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| from typing import TYPE_CHECKING | ||
|
|
||
| from sentry import features | ||
| from sentry.integrations.services.assignment_source import AssignmentSource | ||
| from sentry.integrations.services.integration import integration_service | ||
| from sentry.integrations.tasks.sync_assignee_outbound import sync_assignee_outbound | ||
| from sentry.models.group import Group | ||
|
|
@@ -92,7 +93,11 @@ def sync_group_assignee_inbound( | |
|
|
||
| if not assign: | ||
| for group in affected_groups: | ||
| GroupAssignee.objects.deassign(group) | ||
| GroupAssignee.objects.deassign( | ||
| group, | ||
| assignment_source=AssignmentSource.from_integration(integration), | ||
| ) | ||
|
|
||
| return affected_groups | ||
|
|
||
| users = user_service.get_many_by_email(emails=[email], is_verified=True) | ||
|
|
@@ -104,14 +109,23 @@ def sync_group_assignee_inbound( | |
| user_id = get_user_id(projects_by_user, group) | ||
| user = users_by_id.get(user_id) | ||
| if user: | ||
| GroupAssignee.objects.assign(group, user) | ||
| GroupAssignee.objects.assign( | ||
| group, | ||
| user, | ||
| assignment_source=AssignmentSource.from_integration(integration), | ||
| ) | ||
| groups_assigned.append(group) | ||
| else: | ||
| logger.info("assignee-not-found-inbound", extra=log_context) | ||
| return groups_assigned | ||
|
|
||
|
|
||
| def sync_group_assignee_outbound(group: Group, user_id: int | None, assign: bool = True) -> None: | ||
| def sync_group_assignee_outbound( | ||
| group: Group, | ||
| user_id: int | None, | ||
| assign: bool = True, | ||
| assignment_source: AssignmentSource | None = None, | ||
| ) -> None: | ||
| from sentry.models.grouplink import GroupLink | ||
|
|
||
| external_issue_ids = GroupLink.objects.filter( | ||
|
|
@@ -120,5 +134,12 @@ def sync_group_assignee_outbound(group: Group, user_id: int | None, assign: bool | |
|
|
||
| for external_issue_id in external_issue_ids: | ||
| sync_assignee_outbound.apply_async( | ||
| kwargs={"external_issue_id": external_issue_id, "user_id": user_id, "assign": assign} | ||
| kwargs={ | ||
| "external_issue_id": external_issue_id, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔷 Medium: assignment_source.to_dict() includes a datetime (queued) which may not be JSON-serializable in some Celery configurations. Since only integration_id/source_name are used downstream, pass a minimal dict to avoid serialization issues. |
||
| "user_id": user_id, | ||
| "assign": assign, | ||
| "assignment_source_dict": assignment_source.to_dict() | ||
| if assignment_source | ||
| else None, | ||
| } | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| from typing import Any | ||
|
|
||
| from sentry.integrations.services.assignment_source import AssignmentSource | ||
| from sentry.testutils.cases import TestCase | ||
|
|
||
|
|
||
| class TestAssignmentSource(TestCase): | ||
| def test_from_dict_empty_array(self): | ||
| data: dict[str, Any] = {} | ||
| result = AssignmentSource.from_dict(data) | ||
| assert result is None | ||
|
|
||
| def test_from_dict_inalid_data(self): | ||
| data = { | ||
| "foo": "bar", | ||
| } | ||
|
|
||
| result = AssignmentSource.from_dict(data) | ||
| assert result is None | ||
|
|
||
| def test_from_dict_valid_data(self): | ||
| data = {"source_name": "foo-source", "integration_id": 123} | ||
|
|
||
| result = AssignmentSource.from_dict(data) | ||
| assert result is not None | ||
| assert result.source_name == "foo-source" | ||
| assert result.integration_id == 123 | ||
|
|
||
| def test_to_dict(self): | ||
| source = AssignmentSource( | ||
| source_name="foo-source", | ||
| integration_id=123, | ||
| ) | ||
|
|
||
| result = source.to_dict() | ||
| assert result.get("queued") is not None | ||
| assert result.get("source_name") == "foo-source" | ||
| assert result.get("integration_id") == 123 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.