From eb994d8c75c27f3627e179e910570c31374494f9 Mon Sep 17 00:00:00 2001 From: Anurag Ojha Date: Fri, 24 Oct 2025 06:43:14 +0000 Subject: [PATCH 1/3] Check for existing GitLab webhook before creating to avoid duplicates --- pkg/git/gitlab/gitlab.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/git/gitlab/gitlab.go b/pkg/git/gitlab/gitlab.go index 849c0127b4..2099eff009 100644 --- a/pkg/git/gitlab/gitlab.go +++ b/pkg/git/gitlab/gitlab.go @@ -21,14 +21,27 @@ func (c Client) CreateWebHook(ctx context.Context, repoOwner, repoName, payloadU if err != nil { return fmt.Errorf("cannot create GitLab client: %w", err) } + + projectPath := repoOwner + "/" + repoName + + existingHooks, _, err := glabCli.Projects.ListProjectHooks(projectPath) + if err != nil { + return fmt.Errorf("cannot list existing hooks: %w", err) + } + for _, hook := range existingHooks { + if hook.URL == payloadURL { + return nil + } + } + webhook := &gitlab.AddProjectHookOptions{ EnableSSLVerification: &f, PushEvents: &t, Token: &webhookSecret, URL: &payloadURL, } - // TODO check if the WebHook already exists. GitLab doesn't name WebHooks so there is never 403. - _, _, err = glabCli.Projects.AddProjectHook(repoOwner+"/"+repoName, webhook) + + _, _, err = glabCli.Projects.AddProjectHook(projectPath, webhook) if err != nil { return fmt.Errorf("cannot create gitlab webhook: %w", err) } From 05f02788a1a6d88bf259e323ebe17bbb54cf8862 Mon Sep 17 00:00:00 2001 From: Anurag Ojha Date: Fri, 24 Oct 2025 07:01:25 +0000 Subject: [PATCH 2/3] path issue --- pkg/git/gitlab/gitlab.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/git/gitlab/gitlab.go b/pkg/git/gitlab/gitlab.go index 2099eff009..93b34f9ee9 100644 --- a/pkg/git/gitlab/gitlab.go +++ b/pkg/git/gitlab/gitlab.go @@ -21,10 +21,10 @@ func (c Client) CreateWebHook(ctx context.Context, repoOwner, repoName, payloadU if err != nil { return fmt.Errorf("cannot create GitLab client: %w", err) } - + projectPath := repoOwner + "/" + repoName - existingHooks, _, err := glabCli.Projects.ListProjectHooks(projectPath) + existingHooks, _, err := glabCli.Projects.ListProjectHooks(projectPath, nil) if err != nil { return fmt.Errorf("cannot list existing hooks: %w", err) } From 529932035ff21581f7007220dc1f4c884a09bba5 Mon Sep 17 00:00:00 2001 From: Anurag Ojha Date: Thu, 30 Oct 2025 18:58:01 +0000 Subject: [PATCH 3/3] gitlab hooks --- pkg/git/gitlab/gitlab.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/git/gitlab/gitlab.go b/pkg/git/gitlab/gitlab.go index 93b34f9ee9..5a0f3f9ded 100644 --- a/pkg/git/gitlab/gitlab.go +++ b/pkg/git/gitlab/gitlab.go @@ -30,6 +30,7 @@ func (c Client) CreateWebHook(ctx context.Context, repoOwner, repoName, payloadU } for _, hook := range existingHooks { if hook.URL == payloadURL { + fmt.Printf("GitLab webhook already exists for project %s at URL: %s\n", projectPath, payloadURL) return nil } }