def init_slack_map(unleash_instance): settings = queries.get_app_interface_settings() slack_notifications = unleash_instance['notifications']['slack'] slack_map = {} for slack_info in slack_notifications: workspace = slack_info['workspace'] workspace_name = workspace['name'] slack_integrations = workspace['integrations'] slack_config = \ [i for i in slack_integrations if i['name'] == QONTRACT_INTEGRATION] [slack_config] = slack_config token = slack_config['token'] channel = slack_info['channel'] icon_emoji = slack_info['icon_emoji'] username = slack_info['username'] slack = SlackApi(workspace_name, token, settings=settings, init_usergroups=False, channel=channel, icon_emoji=icon_emoji, username=username) slack_map[channel] = slack return slack_map
def init_slack(jira_board): settings = queries.get_app_interface_settings() slack_info = jira_board['slack'] workspace = slack_info['workspace'] workspace_name = workspace['name'] slack_integrations = workspace['integrations'] client_config = workspace.get('api_client') jira_config = \ [i for i in slack_integrations if i['name'] == QONTRACT_INTEGRATION] [jira_config] = jira_config token = jira_config['token'] default_channel = jira_config['channel'] icon_emoji = jira_config['icon_emoji'] username = jira_config['username'] channel = slack_info.get('channel') or default_channel slack_api_kwargs = { 'settings': settings, 'init_usergroups': False, 'channel': channel, 'icon_emoji': icon_emoji, 'username': username } if client_config: slack_api_kwargs['api_config'] = \ SlackApiConfig.from_dict(client_config) slack = SlackApi(workspace_name, token, **slack_api_kwargs) return slack
def init_slack(slack_info, integration, init_usergroups=True): settings = queries.get_app_interface_settings() workspace_name = slack_info['workspace']['name'] slack_integrations = slack_info['workspace']['integrations'] client_config = slack_info['workspace'].get('api_client') slack_config = \ [i for i in slack_integrations if i['name'] == integration] [slack_config] = slack_config token = slack_config['token'] default_channel = slack_config['channel'] icon_emoji = slack_config['icon_emoji'] username = slack_config['username'] channel = slack_info.get('channel') or default_channel slack_api_kwargs = { 'settings': settings, 'init_usergroups': init_usergroups, 'channel': channel, 'icon_emoji': icon_emoji, 'username': username } if client_config: slack_api_kwargs['api_config'] = \ SlackApiConfig.from_dict(client_config) slack = SlackApi(workspace_name, token, **slack_api_kwargs) return slack
def test_slack_api__client_5xx_doesnt_raise(mock_sleep, mock_secret_reader): """Don't raise an exception if the max retries aren't reached.""" uri_args = (httpretty.POST, 'https://www.slack.com/api/users.list') uri_kwargs_failure = { 'body': json.dumps({ 'ok': 'false', 'error': 'internal_error' }), 'status': 500 } uri_kwargs_success = {'body': json.dumps({'ok': 'true'}), 'status': 200} # These are registered LIFO (3 failures and then 1 success) httpretty.register_uri(*uri_args, **uri_kwargs_success) httpretty.register_uri(*uri_args, **uri_kwargs_failure) httpretty.register_uri(*uri_args, **uri_kwargs_failure) httpretty.register_uri(*uri_args, **uri_kwargs_failure) slack_client = SlackApi('workspace', { 'path': 'some/path', 'field': 'some-field' }, init_usergroups=False) slack_client._sc.api_call('users.list') assert len(httpretty.latest_requests()) == 4
def slackapi_from_permissions( permissions: Mapping[str, Any], app_interface_settings: Mapping[str, Any], init_usergroups: Optional[bool] = True, ) -> SlackApi: if "workspace" not in permissions: raise ValueError('Slack workspace not containing keyword "workspace"') workspace_name = permissions["workspace"]["name"] client_config = permissions["workspace"].get("api_client") token = permissions["workspace"]["token"] if client_config: api_config = SlackApiConfig.from_dict(client_config) else: api_config = SlackApiConfig() api = SlackApi( workspace_name, token, app_interface_settings=app_interface_settings, init_usergroups=init_usergroups, api_config=api_config, ) return api
def init_slack(jira_board): settings = queries.get_app_interface_settings() slack_info = jira_board['slack'] workspace = slack_info['workspace'] workspace_name = workspace['name'] slack_integrations = workspace['integrations'] jira_config = \ [i for i in slack_integrations if i['name'] == QONTRACT_INTEGRATION] [jira_config] = jira_config token = jira_config['token'] default_channel = jira_config['channel'] icon_emoji = jira_config['icon_emoji'] username = jira_config['username'] channel = slack_info.get('channel') or default_channel slack = SlackApi(workspace_name, token, settings=settings, init_usergroups=False, channel=channel, icon_emoji=icon_emoji, username=username) return slack
def get_slack_map(): settings = queries.get_app_interface_settings() permissions = queries.get_permissions_for_slack_usergroup() slack_map = {} for sp in permissions: workspace = sp['workspace'] workspace_name = workspace['name'] if workspace_name in slack_map: continue slack_api_kwargs = { 'settings': settings, } client_config = workspace.get('api_client') if client_config: slack_api_kwargs['api_config'] = \ SlackApiConfig.from_dict(client_config) workspace_spec = { "slack": SlackApi( workspace_name, workspace['token'], **slack_api_kwargs), "managed_usergroups": workspace['managedUsergroups'] } slack_map[workspace_name] = workspace_spec return slack_map
def slackapi_from_slack_workspace( slack_workspace: Mapping[str, Any], app_interface_settings: Mapping[str, Any], integration_name: str, init_usergroups: Optional[bool] = True, channel: Optional[str] = None, ) -> SlackApi: if "workspace" not in slack_workspace: raise ValueError('Slack workspace not containing keyword "workspace"') workspace_name = slack_workspace["workspace"]["name"] client_config = slack_workspace["workspace"].get("api_client") if "integrations" not in slack_workspace["workspace"]: raise ValueError('Slack workspace not containing any "integrations"') [slack_integration_config] = [ i for i in slack_workspace["workspace"]["integrations"] if i["name"] == integration_name ] token = slack_integration_config["token"] icon_emoji = (slack_workspace.get("icon_emoji") or slack_integration_config["icon_emoji"]) username = slack_workspace.get( "username") or slack_integration_config["username"] if channel is None: channel = slack_workspace.get( "channel") or slack_integration_config["channel"] if client_config: api_config = SlackApiConfig.from_dict(client_config) else: api_config = SlackApiConfig() api = SlackApi( workspace_name, token, app_interface_settings=app_interface_settings, channel=channel, api_config=api_config, init_usergroups=init_usergroups, icon_emoji=icon_emoji, username=username, ) return api
def test_slack_api__client_dont_retry(mock_sleep, mock_secret_reader): """Don't retry client-side errors that aren't 429s.""" httpretty.register_uri( httpretty.POST, "https://www.slack.com/api/users.list", body=json.dumps({"ok": "false", "error": "internal_error"}), status=401, ) slack_client = SlackApi( "workspace", {"path": "some/path", "field": "some-field"}, init_usergroups=False ) with pytest.raises(SlackApiError): slack_client._sc.api_call("users.list") assert len(httpretty.latest_requests()) == 1
def test_slack_api__client_5xx_raise(mock_sleep, mock_secret_reader): """Raise an exception if the max retries is exceeded.""" httpretty.register_uri( httpretty.POST, "https://www.slack.com/api/users.list", body=json.dumps({"ok": "false", "error": "internal_error"}), status=500, ) slack_client = SlackApi( "workspace", {"path": "some/path", "field": "some-field"}, init_usergroups=False ) with pytest.raises(SlackApiError): slack_client._sc.api_call("users.list") assert len(httpretty.latest_requests()) == MAX_RETRIES + 1
def slack_api(mocker): mock_secret_reader = mocker.patch.object(reconcile.utils.slack_api, 'SecretReader', autospec=True) mock_slack_client = mocker.patch.object(reconcile.utils.slack_api, 'WebClient', autospec=True) # autospec doesn't know about instance attributes mock_slack_client.return_value.retry_handlers = [] token = {'path': 'some/path', 'field': 'some-field'} slack_api = SlackApi('some-workspace', token) SlackApiMock = namedtuple("SlackApiMock", "client mock_secret_reader " "mock_slack_client") return SlackApiMock(slack_api, mock_secret_reader, mock_slack_client)
def get_slack_map(): settings = queries.get_app_interface_settings() permissions = get_permissions() slack_map = {} for sp in permissions: workspace = sp['workspace'] workspace_name = workspace['name'] if workspace_name in slack_map: continue workspace_spec = { "slack": SlackApi(workspace_name, workspace['token'], settings=settings), "managed_usergroups": workspace['managedUsergroups'] } slack_map[workspace_name] = workspace_spec return slack_map
def test_slack_api__client_dont_retry(mock_sleep, mock_secret_reader): """Don't retry client-side errors that aren't 429s.""" httpretty.register_uri(httpretty.POST, 'https://www.slack.com/api/users.list', body=json.dumps({ 'ok': 'false', 'error': 'internal_error' }), status=401) slack_client = SlackApi('workspace', { 'path': 'some/path', 'field': 'some-field' }, init_usergroups=False) with pytest.raises(SlackApiError): slack_client._sc.api_call('users.list') assert len(httpretty.latest_requests()) == 1
def test_instantiate_slack_api_with_config(mocker): """ When SlackApiConfig is passed into SlackApi, the constructor shouldn't create a default configuration object. """ mocker.patch.object(reconcile.utils.slack_api, "SecretReader", autospec=True) mock_slack_client = mocker.patch.object( reconcile.utils.slack_api, "WebClient", autospec=True ) # autospec doesn't know about instance attributes mock_slack_client.return_value.retry_handlers = [] config = SlackApiConfig() token = {"path": "some/path", "field": "some-field"} slack_api = SlackApi("some-workspace", token, config) assert slack_api.config is config
def test_slack_api__client_5xx_raise(mock_sleep, mock_secret_reader): """Raise an exception if the max retries is exceeded.""" httpretty.register_uri(httpretty.POST, 'https://www.slack.com/api/users.list', body=json.dumps({ 'ok': 'false', 'error': 'internal_error' }), status=500) slack_client = SlackApi('workspace', { 'path': 'some/path', 'field': 'some-field' }, init_usergroups=False) with pytest.raises(SlackApiError): slack_client._sc.api_call('users.list') assert len(httpretty.latest_requests()) == MAX_RETRIES + 1
def test_slack_api__client_5xx_doesnt_raise(mock_sleep, mock_secret_reader): """Don't raise an exception if the max retries aren't reached.""" uri_args = (httpretty.POST, "https://www.slack.com/api/users.list") uri_kwargs_failure = { "body": json.dumps({"ok": "false", "error": "internal_error"}), "status": 500, } uri_kwargs_success = {"body": json.dumps({"ok": "true"}), "status": 200} # These are registered LIFO (3 failures and then 1 success) httpretty.register_uri(*uri_args, **uri_kwargs_success) httpretty.register_uri(*uri_args, **uri_kwargs_failure) httpretty.register_uri(*uri_args, **uri_kwargs_failure) httpretty.register_uri(*uri_args, **uri_kwargs_failure) slack_client = SlackApi( "workspace", {"path": "some/path", "field": "some-field"}, init_usergroups=False ) slack_client._sc.api_call("users.list") assert len(httpretty.latest_requests()) == 4
def init_slack(slack_info, integration, init_usergroups=True): settings = queries.get_app_interface_settings() workspace_name = slack_info['workspace']['name'] slack_integrations = slack_info['workspace']['integrations'] slack_config = \ [i for i in slack_integrations if i['name'] == integration] [slack_config] = slack_config token = slack_config['token'] default_channel = slack_config['channel'] icon_emoji = slack_config['icon_emoji'] username = slack_config['username'] channel = slack_info.get('channel') or default_channel slack = SlackApi(workspace_name, token, settings=settings, init_usergroups=init_usergroups, channel=channel, icon_emoji=icon_emoji, username=username) return slack
def init_slack_map(unleash_instance): settings = queries.get_app_interface_settings() slack_notifications = unleash_instance['notifications']['slack'] slack_map = {} for slack_info in slack_notifications: workspace = slack_info['workspace'] workspace_name = workspace['name'] slack_integrations = workspace['integrations'] client_config = workspace.get('api_client') slack_config = \ [i for i in slack_integrations if i['name'] == QONTRACT_INTEGRATION] [slack_config] = slack_config token = slack_config['token'] channel = slack_info['channel'] icon_emoji = slack_info['icon_emoji'] username = slack_info['username'] slack_api_kwargs = { 'settings': settings, 'init_usergroups': False, 'channel': channel, 'icon_emoji': icon_emoji, 'username': username } if client_config: slack_api_kwargs['api_config'] = \ SlackApiConfig.from_dict(client_config) slack = SlackApi(workspace_name, token, **slack_api_kwargs) slack_map[channel] = slack return slack_map