From 8e31abbf88cd62911f0a0ee77e248c0f34ccde7c Mon Sep 17 00:00:00 2001 From: Robert Cerven Date: Tue, 10 Dec 2024 21:33:46 +0100 Subject: [PATCH] when offbording don't remove webhook from gitlab if other component is using same repository as well KONFLUX-5944 Signed-off-by: Robert Cerven --- controllers/component_build_controller_pac.go | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/controllers/component_build_controller_pac.go b/controllers/component_build_controller_pac.go index d814b7fb..9c89e58a 100644 --- a/controllers/component_build_controller_pac.go +++ b/controllers/component_build_controller_pac.go @@ -33,6 +33,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" ctrllog "sigs.k8s.io/controller-runtime/pkg/log" "github.com/konflux-ci/build-service/pkg/boerrors" @@ -535,7 +536,26 @@ func (r *ComponentBuildReconciler) UnconfigureRepositoryForPaC(ctx context.Conte isAppUsed := IsPaCApplicationConfigured(gitProvider, pacConfig) if !isAppUsed { - if webhookTargetUrl != "" { + componentList := &appstudiov1alpha1.ComponentList{} + if err := r.Client.List(ctx, componentList, &client.ListOptions{Namespace: component.Namespace}); err != nil { + log.Error(err, "failed to list components") + return "", "", "", err + } + gitUrl := strings.TrimSuffix(strings.TrimSuffix(component.Spec.Source.GitSource.URL, ".git"), "/") + + sameRepoUsed := false + for _, comp := range componentList.Items { + if comp.Name == component.Name { + continue + } + componentUrl := strings.TrimSuffix(strings.TrimSuffix(comp.Spec.Source.GitSource.URL, ".git"), "/") + if componentUrl == gitUrl { + sameRepoUsed = true + break + } + } + + if webhookTargetUrl != "" && !sameRepoUsed { err = gitClient.DeletePaCWebhook(repoUrl, webhookTargetUrl) if err != nil { // Just log the error and continue with merge request creation