Fix GitHub avatars for GitHub Apps

Signed-off-by: magic_rb <richard@brezak.sk>
This commit is contained in:
magic_rb 2024-06-02 16:13:14 +02:00 committed by Mic92
parent 5dc05ea3fe
commit 475fbf3952
2 changed files with 24 additions and 7 deletions

View file

@ -889,13 +889,15 @@ class NixConfigurator(ConfiguratorBase):
backend.create_change_hook() 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(): for backend in backends.values():
avatar_method = backend.create_avatar_method() avatar_method = backend.create_avatar_method()
if avatar_method is not None: print(avatar_method)
config["www"]["avatar_methods"].append(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... # TODO one cannot have multiple auth backends...
if auth is not None: if auth is not None:
config["www"]["auth"] = auth config["www"]["auth"] = auth

View file

@ -533,7 +533,22 @@ class GithubBackend(GitBackend):
} }
def create_avatar_method(self) -> AvatarBase | None: 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: def create_auth(self) -> AuthBase:
assert self.config.oauth_id is not None, "GitHub OAuth ID is required" assert self.config.oauth_id is not None, "GitHub OAuth ID is required"