Skip to content

Commit

Permalink
add slack id env
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuyuutsu committed Dec 19, 2024
1 parent 451e374 commit 367a51d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 81 deletions.
1 change: 1 addition & 0 deletions ci/output-results-to-slack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ params:
OUTPUT_BUCKET:
OUTPUT_DIR:
SLACK_CHANNEL_NAME:
SLACK_CHANNEL_ID:
NUMBER_OF_DAYS:
SERVICE_ACCOUNT_JSON: ((gcp.service_account_json))
SLACK_AUTH_TOKEN: ((eq-ons-slack-app.bot_user_oauth_token))
Expand Down
31 changes: 8 additions & 23 deletions scripts/slack_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def parse_environment_variables():
print("'SLACK_CHANNEL_NAME' environment variable must be provided")
sys.exit(1)

slack_channel_id = os.getenv("SLACK_CHANNEL_ID")
if not slack_channel_id:
print("'SLACK_CHANNEL_ID' environment variable must be provided")
sys.exit(1)

content = os.getenv("CONTENT")
attachment_filename = os.getenv("ATTACHMENT_FILENAME")
if content and attachment_filename:
Expand All @@ -42,6 +47,7 @@ def parse_environment_variables():
return {
"slack_auth_token": slack_auth_token,
"slack_channel": slack_channel,
"slack_channel_id": slack_channel_id,
"content": content,
"attachment_filename": attachment_filename,
"file_type": file_type,
Expand All @@ -50,28 +56,6 @@ def parse_environment_variables():
}


def get_channel_id(client, channel_name):
try:
conversation_data = client.conversations_list(
filter="public_channel,private_channel"
)
if not conversation_data.get("ok", False):
print("Failed to fetch channels")
sys.exit(2)

channels = conversation_data.get("channels", [])
for channel in channels:
if channel.get("name") == channel_name:
return channel.get("id")

print(f"Channel: '{channel_name}' not found")
sys.exit(1)

except SlackApiError as e:
print(f'Error fetching channel list \nError: {e.response["error"]}')
sys.exit(2)


def post_slack_notification(
slack_auth_token,
slack_channel,
Expand All @@ -80,9 +64,10 @@ def post_slack_notification(
file_type,
initial_comment,
title,
slack_channel_id,
):
client = slack.WebClient(token=slack_auth_token)
slack_channel_id = get_channel_id(client, slack_channel)

try:
if content:
response = client.files_upload_v2(
Expand Down
76 changes: 18 additions & 58 deletions tests/test_slack_notification.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from scripts.slack_notification import (
get_channel_id,
parse_environment_variables,
post_slack_notification,
)
Expand All @@ -11,6 +10,7 @@ def test_parse_environment_variables(monkeypatch):
monkeypatch.setenv("SLACK_AUTH_TOKEN", "token")
monkeypatch.setenv("SLACK_CHANNEL_NAME", "test-alerts")
monkeypatch.setenv("CONTENT", "Slack message")
monkeypatch.setenv("SLACK_CHANNEL_ID", "C12345")

slack_environment_variables = parse_environment_variables()

Expand All @@ -21,13 +21,15 @@ def test_parse_environment_variables(monkeypatch):
"initial_comment": "",
"slack_auth_token": "token",
"slack_channel": "test-alerts",
"slack_channel_id": "C12345",
"title": "",
}


def test_parse_environment_variables_missing_slack_token(monkeypatch):
monkeypatch.setenv("SLACK_CHANNEL_NAME", "test-alerts")
monkeypatch.setenv("CONTENT", "Slack message")
monkeypatch.setenv("SLACK_CHANNEL_ID", "C12345")

with pytest.raises(SystemExit):
parse_environment_variables()
Expand All @@ -36,6 +38,14 @@ def test_parse_environment_variables_missing_slack_token(monkeypatch):
def test_parse_environment_variables_missing_slack_channel_name(monkeypatch):
monkeypatch.setenv("SLACK_AUTH_TOKEN", "token")
monkeypatch.setenv("CONTENT", "Slack message")
monkeypatch.setenv("SLACK_CHANNEL_ID", "C12345")
with pytest.raises(SystemExit):
parse_environment_variables()

def test_parse_environment_variables_missing_slack_channel_id(monkeypatch):
monkeypatch.setenv("SLACK_AUTH_TOKEN", "token")
monkeypatch.setenv("CONTENT", "Slack message")
monkeypatch.setenv("SLACK_CHANNEL_NAME", "test-alerts")

with pytest.raises(SystemExit):
parse_environment_variables()
Expand All @@ -44,6 +54,7 @@ def test_parse_environment_variables_missing_slack_channel_name(monkeypatch):
def test_parse_environment_variables_content_and_attachment_filename_set(monkeypatch):
monkeypatch.setenv("SLACK_AUTH_TOKEN", "token")
monkeypatch.setenv("SLACK_CHANNEL_NAME", "test-alerts")
monkeypatch.setenv("SLACK_CHANNEL_ID", "C12345")
monkeypatch.setenv("CONTENT", "Slack message")
monkeypatch.setenv("ATTACHMENT_FILENAME", "file_name")

Expand All @@ -54,6 +65,7 @@ def test_parse_environment_variables_content_and_attachment_filename_set(monkeyp
def test_parse_environment_variables_no_content_or_attachment_filename_set(monkeypatch):
monkeypatch.setenv("SLACK_AUTH_TOKEN", "token")
monkeypatch.setenv("SLACK_CHANNEL_NAME", "test-alerts")
monkeypatch.setenv("SLACK_CHANNEL_ID", "C12345")

with pytest.raises(SystemExit):
parse_environment_variables()
Expand All @@ -63,6 +75,7 @@ def test_parse_environment_variables_attachment_filename_not_valid(monkeypatch):
monkeypatch.setenv("SLACK_AUTH_TOKEN", "token")
monkeypatch.setenv("SLACK_CHANNEL_NAME", "test-alerts")
monkeypatch.setenv("ATTACHMENT_FILENAME", "file_name")
monkeypatch.setenv("SLACK_CHANNEL_ID", "C12345")

with pytest.raises(SystemExit):
parse_environment_variables()
Expand All @@ -86,6 +99,7 @@ def test_post_slack_notification_with_ok_response_raises_no_error(mocker):
post_slack_notification(
slack_auth_token="token",
slack_channel="test-alerts",
slack_channel_id="C12345",
content="slack message",
file_type="type",
title="title",
Expand All @@ -97,23 +111,13 @@ def test_post_slack_notification_with_ok_response_raises_no_error(mocker):
def test_post_slack_notification_with_no_content_and_ok_response_raises_no_error(
mocker,
):
mocker.patch(
"slack_sdk.web.client.WebClient.conversations_list",
side_effect=[
{
"ok": True,
"channels": [{"id": "C12345", "name": "test-alerts"}],
"response_metadata": {"next_cursor": ""},
}
],
)

mocker.patch(
"slack_sdk.web.client.WebClient.files_upload_v2", return_value={"ok": True}
)
post_slack_notification(
slack_auth_token="token",
slack_channel="test-alerts",
slack_channel_id="C12345",
file_type="type",
title="title",
content=None,
Expand All @@ -123,7 +127,6 @@ def test_post_slack_notification_with_no_content_and_ok_response_raises_no_error


def test_post_slack_notification_with_bad_response_raises_error(mocker):
mocker.patch("scripts.slack_notification.get_channel_id", return_value="C12345")
mocker.patch(
"slack_sdk.web.client.WebClient.files_upload_v2", return_value={"ok": False}
)
Expand All @@ -132,6 +135,7 @@ def test_post_slack_notification_with_bad_response_raises_error(mocker):
post_slack_notification(
slack_auth_token="token",
slack_channel="test-alerts",
slack_channel_id="C12345",
content="slack message",
file_type="type",
title="title",
Expand All @@ -141,58 +145,14 @@ def test_post_slack_notification_with_bad_response_raises_error(mocker):


def test_post_slack_notification_with_api_error_exits(mocker):
mocker.patch("scripts.slack_notification.get_channel_id", return_value="C12345")
with pytest.raises(SystemExit):
post_slack_notification(
slack_auth_token="token",
slack_channel="test-alerts",
slack_channel_id="C12345",
content="slack message",
file_type="type",
title="title",
initial_comment="comment",
attachment_filename="file",
)


def test_get_channel_id_with_ok_response_raises_no_error(mocker):
mock_response = {
"ok": True,
"channels": [
{"id": "C12345678", "name": "test-alert"},
{"id": "C87654321", "name": "general"},
],
}

mock_client = mocker.Mock()

mock_client.conversations_list.return_value = mock_response

channel_id = get_channel_id(mock_client, "test-alert")

assert channel_id == "C12345678"


def test_get_channel_id_channel_not_found(mocker):
mock_response = {
"ok": True,
"channels": [
{"id": "C12345678", "name": "test-alert"},
{"id": "C87654321", "name": "general"},
],
}

mock_client = mocker.Mock()
mock_client.conversations_list.return_value = mock_response

with pytest.raises(SystemExit):
get_channel_id(mock_client, "random-channel")


def test_get_channel_id_fails_to_fetch_channels(mocker):
mock_response = {"ok": False, "error": "invalid_auth"}

mock_client = mocker.Mock()
mock_client.conversations_list.return_value = mock_response

with pytest.raises(SystemExit):
get_channel_id(mock_client, "test-alert")

0 comments on commit 367a51d

Please sign in to comment.