Skip to content

Commit 475fbf3

Browse files
MagicRBMic92
authored andcommitted
Fix GitHub avatars for GitHub Apps
Signed-off-by: magic_rb <richard@brezak.sk>
1 parent 5dc05ea commit 475fbf3

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

buildbot_nix/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,13 +889,15 @@ def configure(self, config: dict[str, Any]) -> None:
889889
backend.create_change_hook()
890890
)
891891

892-
if "auth" not in config["www"]:
893-
config["www"].setdefault("avatar_methods", [])
892+
config["www"].setdefault("avatar_methods", [])
893+
894+
for backend in backends.values():
895+
avatar_method = backend.create_avatar_method()
896+
print(avatar_method)
897+
if avatar_method is not None:
898+
config["www"]["avatar_methods"].append(avatar_method)
894899

895-
for backend in backends.values():
896-
avatar_method = backend.create_avatar_method()
897-
if avatar_method is not None:
898-
config["www"]["avatar_methods"].append(avatar_method)
900+
if "auth" not in config["www"]:
899901
# TODO one cannot have multiple auth backends...
900902
if auth is not None:
901903
config["www"]["auth"] = auth

buildbot_nix/github_projects.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,22 @@ def create_change_hook(self) -> dict[str, Any]:
533533
}
534534

535535
def create_avatar_method(self) -> AvatarBase | None:
536-
return AvatarGitHub(token=self.auth_backend.get_general_token().get())
536+
avatar = AvatarGitHub(token=self.auth_backend.get_general_token().get())
537+
538+
# TODO: not a proper fix, the /users/{username} endpoint is per installation, but I'm not sure
539+
# how to tell which installation token to use, unless there is a way to build a huge map of
540+
# username -> token, or we just try each one in order
541+
def _get_avatar_by_username(self: Any, username: Any) -> Any:
542+
return f"https://github.com/{username}.png"
543+
544+
import types
545+
546+
avatar._get_avatar_by_username = types.MethodType( # noqa: SLF001
547+
_get_avatar_by_username,
548+
avatar,
549+
)
550+
551+
return avatar
537552

538553
def create_auth(self) -> AuthBase:
539554
assert self.config.oauth_id is not None, "GitHub OAuth ID is required"

0 commit comments

Comments
 (0)