From 1ad6535948a7699b51a3732d7798572be17fe0a5 Mon Sep 17 00:00:00 2001 From: Kasem Alem Date: Thu, 6 Jun 2024 19:38:49 +0300 Subject: [PATCH] feat(stoneintg-937): add cleanup of webhooks in afterall (#1191) Signed-off-by: Kasem Alem --- pkg/clients/gitlab/git.go | 29 +++++++++++++++++++ .../gitlab-integration-reporting.go | 3 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/clients/gitlab/git.go b/pkg/clients/gitlab/git.go index 7d30a7346..d456c3d6c 100644 --- a/pkg/clients/gitlab/git.go +++ b/pkg/clients/gitlab/git.go @@ -3,6 +3,7 @@ package gitlab import ( "fmt" "net/http" + "strings" "time" . "github.com/onsi/gomega" @@ -120,3 +121,31 @@ func (gc *GitlabClient) CloseMergeRequest(projectID string, mergeRequestIID int) return nil } + +// DeleteWebhooks deletes webhooks in Gitlab repo by given project ID, +// and if the webhook URL contains the cluster's domain name. +func (gc *GitlabClient) DeleteWebhooks(projectID, clusterAppDomain string) error { + + // Check if clusterAppDomain is empty returns error, else continue + if clusterAppDomain == "" { + return fmt.Errorf("Framework.ClusterAppDomain is empty") + } + + // List project hooks + webhooks, _, err := gc.client.Projects.ListProjectHooks(projectID, nil) + if err != nil { + return fmt.Errorf("failed to list project hooks: %v", err) + } + + // Delete matching webhooks + for _, webhook := range webhooks { + if strings.Contains(webhook.URL, clusterAppDomain) { + if _, err := gc.client.Projects.DeleteProjectHook(projectID, webhook.ID); err != nil { + return fmt.Errorf("failed to delete webhook (ID: %d): %v", webhook.ID, err) + } + break + } + } + + return nil +} diff --git a/tests/integration-service/gitlab-integration-reporting.go b/tests/integration-service/gitlab-integration-reporting.go index e11c0b816..b2095bb37 100644 --- a/tests/integration-service/gitlab-integration-reporting.go +++ b/tests/integration-service/gitlab-integration-reporting.go @@ -99,9 +99,10 @@ var _ = framework.IntegrationServiceSuiteDescribe("Gitlab Status Reporting of In AfterAll(func() { if !CurrentSpecReport().Failed() { - // Cleanup test, close MR if opned delete created brnach , delete usersignup and namespace. + // Cleanup test: close MR if opened, delete created branch, delete associated Webhooks and delete usersignup Expect(f.AsKubeAdmin.CommonController.Gitlab.CloseMergeRequest(projectID, mrID)).NotTo(HaveOccurred()) Expect(f.AsKubeAdmin.CommonController.Gitlab.DeleteBranch(projectID, componentBaseBranchName)).NotTo(HaveOccurred()) + Expect(f.AsKubeAdmin.CommonController.Gitlab.DeleteWebhooks(projectID, f.ClusterAppDomain)).NotTo(HaveOccurred()) Expect(f.SandboxController.DeleteUserSignup(f.UserName)).To(BeTrue()) }