-
Notifications
You must be signed in to change notification settings - Fork 4
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
Feat: add discord webhook helpers #1045
Merged
Merged
Changes from all commits
Commits
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 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 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,65 @@ | ||
from typing import Any, Optional, TypedDict | ||
|
||
import requests | ||
import os | ||
|
||
from kernelCI_app.helpers.logger import log_message | ||
|
||
# For more information on discord webhook structure, visit | ||
# https://discord.com/developers/docs/resources/webhook#execute-webhook | ||
|
||
AVATAR_URL = "https://avatars.githubusercontent.com/u/11725450?s=200&v=4" | ||
WEBHOOK_NAME = "KernelCI Dashboard Notifications" | ||
|
||
|
||
class DiscordImage(TypedDict): | ||
url: str | ||
width: Optional[int] | ||
height: Optional[int] | ||
|
||
|
||
class DiscordEmbed(TypedDict): | ||
title: str | ||
description: Optional[str] | ||
url: Optional[str] | ||
image: Optional[DiscordImage] | ||
|
||
|
||
def send_discord_notification( | ||
*, | ||
content: Optional[str] = None, | ||
embeds: Optional[list[DiscordEmbed]] = None, | ||
avatar_url: Optional[str] = AVATAR_URL, | ||
webhook_name: Optional[str] = WEBHOOK_NAME, | ||
) -> None: | ||
url = os.getenv("DISCORD_WEBHOOK_URL") | ||
if not url: | ||
log_message("DISCORD_WEBHOOK_URL environment variable is not set.") | ||
return | ||
|
||
if not content and not embeds: | ||
log_message( | ||
"Either content or embeds must be set in order to send notifications." | ||
) | ||
return | ||
|
||
if embeds is not None and len(embeds) > 10: | ||
log_message("The embed list can contain at most 10 elements.") | ||
return | ||
|
||
data: dict[str, Any] = { | ||
"avatar_url": avatar_url, | ||
"username": webhook_name, | ||
} | ||
if content is not None: | ||
data["content"] = content | ||
if embeds is not None: | ||
data["embeds"] = embeds | ||
|
||
try: | ||
result = requests.post(url=url, json=data) | ||
result.raise_for_status() | ||
except requests.HTTPError as e: | ||
log_message(e) | ||
|
||
return |
This file contains 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 |
---|---|---|
@@ -1,4 +1,23 @@ | ||
from django.http import HttpRequest | ||
from datetime import datetime | ||
|
||
|
||
# For logging that we care about, we create a function so we can easily use | ||
# a more sophisticated logging library later. | ||
def log_message(message: str) -> None: | ||
print(message) | ||
|
||
|
||
def create_endpoint_notification(*, message: str, request: HttpRequest) -> str: | ||
return ( | ||
message | ||
+ "\n\nEndpoint:\n" | ||
+ request.build_absolute_uri() | ||
+ ( | ||
("\nBody:\n```json\n" + request.body.decode("utf-8") + "```") | ||
if request.body | ||
else "" | ||
) | ||
+ "\nAccessed in: " | ||
+ datetime.now().strftime("%Y-%m-%d %H:%M:%S") | ||
) |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the env description to the backend README file