Skip to content

Commit

Permalink
fix(github): fixes for assigning github role
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Dec 15, 2024
1 parent 630e2c5 commit 4dd5358
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ platforms such as GitHub discussions/issues might be added in the future.
| GITHUB_CLIENT_ID | True | `None` | GitHub OAuth2 client id. |
| GITHUB_CLIENT_SECRET | True | `None` | GitHub OAuth2 client secret. |
| GITHUB_REDIRECT_URI | False | `https://localhost:8080/github/callback` | The redirect uri for OAuth2. Must be publicly accessible. |
| GITHUB_TOKEN | True | `None` | GitHub personal access token. Must have `read:org` |
| GITHUB_WEBHOOK_SECRET_KEY | True | `None` | A secret value to ensure webhooks are from trusted sources. |
| GRAVATAR_EMAIL | False | `None` | Gravatar email address for bot avatar. |
| IGDB_CLIENT_ID | False | `None` | Required if daily_releases is enabled. |
Expand Down
33 changes: 24 additions & 9 deletions src/discord/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,20 @@ async def daily_task(bot: Bot) -> bool:


@tasks.loop(minutes=1.0)
async def role_update_task(bot: Bot) -> bool:
async def role_update_task(bot: Bot, test_mode: bool = False) -> bool:
"""
Run the role update task.
This function runs on a schedule, every 1 minute.
If the current time is not divisible by 10, return False. e.g. Run every 10 minutes.
Parameters
----------
bot : Bot
The Discord bot instance.
test_mode : bool, optional
Whether to run the task in test mode, by default False. This simply affects how the roles are assigned.
Returns
-------
bool
Expand Down Expand Up @@ -234,14 +241,14 @@ async def role_update_task(bot: Bot) -> bool:
user_data['roles'] = []

if user_data.get('github_username'):
user_data['roles'].append('github-users')
user_data['roles'].append('github-user')

# update the discord user roles
for g in bot.guilds:
roles = g.roles

role_map = {
'github-users': discord.utils.get(roles, name='github-users'),
'github-user': discord.utils.get(roles, name='github-user'),
'supporters': discord.utils.get(roles, name='supporters'),
't1-sponsors': discord.utils.get(roles, name='t1-sponsors'),
't2-sponsors': discord.utils.get(roles, name='t2-sponsors'),
Expand All @@ -258,13 +265,21 @@ async def role_update_task(bot: Bot) -> bool:
continue

if user_role in user_roles:
# await member.add_roles(role)
add_future = asyncio.run_coroutine_threadsafe(member.add_roles(role), bot.loop)
add_future.result()
if not test_mode:
await member.add_roles(role)

Check warning on line 269 in src/discord/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/discord/tasks.py#L269

Added line #L269 was not covered by tests
else:
# using a standard await fails inside unit tests, although it works normally
# RuntimeError: Timeout context manager should be used inside a task
add_future = asyncio.run_coroutine_threadsafe(member.add_roles(role), bot.loop)
add_future.result()
elif user_role in revocable_roles:
# await member.remove_roles(role)
remove_future = asyncio.run_coroutine_threadsafe(member.remove_roles(role), bot.loop)
remove_future.result()
if not test_mode:
await member.remove_roles(role)

Check warning on line 277 in src/discord/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/discord/tasks.py#L277

Added line #L277 was not covered by tests
else:
# using a standard await fails inside unit tests, although it works normally
# RuntimeError: Timeout context manager should be used inside a task
remove_future = asyncio.run_coroutine_threadsafe(member.remove_roles(role), bot.loop)
remove_future.result()

with bot.db as db:
db['discord_users'] = discord_users
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/discord/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async def test_role_update_task(discord_bot, discord_db_users, mocker, skip):
mock_datetime.now.return_value = datetime(2023, 1, 1, 0, 1 if skip else 0, 0, tzinfo=timezone.utc)

# Run the task
result = await tasks.role_update_task(bot=discord_bot)
result = await tasks.role_update_task(bot=discord_bot, test_mode=True)

assert result is not skip

Expand Down

0 comments on commit 4dd5358

Please sign in to comment.