Skip to content

Commit b5a1ce3

Browse files
committed
fix: Actually rename post instead of creating new one, when item is renamed (#4)
1 parent 23fcea8 commit b5a1ce3

5 files changed

Lines changed: 21 additions & 7 deletions

File tree

src/bot.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import os
3+
import shelve
34

45
from hikari import GuildPublicThread, RESTApp, TokenType
56
from hikari.impl import RESTClientImpl
@@ -81,10 +82,16 @@ async def create_post(
8182
f" Link do taska: {create_item_link(event.item_id)}"
8283
)
8384
async with shared_forum_channel.lock.reader_lock:
84-
return await client.create_forum_post(
85+
post = await client.create_forum_post(
8586
shared_forum_channel.forum_channel,
8687
item_name,
8788
message,
8889
auto_archive_duration=10080,
8990
user_mentions=user_mentions,
9091
)
92+
93+
with shelve.open(os.getenv("POST_ID_DB_PATH", "post_id.db")) as db:
94+
db[event.node_id] = str(post.id)
95+
print("meow", db)
96+
97+
return post

src/tests/test_e2e.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async def test_e2e(
9494
await asyncio.sleep(0.01)
9595
else:
9696
pytest.fail("Expected log 'body updated' not found in output")
97-
assert post_id_shelf.get("audacity4") == 621
97+
assert post_id_shelf.get("audacity4") == "621"
9898
mock_create_message.assert_called_with(
9999
621, "Opis taska zaktualizowany przez: nieznany użytkownik. Nowy opis: \nUpdated description", user_mentions=[]
100100
)

src/tests/test_unit/test_bot.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,34 @@
77
from hikari.impl import RESTClientImpl
88

99
from src import bot
10-
from src.tests.conftest import RestClientContextManagerMock
10+
from src.tests.conftest import MockShelf, RestClientContextManagerMock
1111
from src.utils.data_types import SimpleProjectItemEvent
1212
from src.utils.error import ForumChannelNotFound
1313

1414

15+
@patch("shelve.open")
1516
@patch("src.bot.fetch_item_name", new_callable=AsyncMock)
1617
@patch.object(RESTClientImpl, "create_forum_post", new_callable=AsyncMock)
1718
async def test_create_post(
1819
mock_create_forum_post,
1920
mock_fetch_item_name,
21+
mock_shelve_open,
2022
rest_client_mock,
2123
shared_forum_channel_mock,
2224
user_text_mention,
25+
post_mock,
2326
):
2427
mock_fetch_item_name.return_value = "audacity4"
28+
mock_shelf = MockShelf()
29+
mock_shelve_open.return_value = mock_shelf
30+
mock_create_forum_post.return_value = post_mock
2531
message = f"Nowy task stworzony audacity4 przez: {user_text_mention}.\n Link do taska: https://github.com/orgs/my-org/projects/1?pane=issue&item_id=1"
2632
event = SimpleProjectItemEvent(1, "audacity4", "norbiros", "created")
2733
await bot.create_post(event, user_text_mention, shared_forum_channel_mock, rest_client_mock, [])
2834
mock_create_forum_post.assert_called_with(
2935
shared_forum_channel_mock.forum_channel, event.node_id, message, auto_archive_duration=10080, user_mentions=[]
3036
)
37+
assert mock_shelf.get("audacity4") == "621"
3138

3239

3340
@patch("src.bot.create_post", new_callable=AsyncMock)

src/tests/test_unit/test_utils/test_discord_rest_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async def test_get_post_id_active_thread(
6262
mock_fetch_item_name.return_value = "audacity4"
6363

6464
assert await discord_rest_client.get_post_id_or_post("node_id", 1, 1, rest_client_mock) == post_mock
65-
assert mock_shelf.get("audacity4") == 621
65+
assert mock_shelf.get("audacity4") == "621"
6666

6767

6868
@patch("src.utils.discord_rest_client.fetch_item_name", new_callable=AsyncMock)
@@ -84,7 +84,7 @@ async def test_get_post_id_archived_thread(
8484
mock_fetch_item_name.return_value = "audacity4"
8585

8686
assert await discord_rest_client.get_post_id_or_post("node_id", 1, 1, rest_client_mock) == post_mock
87-
assert mock_shelf.get("audacity4") == 621
87+
assert mock_shelf.get("audacity4") == "621"
8888

8989

9090
@patch("src.utils.discord_rest_client.fetch_item_name", new_callable=AsyncMock)

src/utils/discord_rest_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ async def get_post_id_or_post(
3131
name = await fetch_item_name(node_id)
3232
for thread in await rest_client.fetch_active_threads(discord_guild_id):
3333
if thread.name == name:
34-
db[name] = thread.id
34+
db[name] = str(thread.id)
3535
return thread
3636
for thread in await rest_client.fetch_public_archived_threads(forum_channel_id):
3737
if thread.name == name:
38-
db[name] = thread.id
38+
db[name] = str(thread.id)
3939
return thread
4040

4141
return None

0 commit comments

Comments
 (0)