Fix Gitea backend using the Gitea url as a webhook url

Signed-off-by: magic_rb <richard@brezak.sk>
This commit is contained in:
magic_rb 2024-07-24 17:24:47 +02:00
parent 437ebc49b9
commit d44dc3da09
Failed to generate hash of commit
2 changed files with 30 additions and 15 deletions

View file

@ -908,7 +908,7 @@ class NixConfigurator(ConfiguratorBase):
backends["github"] = GithubBackend(self.config.github, self.config.url) backends["github"] = GithubBackend(self.config.github, self.config.url)
if self.config.gitea is not None: 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 = ( auth: AuthBase | None = (
backends[self.config.auth_backend].create_auth() backends[self.config.auth_backend].create_auth()

View file

@ -129,9 +129,12 @@ class ModifyingGiteaStatusPush(GiteaStatusPush):
class GiteaBackend(GitBackend): class GiteaBackend(GitBackend):
config: GiteaConfig 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.config = config
self.instance_url = instance_url
def create_reload_builder(self, worker_names: list[str]) -> BuilderConfig: def create_reload_builder(self, worker_names: list[str]) -> BuilderConfig:
"""Updates the flake an opens a PR for it.""" """Updates the flake an opens a PR for it."""
@ -140,7 +143,10 @@ class GiteaBackend(GitBackend):
ReloadGiteaProjects(self.config, self.config.project_cache_file), ReloadGiteaProjects(self.config, self.config.project_cache_file),
) )
factory.addStep( factory.addStep(
CreateGiteaProjectHooks(self.config), CreateGiteaProjectHooks(
self.config,
self.instance_url,
),
) )
return util.BuilderConfig( return util.BuilderConfig(
name=self.reload_builder_name, name=self.reload_builder_name,
@ -150,8 +156,8 @@ class GiteaBackend(GitBackend):
def create_reporter(self) -> ReporterBase: def create_reporter(self) -> ReporterBase:
return ModifyingGiteaStatusPush( return ModifyingGiteaStatusPush(
self.config.instance_url, baseURL=self.config.instance_url,
Interpolate(self.config.token), token=Interpolate(self.config.token),
context=Interpolate("buildbot/%(prop:status_name)s"), context=Interpolate("buildbot/%(prop:status_name)s"),
context_pr=Interpolate("buildbot/%(prop:status_name)s"), context_pr=Interpolate("buildbot/%(prop:status_name)s"),
modifyingFilter=filter_for_combined_builds, modifyingFilter=filter_for_combined_builds,
@ -221,14 +227,19 @@ class GiteaBackend(GitBackend):
def create_repo_hook( 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: ) -> None:
hooks = paginated_github_request( 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, token,
) )
config = dict( config = dict(
url=webhook_url + "change_hook/gitea", url=instance_url + "change_hook/gitea",
content_type="json", content_type="json",
insecure_ssl="0", insecure_ssl="0",
secret=webhook_secret, secret=webhook_secret,
@ -246,13 +257,13 @@ def create_repo_hook(
"Content-Type": "application/json", "Content-Type": "application/json",
} }
for hook in hooks: 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") log.msg(f"hook for {owner}/{repo} already exists")
return return
log.msg(f"creating hook for {owner}/{repo}") log.msg(f"creating hook for {owner}/{repo}")
http_request( http_request(
f"{webhook_url}/api/v1/repos/{owner}/{repo}/hooks", f"{gitea_url}/api/v1/repos/{owner}/{repo}/hooks",
method="POST", method="POST",
headers=headers, headers=headers,
data=data, data=data,
@ -263,13 +274,16 @@ class CreateGiteaProjectHooks(ThreadDeferredBuildStep):
name = "create_gitea_project_hooks" name = "create_gitea_project_hooks"
config: GiteaConfig config: GiteaConfig
instance_url: str
def __init__( def __init__(
self, self,
config: GiteaConfig, config: GiteaConfig,
instance_url: str,
**kwargs: Any, **kwargs: Any,
) -> None: ) -> None:
self.config = config self.config = config
self.instance_url = instance_url
super().__init__(**kwargs) super().__init__(**kwargs)
def run_deferred(self) -> None: def run_deferred(self) -> None:
@ -277,11 +291,12 @@ class CreateGiteaProjectHooks(ThreadDeferredBuildStep):
for repo in repos: for repo in repos:
create_repo_hook( create_repo_hook(
self.config.token, token=self.config.token,
self.config.webhook_secret, webhook_secret=self.config.webhook_secret,
repo.owner.login, owner=repo.owner.login,
repo.name, repo=repo.name,
self.config.instance_url, gitea_url=self.config.instance_url,
instance_url=self.instance_url,
) )
def run_post(self) -> Any: def run_post(self) -> Any: