From 475fbf3952e8124c2dd11c6da9abd87741ccc4e1 Mon Sep 17 00:00:00 2001 From: magic_rb Date: Sun, 2 Jun 2024 16:13:14 +0200 Subject: [PATCH] Fix GitHub avatars for GitHub Apps Signed-off-by: magic_rb --- buildbot_nix/__init__.py | 14 ++++++++------ buildbot_nix/github_projects.py | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/buildbot_nix/__init__.py b/buildbot_nix/__init__.py index abd18dc..2b1379f 100644 --- a/buildbot_nix/__init__.py +++ b/buildbot_nix/__init__.py @@ -889,13 +889,15 @@ class NixConfigurator(ConfiguratorBase): backend.create_change_hook() ) - if "auth" not in config["www"]: - config["www"].setdefault("avatar_methods", []) + config["www"].setdefault("avatar_methods", []) - for backend in backends.values(): - avatar_method = backend.create_avatar_method() - if avatar_method is not None: - config["www"]["avatar_methods"].append(avatar_method) + for backend in backends.values(): + avatar_method = backend.create_avatar_method() + print(avatar_method) + if avatar_method is not None: + config["www"]["avatar_methods"].append(avatar_method) + + if "auth" not in config["www"]: # TODO one cannot have multiple auth backends... if auth is not None: config["www"]["auth"] = auth diff --git a/buildbot_nix/github_projects.py b/buildbot_nix/github_projects.py index 289d7c4..2901b14 100644 --- a/buildbot_nix/github_projects.py +++ b/buildbot_nix/github_projects.py @@ -533,7 +533,22 @@ class GithubBackend(GitBackend): } def create_avatar_method(self) -> AvatarBase | None: - return AvatarGitHub(token=self.auth_backend.get_general_token().get()) + avatar = AvatarGitHub(token=self.auth_backend.get_general_token().get()) + + # TODO: not a proper fix, the /users/{username} endpoint is per installation, but I'm not sure + # how to tell which installation token to use, unless there is a way to build a huge map of + # username -> token, or we just try each one in order + def _get_avatar_by_username(self: Any, username: Any) -> Any: + return f"https://github.com/{username}.png" + + import types + + avatar._get_avatar_by_username = types.MethodType( # noqa: SLF001 + _get_avatar_by_username, + avatar, + ) + + return avatar def create_auth(self) -> AuthBase: assert self.config.oauth_id is not None, "GitHub OAuth ID is required"