Skip to content

Commit c6c9373

Browse files
committed
Refactor GitHub team handling to use dynamic role mapping and remove hardcoded values
1 parent 88966b8 commit c6c9373

5 files changed

Lines changed: 11 additions & 11 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ These environment variables are required to work on the relevant cog.
2929
| KING_ARTHUR_CLOUDFLARE_TOKEN | Zones | A token for the Cloudflare API used for the Cloudflare commands in King Arthur \* | Required |
3030
| KING_ARTHUR_GITHUB_ORG | GitHubManagement | The github organisation to fetch teams from | python-discord |
3131
| KING_ARTHUR_GITHUB_TOKEN | GitHubManagement | The github token used to manage the GitHub organisation | Required |
32-
| KING_ARTHUR_GITHUB_TEAM | GitHubManagement | The slug of the GitHub team to add new members to | staff |
3332
| KING_ARTHUR_GRAFANA_URL | GrafanaLDAPTeamSync | The URL to the grafana instance to manage teams | https://grafana.pydis.wtf |
3433
| KING_ARTHUR_GRAFANA_TOKEN | GrafanaLDAPTeamSync | The grafana token used to sync teams with LDAP | Required |
3534
| KING_ARTHUR_YOUTUBE_API_KEY | Motivation | The YouTube API key to fetch missions with | Required |

arthur/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class Config(
2323
grafana_token: pydantic.SecretStr | None = None
2424
github_token: pydantic.SecretStr | None = None
2525
github_org: str = "python-discord"
26-
github_team: str = "staff"
2726

2827
devops_role: int = 409416496733880320
2928
helpers_role: int = 267630620367257601

arthur/constants.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ class LDAPGroupMapping(TypedDict):
1010
github_team_slug: str
1111

1212

13-
# Users are only checked for enrollment if they have this role. This doesn't grant them any
14-
# permissions, it is for performance to avoid iterating roles for every other user in the guild.
15-
LDAP_BASE_STAFF_ROLE = 267630620367257601
16-
1713
# This is a mapping of LDAP groups to Discord role IDs and GitHub team IDs. It is used to determine
1814
# which users should be eligible for LDAP enrollment.
1915
LDAP_ROLE_MAPPING: dict[str, LDAPGroupMapping] = {
16+
"helpers": {"discord_role_id": 267630620367257601, "github_team_slug": "helpers"},
2017
"devops": {"discord_role_id": 409416496733880320, "github_team_slug": "devops"},
2118
"administrators": {"discord_role_id": 267628507062992896, "github_team_slug": "admins"},
2219
"moderators": {"discord_role_id": 267629731250176001, "github_team_slug": "moderators"},
2320
"coredevs": {"discord_role_id": 587606783669829632, "github_team_slug": "core-developers"},
2421
"events": {"discord_role_id": 787816728474288181, "github_team_slug": "events"},
2522
"directors": {"discord_role_id": 267627879762755584, "github_team_slug": "directors"},
2623
}
24+
25+
# Users are only checked for enrollment if they have this role. This doesn't grant them any
26+
# permissions, it is for performance to avoid iterating roles for every other user in the guild.
27+
HELPER_ROLE_ID = LDAP_ROLE_MAPPING["helpers"]["discord_role_id"]

arthur/exts/directory/ldap.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from arthur.apis.directory import freeipa, keycloak, ldap
1414
from arthur.config import CONFIG
15-
from arthur.constants import LDAP_BASE_STAFF_ROLE, LDAP_ROLE_MAPPING
15+
from arthur.constants import HELPER_ROLE_ID, LDAP_ROLE_MAPPING
1616
from arthur.log import logger
1717

1818
if TYPE_CHECKING:
@@ -126,7 +126,7 @@ async def generate_creds(self, interaction: discord.Interaction, _button: ui.But
126126
"""Generate credentials for the user."""
127127
user = interaction.user
128128

129-
if LDAP_BASE_STAFF_ROLE not in [role.id for role in user.roles]:
129+
if HELPER_ROLE_ID not in [role.id for role in user.roles]:
130130
await interaction.response.send_message(
131131
"You are not eligible for LDAP enrollment.", ephemeral=True
132132
)
@@ -265,7 +265,7 @@ async def on_member_update(self, before: discord.Member, after: discord.Member)
265265
before_roles = {role.id for role in before.roles}
266266
after_roles = {role.id for role in after.roles}
267267

268-
if LDAP_BASE_STAFF_ROLE in before_roles or LDAP_BASE_STAFF_ROLE in after_roles:
268+
if HELPER_ROLE_ID in before_roles or HELPER_ROLE_ID in after_roles:
269269
await self.sync_users()
270270

271271
async def bootstrap(self, user: discord.Member) -> tuple[BootstrapType, str, str | None]:
@@ -375,7 +375,7 @@ async def get_user_diff(
375375

376376
enrolled_roles = {mapping["discord_role_id"] for mapping in LDAP_ROLE_MAPPING.values()}
377377

378-
base_role = guild.get_role(LDAP_BASE_STAFF_ROLE)
378+
base_role = guild.get_role(HELPER_ROLE_ID)
379379

380380
diff = []
381381
missing_emp = [user for user in users if user.employee_number is None]

arthur/exts/github/management.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from arthur.apis.github import GitHubError, add_member_to_team, remove_org_member
88
from arthur.config import CONFIG
9+
from arthur.constants import LDAP_ROLE_MAPPING
910

1011
if TYPE_CHECKING:
1112
from arthur.bot import KingArthurTheTerrible
@@ -35,7 +36,7 @@ async def github(self, ctx: Context) -> None:
3536
async def add_team_member(self, ctx: Context, username: str) -> None:
3637
"""Add a user to the default GitHub team."""
3738
try:
38-
await add_member_to_team(username, CONFIG.github_team)
39+
await add_member_to_team(username, LDAP_ROLE_MAPPING["helpers"]["github_team_slug"])
3940
await ctx.send(f":white_check_mark: Successfully invited {username} to the staff team.")
4041
except GitHubError as e:
4142
await ctx.send(f":x: Failed to add {username} to the staff team: {e}")

0 commit comments

Comments
 (0)