Skip to content

Commit ba4c842

Browse files
fix(github): fixes for assigning github role (#375)
1 parent 630e2c5 commit ba4c842

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ platforms such as GitHub discussions/issues might be added in the future.
5353
| GITHUB_CLIENT_ID | True | `None` | GitHub OAuth2 client id. |
5454
| GITHUB_CLIENT_SECRET | True | `None` | GitHub OAuth2 client secret. |
5555
| GITHUB_REDIRECT_URI | False | `https://localhost:8080/github/callback` | The redirect uri for OAuth2. Must be publicly accessible. |
56+
| GITHUB_TOKEN | True | `None` | GitHub personal access token. Must have `read:org` |
5657
| GITHUB_WEBHOOK_SECRET_KEY | True | `None` | A secret value to ensure webhooks are from trusted sources. |
5758
| GRAVATAR_EMAIL | False | `None` | Gravatar email address for bot avatar. |
5859
| IGDB_CLIENT_ID | False | `None` | Required if daily_releases is enabled. |

src/discord/tasks.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,20 @@ async def daily_task(bot: Bot) -> bool:
182182

183183

184184
@tasks.loop(minutes=1.0)
185-
async def role_update_task(bot: Bot) -> bool:
185+
async def role_update_task(bot: Bot, test_mode: bool = False) -> bool:
186186
"""
187187
Run the role update task.
188188
189189
This function runs on a schedule, every 1 minute.
190190
If the current time is not divisible by 10, return False. e.g. Run every 10 minutes.
191191
192+
Parameters
193+
----------
194+
bot : Bot
195+
The Discord bot instance.
196+
test_mode : bool, optional
197+
Whether to run the task in test mode, by default False. This simply affects how the roles are assigned.
198+
192199
Returns
193200
-------
194201
bool
@@ -234,14 +241,14 @@ async def role_update_task(bot: Bot) -> bool:
234241
user_data['roles'] = []
235242

236243
if user_data.get('github_username'):
237-
user_data['roles'].append('github-users')
244+
user_data['roles'].append('github-user')
238245

239246
# update the discord user roles
240247
for g in bot.guilds:
241248
roles = g.roles
242249

243250
role_map = {
244-
'github-users': discord.utils.get(roles, name='github-users'),
251+
'github-user': discord.utils.get(roles, name='github-user'),
245252
'supporters': discord.utils.get(roles, name='supporters'),
246253
't1-sponsors': discord.utils.get(roles, name='t1-sponsors'),
247254
't2-sponsors': discord.utils.get(roles, name='t2-sponsors'),
@@ -258,13 +265,21 @@ async def role_update_task(bot: Bot) -> bool:
258265
continue
259266

260267
if user_role in user_roles:
261-
# await member.add_roles(role)
262-
add_future = asyncio.run_coroutine_threadsafe(member.add_roles(role), bot.loop)
263-
add_future.result()
268+
if not test_mode:
269+
await member.add_roles(role)
270+
else:
271+
# using a standard await fails inside unit tests, although it works normally
272+
# RuntimeError: Timeout context manager should be used inside a task
273+
add_future = asyncio.run_coroutine_threadsafe(member.add_roles(role), bot.loop)
274+
add_future.result()
264275
elif user_role in revocable_roles:
265-
# await member.remove_roles(role)
266-
remove_future = asyncio.run_coroutine_threadsafe(member.remove_roles(role), bot.loop)
267-
remove_future.result()
276+
if not test_mode:
277+
await member.remove_roles(role)
278+
else:
279+
# using a standard await fails inside unit tests, although it works normally
280+
# RuntimeError: Timeout context manager should be used inside a task
281+
remove_future = asyncio.run_coroutine_threadsafe(member.remove_roles(role), bot.loop)
282+
remove_future.result()
268283

269284
with bot.db as db:
270285
db['discord_users'] = discord_users

tests/unit/discord/test_tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async def test_role_update_task(discord_bot, discord_db_users, mocker, skip):
122122
mock_datetime.now.return_value = datetime(2023, 1, 1, 0, 1 if skip else 0, 0, tzinfo=timezone.utc)
123123

124124
# Run the task
125-
result = await tasks.role_update_task(bot=discord_bot)
125+
result = await tasks.role_update_task(bot=discord_bot, test_mode=True)
126126

127127
assert result is not skip
128128

0 commit comments

Comments
 (0)