From eeb21e9dddc9e457f657c2506191651fcdb5d0ec Mon Sep 17 00:00:00 2001 From: magic_rb Date: Wed, 12 Jun 2024 17:19:11 +0200 Subject: [PATCH] Streamline `Legacy` <-> `App` GitHub auth backend migrations Signed-off-by: magic_rb --- buildbot_nix/github_projects.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/buildbot_nix/github_projects.py b/buildbot_nix/github_projects.py index 6418500..1055084 100644 --- a/buildbot_nix/github_projects.py +++ b/buildbot_nix/github_projects.py @@ -153,12 +153,9 @@ class ReloadGithubProjects(BuildStep): super().__init__(**kwargs) def reload_projects(self) -> None: - repos: list[Any] = [] + repos: list[Any] = refresh_projects(self.token.get(), self.project_cache_file) - if self.project_cache_file.exists(): - repos = json.loads(self.project_cache_file.read_text()) - - refresh_projects(self.token.get(), self.project_cache_file) + log.msg(repos) atomic_write_file(self.project_cache_file, json.dumps(repos)) @@ -569,6 +566,19 @@ class GithubBackend(GitBackend): json.loads(self.config.project_cache_file.read_text()), key=lambda x: x["full_name"], ) + + if isinstance(self.auth_backend, GithubAppAuthBackend): + dropped_repos = list( + filter(lambda repo: not "installation_id" in repo, repos) + ) + if dropped_repos: + tlog.info( + "Dropped projects follow, refresh will follow after initialisation:" + ) + for dropped_repo in dropped_repos: + tlog.info(f"\tDropping repo {dropped_repo['full_name']}") + repos = list(filter(lambda repo: "installation_id" in repo, repos)) + tlog.info(f"Loading {len(repos)} cached repositories.") return list( filter( @@ -587,7 +597,16 @@ class GithubBackend(GitBackend): ) def are_projects_cached(self) -> bool: - return self.config.project_cache_file.exists() + if not self.config.project_cache_file.exists(): + return False + + all_have_installation_id = True + for project in json.loads(self.config.project_cache_file.read_text()): + if not "installation_id" in project: + all_have_installation_id = False + break + + return all_have_installation_id @property def type(self) -> str: