-
Notifications
You must be signed in to change notification settings - Fork 0
Discord channel router v1 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
76be383
initial router placeholder
artcz 58fc33e
first semi-working version of the discord channel router
artcz 5672fc8
make lint/fix
artcz 1fcbe8b
make tests pass
artcz d1e15bd
improve test coverage
artcz 208d167
fix lint
artcz 6129156
fix grammar
artcz 6ef6519
fix type check
artcz 8a301c6
slightly nicer fix for mypy
artcz 16cd3fe
remove uncesseary DATABASE_URL from ci
artcz b29341f
Update intbot/core/integrations/github.py
artcz 8727a54
review feedback
artcz a804f5a
more review feedback
artcz 880ac0e
update env example for deployment
artcz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
artcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.