Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ jobs:
run: |
docker run --rm \
-e DJANGO_SETTINGS_MODULE=intbot.settings \
-e DATABASE_URL=postgres://testuser:testpassword@localhost:5432/testdb \
--network host \
intbot \
make in-container/tests
Expand Down
14 changes: 14 additions & 0 deletions deploy/templates/app/intbot.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@ POSTGRES_PASSWORD="RandomPasswordPleaseChange"
DISCORD_BOT_TOKEN="Token Goes Here"
DISCORD_TEST_CHANNEL_ID="123123123123123123123123"
DISCORD_TEST_CHANNEL_NAME="#test-channel"
DISCORD_BOARD_CHANNEL_ID="DISCORD_BOARD_CHANNEL_ID"
DISCORD_BOARD_CHANNEL_NAME="DISCORD_BOARD_CHANNEL_NAME"
DISCORD_EP2025_CHANNEL_ID="DISCORD_EP2025_CHANNEL_ID"
DISCORD_EP2025_CHANNEL_NAME="DISCORD_EP2025_CHANNEL_NAME"
DISCORD_EM_CHANNEL_ID="DISCORD_EM_CHANNEL_ID"
DISCORD_EM_CHANNEL_NAME="DISCORD_EM_CHANNEL_NAME"
DISCORD_WEBSITE_CHANNEL_ID="DISCORD_WEBSITE_CHANNEL_ID"
DISCORD_WEBSITE_CHANNEL_NAME="DISCORD_WEBSITE_CHANNEL_NAME"
DISCORD_BOT_CHANNEL_ID="DISCORD_BOT_CHANNEL_ID"
DISCORD_BOT_CHANNEL_NAME="DISCORD_BOT_CHANNEL_NAME"

# Webhooks
WEBHOOK_INTERNAL_TOKEN="asdf"

# Github
GITHUB_API_TOKEN="github-api-token-goes-here"
GITHUB_WEBHOOK_SECRET_TOKEN="github-webhook-secret-token"

GITHUB_BOARD_PROJECT_ID="GITHUB_BOARD_PROJECT_ID"
GITHUB_EP2025_PROJECT_ID="GITHUB_EP2025_PROJECT_ID"
GITHUB_EM_PROJECT_ID="GITHUB_EM_PROJECT_ID"
19 changes: 19 additions & 0 deletions intbot/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json

import pytest
from django.conf import settings


@pytest.fixture(scope="session")
def github_data():
"""Pytest fixture with examples of webhooks from github"""
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"],
}
96 changes: 96 additions & 0 deletions intbot/core/bot/channel_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
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="DONT_SEND_IT")


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.
PROJECTS = {
GithubProjects.board_project: Channels.board_channel,
GithubProjects.ep2025_project: Channels.ep2025_channel,
GithubProjects.em_project: Channels.em_channel,
}

if channel := PROJECTS.get(project.id):
return channel

# Then we have our open source repositories, like website, this bot, and
# some others, that we also might want to route to different channels
REPOSITORIES = {
GithubRepositories.website_repo: Channels.website_channel,
GithubRepositories.bot_repo: Channels.bot_channel,
}

if channel := REPOSITORIES.get(repository.id):
return channel

# Finally, we can use this to drop notifications that we don't want to
# support, by not sending them.
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