From d44dc3da09249673838822a51c5385e895691330 Mon Sep 17 00:00:00 2001 From: magic_rb Date: Wed, 24 Jul 2024 17:24:47 +0200 Subject: [PATCH] Fix Gitea backend using the Gitea url as a webhook url Signed-off-by: magic_rb --- buildbot_nix/__init__.py | 2 +- buildbot_nix/gitea_projects.py | 43 +++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/buildbot_nix/__init__.py b/buildbot_nix/__init__.py index 36cc22e..2bab8d5 100644 --- a/buildbot_nix/__init__.py +++ b/buildbot_nix/__init__.py @@ -908,7 +908,7 @@ class NixConfigurator(ConfiguratorBase): backends["github"] = GithubBackend(self.config.github, self.config.url) if self.config.gitea is not None: - backends["gitea"] = GiteaBackend(self.config.gitea) + backends["gitea"] = GiteaBackend(self.config.gitea, self.config.url) auth: AuthBase | None = ( backends[self.config.auth_backend].create_auth() diff --git a/buildbot_nix/gitea_projects.py b/buildbot_nix/gitea_projects.py index 770dc49..1282b26 100644 --- a/buildbot_nix/gitea_projects.py +++ b/buildbot_nix/gitea_projects.py @@ -129,9 +129,12 @@ class ModifyingGiteaStatusPush(GiteaStatusPush): class GiteaBackend(GitBackend): config: GiteaConfig + webhook_secret: str + instance_url: str - def __init__(self, config: GiteaConfig) -> None: + def __init__(self, config: GiteaConfig, instance_url: str) -> None: self.config = config + self.instance_url = instance_url def create_reload_builder(self, worker_names: list[str]) -> BuilderConfig: """Updates the flake an opens a PR for it.""" @@ -140,7 +143,10 @@ class GiteaBackend(GitBackend): ReloadGiteaProjects(self.config, self.config.project_cache_file), ) factory.addStep( - CreateGiteaProjectHooks(self.config), + CreateGiteaProjectHooks( + self.config, + self.instance_url, + ), ) return util.BuilderConfig( name=self.reload_builder_name, @@ -150,8 +156,8 @@ class GiteaBackend(GitBackend): def create_reporter(self) -> ReporterBase: return ModifyingGiteaStatusPush( - self.config.instance_url, - Interpolate(self.config.token), + baseURL=self.config.instance_url, + token=Interpolate(self.config.token), context=Interpolate("buildbot/%(prop:status_name)s"), context_pr=Interpolate("buildbot/%(prop:status_name)s"), modifyingFilter=filter_for_combined_builds, @@ -221,14 +227,19 @@ class GiteaBackend(GitBackend): def create_repo_hook( - token: str, webhook_secret: str, owner: str, repo: str, webhook_url: str + token: str, + webhook_secret: str, + owner: str, + repo: str, + gitea_url: str, + instance_url: str, ) -> None: hooks = paginated_github_request( - f"{webhook_url}/api/v1/repos/{owner}/{repo}/hooks?limit=100", + f"{gitea_url}/api/v1/repos/{owner}/{repo}/hooks?limit=100", token, ) config = dict( - url=webhook_url + "change_hook/gitea", + url=instance_url + "change_hook/gitea", content_type="json", insecure_ssl="0", secret=webhook_secret, @@ -246,13 +257,13 @@ def create_repo_hook( "Content-Type": "application/json", } for hook in hooks: - if hook["config"]["url"] == webhook_url + "change_hook/gitea": + if hook["config"]["url"] == instance_url + "change_hook/gitea": log.msg(f"hook for {owner}/{repo} already exists") return log.msg(f"creating hook for {owner}/{repo}") http_request( - f"{webhook_url}/api/v1/repos/{owner}/{repo}/hooks", + f"{gitea_url}/api/v1/repos/{owner}/{repo}/hooks", method="POST", headers=headers, data=data, @@ -263,13 +274,16 @@ class CreateGiteaProjectHooks(ThreadDeferredBuildStep): name = "create_gitea_project_hooks" config: GiteaConfig + instance_url: str def __init__( self, config: GiteaConfig, + instance_url: str, **kwargs: Any, ) -> None: self.config = config + self.instance_url = instance_url super().__init__(**kwargs) def run_deferred(self) -> None: @@ -277,11 +291,12 @@ class CreateGiteaProjectHooks(ThreadDeferredBuildStep): for repo in repos: create_repo_hook( - self.config.token, - self.config.webhook_secret, - repo.owner.login, - repo.name, - self.config.instance_url, + token=self.config.token, + webhook_secret=self.config.webhook_secret, + owner=repo.owner.login, + repo=repo.name, + gitea_url=self.config.instance_url, + instance_url=self.instance_url, ) def run_post(self) -> Any: