Skip to content

Commit

Permalink
feat(stoneintg-937): add cleanup of webhooks in afterall (konflux-ci#…
Browse files Browse the repository at this point in the history
…1191)

Signed-off-by: Kasem Alem <[email protected]>
  • Loading branch information
kasemAlem authored Jun 6, 2024
1 parent 4edb136 commit 1ad6535
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions pkg/clients/gitlab/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gitlab
import (
"fmt"
"net/http"
"strings"
"time"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion tests/integration-service/gitlab-integration-reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

}
Expand Down

0 comments on commit 1ad6535

Please sign in to comment.