webhook

package
v0.7.0-preview3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProviderSlack   = "slack"
	ProviderDiscord = "discord"
	ProviderGeneric = "generic"
)

Variables

View Source
var (
	ErrNotFound = errors.New("webhook not found")

	ValidProviders = map[string]bool{
		ProviderSlack:   true,
		ProviderDiscord: true,
		ProviderGeneric: true,
	}
)

Functions

func IsValidationError

func IsValidationError(err error) bool

IsValidationError reports whether err is a user-facing validation error.

Types

type CreateWebhookInput

type CreateWebhookInput struct {
	TeamID            string   `json:"team_id"`
	Name              string   `json:"name"`
	Provider          string   `json:"provider"`
	WebhookURL        string   `json:"webhook_url"`
	NotificationTypes []string `json:"notification_types"`
	Enabled           *bool    `json:"enabled"`
}

CreateWebhookInput is the input for creating a webhook.

type DiscordProvider

type DiscordProvider struct{}

DiscordProvider formats messages for Discord webhooks using embeds.

func (*DiscordProvider) ContentType

func (p *DiscordProvider) ContentType() string

func (*DiscordProvider) FormatMessage

func (p *DiscordProvider) FormatMessage(notification WebhookNotification) ([]byte, error)

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}

Dispatcher handles async delivery of webhook notifications.

func NewDispatcher

func NewDispatcher(repo Repository, registry *ProviderRegistry, config DispatcherConfig) *Dispatcher

NewDispatcher creates a new webhook dispatcher.

func (*Dispatcher) Dispatch

func (d *Dispatcher) Dispatch(webhook *Webhook, notification WebhookNotification)

Dispatch queues a webhook delivery job.

func (*Dispatcher) Start

func (d *Dispatcher) Start(ctx context.Context)

Start begins the dispatcher worker pool.

func (*Dispatcher) Stop

func (d *Dispatcher) Stop()

Stop gracefully shuts down the dispatcher.

type DispatcherConfig

type DispatcherConfig struct {
	MaxWorkers int
	QueueSize  int
	Timeout    time.Duration
	MaxRetries int
	RetryDelay time.Duration
}

DispatcherConfig configures the webhook dispatcher.

type GenericProvider

type GenericProvider struct{}

GenericProvider formats messages as a simple JSON payload for custom integrations.

func (*GenericProvider) ContentType

func (p *GenericProvider) ContentType() string

func (*GenericProvider) FormatMessage

func (p *GenericProvider) FormatMessage(notification WebhookNotification) ([]byte, error)

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient interface for testability.

type PostgresRepository

type PostgresRepository struct {
	// contains filtered or unexported fields
}

func (*PostgresRepository) Create

func (r *PostgresRepository) Create(ctx context.Context, webhook *Webhook) error

func (*PostgresRepository) Delete

func (r *PostgresRepository) Delete(ctx context.Context, id string) error

func (*PostgresRepository) Get

func (r *PostgresRepository) Get(ctx context.Context, id string) (*Webhook, error)

func (*PostgresRepository) GetEnabledForNotificationType

func (r *PostgresRepository) GetEnabledForNotificationType(ctx context.Context, teamID string, notificationType string) ([]*Webhook, error)

func (*PostgresRepository) ListByTeam

func (r *PostgresRepository) ListByTeam(ctx context.Context, teamID string) ([]*Webhook, error)

func (*PostgresRepository) Update

func (*PostgresRepository) UpdateLastTriggered

func (r *PostgresRepository) UpdateLastTriggered(ctx context.Context, id string, lastError *string) error

type Provider

type Provider interface {
	// FormatMessage converts a notification into the provider-specific payload.
	FormatMessage(notification WebhookNotification) ([]byte, error)
	// ContentType returns the HTTP Content-Type header for requests.
	ContentType() string
}

Provider formats notification messages for a specific webhook destination.

type ProviderRegistry

type ProviderRegistry struct {
	// contains filtered or unexported fields
}

ProviderRegistry holds all registered webhook providers.

func DefaultRegistry

func DefaultRegistry() *ProviderRegistry

DefaultRegistry creates a registry with the built-in providers.

func NewProviderRegistry

func NewProviderRegistry() *ProviderRegistry

NewProviderRegistry creates a new empty registry.

func (*ProviderRegistry) Get

func (r *ProviderRegistry) Get(name string) (Provider, bool)

Get retrieves a provider by name.

func (*ProviderRegistry) Register

func (r *ProviderRegistry) Register(name string, provider Provider)

Register adds a provider to the registry.

type Repository

type Repository interface {
	Create(ctx context.Context, webhook *Webhook) error
	Get(ctx context.Context, id string) (*Webhook, error)
	Update(ctx context.Context, id string, input UpdateWebhookInput) (*Webhook, error)
	Delete(ctx context.Context, id string) error
	ListByTeam(ctx context.Context, teamID string) ([]*Webhook, error)
	GetEnabledForNotificationType(ctx context.Context, teamID string, notificationType string) ([]*Webhook, error)
	UpdateLastTriggered(ctx context.Context, id string, lastError *string) error
}

Repository defines the webhook data access interface.

func NewPostgresRepository

func NewPostgresRepository(db *pgxpool.Pool) Repository

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service handles webhook operations.

func NewService

func NewService(repo Repository, encryptor *crypto.Encryptor, dispatcher *Dispatcher) *Service

NewService creates a new webhook service.

func (*Service) Create

func (s *Service) Create(ctx context.Context, input CreateWebhookInput) (*Webhook, error)

Create creates a new webhook.

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, id string) error

Delete deletes a webhook.

func (*Service) DispatchToTeam

func (s *Service) DispatchToTeam(ctx context.Context, teamID, notificationType, title, message string, data map[string]interface{})

DispatchToTeam finds matching webhooks for a team and dispatches the notification. This implements the ExternalNotifier interface for the notification service.

func (*Service) Get

func (s *Service) Get(ctx context.Context, id string) (*Webhook, error)

Get retrieves a webhook by ID with the URL decrypted.

func (*Service) GetMasked

func (s *Service) GetMasked(ctx context.Context, id string) (*Webhook, error)

GetMasked retrieves a webhook by ID with the URL masked.

func (*Service) ListByTeam

func (s *Service) ListByTeam(ctx context.Context, teamID string) ([]*Webhook, error)

ListByTeam lists all webhooks for a team with masked URLs.

func (*Service) TestWebhook

func (s *Service) TestWebhook(ctx context.Context, id string) error

TestWebhook sends a test notification to the webhook.

func (*Service) Update

func (s *Service) Update(ctx context.Context, id string, input UpdateWebhookInput) (*Webhook, error)

Update updates a webhook.

type SlackProvider

type SlackProvider struct{}

SlackProvider formats messages for Slack incoming webhooks using Block Kit.

func (*SlackProvider) ContentType

func (p *SlackProvider) ContentType() string

func (*SlackProvider) FormatMessage

func (p *SlackProvider) FormatMessage(notification WebhookNotification) ([]byte, error)

type UpdateWebhookInput

type UpdateWebhookInput struct {
	Name              *string  `json:"name,omitempty"`
	WebhookURL        *string  `json:"webhook_url,omitempty"`
	NotificationTypes []string `json:"notification_types,omitempty"`
	Enabled           *bool    `json:"enabled,omitempty"`
}

UpdateWebhookInput is the input for updating a webhook.

type ValidationError

type ValidationError struct {
	Message string
}

ValidationError represents a user-facing validation failure.

func (*ValidationError) Error

func (e *ValidationError) Error() string

type Webhook

type Webhook struct {
	ID                string     `json:"id"`
	TeamID            string     `json:"team_id"`
	Name              string     `json:"name"`
	Provider          string     `json:"provider"`
	WebhookURL        string     `json:"webhook_url"`
	NotificationTypes []string   `json:"notification_types"`
	Enabled           bool       `json:"enabled"`
	LastTriggeredAt   *time.Time `json:"last_triggered_at,omitempty"`
	LastError         *string    `json:"last_error,omitempty"`
	CreatedAt         time.Time  `json:"created_at"`
	UpdatedAt         time.Time  `json:"updated_at"`
}

Webhook represents a team webhook configuration.

type WebhookNotification

type WebhookNotification struct {
	Type    string                 `json:"type"`
	Title   string                 `json:"title"`
	Message string                 `json:"message"`
	Data    map[string]interface{} `json:"data,omitempty"`
}

WebhookNotification contains the data needed to format a webhook message.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL