def test_rate_limiting(self): """Should handle 429 from Slack when searching for channels""" self.resp.add( method=responses.GET, url="https://slack.com/api/conversations.list", status=429, content_type="application/json", body=json.dumps({ "ok": "false", "error": "ratelimited" }), ) with pytest.raises(ApiRateLimitedError): get_channel_id(self.organization, self.integration, "@user")
def get_alert_rule_trigger_action_slack_channel_id(organization, integration_id, name): from sentry.integrations.slack.utils import get_channel_id try: _prefix, channel_id, timed_out = get_channel_id(organization, integration_id, name) except DuplicateDisplayNameError as e: integration = Integration.objects.get(id=integration_id) domain = integration.metadata["domain_name"] raise InvalidTriggerActionError( 'Multiple users were found with display name "%s". Please use your username, found at %s/account/settings.' % (e.message, domain) ) if timed_out: raise InvalidTriggerActionError( "Could not find channel %s. We have timed out trying to look for it. " % name ) if channel_id is None: raise InvalidTriggerActionError( "Could not find channel %s. Channel may not exist, or Sentry may not " "have been granted permission to access it" % name ) return channel_id
def get_alert_rule_trigger_action_msteams_channel_id(organization, integration_id, name): from sentry.integrations.msteams.utils import get_channel_id channel_id = get_channel_id(organization, integration_id, name) if channel_id is None: # no granting access for msteams channels unlike slack raise InvalidTriggerActionError("Could not find channel %s." % name) return channel_id
def create_alert_rule_trigger_action(trigger, type, target_type, target_identifier=None, integration=None): """ Creates an AlertRuleTriggerAction :param trigger: The trigger to create the action on :param type: Which sort of action to take :param target_type: Which type of target to send to :param target_identifier: (Optional) The identifier of the target :param target_display: (Optional) Human readable name for the target :param integration: (Optional) The Integration related to this action. :return: The created action """ target_display = None if type == AlertRuleTriggerAction.Type.SLACK: from sentry.integrations.slack.utils import get_channel_id if target_type != AlertRuleTriggerAction.TargetType.SPECIFIC: raise InvalidTriggerActionError( "Slack action must specify channel") channel_result = get_channel_id(trigger.alert_rule.organization, integration.id, target_identifier) if channel_result is not None: channel_id = channel_result[1] else: raise InvalidTriggerActionError( "Could not find channel %s. Channel may not exist, or Sentry may not " "have been granted permission to access it" % target_identifier) # Use the channel name for display target_display = target_identifier target_identifier = channel_id return AlertRuleTriggerAction.objects.create( alert_rule_trigger=trigger, type=type.value, target_type=target_type.value, target_identifier=target_identifier, target_display=target_display, integration=integration, )
def update_alert_rule_trigger_action(trigger_action, type=None, target_type=None, target_identifier=None, integration=None): """ Updates values on an AlertRuleTriggerAction :param trigger_action: The trigger action to update :param type: Which sort of action to take :param target_type: Which type of target to send to :param target_identifier: The identifier of the target :param target_display: Human readable name for the target :param integration: The Integration related to this action. :return: """ updated_fields = {} if type is not None: updated_fields["type"] = type.value if target_type is not None: updated_fields["target_type"] = target_type.value if integration is not None: updated_fields["integration"] = integration if target_identifier is not None: type = updated_fields.get("type", trigger_action.type) if type == AlertRuleTriggerAction.Type.SLACK.value: from sentry.integrations.slack.utils import get_channel_id integration = updated_fields.get("integration", trigger_action.integration) prefix, channel_id, _ = get_channel_id( trigger_action.alert_rule_trigger.alert_rule.organization, integration.id, target_identifier, ) # Use the channel name for display updated_fields["target_display"] = target_identifier updated_fields["target_identifier"] = channel_id else: updated_fields["target_identifier"] = target_identifier trigger_action.update(**updated_fields) return trigger_action
def get_alert_rule_trigger_action_slack_channel_id(organization, integration_id, name): from sentry.integrations.slack.utils import get_channel_id _prefix, channel_id, timed_out = get_channel_id( organization, integration_id, name, ) if timed_out: raise InvalidTriggerActionError( "Could not find channel %s. We have timed out trying to look for it. " % name) if channel_id is None: raise InvalidTriggerActionError( "Could not find channel %s. Channel may not exist, or Sentry may not " "have been granted permission to access it" % name) return channel_id
def test_invalid_channel_selected(self): assert get_channel_id(self.organization, self.integration, "#fake-channel")[1] is None assert get_channel_id(self.organization, self.integration, "@fake-user")[1] is None
def test_invalid_member_selected_display_name(self): with pytest.raises(DuplicateDisplayNameError): get_channel_id(self.organization, self.integration, "@Morty")
def run_valid_test(self, channel, expected_prefix, expected_id, timed_out): assert (expected_prefix, expected_id, timed_out) == get_channel_id(self.organization, self.integration, channel)
def test_invalid_deprecated_workspace_app(self): with pytest.raises(DeprecatedIntegrationError): get_channel_id(self.organization, self.integration, "#hello")