-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(integration): Add new endpoint to list channels #101321
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
Merged
priscilawebdev
merged 13 commits into
master
from
priscila/feat/add-new-endpoint-to-list-integration-channels
Oct 15, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4397a7a
feat(integration): Add new endpoint to list channels
priscilawebdev 16769df
fix test
priscilawebdev 280d89c
fix tests
priscilawebdev 6739e23
fix type issue
priscilawebdev 6f28480
Merge branch 'master' into priscila/feat/add-new-endpoint-to-list-int…
priscilawebdev adec286
feedback
priscilawebdev fb40b9a
fix types
priscilawebdev ba2245d
Merge branch 'master' into priscila/feat/add-new-endpoint-to-list-int…
priscilawebdev cfc551c
add int channels to silo url patterns
priscilawebdev 1488f2a
cursor feedback
priscilawebdev f016f7f
fix integration initialization
priscilawebdev f1fadad
Merge branch 'master' into priscila/feat/add-new-endpoint-to-list-int…
priscilawebdev 8785006
cursor feedback
priscilawebdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
256 changes: 256 additions & 0 deletions
256
src/sentry/integrations/api/endpoints/organization_integration_channels.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from typing import Any | ||
|
|
||
| from rest_framework.request import Request | ||
| from rest_framework.response import Response | ||
|
|
||
| from sentry.api.api_owners import ApiOwner | ||
| from sentry.api.api_publish_status import ApiPublishStatus | ||
| from sentry.api.base import control_silo_endpoint | ||
| from sentry.integrations.api.bases.organization_integrations import ( | ||
| OrganizationIntegrationBaseEndpoint, | ||
| ) | ||
| from sentry.integrations.discord.client import DiscordClient | ||
| from sentry.integrations.models import Integration | ||
| from sentry.integrations.msteams.client import MsTeamsClient | ||
| from sentry.integrations.services.integration.model import RpcIntegration | ||
| from sentry.integrations.types import IntegrationProviderSlug | ||
| from sentry.organizations.services.organization import RpcUserOrganizationContext | ||
| from sentry.shared_integrations.exceptions import ApiError | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _slack_list_channels(*, integration_id: int) -> list[dict[str, Any]]: | ||
| """ | ||
| List Slack channels for a given integration. | ||
|
|
||
| Fetches up to the Slack API limit (1000 channels). | ||
| Handles authentication via integration context and validates responses. | ||
| """ | ||
|
|
||
| from sentry.integrations.slack.sdk_client import SlackSdkClient | ||
|
|
||
| client = SlackSdkClient(integration_id=integration_id) | ||
|
|
||
| try: | ||
| response = client.conversations_list( | ||
| exclude_archived=True, | ||
| types="public_channel,private_channel", | ||
| limit=1000, # Max allowed by Slack API | ||
| ) | ||
| resp_data: dict[str, Any] = response.data if isinstance(response.data, dict) else {} | ||
| except Exception as e: | ||
| logger.warning("Slack API request failed for integration_id=%s: %s", integration_id, e) | ||
| return [] | ||
|
|
||
| # Validate structure | ||
| raw_channels = resp_data.get("channels") | ||
| if not isinstance(raw_channels, list): | ||
| logger.warning( | ||
| "Unexpected Slack API response structure for integration_id=%s: %r", | ||
| integration_id, | ||
| resp_data, | ||
| ) | ||
| return [] | ||
|
|
||
| results: list[dict[str, Any]] = [] | ||
| for ch in raw_channels: | ||
| if not isinstance(ch, dict): | ||
| continue | ||
|
|
||
| ch_id = ch.get("id") | ||
| ch_name = ch.get("name") | ||
| if not ch_id or not ch_name: | ||
| continue | ||
|
|
||
| is_private = bool(ch.get("is_private")) | ||
| name_str = str(ch_name) | ||
|
|
||
| results.append( | ||
| { | ||
| "id": str(ch_id), | ||
| "name": name_str, | ||
| "display": f"#{name_str}", | ||
| "type": "private" if is_private else "public", | ||
| } | ||
| ) | ||
|
|
||
| return results | ||
|
|
||
|
|
||
| def _discord_list_channels(*, guild_id: str) -> list[dict[str, Any]]: | ||
| """ | ||
| List Discord channels for a given guild that can receive messages. | ||
|
|
||
| The Discord API returns all guild channels in a single call. | ||
| This function filters for messageable channels only. | ||
| """ | ||
|
|
||
| DISCORD_CHANNEL_TYPES = { | ||
| 0: "text", | ||
| 5: "announcement", | ||
| 15: "forum", | ||
| } | ||
|
|
||
| client = DiscordClient() | ||
|
|
||
| try: | ||
| raw_resp = client.get( | ||
| f"/guilds/{guild_id}/channels", | ||
| headers=client.prepare_auth_header(), | ||
| ) | ||
| except Exception as e: | ||
| logger.warning( | ||
| "Discord API request failed for guild_id=%s: %s", | ||
| guild_id, | ||
| e, | ||
| ) | ||
| return [] | ||
|
|
||
| if not isinstance(raw_resp, list): | ||
| logger.warning( | ||
| "Unexpected Discord API response for guild_id=%s: %r", | ||
| guild_id, | ||
| raw_resp, | ||
| ) | ||
| return [] | ||
|
|
||
| selectable_types = set(DISCORD_CHANNEL_TYPES.keys()) | ||
| results: list[dict[str, Any]] = [] | ||
|
|
||
| for item in raw_resp: | ||
| if not isinstance(item, dict): | ||
| continue | ||
|
|
||
| ch_type = item.get("type") | ||
| if not isinstance(ch_type, int) or ch_type not in selectable_types: | ||
| continue | ||
|
|
||
| ch_id = item.get("id") | ||
| ch_name = item.get("name") | ||
| if not ch_id or not ch_name: | ||
| continue | ||
|
|
||
| results.append( | ||
| { | ||
| "id": str(ch_id), | ||
| "name": str(ch_name), | ||
| "display": f"#{ch_name}", | ||
| "type": DISCORD_CHANNEL_TYPES.get(ch_type, "unknown"), | ||
| } | ||
| ) | ||
|
|
||
| return results | ||
|
|
||
|
|
||
| def _msteams_list_channels( | ||
| *, integration: Integration | RpcIntegration, team_id: str | ||
| ) -> list[dict[str, Any]]: | ||
| """ | ||
| List Microsoft Teams channels for a given team. | ||
|
|
||
| The Teams API returns all channels at once. | ||
| Only standard and private channels are included. | ||
| """ | ||
|
|
||
| client = MsTeamsClient(integration) | ||
|
|
||
| try: | ||
| raw_resp = client.get(client.CHANNEL_URL % team_id) | ||
| except Exception as e: | ||
| logger.warning( | ||
| "Microsoft Teams API request failed for integration_id=%s, team_id=%s: %s", | ||
| integration.id, | ||
| team_id, | ||
| e, | ||
| ) | ||
| return [] | ||
|
|
||
| if not isinstance(raw_resp, dict): | ||
| logger.warning( | ||
| "Unexpected Microsoft Teams API response for integration_id=%s, team_id=%s: %r", | ||
| integration.id, | ||
| team_id, | ||
| raw_resp, | ||
| ) | ||
| return [] | ||
|
|
||
| raw_channels = raw_resp.get("conversations") | ||
| if not isinstance(raw_channels, list): | ||
| logger.warning( | ||
| "Missing or invalid 'conversations' in Teams API response for integration_id=%s, team_id=%s: %r", | ||
| integration.id, | ||
| team_id, | ||
| raw_resp, | ||
| ) | ||
| return [] | ||
|
|
||
| results: list[dict[str, Any]] = [] | ||
| for item in raw_channels: | ||
| if not isinstance(item, dict): | ||
| continue | ||
|
|
||
| ch_id = item.get("id") | ||
| display_name = item.get("displayName") | ||
| if not ch_id or not display_name: | ||
| continue | ||
|
|
||
| ch_type = str(item.get("membershipType") or "standard") | ||
|
|
||
| results.append( | ||
| { | ||
| "id": str(ch_id), | ||
| "name": str(display_name), | ||
| "display": str(display_name), | ||
| "type": ch_type, # "standard" or "private" | ||
| } | ||
| ) | ||
|
|
||
| return results | ||
|
|
||
|
|
||
| @control_silo_endpoint | ||
| class OrganizationIntegrationChannelsEndpoint(OrganizationIntegrationBaseEndpoint): | ||
| publish_status = { | ||
| "GET": ApiPublishStatus.PRIVATE, | ||
| } | ||
| owner = ApiOwner.TELEMETRY_EXPERIENCE | ||
|
|
||
| def get( | ||
| self, | ||
| request: Request, | ||
| organization_context: RpcUserOrganizationContext, | ||
| integration_id: int, | ||
| **kwargs: Any, | ||
| ) -> Response: | ||
| """ | ||
| List all messaging channels for an integration. | ||
| """ | ||
|
|
||
| integration = self.get_integration(organization_context.organization.id, integration_id) | ||
|
|
||
| try: | ||
| match integration.provider: | ||
| case IntegrationProviderSlug.SLACK.value: | ||
| results = _slack_list_channels(integration_id=integration.id) | ||
| case IntegrationProviderSlug.DISCORD.value: | ||
| results = _discord_list_channels(guild_id=str(integration.external_id)) | ||
| case IntegrationProviderSlug.MSTEAMS.value: | ||
| results = _msteams_list_channels( | ||
| integration=integration, | ||
| team_id=str(integration.external_id), | ||
| ) | ||
| case _: | ||
| return self.respond( | ||
| { | ||
| "results": [], | ||
| "warning": f"Channel listing not supported for provider '{integration.provider}'.", | ||
| } | ||
| ) | ||
| except ApiError as e: | ||
| return self.respond({"detail": str(e)}, status=400) | ||
|
|
||
| return self.respond({"results": results}) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.