Skip to content

Commit d081d51

Browse files
committed
Check for existing GitLab webhook before creating to avoid duplicates
1 parent b990215 commit d081d51

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

pkg/git/gitlab/gitlab.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,27 @@ func (c Client) CreateWebHook(ctx context.Context, repoOwner, repoName, payloadU
2121
if err != nil {
2222
return fmt.Errorf("cannot create GitLab client: %w", err)
2323
}
24+
25+
projectPath := repoOwner + "/" + repoName
26+
27+
existingHooks, _, err := glabCli.Projects.ListProjectHooks(projectPath)
28+
if err != nil {
29+
return fmt.Errorf("cannot list existing hooks: %w", err)
30+
}
31+
for _, hook := range existingHooks {
32+
if hook.URL == payloadURL {
33+
return nil
34+
}
35+
}
36+
2437
webhook := &gitlab.AddProjectHookOptions{
2538
EnableSSLVerification: &f,
2639
PushEvents: &t,
2740
Token: &webhookSecret,
2841
URL: &payloadURL,
2942
}
30-
// TODO check if the WebHook already exists. GitLab doesn't name WebHooks so there is never 403.
31-
_, _, err = glabCli.Projects.AddProjectHook(repoOwner+"/"+repoName, webhook)
43+
44+
_, _, err = glabCli.Projects.AddProjectHook(projectPath, webhook)
3245
if err != nil {
3346
return fmt.Errorf("cannot create gitlab webhook: %w", err)
3447
}

0 commit comments

Comments
 (0)