diff --git a/Makefile b/Makefile index ca16975da..1808d63d0 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,10 @@ clean-gitops-repositories: DRY_RUN=false ./mage -v local:cleanupGithubOrg clean-github-webhooks: - ./mage -v cleanWebHooks + ./mage -v cleanGitHubWebHooks + +clean-gitlab-webhooks: + ./mage -v cleanGitLabWebHooks clean-quay-repos-and-robots: ./mage -v local:cleanupQuayReposAndRobots diff --git a/magefiles/magefile.go b/magefiles/magefile.go index d8f0eb753..4b2841de5 100644 --- a/magefiles/magefile.go +++ b/magefiles/magefile.go @@ -26,6 +26,7 @@ import ( "github.com/konflux-ci/e2e-tests/magefiles/installation" "github.com/konflux-ci/e2e-tests/magefiles/upgrade" "github.com/konflux-ci/e2e-tests/pkg/clients/github" + "github.com/konflux-ci/e2e-tests/pkg/clients/gitlab" "github.com/konflux-ci/e2e-tests/pkg/clients/slack" "github.com/konflux-ci/e2e-tests/pkg/clients/sprayproxy" "github.com/konflux-ci/e2e-tests/pkg/constants" @@ -35,6 +36,7 @@ import ( "github.com/konflux-ci/image-controller/pkg/quay" "github.com/magefile/mage/sh" tektonapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + gl "github.com/xanzy/go-gitlab" ) const ( @@ -823,8 +825,9 @@ func GenerateTestSuiteFile(packageName string) error { return nil } -// Remove all webhooks which with 1 day lifetime. By default will delete webooks from redhat-appstudio-qe -func CleanWebHooks() error { +// Remove all webhooks older than 1 day from GitHub repo. +// By default will delete webhooks from redhat-appstudio-qe +func CleanGitHubWebHooks() error { token := utils.GetEnv(constants.GITHUB_TOKEN_ENV, "") if token == "" { return fmt.Errorf("empty GITHUB_TOKEN env. Please provide a valid github token") @@ -853,6 +856,38 @@ func CleanWebHooks() error { return nil } +// Remove all webhooks older than 1 day from GitLab repo. +func CleanGitLabWebHooks() error { + gcToken := utils.GetEnv(constants.GITLAB_TOKEN_ENV, "") + if gcToken == "" { + return fmt.Errorf("empty PAC_GITLAB_TOKEN env") + } + projectID := utils.GetEnv(constants.GITLAB_PROJECT_ID, "") + if projectID == "" { + return fmt.Errorf("empty PAC_PROJECT_ID env. Please provide a valid GitLab Project ID") + } + gitlabURL := utils.GetEnv(constants.GITLAB_URL_ENV, "https://gitlab.com/api/v4") + gc, err := gitlab.NewGitlabClient(gcToken, gitlabURL) + if err != nil { + return err + } + webhooks, _, err := gc.GetClient().Projects.ListProjectHooks(projectID, &gl.ListProjectHooksOptions{PerPage: 100}) + if err != nil { + return fmt.Errorf("failed to list project hooks: %v", err) + } + // Delete webhooks that are older than 1 day + for _, webhook := range webhooks { + dayDuration, _ := time.ParseDuration("24h") + if time.Since(*webhook.CreatedAt) > dayDuration { + klog.Infof("removing webhookURL: %s", webhook.URL) + if _, err := gc.GetClient().Projects.DeleteProjectHook(projectID, webhook.ID); err != nil { + return fmt.Errorf("failed to delete webhook (URL: %s): %v", webhook.URL, err) + } + } + } + return nil +} + // Generate a Text Outline file from a Ginkgo Spec func GenerateTextOutlineFromGinkgoSpec(source string, destination string) error {