|
| 1 | +""" |
| 2 | +Channel router that decides where to send particular message |
| 3 | +""" |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | + |
| 7 | +from core.integrations.github import ( |
| 8 | + GithubProjects, |
| 9 | + GithubRepositories, |
| 10 | + parse_github_webhook, |
| 11 | +) |
| 12 | +from core.models import Webhook |
| 13 | +from django.conf import settings |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class DiscordChannel: |
| 18 | + channel_id: str |
| 19 | + channel_name: str |
| 20 | + |
| 21 | + |
| 22 | +dont_send_it = DiscordChannel(channel_id="0", channel_name="DONT_SEND_IT") |
| 23 | + |
| 24 | + |
| 25 | +class Channels: |
| 26 | + test_channel = DiscordChannel( |
| 27 | + channel_id=settings.DISCORD_TEST_CHANNEL_ID, |
| 28 | + channel_name=settings.DISCORD_TEST_CHANNEL_NAME, |
| 29 | + ) |
| 30 | + |
| 31 | + board_channel = DiscordChannel( |
| 32 | + channel_id=settings.DISCORD_BOARD_CHANNEL_ID, |
| 33 | + channel_name=settings.DISCORD_BOARD_CHANNEL_NAME, |
| 34 | + ) |
| 35 | + ep2025_channel = DiscordChannel( |
| 36 | + channel_id=settings.DISCORD_EP2025_CHANNEL_ID, |
| 37 | + channel_name=settings.DISCORD_EP2025_CHANNEL_NAME, |
| 38 | + ) |
| 39 | + em_channel = DiscordChannel( |
| 40 | + channel_id=settings.DISCORD_EM_CHANNEL_ID, |
| 41 | + channel_name=settings.DISCORD_EM_CHANNEL_NAME, |
| 42 | + ) |
| 43 | + website_channel = DiscordChannel( |
| 44 | + channel_id=settings.DISCORD_WEBSITE_CHANNEL_ID, |
| 45 | + channel_name=settings.DISCORD_WEBSITE_CHANNEL_NAME, |
| 46 | + ) |
| 47 | + bot_channel = DiscordChannel( |
| 48 | + channel_id=settings.DISCORD_BOT_CHANNEL_ID, |
| 49 | + channel_name=settings.DISCORD_BOT_CHANNEL_NAME, |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def discord_channel_router(wh: Webhook) -> DiscordChannel: |
| 54 | + if wh.source == "github": |
| 55 | + return github_router(wh) |
| 56 | + |
| 57 | + elif wh.source == "internal": |
| 58 | + return internal_router(wh) |
| 59 | + |
| 60 | + return dont_send_it |
| 61 | + |
| 62 | + |
| 63 | +def github_router(wh: Webhook) -> DiscordChannel: |
| 64 | + gwh = parse_github_webhook(wh) |
| 65 | + project = gwh.get_project() |
| 66 | + repository = gwh.get_repository() |
| 67 | + |
| 68 | + # We have three github projects, that we want to route to three different |
| 69 | + # channels - one for ep2025, one for EM, and one for the board. |
| 70 | + PROJECTS = { |
| 71 | + GithubProjects.board_project: Channels.board_channel, |
| 72 | + GithubProjects.ep2025_project: Channels.ep2025_channel, |
| 73 | + GithubProjects.em_project: Channels.em_channel, |
| 74 | + } |
| 75 | + |
| 76 | + if channel := PROJECTS.get(project.id): |
| 77 | + return channel |
| 78 | + |
| 79 | + # Then we have our open source repositories, like website, this bot, and |
| 80 | + # some others, that we also might want to route to different channels |
| 81 | + REPOSITORIES = { |
| 82 | + GithubRepositories.website_repo: Channels.website_channel, |
| 83 | + GithubRepositories.bot_repo: Channels.bot_channel, |
| 84 | + } |
| 85 | + |
| 86 | + if channel := REPOSITORIES.get(repository.id): |
| 87 | + return channel |
| 88 | + |
| 89 | + # Finally, we can use this to drop notifications that we don't want to |
| 90 | + # support, by not sending them. |
| 91 | + return dont_send_it |
| 92 | + |
| 93 | + |
| 94 | +def internal_router(wh: Webhook) -> DiscordChannel: |
| 95 | + # For now just send all the internal messages to a test channel |
| 96 | + return Channels.test_channel |
0 commit comments