Skip to content

Commit 71c89dc

Browse files
authored
Merge pull request #513 from nix-community/fix-github-reload-efficient
fix: update project maps directly in reload services for GitHub and G…
2 parents 3a39cdd + f5eb29c commit 71c89dc

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

buildbot_nix/buildbot_nix/gitea_projects.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ def create_reload_builder(self, worker_names: list[str]) -> BuilderConfig:
181181
"""Updates the flake an opens a PR for it."""
182182
factory = util.BuildFactory()
183183
factory.addStep(
184-
ReloadGiteaProjects(self.config, self.config.project_cache_file),
184+
ReloadGiteaProjects(
185+
self.config, self.config.project_cache_file, backend=self
186+
),
185187
)
186188
factory.addStep(
187189
CreateGiteaProjectHooks(
@@ -259,6 +261,10 @@ def load_projects(self) -> list[GitProject]:
259261
for repo in repos
260262
]
261263

264+
def update_projects(self, projects: set[str]) -> None:
265+
# Replace atomically to avoid mutating a set read elsewhere
266+
self.gitea_projects = projects
267+
262268
def are_projects_cached(self) -> bool:
263269
return self.config.project_cache_file.exists()
264270

@@ -371,10 +377,12 @@ def __init__(
371377
self,
372378
config: GiteaConfig,
373379
project_cache_file: Path,
380+
backend: GiteaBackend | None = None,
374381
**kwargs: Any,
375382
) -> None:
376383
self.config = config
377384
self.project_cache_file = project_cache_file
385+
self.backend = backend
378386
super().__init__(**kwargs)
379387

380388
def run_deferred(self) -> None:
@@ -390,6 +398,11 @@ def run_deferred(self) -> None:
390398

391399
atomic_write_file(self.project_cache_file, model_dump_project_cache(repos))
392400

401+
# Update the backend's in-memory gitea_projects set directly
402+
if self.backend:
403+
self.backend.update_projects({repo.full_name for repo in repos})
404+
tlog.info(f"Updated backend gitea_projects with {len(repos)} projects")
405+
393406
def run_post(self) -> Any:
394407
return util.SUCCESS
395408

buildbot_nix/buildbot_nix/github_projects.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,12 @@ def __init__(
192192
self,
193193
project_config: GitHubProjectConfig,
194194
app_config: GitHubAppInstallationConfig,
195+
backend: GithubAppAuthBackend | None = None,
195196
**kwargs: Any,
196197
) -> None:
197198
self.project_config = project_config
198199
self.app_config = app_config
200+
self.backend = backend
199201
super().__init__(**kwargs)
200202

201203
def run_deferred(self) -> None:
@@ -240,6 +242,13 @@ def run_deferred(self) -> None:
240242
self.app_config.project_id_map_name, json.dumps(project_id_map)
241243
)
242244

245+
# Update the backend's in-memory data directly
246+
if self.backend:
247+
self.backend.update_reload_data(installation_token_map, project_id_map)
248+
tlog.info(
249+
f"Updated backend with {len(installation_token_map)} installations and {len(project_id_map)} projects"
250+
)
251+
243252
tlog.info(
244253
f"Fetched {len(repos)} repositories from {len(installation_token_map.items())} installation tokens."
245254
)
@@ -373,6 +382,14 @@ def __init__(self, config: GitHubConfig) -> None:
373382
)
374383
self.project_id_map = {}
375384

385+
def update_reload_data(
386+
self,
387+
installation_tokens: dict[int, InstallationToken],
388+
project_map: dict[str, int],
389+
) -> None:
390+
self.installation_tokens = installation_tokens
391+
self.project_id_map = project_map
392+
376393
def get_general_token(self) -> RepoToken:
377394
return self.jwt_token
378395

@@ -422,6 +439,7 @@ def create_reload_builder_steps(
422439
ReloadGithubInstallations(
423440
project_config=config,
424441
app_config=app_config,
442+
backend=self,
425443
),
426444
CreateGitHubInstallationHooks(
427445
project_config=config,

0 commit comments

Comments
 (0)