diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index d6280de..cb089bf 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -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 diff --git a/deploy/templates/app/intbot.env.example b/deploy/templates/app/intbot.env.example index cf0f3d4..2fbdb49 100644 --- a/deploy/templates/app/intbot.env.example +++ b/deploy/templates/app/intbot.env.example @@ -13,6 +13,16 @@ 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" @@ -20,3 +30,7 @@ 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" diff --git a/intbot/conftest.py b/intbot/conftest.py new file mode 100644 index 0000000..d3fd4d9 --- /dev/null +++ b/intbot/conftest.py @@ -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"], + } diff --git a/intbot/core/bot/channel_router.py b/intbot/core/bot/channel_router.py new file mode 100644 index 0000000..66996d5 --- /dev/null +++ b/intbot/core/bot/channel_router.py @@ -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 diff --git a/intbot/core/endpoints/webhooks.py b/intbot/core/endpoints/webhooks.py index 94a8d4a..6ae2cf6 100644 --- a/intbot/core/endpoints/webhooks.py +++ b/intbot/core/endpoints/webhooks.py @@ -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)) @@ -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"}) diff --git a/intbot/core/integrations/github.py b/intbot/core/integrations/github.py index 7cb217b..09532be 100644 --- a/intbot/core/integrations/github.py +++ b/intbot/core/integrations/github.py @@ -1,17 +1,35 @@ -import dataclasses +""" +The Github Integration is mostly about parsing Github Webhooks. + +This is split into three parts. + +1. Save the webhook in the database (not in this file) +2. Prep webhook - download extra data that's missing in the webhook +3. Proces webhook - usually means create a discord message with a notification +""" + +from typing import Any import httpx +from core.models import Webhook from django.conf import settings +from pydantic import BaseModel GITHUB_API_URL = "https://api.github.com/graphql" # GraphQL query -query = """ +project_item_details_query = """ query($itemId: ID!) { node(id: $itemId) { ... on ProjectV2Item { id + project { + id + title + url + } content { + __typename ... on DraftIssue { id title @@ -21,32 +39,41 @@ title url } - } + } } } } """ -def parse_github_webhook(headers: dict, content: dict) -> tuple[str, str]: - event = headers["X-Github-Event"] +class GithubRepositories: + website_repo = ... + bot_repo = ... - if event == "projects_v2_item": - parser = GithubProjectV2Item(content) - formatted = parser.as_str() - action = parser.action() - event_action = f"{event}.{action}" - return formatted, event_action - elif event == "...": - return "", "" +class GithubProjects: + board_project = settings.GITHUB_BOARD_PROJECT_ID + ep2025_project = settings.GITHUB_EP2025_PROJECT_ID + em_project = settings.GITHUB_EM_PROJECT_ID - else: - raise ValueError(f"Event `{event}` not supported") + +class GithubProject(BaseModel): + id: str + title: str + url: str -@dataclasses.dataclass -class GithubIssue: +class GithubRepository(BaseModel): + id: str + name: str + + +class GithubSender(BaseModel): + login: str + html_url: str + + +class GithubIssue(BaseModel): id: str title: str url: str @@ -55,127 +82,179 @@ def as_discord_message(self): return f"[{self.title}]({self.url})" -@dataclasses.dataclass -class GithubDraftIssue: +class GithubDraftIssue(BaseModel): id: str title: str def as_discord_message(self): return self.title +JsonType = dict[str, Any] -def fetch_github_item_details(item_id): - headers = { - "Authorization": f"Bearer {settings.GITHUB_API_TOKEN}", - "Content-Type": "application/json", - } - payload = {"query": query, "variables": {"itemId": item_id}} - response = httpx.post(GITHUB_API_URL, json=payload, headers=headers) - if response.status_code == 200: - return response.json()["data"]["node"]["content"] - else: - raise Exception(f"GitHub API error: {response.status_code} - {response.text}") +class GithubWebhook: + """ + Base class for all the other specific types of webhooks. + """ + + def __init__(self, action: str, headers: JsonType, content: JsonType, extra: JsonType): + self.action = action + self.headers = headers + self.content = content + self.extra = extra + self._project = None + self._repository = None + + @classmethod + def from_webhook(cls, wh: Webhook): + assert wh.extra, "Extra should be set already at this point" + return cls( + content=wh.content, + headers=wh.meta, + extra=wh.extra, + action=wh.event, + ) + def get_project(self): # pragma: no cover + """Used in the discord channel router""" + raise NotImplementedError("Implement in child class") -CONTENT_TYPE_MAP = { - "Issue": GithubIssue, - "DraftIssue": GithubDraftIssue, -} + def get_repository(self): # pragma: no cover + """Used in the discord channel router""" + raise NotImplementedError("Implement in child class") -class GithubProjectV2Item: +class GithubProjectV2Item(GithubWebhook): # NOTE: This might be something for pydantic schemas in the future - def __init__(self, content: dict): - self.content = content + @property + def sender(self): + sender = self.get_sender() - def action(self): - if self.content["action"] == "edited": - action = "changed" - elif self.content["action"] == "created": - action = "created" - else: - raise ValueError(f"Action unsupported {self.content['action']}") + return f"[@{sender.login}]({sender.html_url})" - return action + def get_sender(self) -> GithubSender: + return GithubSender.model_validate(self.content["sender"]) - def sender(self): - login = self.content["sender"]["login"] - url = self.content["sender"]["html_url"] + def github_object(self) -> GithubDraftIssue | GithubIssue: + content = self.extra["content"] + typename = content.pop("__typename") - return f"[@{login}]({url})" + if typename == "Issue": + return GithubIssue.model_validate(content) + elif typename == "DraftIssue": + return GithubDraftIssue.model_validate(content) + else: + raise ValueError("Other types are not supported") - def content_type(self): - return self.content["projects_v2_item"]["content_type"] + def get_project(self) -> GithubProject: + return GithubProject.model_validate(self.extra["project"]) - def node_id(self): - # NOTE(artcz): This is relevant, because of how the graphql query above - # is constructed. - # Using node_id, which is an id of a ProjectV2Item we can get both - # DraftIssue and Issue from one query. - # If we use the content_node_id we would probably need two separate - # ways of getting that data. - return self.content["projects_v2_item"]["node_id"] + def get_repository(self) -> GithubRepository: + # Not relevant at the moment + return GithubRepository(name="Placeholder", id="placeholder-repo") def changes(self) -> dict: - if "changes" in self.content: - fv = self.content["changes"]["field_value"] - field_name = fv["field_name"] - field_type = fv["field_type"] - - if field_type == "date": - changed_from = ( - fv["from"].split("T")[0] if fv["from"] is not None else "None" - ) - changed_to = fv["to"].split("T")[0] if fv["to"] is not None else "None" - - elif field_type == "single_select": - changed_from = fv["from"]["name"] if fv["from"] is not None else "None" - changed_to = fv["to"]["name"] if fv["to"] is not None else "None" - - else: - changed_from = "None" - changed_to = "None" - - return { - "field": field_name, - "from": changed_from, - "to": changed_to, - } - return {} + # Early return! \o/ + if "changes" not in self.content: + # Fallback because some webhooks just don't have changes. + return {} - def as_discord_message(self, github_object: GithubDraftIssue | GithubIssue) -> str: - message = "{sender} {action} {details}".format + fv = self.content["changes"]["field_value"] + field_name = fv["field_name"] + field_type = fv["field_type"] - action = self.action() - sender = self.sender() + changed_from: str + changed_to: str + + if field_type == "date": + changed_from = ( + fv["from"].split("T")[0] if fv["from"] is not None else "None" + ) + changed_to = fv["to"].split("T")[0] if fv["to"] is not None else "None" + + elif field_type == "single_select": + changed_from = fv["from"]["name"] if fv["from"] is not None else "None" + changed_to = fv["to"]["name"] if fv["to"] is not None else "None" + + else: + changed_from = "None" + changed_to = "None" + + return { + "field": field_name, + "from": changed_from, + "to": changed_to, + } + + def as_discord_message(self) -> str: + message = "{sender} {action} {details}".format changes = self.changes() if changes: details = "**{field}** of **{obj}** from **{from}** to **{to}**".format( - **{"obj": github_object.as_discord_message(), **changes} + **{"obj": self.github_object().as_discord_message(), **changes} ) else: - details = github_object.as_discord_message() + details = self.github_object().as_discord_message() return message( **{ - "sender": sender, - "action": action, + "sender": self.sender, + "action": self.action, "details": details, } ) - def fetch_quoted_github_object(self) -> GithubIssue | GithubDraftIssue: - obj = fetch_github_item_details(self.node_id()) - obj = CONTENT_TYPE_MAP[self.content_type()](**obj) +def prep_github_webhook(wh: Webhook): + """ + Downloads the extra data that is missing in the webhook but needed for processing. + """ + event = wh.meta["X-Github-Event"] + + if event == "projects_v2_item": + node_id = wh.content["projects_v2_item"]["node_id"] + project_item = fetch_github_project_item(node_id) + wh.event = f"{event}.{wh.content['projects_v2_item']['action']}" + wh.extra = project_item + wh.save() + return wh + + raise ValueError(f"Event `{event}` not supported") - return obj - def as_str(self): - github_obj = self.fetch_quoted_github_object() - return self.as_discord_message(github_obj) +class GithubAPIError(Exception): + """Custom exception for GithubAPI Errors""" + pass + + +# Should we have a separate GithubClient that encapsulates this? +# Or at least a function that runs the request. +def fetch_github_project_item(item_id: str) -> dict[str, Any]: + headers = { + "Authorization": f"Bearer {settings.GITHUB_API_TOKEN}", + "Content-Type": "application/json", + } + payload = {"query": project_item_details_query, "variables": {"itemId": item_id}} + response = httpx.post(GITHUB_API_URL, json=payload, headers=headers) + + if response.status_code == 200: + return response.json()["data"]["node"] + else: + raise GithubAPIError(f"GitHub API error: {response.status_code} - {response.text}") + + +def parse_github_webhook(wh: Webhook): + event = wh.meta["X-Github-Event"] + if not wh.extra: + raise ValueError( + "Make sure the webhook is ready for pickup. See prep_github_webhook" + ) + + if event == "projects_v2_item": + return GithubProjectV2Item.from_webhook(wh) + + raise ValueError(f"Event not supported `{event}`") diff --git a/intbot/core/migrations/0003_added_extra_field_to_webhook.py b/intbot/core/migrations/0003_added_extra_field_to_webhook.py new file mode 100644 index 0000000..5897cc2 --- /dev/null +++ b/intbot/core/migrations/0003_added_extra_field_to_webhook.py @@ -0,0 +1,19 @@ +# Generated by Django 5.1.4 on 2025-01-24 21:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0002_test'), + ] + + operations = [ + migrations.AddField( + model_name='webhook', + name='extra', + field=models.JSONField(default={}), + preserve_default=False, + ), + ] diff --git a/intbot/core/models.py b/intbot/core/models.py index 6fa4f54..bdf9df3 100644 --- a/intbot/core/models.py +++ b/intbot/core/models.py @@ -18,6 +18,10 @@ class Webhook(models.Model): content = models.JSONField() + # Sometimes processing the webhook requires setting up or downloading extra + # information from other sources. This is the field to put that data in. + extra = models.JSONField() + created_at = models.DateTimeField(auto_now_add=True) modified_at = models.DateTimeField(auto_now=True) processed_at = models.DateTimeField(blank=True, null=True) diff --git a/intbot/core/tasks.py b/intbot/core/tasks.py index e683307..90487bb 100644 --- a/intbot/core/tasks.py +++ b/intbot/core/tasks.py @@ -1,14 +1,15 @@ import logging -from core.integrations.github import parse_github_webhook +from core.integrations.github import parse_github_webhook, prep_github_webhook +from core.bot.channel_router import discord_channel_router, dont_send_it from core.models import DiscordMessage, Webhook -from django.conf import settings from django.utils import timezone from django_tasks import task logger = logging.getLogger() + @task def process_webhook(wh_uuid: str): wh = Webhook.objects.get(uuid=wh_uuid) @@ -27,9 +28,13 @@ def process_internal_webhook(wh: Webhook): if wh.source != "internal": raise ValueError("Incorrect wh.source = {wh.source}") + channel = discord_channel_router(wh) + DiscordMessage.objects.create( - channel_id=settings.DISCORD_TEST_CHANNEL_ID, - channel_name=settings.DISCORD_TEST_CHANNEL_NAME, + channel_id=channel.channel_id, + channel_name=channel.channel_name, + # channel_id=settings.DISCORD_TEST_CHANNEL_ID, + # channel_name=settings.DISCORD_TEST_CHANNEL_NAME, content=f"Webhook content: {wh.content}", # Mark as not sent - to be sent with the next batch sent_at=None, @@ -43,22 +48,27 @@ def process_github_webhook(wh: Webhook): raise ValueError("Incorrect wh.source = {wh.source}") try: - message, event_action = parse_github_webhook( - headers=wh.meta, content=wh.content - ) + wh = prep_github_webhook(wh) except ValueError as e: # Downgrading to info because it's most likely event not supported logger.info(f"Not processing Github Webhook {wh.uuid}: {e}") return - # NOTE WHERE SHOULD WE GET THE CHANNEL ID FROM? + parsed = parse_github_webhook(wh) + channel = discord_channel_router(wh) + + if channel == dont_send_it: + wh.processed_at = timezone.now() + wh.save() + return + DiscordMessage.objects.create( - channel_id=settings.DISCORD_TEST_CHANNEL_ID, - channel_name=settings.DISCORD_TEST_CHANNEL_NAME, - content=f"GitHub: {message}", + channel_id=channel.channel_id, + channel_name=channel.channel_name, + content=f"GitHub: {parsed.message}", # Mark as unsend - to be sent with the next batch sent_at=None, ) - wh.event = event_action + wh.event = parsed.event_action wh.processed_at = timezone.now() wh.save() diff --git a/intbot/intbot/settings.py b/intbot/intbot/settings.py index 1e9463b..3d71806 100644 --- a/intbot/intbot/settings.py +++ b/intbot/intbot/settings.py @@ -116,6 +116,56 @@ # Just to make mypy happy TASKS: dict[str, Any] + +# There are bunch of settings that we can skip on dev/testing environments if +# not used - that should be always present on prod/staging deployments. +# Instead of repeating them per-env below, they go here. +def get(name) -> str: + value = os.environ.get(name) + + if DJANGO_ENV == "prod": + if value is None: + raise ValueError(f"{name} is not set") + + elif DJANGO_ENV == "test": + # For tests we hardcode below, and if something is missing the tests + # will fail. + pass + else: + warnings.warn(f"{name} not set") + + return value or "" + +# Discord +# This is only needed if you end up running the bot locally, hence it +# doesn't fail explicilty – however it does emit a warning. +# Please check the documentation and/or current online guides how to get +# one from the developer portal. +# If you run it locally, you probably want to run it against your own test +# bot and a test server. +DISCORD_BOT_TOKEN = get("DISCORD_BOT_TOKEN") + +DISCORD_TEST_CHANNEL_ID = get("DISCORD_TEST_CHANNEL_ID") +DISCORD_TEST_CHANNEL_NAME = get("DISCORD_TEST_CHANNEL_NAME") +DISCORD_BOARD_CHANNEL_ID = get("DISCORD_BOARD_CHANNEL_ID") +DISCORD_BOARD_CHANNEL_NAME = get("DISCORD_BOARD_CHANNEL_NAME") +DISCORD_EP2025_CHANNEL_ID = get("DISCORD_EP2025_CHANNEL_ID") +DISCORD_EP2025_CHANNEL_NAME = get("DISCORD_EP2025_CHANNEL_NAME") +DISCORD_EM_CHANNEL_ID = get("DISCORD_EM_CHANNEL_ID") +DISCORD_EM_CHANNEL_NAME = get("DISCORD_EM_CHANNEL_NAME") +DISCORD_WEBSITE_CHANNEL_ID = get("DISCORD_WEBSITE_CHANNEL_ID") +DISCORD_WEBSITE_CHANNEL_NAME = get("DISCORD_WEBSITE_CHANNEL_NAME") +DISCORD_BOT_CHANNEL_ID = get("DISCORD_BOT_CHANNEL_ID") +DISCORD_BOT_CHANNEL_NAME = get("DISCORD_BOT_CHANNEL_NAME") + +# Github +GITHUB_API_TOKEN = get("GITHUB_API_TOKEN") +GITHUB_WEBHOOK_SECRET_TOKEN = get("GITHUB_WEBHOOK_SECRET_TOKEN") + +GITHUB_BOARD_PROJECT_ID = get("GITHUB_BOARD_PROJECT_ID") +GITHUB_EP2025_PROJECT_ID = get("GITHUB_EP2025_PROJECT_ID") +GITHUB_EM_PROJECT_ID = get("GITHUB_EM_PROJECT_ID") + if DJANGO_ENV == "dev": DEBUG = True ALLOWED_HOSTS = ["127.0.0.1", "localhost"] @@ -141,23 +191,6 @@ WEBHOOK_INTERNAL_TOKEN = "dev-token" - # This is only needed if you end up running the bot locally, hence it - # doesn't fail explicilty – however it does emit a warning. - # Please check the documentation and/or current online guides how to get - # one from the developer portal. - # If you run it locally, you probably want to run it against your own test - # bot and a test server. - - def warn_if_missing(name, default=""): - value = os.environ.get(name, default) - if not value: - warnings.warn(f"{name} not set") - - DISCORD_TEST_CHANNEL_ID = warn_if_missing("DISCORD_TEST_CHANNEL_ID", "") - DISCORD_TEST_CHANNEL_NAME = warn_if_missing("DISCORD_TEST_CHANNEL_NAME", "") - DISCORD_BOT_TOKEN = warn_if_missing("DISCORD_BOT_TOKEN", "") - GITHUB_API_TOKEN = warn_if_missing("GITHUB_API_TOKEN", "") - GITHUB_WEBHOOK_SECRET_TOKEN = warn_if_missing("GITHUB_WEBHOOK_SECRET_TOKEN", "") elif DJANGO_ENV == "test": @@ -198,6 +231,18 @@ def warn_if_missing(name, default=""): GITHUB_API_TOKEN = "github-test-token" GITHUB_WEBHOOK_SECRET_TOKEN = "github-webhook-secret-token-token" + # IRL those IDs are random and look like "id": "PVT_kwDOAFSD_s4AtxZm" + GITHUB_BOARD_PROJECT_ID = "PVT_Test_Board_Project" + GITHUB_EP2025_PROJECT_ID = "PVT_Test_ep2025_Project" + GITHUB_EM_PROJECT_ID = "PVT_Test_EM_Project" + + DISCORD_BOARD_CHANNEL_NAME = "board_channel" + DISCORD_BOARD_CHANNEL_ID = "1234567" + DISCORD_EP2025_CHANNEL_NAME = "ep2025_channel" + DISCORD_EP2025_CHANNEL_ID = "1232025" + DISCORD_EM_CHANNEL_NAME = "em_channel" + DISCORD_EM_CHANNEL_ID = "123123" + elif DJANGO_ENV == "local_container": DEBUG = False @@ -293,17 +338,10 @@ def warn_if_missing(name, default=""): }, } - DISCORD_BOT_TOKEN = os.environ["DISCORD_BOT_TOKEN"] - DISCORD_TEST_CHANNEL_ID = os.environ["DISCORD_TEST_CHANNEL_ID"] - DISCORD_TEST_CHANNEL_NAME = os.environ["DISCORD_TEST_CHANNEL_NAME"] - CSRF_TRUSTED_ORIGINS = [ "https://internal.europython.eu", ] - WEBHOOK_INTERNAL_TOKEN = os.environ["WEBHOOK_INTERNAL_TOKEN"] - GITHUB_API_TOKEN = os.environ["GITHUB_API_TOKEN"] - GITHUB_WEBHOOK_SECRET_TOKEN = os.environ["GITHUB_WEBHOOK_SECRET_TOKEN"] elif DJANGO_ENV == "build": diff --git a/intbot/tests/test_admin.py b/intbot/tests/test_admin.py index 4fcc919..d731edc 100644 --- a/intbot/tests/test_admin.py +++ b/intbot/tests/test_admin.py @@ -7,7 +7,7 @@ def test_admin_for_webhooks_sanity_check(admin_client): url = "/admin/core/webhook/" - wh = Webhook.objects.create(content={}) + wh = Webhook.objects.create(content={}, extra={}) assert wh.uuid response = admin_client.get(url) diff --git a/intbot/tests/test_bot/test_channel_router.py b/intbot/tests/test_bot/test_channel_router.py new file mode 100644 index 0000000..536a6e6 --- /dev/null +++ b/intbot/tests/test_bot/test_channel_router.py @@ -0,0 +1,68 @@ +""" +Integrated tests for the Discord Channel Router +""" + +from core.bot.channel_router import Channels, discord_channel_router +from core.models import Webhook + + +class TestDiscordChannelRouter: + def test_it_routes_board_project_to_board_channel(self): + wh = Webhook( + source="github", + meta={"X-Github-Event": "projects_v2_item"}, + content={}, + extra={ + "project": { + "id": "PVT_Test_Board_Project", + "title": "Board Project", + "url": "https://github.com/europython", + }, + }, + ) + + channel = discord_channel_router(wh) + + assert channel == Channels.board_channel + assert channel.channel_name == "board_channel" + assert channel.channel_id == "1234567" + + def test_it_routes_ep2025_project_to_ep2025_channel(self): + wh = Webhook( + source="github", + meta={"X-Github-Event": "projects_v2_item"}, + content={}, + extra={ + "project": { + "id": "PVT_Test_ep2025_Project", + "title": "EP2025 Project", + "url": "https://github.com/europython", + }, + }, + ) + + channel = discord_channel_router(wh) + + assert channel == Channels.ep2025_channel + assert channel.channel_name == "ep2025_channel" + assert channel.channel_id == "1232025" + + def test_it_routes_EM_project_to_EM_channel(self): + wh = Webhook( + source="github", + meta={"X-Github-Event": "projects_v2_item"}, + content={}, + extra={ + "project": { + "id": "PVT_Test_EM_Project", + "title": "EM Project", + "url": "https://github.com/europython", + }, + }, + ) + + channel = discord_channel_router(wh) + + assert channel == Channels.em_channel + assert channel.channel_name == "em_channel" + assert channel.channel_id == "123123" diff --git a/intbot/tests/test_bot.py b/intbot/tests/test_bot/test_main.py similarity index 100% rename from intbot/tests/test_bot.py rename to intbot/tests/test_bot/test_main.py diff --git a/intbot/tests/test_integrations/github/issues.reopned.json b/intbot/tests/test_integrations/github/issues.reopned.json new file mode 100644 index 0000000..45a9e25 --- /dev/null +++ b/intbot/tests/test_integrations/github/issues.reopned.json @@ -0,0 +1,255 @@ +{ + "action": "reopened", + "issue": { + "url": "https://api.github.com/repos/EuroPython/board-2025-tasks/issues/80", + "repository_url": "https://api.github.com/repos/EuroPython/board-2025-tasks", + "labels_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/issues/80/labels{/name}", + "comments_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/issues/80/comments", + "events_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/issues/80/events", + "html_url": "https://github.com/EuroPython/board-2025-tasks/issues/80", + "id": 2792970644, + "node_id": "I_randomIssueID", + "number": 80, + "title": "Testing Epics", + "user": { + "login": "artcz", + "id": 126820, + "node_id": "MDQ6VXNlcjEyNjgyMA==", + "avatar_url": "https://avatars.githubusercontent.com/u/126820?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/artcz", + "html_url": "https://github.com/artcz", + "followers_url": "https://api.github.com/users/artcz/followers", + "following_url": "https://api.github.com/users/artcz/following{/other_user}", + "gists_url": "https://api.github.com/users/artcz/gists{/gist_id}", + "starred_url": "https://api.github.com/users/artcz/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/artcz/subscriptions", + "organizations_url": "https://api.github.com/users/artcz/orgs", + "repos_url": "https://api.github.com/users/artcz/repos", + "events_url": "https://api.github.com/users/artcz/events{/privacy}", + "received_events_url": "https://api.github.com/users/artcz/received_events", + "type": "User", + "user_view_type": "public", + "site_admin": false + }, + "labels": [ + + ], + "state": "open", + "locked": false, + "assignee": { + "login": "artcz", + "id": 126820, + "node_id": "MDQ6VXNlcjEyNjgyMA==", + "avatar_url": "https://avatars.githubusercontent.com/u/126820?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/artcz", + "html_url": "https://github.com/artcz", + "followers_url": "https://api.github.com/users/artcz/followers", + "following_url": "https://api.github.com/users/artcz/following{/other_user}", + "gists_url": "https://api.github.com/users/artcz/gists{/gist_id}", + "starred_url": "https://api.github.com/users/artcz/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/artcz/subscriptions", + "organizations_url": "https://api.github.com/users/artcz/orgs", + "repos_url": "https://api.github.com/users/artcz/repos", + "events_url": "https://api.github.com/users/artcz/events{/privacy}", + "received_events_url": "https://api.github.com/users/artcz/received_events", + "type": "User", + "user_view_type": "public", + "site_admin": false + }, + "assignees": [ + { + "login": "artcz", + "id": 126820, + "node_id": "MDQ6VXNlcjEyNjgyMA==", + "avatar_url": "https://avatars.githubusercontent.com/u/126820?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/artcz", + "html_url": "https://github.com/artcz", + "followers_url": "https://api.github.com/users/artcz/followers", + "following_url": "https://api.github.com/users/artcz/following{/other_user}", + "gists_url": "https://api.github.com/users/artcz/gists{/gist_id}", + "starred_url": "https://api.github.com/users/artcz/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/artcz/subscriptions", + "organizations_url": "https://api.github.com/users/artcz/orgs", + "repos_url": "https://api.github.com/users/artcz/repos", + "events_url": "https://api.github.com/users/artcz/events{/privacy}", + "received_events_url": "https://api.github.com/users/artcz/received_events", + "type": "User", + "user_view_type": "public", + "site_admin": false + } + ], + "milestone": null, + "comments": 1, + "created_at": "2025-01-16T14:41:24Z", + "updated_at": "2025-01-24T10:37:07Z", + "closed_at": null, + "author_association": "NONE", + "type": null, + "sub_issues_summary": { + "total": 9, + "completed": 7, + "percent_completed": 77 + }, + "active_lock_reason": null, + "body": "This is ticket's body", + "reactions": { + "url": "https://api.github.com/repos/EuroPython/board-2025-tasks/issues/80/reactions", + "total_count": 0, + "+1": 0, + "-1": 0, + "laugh": 0, + "hooray": 0, + "confused": 0, + "heart": 0, + "rocket": 0, + "eyes": 0 + }, + "timeline_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/issues/80/timeline", + "performed_via_github_app": null, + "state_reason": "reopened" + }, + "repository": { + "id": 899210959, + "node_id": "R_randomRepositoryID", + "name": "board-2025-tasks", + "full_name": "EuroPython/board-2025-tasks", + "private": true, + "owner": { + "login": "EuroPython", + "id": 5538814, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjU1Mzg4MTQ=", + "avatar_url": "https://avatars.githubusercontent.com/u/5538814?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/EuroPython", + "html_url": "https://github.com/EuroPython", + "followers_url": "https://api.github.com/users/EuroPython/followers", + "following_url": "https://api.github.com/users/EuroPython/following{/other_user}", + "gists_url": "https://api.github.com/users/EuroPython/gists{/gist_id}", + "starred_url": "https://api.github.com/users/EuroPython/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/EuroPython/subscriptions", + "organizations_url": "https://api.github.com/users/EuroPython/orgs", + "repos_url": "https://api.github.com/users/EuroPython/repos", + "events_url": "https://api.github.com/users/EuroPython/events{/privacy}", + "received_events_url": "https://api.github.com/users/EuroPython/received_events", + "type": "Organization", + "user_view_type": "public", + "site_admin": false + }, + "html_url": "https://github.com/EuroPython/board-2025-tasks", + "description": "Tasks for the Board 2025", + "fork": false, + "url": "https://api.github.com/repos/EuroPython/board-2025-tasks", + "forks_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/forks", + "keys_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/teams", + "hooks_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/hooks", + "issue_events_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/issues/events{/number}", + "events_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/events", + "assignees_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/assignees{/user}", + "branches_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/branches{/branch}", + "tags_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/tags", + "blobs_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/statuses/{sha}", + "languages_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/languages", + "stargazers_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/stargazers", + "contributors_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/contributors", + "subscribers_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/subscribers", + "subscription_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/subscription", + "commits_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/contents/{+path}", + "compare_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/merges", + "archive_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/downloads", + "issues_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/issues{/number}", + "pulls_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/pulls{/number}", + "milestones_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/milestones{/number}", + "notifications_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/labels{/name}", + "releases_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/releases{/id}", + "deployments_url": "https://api.github.com/repos/EuroPython/board-2025-tasks/deployments", + "created_at": "2024-12-05T20:31:16Z", + "updated_at": "2024-12-18T14:16:21Z", + "pushed_at": "2024-12-05T20:31:17Z", + "git_url": "git://github.com/EuroPython/board-2025-tasks.git", + "ssh_url": "git@github.com:EuroPython/board-2025-tasks.git", + "clone_url": "https://github.com/EuroPython/board-2025-tasks.git", + "svn_url": "https://github.com/EuroPython/board-2025-tasks", + "homepage": null, + "size": 2, + "stargazers_count": 0, + "watchers_count": 0, + "language": null, + "has_issues": true, + "has_projects": true, + "has_downloads": true, + "has_wiki": true, + "has_pages": false, + "has_discussions": false, + "forks_count": 0, + "mirror_url": null, + "archived": false, + "disabled": false, + "open_issues_count": 63, + "license": null, + "allow_forking": false, + "is_template": false, + "web_commit_signoff_required": false, + "topics": [ + + ], + "visibility": "private", + "forks": 0, + "open_issues": 63, + "watchers": 0, + "default_branch": "main", + "custom_properties": { + + } + }, + "organization": { + "login": "EuroPython", + "id": 5538814, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjU1Mzg4MTQ=", + "url": "https://api.github.com/orgs/EuroPython", + "repos_url": "https://api.github.com/orgs/EuroPython/repos", + "events_url": "https://api.github.com/orgs/EuroPython/events", + "hooks_url": "https://api.github.com/orgs/EuroPython/hooks", + "issues_url": "https://api.github.com/orgs/EuroPython/issues", + "members_url": "https://api.github.com/orgs/EuroPython/members{/member}", + "public_members_url": "https://api.github.com/orgs/EuroPython/public_members{/member}", + "avatar_url": "https://avatars.githubusercontent.com/u/5538814?v=4", + "description": "EuroPython Conference Series Software" + }, + "sender": { + "login": "artcz", + "id": 126820, + "node_id": "MDQ6VXNlcjEyNjgyMA==", + "avatar_url": "https://avatars.githubusercontent.com/u/126820?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/artcz", + "html_url": "https://github.com/artcz", + "followers_url": "https://api.github.com/users/artcz/followers", + "following_url": "https://api.github.com/users/artcz/following{/other_user}", + "gists_url": "https://api.github.com/users/artcz/gists{/gist_id}", + "starred_url": "https://api.github.com/users/artcz/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/artcz/subscriptions", + "organizations_url": "https://api.github.com/users/artcz/orgs", + "repos_url": "https://api.github.com/users/artcz/repos", + "events_url": "https://api.github.com/users/artcz/events{/privacy}", + "received_events_url": "https://api.github.com/users/artcz/received_events", + "type": "User", + "user_view_type": "public", + "site_admin": false + } +} diff --git a/intbot/tests/test_integrations/github/project_v2_item.edited.json b/intbot/tests/test_integrations/github/project_v2_item.edited.json new file mode 100644 index 0000000..5c008d5 --- /dev/null +++ b/intbot/tests/test_integrations/github/project_v2_item.edited.json @@ -0,0 +1,89 @@ +{ + "action": "edited", + "projects_v2_item": { + "id": 94021398, + "node_id": "PVTI_random_projectItemV2ID", + "project_node_id": "PVT_random_projectID", + "content_node_id": "I_randomIssueID", + "content_type": "Issue", + "creator": { + "login": "artcz", + "id": 126820, + "node_id": "MDQ6VXNlcjEyNjgyMA==", + "avatar_url": "https://avatars.githubusercontent.com/u/126820?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/artcz", + "html_url": "https://github.com/artcz", + "followers_url": "https://api.github.com/users/artcz/followers", + "following_url": "https://api.github.com/users/artcz/following{/other_user}", + "gists_url": "https://api.github.com/users/artcz/gists{/gist_id}", + "starred_url": "https://api.github.com/users/artcz/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/artcz/subscriptions", + "organizations_url": "https://api.github.com/users/artcz/orgs", + "repos_url": "https://api.github.com/users/artcz/repos", + "events_url": "https://api.github.com/users/artcz/events{/privacy}", + "received_events_url": "https://api.github.com/users/artcz/received_events", + "type": "User", + "user_view_type": "public", + "site_admin": false + }, + "created_at": "2025-01-16T14:41:25Z", + "updated_at": "2025-01-24T10:37:09Z", + "archived_at": null + }, + "changes": { + "field_value": { + "field_node_id": "PVTSSF_fieldNode_ID", + "field_type": "single_select", + "field_name": "Status", + "project_number": 5, + "from": { + "id": "98236657", + "name": "Done", + "color": "ORANGE", + "description": "This has been completed" + }, + "to": { + "id": "47fc9ee4", + "name": "In progress", + "color": "YELLOW", + "description": "This is actively being worked on" + } + } + }, + "organization": { + "login": "EuroPython", + "id": 5538814, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjU1Mzg4MTQ=", + "url": "https://api.github.com/orgs/EuroPython", + "repos_url": "https://api.github.com/orgs/EuroPython/repos", + "events_url": "https://api.github.com/orgs/EuroPython/events", + "hooks_url": "https://api.github.com/orgs/EuroPython/hooks", + "issues_url": "https://api.github.com/orgs/EuroPython/issues", + "members_url": "https://api.github.com/orgs/EuroPython/members{/member}", + "public_members_url": "https://api.github.com/orgs/EuroPython/public_members{/member}", + "avatar_url": "https://avatars.githubusercontent.com/u/5538814?v=4", + "description": "EuroPython Conference Series Software" + }, + "sender": { + "login": "github-project-automation[bot]", + "id": 113052574, + "node_id": "BOT_kgDOBr0Lng", + "avatar_url": "https://avatars.githubusercontent.com/in/235829?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/github-project-automation%5Bbot%5D", + "html_url": "https://github.com/apps/github-project-automation", + "followers_url": "https://api.github.com/users/github-project-automation%5Bbot%5D/followers", + "following_url": "https://api.github.com/users/github-project-automation%5Bbot%5D/following{/other_user}", + "gists_url": "https://api.github.com/users/github-project-automation%5Bbot%5D/gists{/gist_id}", + "starred_url": "https://api.github.com/users/github-project-automation%5Bbot%5D/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/github-project-automation%5Bbot%5D/subscriptions", + "organizations_url": "https://api.github.com/users/github-project-automation%5Bbot%5D/orgs", + "repos_url": "https://api.github.com/users/github-project-automation%5Bbot%5D/repos", + "events_url": "https://api.github.com/users/github-project-automation%5Bbot%5D/events{/privacy}", + "received_events_url": "https://api.github.com/users/github-project-automation%5Bbot%5D/received_events", + "type": "Bot", + "user_view_type": "public", + "site_admin": false + } +} diff --git a/intbot/tests/test_integrations/github/query_result.json b/intbot/tests/test_integrations/github/query_result.json new file mode 100644 index 0000000..7d15581 --- /dev/null +++ b/intbot/tests/test_integrations/github/query_result.json @@ -0,0 +1,18 @@ +{ + "data": { + "node": { + "id": "PVTI_random_projectItemV2ID", + "project": { + "id": "PVT_Test_Board_Project", + "title": "Board Project", + "url": "https://github.com/orgs/EuroPython/projects/1337" + }, + "content": { + "__typename": "Issue", + "id": "I_randomIssueID", + "title": "Testing Epics", + "url": "https://github.com/EuroPython/board-2025-tasks/issues/80" + } + } + } +} diff --git a/intbot/tests/test_integrations/test_github.py b/intbot/tests/test_integrations/test_github.py index 818160d..645a4bd 100644 --- a/intbot/tests/test_integrations/test_github.py +++ b/intbot/tests/test_integrations/test_github.py @@ -3,201 +3,367 @@ from core.integrations.github import ( GITHUB_API_URL, GithubProjectV2Item, - fetch_github_item_details, + GithubAPIError, + GithubSender, parse_github_webhook, + prep_github_webhook, ) +from core.models import Webhook from httpx import Response def test_parse_github_webhook_raises_value_error_for_unsupported(): headers = {"X-Github-Event": "random_event"} - content = {} + wh = Webhook(meta=headers, content={}, extra={}) with pytest.raises(ValueError): - parse_github_webhook(headers, content) - + parse_github_webhook(wh) + + +def test_github_project_created_event(): + parser = GithubProjectV2Item( + action="created", + headers={}, + content={ + "sender": {"login": "testuser", "html_url": "https://github.com/testuser"}, + "projects_v2_item": { + "content_type": "Issue", + "node_id": "test_node_id", + }, + "action": "created", + }, + extra={ + "content": { + "__typename": "Issue", + "id": "I_randomIssueID", + "title": "Test Issue", + "url": "https://github.com/test-issue", + } + }, + ) -@pytest.fixture -def mocked_github_response(): - def _mock_response(item_type, content_data): - return {"data": {"node": {"content": content_data}}} + message = parser.as_discord_message() - return _mock_response + assert message == ( + "[@testuser](https://github.com/testuser) created " + "[Test Issue](https://github.com/test-issue)" + ) -@pytest.fixture -def sample_content(): - return { - "sender": {"login": "testuser", "html_url": "https://github.com/testuser"}, - "projects_v2_item": { - "content_type": "Issue", - "node_id": "test_node_id", +def test_github_project_edited_event_for_status_change(): + parser = GithubProjectV2Item( + action="changed", + headers={}, + content={ + "sender": {"login": "testuser", "html_url": "https://github.com/testuser"}, + "projects_v2_item": { + "content_type": "Issue", + "node_id": "test_node_id", + }, + "action": "edited", + "changes": { + "field_value": { + "field_name": "Status", + "field_type": "single_select", + "from": {"name": "To Do"}, + "to": {"name": "In Progress"}, + } + }, }, - } - - -@respx.mock -def test_fetch_github_item_details(mocked_github_response): - mocked_response = mocked_github_response( - "Issue", - { - "id": "test_issue_id", - "title": "Test Issue", - "url": "https://github.com/test/repo/issues/1", + extra={ + "content": { + "__typename": "Issue", + "id": "I_randomIssueID", + "title": "Test Issue", + "url": "https://github.com/test-issue", + } }, ) - respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) - result = fetch_github_item_details("test_node_id") + message = parser.as_discord_message() - assert result["id"] == "test_issue_id" - assert result["title"] == "Test Issue" - assert result["url"] == "https://github.com/test/repo/issues/1" + assert message == ( + "[@testuser](https://github.com/testuser) changed **Status** of " + "**[Test Issue](https://github.com/test-issue)** " + "from **To Do** to **In Progress**" + ) -@respx.mock -def test_github_project_created_event(mocked_github_response, sample_content): - sample_content["action"] = "created" - mocked_response = mocked_github_response( - "Issue", - { - "id": "test_issue_id", - "title": "Test Issue", - "url": "https://github.com/test/repo/issues/1", +def test_github_project_edited_event_for_date_change(): + parser = GithubProjectV2Item( + action="edited", + headers={}, + content={ + "sender": {"login": "testuser", "html_url": "https://github.com/testuser"}, + "projects_v2_item": { + "content_type": "Issue", + "node_id": "test_node_id", + }, + "action": "edited", + "changes": { + "field_value": { + "field_name": "Deadline", + "field_type": "date", + "from": "2024-01-01T10:20:30", + "to": "2025-01-05T20:30:10", + } + }, + }, + extra={ + "content": { + "__typename": "Issue", + "id": "I_randomIssueID", + "title": "Test Issue", + "url": "https://github.com/test-issue", + } }, ) - respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) - parser = GithubProjectV2Item(sample_content) - message = parser.as_str() + message = parser.as_discord_message() assert message == ( - "[@testuser](https://github.com/testuser) created " - "[Test Issue](https://github.com/test/repo/issues/1)" + "[@testuser](https://github.com/testuser) edited **Deadline** of " + "**[Test Issue](https://github.com/test-issue)** " + "from **2024-01-01** to **2025-01-05**" ) -@respx.mock -def test_github_project_edited_event_for_status_change( - mocked_github_response, sample_content -): - sample_content["action"] = "edited" - sample_content["changes"] = { - "field_value": { - "field_name": "Status", - "field_type": "single_select", - "from": {"name": "To Do"}, - "to": {"name": "In Progress"}, - } - } - mocked_response = mocked_github_response( - "Issue", - { - "id": "test_issue_id", - "title": "Test Issue", - "url": "https://github.com/test/repo/issues/1", +def test_github_project_item_draft_issue_created(): + parser = GithubProjectV2Item( + action="created", + headers={}, + content={ + "sender": {"login": "testuser", "html_url": "https://github.com/testuser"}, + "projects_v2_item": {}, + "action": "created", + }, + extra={ + "content": { + "__typename": "DraftIssue", + "id": "DI_randomDraftIssueID", + "title": "Draft Title", + } }, ) - respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) - parser = GithubProjectV2Item(sample_content) - message = parser.as_str() + message = parser.as_discord_message() - assert message == ( - "[@testuser](https://github.com/testuser) changed **Status** of " - "**[Test Issue](https://github.com/test/repo/issues/1)** " - "from **To Do** to **In Progress**" - ) + assert message == "[@testuser](https://github.com/testuser) created Draft Title" -@respx.mock -def test_github_project_edited_event_for_date_change( - mocked_github_response, sample_content -): - sample_content["action"] = "edited" - sample_content["changes"] = { - "field_value": { - "field_name": "Deadline", - "field_type": "date", - "from": "2024-01-01T10:20:30", - "to": "2025-01-05T20:30:10", - } - } - mocked_response = mocked_github_response( - "Issue", - { - "id": "test_issue_id", - "title": "Test Issue", - "url": "https://github.com/test/repo/issues/1", +def test_github_project_item_edited_event_no_changes(): + parser = GithubProjectV2Item( + action="edited", + headers={}, + content={ + "sender": {"login": "testuser", "html_url": "https://github.com/testuser"}, + "projects_v2_item": {}, + "action": "edited", + }, + extra={ + "content": { + "__typename": "Issue", + "id": "I_randomIssueID", + "title": "Test Issue", + "url": "https://github.com/test-issue", + } }, ) - respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) - parser = GithubProjectV2Item(sample_content) - message = parser.as_str() + message = parser.as_discord_message() assert message == ( - "[@testuser](https://github.com/testuser) changed **Deadline** of " - "**[Test Issue](https://github.com/test/repo/issues/1)** " - "from **2024-01-01** to **2025-01-05**" + "[@testuser](https://github.com/testuser) edited " + "[Test Issue](https://github.com/test-issue)" ) -@respx.mock -def test_github_project_draft_issue_event(mocked_github_response, sample_content): - sample_content["action"] = "created" - sample_content["projects_v2_item"]["content_type"] = "DraftIssue" - mocked_response = mocked_github_response( - "DraftIssue", - { - "id": "draft_issue_id", - "title": "Draft Title", - }, - ) - respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) +class TestGithubProjectV2Item: + + def test_changes_for_single_select(self): + parser = GithubProjectV2Item( + action="changed", + headers={}, + content={ + "changes": { + "field_value": { + "field_name": "Status", + "field_type": "single_select", + "from": {"name": "To Do"}, + "to": {"name": "In Progress"}, + } + }, + }, + extra={}, + ) + + assert parser.changes() == { + "from": "To Do", + "to": "In Progress", + "field": "Status", + } - parser = GithubProjectV2Item(sample_content) - message = parser.as_str() + def test_changes_for_date(self): + parser = GithubProjectV2Item( + action="changed", + headers={}, + content={ + "changes": { + "field_value": { + "field_name": "Deadline", + "field_type": "date", + "from": "2024-01-01T10:20:30", + "to": "2025-01-05T20:30:10", + } + }, + }, + extra={}, + ) + + assert parser.changes() == { + "from": "2024-01-01", + "to": "2025-01-05", + "field": "Deadline", + } - assert message == "[@testuser](https://github.com/testuser) created Draft Title" + def test_changes_for_unsupported_format(self): + parser = GithubProjectV2Item( + action="changed", + headers={}, + content={ + "changes": { + "field_value": { + "field_name": "Randomfield", + "field_type": "unsupported", + "from": "This", + "to": "That", + } + }, + }, + extra={}, + ) + + assert parser.changes() == { + "from": "None", + "to": "None", + "field": "Randomfield", + } + + + def test_get_project_parses_project_correctly(self, github_data): + wh = Webhook( + meta={"X-Github-Event": "projects_v2_item"}, + content=github_data["project_v2_item.edited"], + extra=github_data["query_result"], + ) + gwh = parse_github_webhook(wh) + + ghp = gwh.get_project() + + assert ghp.title == "Board Project" + assert ghp.url == "https://github.com/orgs/EuroPython/projects/1337" + def test_get_sender_parses_sender_correctly(self, github_data): + wh = Webhook( + meta={"X-Github-Event": "projects_v2_item"}, + content=github_data["project_v2_item.edited"], + extra=github_data["query_result"], + ) + gwh = parse_github_webhook(wh) -def test_github_project_unsupported_action(sample_content): - sample_content["action"] = "unsupported_action" + sender = gwh.get_sender() - parser = GithubProjectV2Item(sample_content) + assert isinstance(sender, GithubSender) + assert sender.login == "github-project-automation[bot]" + assert sender.html_url == "https://github.com/apps/github-project-automation" - with pytest.raises(ValueError, match="Action unsupported unsupported_action"): - parser.action() + def test_sender_formats_sender_correctly(self, github_data): + wh = Webhook( + meta={"X-Github-Event": "projects_v2_item"}, + content=github_data["project_v2_item.edited"], + extra=github_data["query_result"], + ) + gwh = parse_github_webhook(wh) + + + assert isinstance(gwh.sender, str) + assert ( + gwh.sender == "[@github-project-automation[bot]](" + "https://github.com/apps/github-project-automation" + ")" + ) + + +def test_prep_github_webhook_fails_if_event_not_supported(): + wh = Webhook(meta={"X-Github-Event": "issue.fixed"}) + + with pytest.raises(ValueError): + prep_github_webhook(wh) @respx.mock -def test_github_project_edited_event_no_changes(mocked_github_response, sample_content): - sample_content["action"] = "edited" - mocked_response = mocked_github_response( - "Issue", - { - "id": "test_issue_id", - "title": "Test Issue", - "url": "https://github.com/test/repo/issues/1", +@pytest.mark.django_db +def test_prep_github_webhook_fetches_extra_data_for_project_v2_item(): + wh = Webhook( + meta={"X-Github-Event": "projects_v2_item"}, + content={ + "projects_v2_item": { + "node_id": "PVTI_random_projectItemV2ID", + "action": "random", + } }, ) + node = { + "project": { + "id": "PVT_Random_Project", + "title": "Random Project", + "url": "https://github.com/europython", + }, + "content": { + "__typename": "Issue", + "id": "I_randomIssueID", + "title": "Test Issue", + "url": "https://github.com/test-issue", + }, + } + + mocked_response = { + "data": { + "node": node, + } + } + respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) + wh = prep_github_webhook(wh) - parser = GithubProjectV2Item(sample_content) - message = parser.as_str() + assert wh.event == "projects_v2_item.random" + assert wh.extra == node - assert message == ( - "[@testuser](https://github.com/testuser) changed " - "[Test Issue](https://github.com/test/repo/issues/1)" + +@respx.mock +def test_prep_github_webhook_reraises_exception_in_case_of_API_error(): + wh = Webhook( + meta={"X-Github-Event": "projects_v2_item"}, + content={ + "projects_v2_item": { + "node_id": "PVTI_random_projectItemV2ID", + "action": "random", + } + }, ) + respx.post(GITHUB_API_URL).mock(return_value=Response(500, json={"lol": "failed"})) -@respx.mock -def test_fetch_github_item_details_api_error(): - respx.post(GITHUB_API_URL).mock( - return_value=Response(500, json={"message": "Internal Server Error"}) + with pytest.raises(GithubAPIError, match='GitHub API error: 500 - {"lol":"failed"}'): + wh = prep_github_webhook(wh) + + +def test_parse_github_webhook_raises_exception_for_unsupported_events(): + wh = Webhook( + meta={"X-Github-Event": "long_form_content"}, + content={}, + extra={"something": "extra"}, ) - with pytest.raises(Exception, match="GitHub API error: 500 - .*"): - fetch_github_item_details("test_node_id") + with pytest.raises(ValueError, match="Event not supported `long_form_content`"): + parse_github_webhook(wh) diff --git a/intbot/tests/test_tasks.py b/intbot/tests/test_tasks.py index 674bcab..689bf8d 100644 --- a/intbot/tests/test_tasks.py +++ b/intbot/tests/test_tasks.py @@ -14,6 +14,7 @@ def test_process_internal_webhook_handles_internal_webhook_correctly(): content={ "random": "content", }, + extra={}, ) process_internal_webhook(wh) @@ -34,7 +35,12 @@ def test_process_internal_webhook_fails_if_incorrect_source(): @pytest.mark.django_db def test_process_webhook_fails_if_unsupported_source(): - wh = Webhook.objects.create(source="asdf", event="test1", content={}) + wh = Webhook.objects.create( + source="asdf", + event="test1", + content={}, + extra={}, + ) # If the task is enqueued the errors are not emited. # Instead we have to check the result @@ -51,6 +57,7 @@ def test_process_github_webhook_logs_unsupported_event(caplog): event="", meta={"X-Github-Event": "testrandom"}, content={}, + extra={}, ) with caplog.at_level(logging.INFO): diff --git a/pyproject.toml b/pyproject.toml index 3f18adb..6a178d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,6 +23,7 @@ dependencies = [ "pytest-cov>=6.0.0", "pytest-socket>=0.7.0", "respx>=0.22.0", + "pydantic>=2.10.6", ] [tool.pytest.ini_options] @@ -47,4 +48,5 @@ exclude_lines = [ "if 0:", "def __repr__", "def __str__", + "pragma: no cover", ] diff --git a/uv.lock b/uv.lock index dccf480..cc64c0c 100644 --- a/uv.lock +++ b/uv.lock @@ -54,6 +54,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/ec/6a/bc7e17a3e87a2985d3e8f4da4cd0f481060eb78fb08596c42be62c90a4d9/aiosignal-1.3.2-py2.py3-none-any.whl", hash = "sha256:45cde58e409a301715980c2b01d0c28bdde3770d8290b5eb2173759d9acb31a5", size = 7597 }, ] +[[package]] +name = "annotated-types" +version = "0.7.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/ee/67/531ea369ba64dcff5ec9c3402f9f51bf748cec26dde048a2f973a4eea7f5/annotated_types-0.7.0.tar.gz", hash = "sha256:aff07c09a53a08bc8cfccb9c85b05f1aa9a2a6f23728d790723543408344ce89", size = 16081 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/78/b6/6307fbef88d9b5ee7421e68d78a9f162e0da4900bc5f5793f6d3d0e34fb8/annotated_types-0.7.0-py3-none-any.whl", hash = "sha256:1f02e8b43a8fbbc3f3e0d4f0f4bfc8131bcb4eebe8849b8e5c773f3a1c582a53", size = 13643 }, +] + [[package]] name = "anyio" version = "4.8.0" @@ -322,6 +331,7 @@ dependencies = [ { name = "mypy" }, { name = "pdbpp" }, { name = "psycopg" }, + { name = "pydantic" }, { name = "pytest" }, { name = "pytest-asyncio" }, { name = "pytest-cov" }, @@ -344,6 +354,7 @@ requires-dist = [ { name = "mypy", specifier = ">=1.14.1" }, { name = "pdbpp", specifier = ">=0.10.3" }, { name = "psycopg", specifier = ">=3.2.3" }, + { name = "pydantic", specifier = ">=2.10.6" }, { name = "pytest", specifier = ">=8.3.4" }, { name = "pytest-asyncio", specifier = ">=0.25.2" }, { name = "pytest-cov", specifier = ">=6.0.0" }, @@ -476,6 +487,45 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/ce/21/534b8f5bd9734b7a2fcd3a16b1ee82ef6cad81a4796e95ebf4e0c6a24119/psycopg-3.2.3-py3-none-any.whl", hash = "sha256:644d3973fe26908c73d4be746074f6e5224b03c1101d302d9a53bf565ad64907", size = 197934 }, ] +[[package]] +name = "pydantic" +version = "2.10.6" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "annotated-types" }, + { name = "pydantic-core" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b7/ae/d5220c5c52b158b1de7ca89fc5edb72f304a70a4c540c84c8844bf4008de/pydantic-2.10.6.tar.gz", hash = "sha256:ca5daa827cce33de7a42be142548b0096bf05a7e7b365aebfa5f8eeec7128236", size = 761681 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/f4/3c/8cc1cc84deffa6e25d2d0c688ebb80635dfdbf1dbea3e30c541c8cf4d860/pydantic-2.10.6-py3-none-any.whl", hash = "sha256:427d664bf0b8a2b34ff5dd0f5a18df00591adcee7198fbd71981054cef37b584", size = 431696 }, +] + +[[package]] +name = "pydantic-core" +version = "2.27.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/fc/01/f3e5ac5e7c25833db5eb555f7b7ab24cd6f8c322d3a3ad2d67a952dc0abc/pydantic_core-2.27.2.tar.gz", hash = "sha256:eb026e5a4c1fee05726072337ff51d1efb6f59090b7da90d30ea58625b1ffb39", size = 413443 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d6/74/51c8a5482ca447871c93e142d9d4a92ead74de6c8dc5e66733e22c9bba89/pydantic_core-2.27.2-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:9e0c8cfefa0ef83b4da9588448b6d8d2a2bf1a53c3f1ae5fca39eb3061e2f0b0", size = 1893127 }, + { url = "https://files.pythonhosted.org/packages/d3/f3/c97e80721735868313c58b89d2de85fa80fe8dfeeed84dc51598b92a135e/pydantic_core-2.27.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:83097677b8e3bd7eaa6775720ec8e0405f1575015a463285a92bfdfe254529ef", size = 1811340 }, + { url = "https://files.pythonhosted.org/packages/9e/91/840ec1375e686dbae1bd80a9e46c26a1e0083e1186abc610efa3d9a36180/pydantic_core-2.27.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:172fce187655fece0c90d90a678424b013f8fbb0ca8b036ac266749c09438cb7", size = 1822900 }, + { url = "https://files.pythonhosted.org/packages/f6/31/4240bc96025035500c18adc149aa6ffdf1a0062a4b525c932065ceb4d868/pydantic_core-2.27.2-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:519f29f5213271eeeeb3093f662ba2fd512b91c5f188f3bb7b27bc5973816934", size = 1869177 }, + { url = "https://files.pythonhosted.org/packages/fa/20/02fbaadb7808be578317015c462655c317a77a7c8f0ef274bc016a784c54/pydantic_core-2.27.2-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:05e3a55d124407fffba0dd6b0c0cd056d10e983ceb4e5dbd10dda135c31071d6", size = 2038046 }, + { url = "https://files.pythonhosted.org/packages/06/86/7f306b904e6c9eccf0668248b3f272090e49c275bc488a7b88b0823444a4/pydantic_core-2.27.2-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:9c3ed807c7b91de05e63930188f19e921d1fe90de6b4f5cd43ee7fcc3525cb8c", size = 2685386 }, + { url = "https://files.pythonhosted.org/packages/8d/f0/49129b27c43396581a635d8710dae54a791b17dfc50c70164866bbf865e3/pydantic_core-2.27.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6fb4aadc0b9a0c063206846d603b92030eb6f03069151a625667f982887153e2", size = 1997060 }, + { url = "https://files.pythonhosted.org/packages/0d/0f/943b4af7cd416c477fd40b187036c4f89b416a33d3cc0ab7b82708a667aa/pydantic_core-2.27.2-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:28ccb213807e037460326424ceb8b5245acb88f32f3d2777427476e1b32c48c4", size = 2004870 }, + { url = "https://files.pythonhosted.org/packages/35/40/aea70b5b1a63911c53a4c8117c0a828d6790483f858041f47bab0b779f44/pydantic_core-2.27.2-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:de3cd1899e2c279b140adde9357c4495ed9d47131b4a4eaff9052f23398076b3", size = 1999822 }, + { url = "https://files.pythonhosted.org/packages/f2/b3/807b94fd337d58effc5498fd1a7a4d9d59af4133e83e32ae39a96fddec9d/pydantic_core-2.27.2-cp312-cp312-musllinux_1_1_armv7l.whl", hash = "sha256:220f892729375e2d736b97d0e51466252ad84c51857d4d15f5e9692f9ef12be4", size = 2130364 }, + { url = "https://files.pythonhosted.org/packages/fc/df/791c827cd4ee6efd59248dca9369fb35e80a9484462c33c6649a8d02b565/pydantic_core-2.27.2-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:a0fcd29cd6b4e74fe8ddd2c90330fd8edf2e30cb52acda47f06dd615ae72da57", size = 2158303 }, + { url = "https://files.pythonhosted.org/packages/9b/67/4e197c300976af185b7cef4c02203e175fb127e414125916bf1128b639a9/pydantic_core-2.27.2-cp312-cp312-win32.whl", hash = "sha256:1e2cb691ed9834cd6a8be61228471d0a503731abfb42f82458ff27be7b2186fc", size = 1834064 }, + { url = "https://files.pythonhosted.org/packages/1f/ea/cd7209a889163b8dcca139fe32b9687dd05249161a3edda62860430457a5/pydantic_core-2.27.2-cp312-cp312-win_amd64.whl", hash = "sha256:cc3f1a99a4f4f9dd1de4fe0312c114e740b5ddead65bb4102884b384c15d8bc9", size = 1989046 }, + { url = "https://files.pythonhosted.org/packages/bc/49/c54baab2f4658c26ac633d798dab66b4c3a9bbf47cff5284e9c182f4137a/pydantic_core-2.27.2-cp312-cp312-win_arm64.whl", hash = "sha256:3911ac9284cd8a1792d3cb26a2da18f3ca26c6908cc434a18f730dc0db7bfa3b", size = 1885092 }, +] + [[package]] name = "pygments" version = "2.19.1"