mirror of
https://github.com/harness/drone.git
synced 2025-05-05 15:10:29 +00:00
This change introduces: - webhook type / store / controller / handler - webhookExecution type / store / controller / handler - foreign key fix for sqlite3
91 lines
2.0 KiB
Go
91 lines
2.0 KiB
Go
// Copyright 2022 Harness Inc. All rights reserved.
|
|
// Use of this source code is governed by the Polyform Free Trial License
|
|
// that can be found in the LICENSE.md file for this repository.
|
|
|
|
package webhook
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/harness/gitness/internal/auth"
|
|
"github.com/harness/gitness/types"
|
|
"github.com/harness/gitness/types/enum"
|
|
)
|
|
|
|
type UpdateInput struct {
|
|
URL *string `json:"url"`
|
|
Secret *string `json:"secret"`
|
|
Enabled *bool `json:"enabled"`
|
|
Insecure *bool `json:"insecure"`
|
|
Triggers []enum.WebhookTrigger `json:"triggers"`
|
|
}
|
|
|
|
// Update updates an existing webhook.
|
|
func (c *Controller) Update(
|
|
ctx context.Context,
|
|
session *auth.Session,
|
|
repoRef string,
|
|
webhookID int64,
|
|
in *UpdateInput,
|
|
) (*types.Webhook, error) {
|
|
repo, err := c.getRepoCheckAccess(ctx, session, repoRef, enum.PermissionRepoEdit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// get the hook and ensure it belongs to us
|
|
hook, err := c.getWebhookVerifyOwnership(ctx, repo.ID, webhookID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// validate input
|
|
if err = checkUpdateInput(in, c.allowLoopback, c.allowPrivateNetwork); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// update webhook struct (only for values that are provided)
|
|
if in.URL != nil {
|
|
hook.URL = *in.URL
|
|
}
|
|
if in.Secret != nil {
|
|
hook.Secret = *in.Secret
|
|
}
|
|
if in.Enabled != nil {
|
|
hook.Enabled = *in.Enabled
|
|
}
|
|
if in.Insecure != nil {
|
|
hook.Insecure = *in.Insecure
|
|
}
|
|
if in.Triggers != nil {
|
|
hook.Triggers = deduplicateTriggers(in.Triggers)
|
|
}
|
|
|
|
if err = c.webhookStore.Update(ctx, hook); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return hook, nil
|
|
}
|
|
|
|
func checkUpdateInput(in *UpdateInput, allowLoopback bool, allowPrivateNetwork bool) error {
|
|
if in.URL != nil {
|
|
if err := checkURL(*in.URL, allowLoopback, allowPrivateNetwork); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if in.Secret != nil {
|
|
if err := checkSecret(*in.Secret); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if in.Triggers != nil {
|
|
if err := checkTriggers(in.Triggers); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|