From 437ebc49b93f968e2f21a147e2bfc8028c4911d5 Mon Sep 17 00:00:00 2001 From: magic_rb Date: Mon, 15 Jul 2024 12:09:07 +0200 Subject: [PATCH] Support combined builds for Gitea Signed-off-by: magic_rb --- buildbot_nix/common.py | 9 +++++++++ buildbot_nix/gitea_projects.py | 27 +++++++++++++++++++++++++-- buildbot_nix/github_projects.py | 9 ++------- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/buildbot_nix/common.py b/buildbot_nix/common.py index 2b0c101..ed0a29d 100644 --- a/buildbot_nix/common.py +++ b/buildbot_nix/common.py @@ -172,3 +172,12 @@ def model_validate_project_cache(cls: type[_T], project_cache_file: Path) -> lis def model_dump_project_cache(repos: list[_T]) -> str: return json.dumps([repo.model_dump() for repo in repos]) + + +def filter_for_combined_builds(reports: Any) -> Any | None: + properties = reports[0]["builds"][0]["properties"] + + if "report_status" in properties and not properties["report_status"][0]: + return None + return reports + diff --git a/buildbot_nix/gitea_projects.py b/buildbot_nix/gitea_projects.py index 9eef8eb..770dc49 100644 --- a/buildbot_nix/gitea_projects.py +++ b/buildbot_nix/gitea_projects.py @@ -1,7 +1,7 @@ import os import signal from pathlib import Path -from typing import Any +from typing import Any, Callable from urllib.parse import urlparse from buildbot.config.builder import BuilderConfig @@ -15,10 +15,12 @@ from buildbot_gitea.reporter import GiteaStatusPush # type: ignore[import] from pydantic import BaseModel from twisted.logger import Logger from twisted.python import log +from twisted.internet import defer from .common import ( ThreadDeferredBuildStep, atomic_write_file, + filter_for_combined_builds, filter_repos_by_topic, http_request, model_dump_project_cache, @@ -104,6 +106,26 @@ class GiteaProject(GitProject): # TODO Gitea doesn't include this information return False # self.data["owner"]["type"] == "Organization" +class ModifyingGiteaStatusPush(GiteaStatusPush): + def checkConfig(self, modifyingFilter: Callable[[Any], Any | None] = lambda x: x, **kwargs: Any) -> Any: + self.modifyingFilter = modifyingFilter + + return super().checkConfig(**kwargs) + + def reconfigService(self, modifyingFilter: Callable[[Any], Any | None] = lambda x: x, **kwargs: Any) -> Any: + self.modifyingFilter = modifyingFilter + + return super().reconfigService(**kwargs) + + @defer.inlineCallbacks + def sendMessage(self, reports: Any) -> Any: + reports = self.modifyingFilter(reports) + if reports is None: + return + + result = yield super().sendMessage(reports) + return result + class GiteaBackend(GitBackend): config: GiteaConfig @@ -127,11 +149,12 @@ class GiteaBackend(GitBackend): ) def create_reporter(self) -> ReporterBase: - return GiteaStatusPush( + return ModifyingGiteaStatusPush( self.config.instance_url, Interpolate(self.config.token), context=Interpolate("buildbot/%(prop:status_name)s"), context_pr=Interpolate("buildbot/%(prop:status_name)s"), + modifyingFilter=filter_for_combined_builds, ) def create_change_hook(self) -> dict[str, Any]: diff --git a/buildbot_nix/github_projects.py b/buildbot_nix/github_projects.py index b605d72..35d5fe5 100644 --- a/buildbot_nix/github_projects.py +++ b/buildbot_nix/github_projects.py @@ -25,6 +25,7 @@ from twisted.internet import defer from .common import ( ThreadDeferredBuildStep, atomic_write_file, + filter_for_combined_builds, filter_repos_by_topic, http_request, model_dump_project_cache, @@ -330,13 +331,6 @@ class ModifyingGitHubStatusPush(GitHubStatusPush): result = yield super().sendMessage(reports) return result -def filter_for_combined_builds(reports: Any) -> Any | None: - properties = reports[0]["builds"][0]["properties"] - - if "report_status" in properties and not properties["report_status"][0]: - return None - return reports - class GithubLegacyAuthBackend(GithubAuthBackend): auth_type: GitHubLegacyConfig @@ -444,6 +438,7 @@ class GithubAppAuthBackend(GithubAuthBackend): self.project_id_map[props["projectname"]] ].get() + return ModifyingGitHubStatusPush( token=WithProperties("%(github_token)s", github_token=get_github_token), # Since we dynamically create build steps,