-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel_router.py
More file actions
155 lines (126 loc) · 4.86 KB
/
channel_router.py
File metadata and controls
155 lines (126 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
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.integrations.zammad import ZammadConfig
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:
# Github
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,
)
# Zammad
billing_channel = DiscordChannel(
channel_id=settings.DISCORD_BILLING_CHANNEL_ID,
channel_name=settings.DISCORD_BILLING_CHANNEL_NAME,
)
helpdesk_channel = DiscordChannel(
channel_id=settings.DISCORD_HELPDESK_CHANNEL_ID,
channel_name=settings.DISCORD_HELPDESK_CHANNEL_NAME,
)
volunteers_channel = DiscordChannel(
channel_id=settings.DISCORD_VOLUNTEERS_CHANNEL_ID,
channel_name=settings.DISCORD_VOLUNTEERS_CHANNEL_NAME,
)
programme_channel = DiscordChannel(
channel_id=settings.DISCORD_PROGRAMME_CHANNEL_ID,
channel_name=settings.DISCORD_PROGRAMME_CHANNEL_NAME,
)
finaid_channel = DiscordChannel(
channel_id=settings.DISCORD_FINAID_CHANNEL_ID,
channel_name=settings.DISCORD_FINAID_CHANNEL_NAME,
)
sponsors_channel = DiscordChannel(
channel_id=settings.DISCORD_SPONSORS_CHANNEL_ID,
channel_name=settings.DISCORD_SPONSORS_CHANNEL_NAME,
)
grants_channel = DiscordChannel(
channel_id=settings.DISCORD_GRANTS_CHANNEL_ID,
channel_name=settings.DISCORD_GRANTS_CHANNEL_NAME,
)
# scheduled messages
standup_channel = DiscordChannel(
channel_id=settings.DISCORD_STANDUP_CHANNEL_ID,
channel_name=settings.DISCORD_STANDUP_CHANNEL_NAME,
)
def discord_channel_router(wh: Webhook) -> DiscordChannel:
if wh.source == "github":
return github_router(wh)
elif wh.source == "zammad":
return zammad_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 zammad_router(wh: Webhook) -> DiscordChannel:
groups = {
ZammadConfig.helpdesk_group: Channels.helpdesk_channel,
ZammadConfig.volunteers_group: Channels.volunteers_channel,
ZammadConfig.billing_group: Channels.billing_channel,
ZammadConfig.programme_group: Channels.programme_channel,
ZammadConfig.finaid_group: Channels.finaid_channel,
ZammadConfig.sponsors_group: Channels.sponsors_channel,
ZammadConfig.grants_group: Channels.grants_channel,
}
if channel := groups.get(wh.extra["group"]):
return channel
# If it doesn't match any of the groups, just skip it
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