Skip to content

Commit

Permalink
feat(STONEINTG-937): add nightly Gitlab webhooks cleanup (#1216)
Browse files Browse the repository at this point in the history
* feat(STONEINTG-937): add nightly Gitlab webhooks cleanup

Signed-off-by: Kasem Alem <kasem.alem@gmail.com>

* chore(STONEINTG-937): improve function names and comments

Signed-off-by: Dheeraj<djodha@redhat.com>

* fix: list all gitlab hooks

---------

Signed-off-by: Kasem Alem <kasem.alem@gmail.com>
Signed-off-by: Dheeraj<djodha@redhat.com>
Co-authored-by: Dheeraj Singh Jodha <dheerajsinghjodha@yahoo.com.au>
Co-authored-by: Pavel Sturc <psturc@redhat.com>
  • Loading branch information
3 people authored Jun 24, 2024
1 parent a1fd47c commit ed83b13
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 37 additions & 2 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit ed83b13

Please sign in to comment.