webhooks

package
v0.3.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package webhooks runs actions on stacks and containers when a secret URL is called (#85, #91, ADR-0041), e.g. from CI after a new image was published.

Index

Constants

View Source
const (
	KindStack     = "stack"
	KindContainer = "container"

	// ActionRedeploy pulls images and recreates: compose pull + up for a stack, pull and
	// recreate with the same settings for a container.
	ActionRedeploy = "redeploy"
	// ActionRestart restarts as is.
	ActionRestart = "restart"
)

Target kinds and actions.

Variables

View Source
var ErrReadOnly = fmt.Errorf("%w: the environment is in read-only mode", errdefs.ErrUnavailable)

ErrReadOnly is returned when the webhook's environment cannot be changed (ADR-0011).

Functions

func Path

func Path(token string) string

Path is the URL path of a token.

Types

type Action

type Action struct {
	Environment, Namespace string
	TargetKind, Target     string
	Action                 string
	// User is the job's user; Label is added to the job title, e.g. "webhook ci".
	User, Label string
}

Action is a restart or redeploy of a stack or container, run by a webhook or a schedule.

type Environments

type Environments interface {
	Get(id string) (*environment.Environment, bool)
}

Environments finds environments; *environment.Registry implements it.

type Input

type Input struct {
	Name        string `json:"name"`
	Environment string `json:"environment"`
	Namespace   string `json:"namespace"`
	TargetKind  string `json:"targetKind"`
	Target      string `json:"target"`
	Action      string `json:"action"`
	Enabled     bool   `json:"enabled"`
}

Input creates or changes a webhook. Keep in sync with api/openapi.yaml.

type JobRunner

type JobRunner interface {
	Start(spec jobs.Spec, fn jobs.Func) (jobs.Job, error)
}

JobRunner starts jobs; *jobs.Manager implements it.

type Service

type Service struct {
	Store        Store
	Environments Environments
	Stacks       StackFinder // nil: stack webhooks fail
	Jobs         JobRunner
	Now          func() time.Time
}

Service manages webhooks and runs them.

func (*Service) Call

func (s *Service) Call(ctx context.Context, token string) (Webhook, jobs.Job, error)

Call runs the webhook of a token as a job. Unknown and disabled tokens are not found.

func (*Service) Create

func (s *Service) Create(ctx context.Context, in Input, user string) (Webhook, string, error)

Create saves a webhook and returns it with its token, which is not stored.

func (*Service) Delete

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

Delete removes a webhook.

func (*Service) List

func (s *Service) List(ctx context.Context) ([]Webhook, error)

List lists the webhooks.

func (*Service) NewToken

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

NewToken replaces the token of a webhook; the previous URL stops working.

func (*Service) StartAction

func (s *Service) StartAction(ctx context.Context, w Action) (jobs.Job, error)

StartAction starts the job of an action.

func (*Service) Update

func (s *Service) Update(ctx context.Context, id int64, in Input) (Webhook, error)

Update changes a webhook; its token stays.

type StackFinder

type StackFinder interface {
	Find(ctx context.Context, name string) (stacks.Definition, error)
}

StackFinder finds the compose file of a stack; *stacks.Service implements it.

type Store

type Store interface {
	ListWebhooks(ctx context.Context) ([]db.Webhook, error)
	GetWebhook(ctx context.Context, id int64) (db.Webhook, error)
	GetWebhookByToken(ctx context.Context, tokenHash string) (db.Webhook, error)
	CreateWebhook(ctx context.Context, arg db.CreateWebhookParams) (db.Webhook, error)
	UpdateWebhook(ctx context.Context, arg db.UpdateWebhookParams) (db.Webhook, error)
	SetWebhookToken(ctx context.Context, arg db.SetWebhookTokenParams) (int64, error)
	SetWebhookCalled(ctx context.Context, arg db.SetWebhookCalledParams) error
	DeleteWebhook(ctx context.Context, id int64) (int64, error)
}

Store is the database of webhooks; *db.Queries of the write connection implements it.

type Webhook

type Webhook struct {
	ID           int64      `json:"id"`
	Name         string     `json:"name"`
	Environment  string     `json:"environment"`
	Namespace    string     `json:"namespace"`
	TargetKind   string     `json:"targetKind"`
	Target       string     `json:"target"`
	Action       string     `json:"action"`
	Enabled      bool       `json:"enabled"`
	CreatedBy    string     `json:"createdBy,omitempty"`
	CreatedAt    time.Time  `json:"createdAt"`
	LastCalledAt *time.Time `json:"lastCalledAt,omitempty"`
	// LastJob is the job of the newest call; LastError why it could not start.
	LastJob   string `json:"lastJob,omitempty"`
	LastError string `json:"lastError,omitempty"`
}

Webhook is a webhook without its token. Keep in sync with api/openapi.yaml.

Jump to

Keyboard shortcuts

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