Skip to content

Commit

Permalink
remove nonexistent channels' settings on posting error
Browse files Browse the repository at this point in the history
  • Loading branch information
TicClick committed Apr 16, 2021
1 parent 2327f90 commit ff041be
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
11 changes: 8 additions & 3 deletions librarian/discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import logging

import discord
import discord.errors
from discord.ext import commands

from librarian.discord import errors
from librarian.discord.cogs import (
pulls,
server,
Expand Down Expand Up @@ -53,9 +55,12 @@ async def on_ready(self):
await self.start_routines()

async def post_or_update(self, channel_id, message_id=None, content=None, embed=None):
channel = self.get_channel(channel_id)
if channel is None:
channel = await self.fetch_channel(channel_id)
try:
channel = self.get_channel(channel_id)
if channel is None:
channel = await self.fetch_channel(channel_id)
except discord.errors.NotFound:
raise errors.NoDiscordChannel(channel_id)

if message_id is None:
message = await channel.send(content=content, embed=embed) # type: discord.Message
Expand Down
7 changes: 6 additions & 1 deletion librarian/discord/cogs/background/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sqlalchemy import exc as sql_exc

from librarian import storage
from librarian.discord import formatters
from librarian.discord import formatters, errors
from librarian.discord.settings import custom
from librarian.discord.cogs.background import base

Expand Down Expand Up @@ -265,11 +265,16 @@ async def sort_for_updates(self, pulls: typing.List[storage.models.pull.Pull]) -
"%s: failed to post an update for pull #%d in channel #%d: %s",
self.name, item[0], item[1], result
)
await self.handle_update_exception(result, item)
elif result is not None:
new_messages.append(result)

self.storage.discord.save_messages(*new_messages)

async def handle_update_exception(self, exc: errors.LibrarianException, item: typing.Tuple[int, int]):
if isinstance(exc, errors.NoDiscordChannel):
await self.bot.settings.reset(exc.channel_id)

async def status(self) -> dict:
# FIXME: make something useful here
return {}
8 changes: 8 additions & 0 deletions librarian/discord/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class LibrarianException(Exception):
pass


class NoDiscordChannel(LibrarianException):
def __init__(self, channel_id):
self.channel_id = channel_id
super().__init__(f"Discord channel #{channel_id} does not exist")
30 changes: 30 additions & 0 deletions tests/discord/cogs/background/test_github.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import collections
import random

import discord as discord_py
import discord.errors as discord_errors
import pytest

from librarian.storage.models import (
Expand Down Expand Up @@ -240,3 +242,31 @@ async def test__sort_for_updates__existing_pull(
assert done[0].number == expected[0].number
assert done[1] == expected[1]
assert done[2].id == expected[2].id

async def test__exception_handling_no_channel(
self, client, storage, existing_pulls, mocker, codes_by_titles
):
monitor = github.MonitorPulls(client)
monitor.CUTOFF_PULL_NUMBER = 0

p = next(iter(
_
for _ in existing_pulls if
(
codes_by_titles[_["title"]] and
_["state"] == "open"
)
))
language = custom.Language(codes_by_titles[p["title"]])
await client.settings.update(123, 1234, [language.name, language.code])
storage.pulls.save_from_payload(p)
pp = storage.pulls.by_number(p["number"])

response = collections.namedtuple("Response", "status reason")(404, "testing stuff")
client.fetch_channel = mocker.AsyncMock(side_effect=discord_errors.NotFound(response, "error"))
client.settings.reset = mocker.AsyncMock()
await monitor.sort_for_updates([pp])

client.settings.reset.assert_called()
args, _ = client.settings.reset.call_args
assert args == (123,)

0 comments on commit ff041be

Please sign in to comment.