Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
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
18 changes: 18 additions & 0 deletions intbot/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json

import pytest
from django.conf import settings


@pytest.fixture(scope="session")
def gh_data():
base_path = settings.BASE_DIR / "tests" / "test_integrations" / "github"

return {
"project_v2_item.edited": json.load(
open(base_path / "project_v2_item.edited.json"),
),
"query_result": json.load(
open(base_path / "query_result.json"),
)["data"]["node"],
}
100 changes: 100 additions & 0 deletions intbot/core/bot/channel_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""
Channel router that decides where to send particular message
"""

from dataclasses import dataclass

from core.integrations.github import (
GithubProjects,
GithubRepositories,
parse_github_webhook,
)
from core.models import Webhook
from django.conf import settings


@dataclass
class DiscordChannel:
channel_id: str
channel_name: str


dont_send_it = DiscordChannel(channel_id="0", channel_name="/dev/null")


class Channels:
test_channel = DiscordChannel(
channel_id=settings.DISCORD_TEST_CHANNEL_ID,
channel_name=settings.DISCORD_TEST_CHANNEL_NAME,
)

board_channel = DiscordChannel(
channel_id=settings.DISCORD_BOARD_CHANNEL_ID,
channel_name=settings.DISCORD_BOARD_CHANNEL_NAME,
)
ep2025_channel = DiscordChannel(
channel_id=settings.DISCORD_EP2025_CHANNEL_ID,
channel_name=settings.DISCORD_EP2025_CHANNEL_NAME,
)
em_channel = DiscordChannel(
channel_id=settings.DISCORD_EM_CHANNEL_ID,
channel_name=settings.DISCORD_EM_CHANNEL_NAME,
)
website_channel = DiscordChannel(
channel_id=settings.DISCORD_WEBSITE_CHANNEL_ID,
channel_name=settings.DISCORD_WEBSITE_CHANNEL_NAME,
)
bot_channel = DiscordChannel(
channel_id=settings.DISCORD_BOT_CHANNEL_ID,
channel_name=settings.DISCORD_BOT_CHANNEL_NAME,
)


def discord_channel_router(wh: Webhook) -> DiscordChannel:
if wh.source == "github":
return github_router(wh)

elif wh.source == "internal":
return internal_router(wh)

return dont_send_it


def github_router(wh: Webhook) -> DiscordChannel:
gwh = parse_github_webhook(wh)
project = gwh.get_project()
repository = gwh.get_repository()

# We have three github projects, that we want to route to three different
# channels - one for ep2025, one for EM, and one for the board.
if project.id == GithubProjects.board_project:
return Channels.board_channel

elif project.id == GithubProjects.ep2025_project:
return Channels.ep2025_channel

elif project.id == GithubProjects.em_project:
return Channels.em_channel

else:
...

# Then we have our open source repositories, like website, this bot, and
# some others, that we also might want to route to different channels
if repository == GithubRepositories.website_repo:
return Channels.website_channel

elif repository == GithubRepositories.bot_repo:
return Channels.bot_channel

elif repository == ...:
...

# Finally, we can use this to drop notifications that we don't want to
# support, by routing them to /dev/null
return dont_send_it


def internal_router(wh: Webhook) -> DiscordChannel:
# For now just send all the internal messages to a test channel
return Channels.test_channel
2 changes: 2 additions & 0 deletions intbot/core/endpoints/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def internal_webhook_endpoint(request):
wh = Webhook.objects.create(
source="internal",
content=json.loads(request.body),
extra={},
)
process_webhook.enqueue(str(wh.uuid))

Expand Down Expand Up @@ -58,6 +59,7 @@ def github_webhook_endpoint(request):
meta=github_headers,
signature=signature,
content=json.loads(request.body),
extra={},
)
process_webhook.enqueue(str(wh.uuid))
return JsonResponse({"status": "ok"})
Expand Down
Loading
Loading