Skip to content
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

feat: support exempt notification user #2805

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion saas/backend/component/bkbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from .constants import ComponentEnum
from .http import http_post
from .util import do_blueking_http_request
from .util import do_blueking_http_request, remove_notification_exemption_user


def _call_bk_bot_approval_api(http_func, url_path, data, timeout=30):
Expand All @@ -31,5 +31,10 @@ def _call_bk_bot_approval_api(http_func, url_path, data, timeout=30):


def send_iam_ticket(data):
# 移除豁免的用户,如果为空,则直接返回
data["approvers"] = ",".join(remove_notification_exemption_user(data["approvers"].split(",")))
if not data["approvers"]:
nannan00 marked this conversation as resolved.
Show resolved Hide resolved
return {}

url_path = "/iam_app_ticket/"
return _call_bk_bot_approval_api(http_post, url_path, data=data)
7 changes: 6 additions & 1 deletion saas/backend/component/esb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from .constants import ComponentEnum
from .http import http_get, http_post
from .util import do_blueking_http_request
from .util import do_blueking_http_request, remove_notification_exemption_user


def _call_esb_api(http_func, url_path, data, timeout=30, request_session=None):
Expand Down Expand Up @@ -49,6 +49,11 @@ def get_api_public_key() -> Dict:

def send_mail(username, title, content, body_format="Html"):
"""发送邮件"""
# 移除豁免的用户,如果为空,则直接返回
username = ",".join(remove_notification_exemption_user(username.split(",")))
if not username:
nannan00 marked this conversation as resolved.
Show resolved Hide resolved
return None

url_path = "/api/c/compapi/cmsi/send_mail/"
data = {"receiver__username": username, "title": title, "content": content, "body_format": body_format}
return _call_esb_api(http_post, url_path, data=data)
13 changes: 13 additions & 0 deletions saas/backend/component/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from typing import Any, Callable, Dict, List, Optional, Tuple
from urllib.parse import urlparse

from django.conf import settings

from backend.common.error_codes import error_codes
from backend.common.local import local

Expand Down Expand Up @@ -124,3 +126,14 @@ def do_blueking_http_request(
f"Request=[{http_func.__name__} {urlparse(url).path} request_id={local.request_id}] "
f"Response[code={code}, message={message}]"
)


def remove_notification_exemption_user(usernames: List[str]) -> List[str]:
"""
从给定的用户列表里移除豁免通知的人员
:param usernames: 待处理的用户名列表
:return: 处理后的用户名列表
"""
exemption_user_set = {u.strip().lower() for u in settings.BK_NOTIFICATION_EXEMPTION_USERS if u.strip()}

return [u for u in usernames if u.strip().lower() not in exemption_user_set]
2 changes: 2 additions & 0 deletions saas/config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@
# BK BOT approval审批机器人通知
BK_IAM_BOT_APPROVAL_CALLBACK_APIGW_URL = env.str("BK_IAM_BOT_APPROVAL_CALLBACK_APIGW_URL", default="")

# 通知的豁免名单,企业内部分人员不接收通知
BK_NOTIFICATION_EXEMPTION_USERS = env.list("BK_NOTIFICATION_EXEMPTION_USERS", default="")
nannan00 marked this conversation as resolved.
Show resolved Hide resolved

# 文档地址
BK_DOCS_URL_PREFIX = env.str("BK_DOCS_URL_PREFIX", default="https://bk.tencent.com/docs/")
Expand Down
10 changes: 10 additions & 0 deletions saas/tests/component/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
35 changes: 35 additions & 0 deletions saas/tests/component/util_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import pytest
from django.test.utils import override_settings

from backend.component.util import remove_notification_exemption_user


@pytest.mark.parametrize(
"usernames, expected",
[
# Case 1: List without any exempt users
(["user1", "user2"], ["user1", "user2"]),
# Case 2: List with some exempt users
(["exempt_user1", "user1", "exempt_user2", "user2"], ["user1", "user2"]),
# Case 3: List with only exempt users
(["exempt_user1", "exempt_user2"], []),
# Case 4: List with mixed cases and spaces
(["Exempt_User1", " user1 ", "ExEmPt_UsEr2", " user2 ", " exempt_user3 "], [" user1 ", " user2 "]),
# Case 5: Empty list
([], []),
],
)
def test_remove_notification_exemption_user(usernames, expected):
with override_settings(BK_NOTIFICATION_EXEMPTION_USERS=["exempt_user1", "exempt_user2", "exempt_user3 "]):
result = remove_notification_exemption_user(usernames)
assert result == expected
Loading