webhooks

package
v0.38.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: Apache-2.0 Imports: 43 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

AuthZProvider is the authz registry for experiments.

Functions

func AddWebhook

func AddWebhook(ctx context.Context, w *Webhook) error

AddWebhook adds a Webhook and its Triggers to the DB.

func Deinit

func Deinit()

Deinit closes a shipper.

func DeleteWebhook

func DeleteWebhook(ctx context.Context, id WebhookID) error

DeleteWebhook deletes a Webhook and its Triggers from the DB.

func Init

func Init()

Init creates a shipper singleton.

func ReportExperimentStateChanged

func ReportExperimentStateChanged(
	ctx context.Context, e model.Experiment, activeConfig expconf.ExperimentConfig,
) error

ReportExperimentStateChanged adds webhook events to the queue. This function assumes ID presents in e. TODO(DET-8577): Remove unnecessary active config usage (remove the activeConfig parameter).

func ScanLogs

func ScanLogs(ctx context.Context, logs []*model.TaskLog, workspaceID model.AccessScopeID, expID *int) error

ScanLogs sends webhooks for task logs. This should be called wherever we add task logs.

func SetDefault

func SetDefault(w *WebhookManager)

SetDefault sets the default webhook manager singleton.

func UpdateWebhook

func UpdateWebhook(ctx context.Context, id int32, p *webhookv1.PatchWebhook) error

UpdateWebhook updates a Webhook in the DB.

Types

type Condition

type Condition struct {
	State model.State `json:"state,omitempty"`
	Regex string      `json:"regex,omitempty"`
}

Condition represents a trigger condition.

type CustomTriggerData

type CustomTriggerData struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	Level       string `json:"level"`
}

CustomTriggerData is the data for custom trigger.

type Event

type Event struct {
	bun.BaseModel `bun:"table:webhook_events_queue"`

	ID      WebhookEventID `bun:"id,pk,autoincrement"`
	URL     string         `bun:"url,notnull"`
	Payload []byte         `bun:"payload,notnull"`
}

Event corresponds to a row in the "webhook_events" DB table.

type EventData

type EventData struct {
	TestData   *string            `json:"data,omitempty"`
	Experiment *ExperimentPayload `json:"experiment,omitempty"`
	TaskLog    *TaskLogPayload    `json:"task_log,omitempty"`
	CustomData *CustomTriggerData `json:"custom_data,omitempty"`
}

EventData represents the event_data for a webhook event.

type EventPayload

type EventPayload struct {
	ID        uuid.UUID   `json:"event_id"`
	Type      TriggerType `json:"event_type"`
	Timestamp int64       `json:"timestamp"`
	Condition Condition   `json:"condition"`
	Data      EventData   `json:"event_data"`
}

EventPayload respresents a webhook event.

type ExperimentPayload

type ExperimentPayload struct {
	ID            int          `json:"id"`
	State         model.State  `json:"state"`
	Name          expconf.Name `json:"name"`
	Duration      int          `json:"duration"`
	ResourcePool  string       `json:"resource_pool"`
	SlotsPerTrial int          `json:"slots_per_trial"`
	WorkspaceName string       `json:"workspace"`
	ProjectName   string       `json:"project"`
	TrialID       int          `json:"trial_id,omitempty"`
}

ExperimentPayload is the webhook request representation of an experiment.

type SlackAttachment

type SlackAttachment struct {
	Color  string       `json:"color,omitempty"`
	Blocks []SlackBlock `json:"blocks,omitempty"`
}

SlackAttachment corresponds to an Attachment Slack Block element.

type SlackBlock

type SlackBlock struct {
	Type   string        `json:"type,omitempty"`
	Text   SlackField    `json:"text,omitempty"`
	Fields *[]SlackField `json:"fields,omitempty"`
}

SlackBlock corresponds to a Slack Block element.

type SlackField

type SlackField struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

SlackField corresponds to a field in a Slack Block element.

type SlackMessageBody

type SlackMessageBody struct {
	Blocks      []SlackBlock       `json:"blocks,omitempty"`
	Attachments *[]SlackAttachment `json:"attachments,omitempty"`
}

SlackMessageBody corresponds to an entire message as a Slack Block.

type TaskLogPayload

type TaskLogPayload struct {
	TaskID        model.TaskID `json:"task_id"`
	NodeName      string       `json:"node_name"`
	TriggeringLog string       `json:"triggering_log"`
}

TaskLogPayload is the webhook request representation of a trigger of a task log.

type Trigger

type Trigger struct {
	bun.BaseModel `bun:"table:webhook_triggers"`

	ID          TriggerID              `bun:"id,pk,autoincrement"`
	TriggerType TriggerType            `bun:"trigger_type,notnull"`
	Condition   map[string]interface{} `bun:"condition,notnull"`
	WebhookID   WebhookID              `bun:"webhook_id,notnull"`

	Webhook *Webhook `bun:"rel:belongs-to,join:webhook_id=id"`
}

Trigger corresponds to a row in the "webhook_triggers" DB table.

func TriggerFromProto

func TriggerFromProto(t *webhookv1.Trigger) *Trigger

TriggerFromProto returns a Trigger from a proto definition.

func (*Trigger) Proto

func (t *Trigger) Proto() *webhookv1.Trigger

Proto converts a Trigger to its protobuf representation.

type TriggerID

type TriggerID int

TriggerID is the type for Trigger IDs.

type TriggerType

type TriggerType string

TriggerType is type for the TriggerType enum.

const (
	// TriggerTypeStateChange represents a change in experiment state.
	TriggerTypeStateChange TriggerType = "EXPERIMENT_STATE_CHANGE"

	// TriggerTypeMetricThresholdExceeded represents a threshold for a training metric value.
	TriggerTypeMetricThresholdExceeded TriggerType = "METRIC_THRESHOLD_EXCEEDED"

	// TriggerTypeTaskLog represents a trigger for a task logs.
	TriggerTypeTaskLog TriggerType = "TASK_LOG"

	// TriggerTypeCustom represents a custom trigger.
	TriggerTypeCustom TriggerType = "CUSTOM"
)

func TriggerTypeFromProto

func TriggerTypeFromProto(t webhookv1.TriggerType) TriggerType

TriggerTypeFromProto returns a TriggerType from a proto.

func (TriggerType) Proto

func (t TriggerType) Proto() webhookv1.TriggerType

Proto returns a proto from a TriggerType.

type Triggers

type Triggers []*Trigger

Triggers is a slice of Trigger objects—primarily useful for its methods.

func TriggersFromProto

func TriggersFromProto(ts []*webhookv1.Trigger) Triggers

TriggersFromProto returns a slice of model Triggers from a proto definition.

func (Triggers) Proto

func (ts Triggers) Proto() []*webhookv1.Trigger

Proto converts a slice of triggers to its protobuf representation.

type Webhook

type Webhook struct {
	bun.BaseModel `bun:"table:webhooks"`

	ID          WebhookID   `bun:"id,pk,autoincrement"`
	WebhookType WebhookType `bun:"webhook_type,notnull"`
	URL         string      `bun:"url,notnull"`
	Mode        WebhookMode `bun:"mode,notnull"`
	WorkspaceID *int32      `bun:"workspace_id"`
	Name        string      `bun:"name,notnull"`

	Triggers Triggers `bun:"rel:has-many,join:id=webhook_id"`
}

Webhook corresponds to a row in the "webhooks" DB table.

func GetWebhook

func GetWebhook(ctx context.Context, webhookID int) (*Webhook, error)

GetWebhook returns a single Webhooks from the DB.

func WebhookFromProto

func WebhookFromProto(w *webhookv1.Webhook) Webhook

WebhookFromProto returns a model Webhook from a proto definition.

func (*Webhook) Proto

func (w *Webhook) Proto() *webhookv1.Webhook

Proto converts a webhook to its protobuf representation.

type WebhookAuthZ

type WebhookAuthZ interface {
	// GET /api/v1/webhooks
	WebhookAvailableWorkspaces(
		ctx context.Context, curUser *model.User) (workspaceIDsWithPermsFilter []int32, serverError error)
	// POST /api/v1/webhooks
	// DELETE /api/v1/webhooks/:webhook_id
	// POST /api/v1/webhooks/test/:webhook_id
	CanEditWebhooks(ctx context.Context, curUser *model.User, workspace *model.Workspace) (serverError error)
}

WebhookAuthZ describes authz methods for experiments.

type WebhookAuthZBasic

type WebhookAuthZBasic struct{}

WebhookAuthZBasic is basic OSS controls.

func (*WebhookAuthZBasic) CanEditWebhooks

func (a *WebhookAuthZBasic) CanEditWebhooks(
	ctx context.Context, curUser *model.User, workspace *model.Workspace,
) (serverError error)

CanEditWebhooks always returns true and a nil error. workspace being nil means the webhook is globally scoped.

func (*WebhookAuthZBasic) WebhookAvailableWorkspaces

func (a *WebhookAuthZBasic) WebhookAvailableWorkspaces(
	ctx context.Context, curUser *model.User,
) (workspaceIDsWithPermsFilter []int32, serverError error)

WebhookAvailableWorkspaces returns a list of workspaces that user can get webhooks from.

type WebhookAuthZPermissive

type WebhookAuthZPermissive struct{}

WebhookAuthZPermissive is the permission implementation.

func (*WebhookAuthZPermissive) CanEditWebhooks

func (p *WebhookAuthZPermissive) CanEditWebhooks(
	ctx context.Context, curUser *model.User, workspace *model.Workspace,
) error

CanEditWebhooks calls RBAC authz but enforces basic authz.

func (*WebhookAuthZPermissive) WebhookAvailableWorkspaces

func (p *WebhookAuthZPermissive) WebhookAvailableWorkspaces(
	ctx context.Context, curUser *model.User,
) (workspaceIDsWithPermsFilter []int32, serverError error)

WebhookAvailableWorkspaces calls RBAC authz but enforces basic authz.

type WebhookAuthZRBAC

type WebhookAuthZRBAC struct{}

WebhookAuthZRBAC is RBAC webhook access controls.

func (*WebhookAuthZRBAC) CanEditWebhooks

func (a *WebhookAuthZRBAC) CanEditWebhooks(
	ctx context.Context, curUser *model.User, workspace *model.Workspace,
) error

CanEditWebhooks checks if a user can edit webhooks. workspace being nil means the webhook is globally scoped.

func (*WebhookAuthZRBAC) WebhookAvailableWorkspaces

func (a *WebhookAuthZRBAC) WebhookAvailableWorkspaces(
	ctx context.Context, curUser *model.User,
) (workspaceIDsWithPermsFilter []int32, serverError error)

WebhookAvailableWorkspaces returns a list of workspaces that user can get webhooks from.

type WebhookEventID

type WebhookEventID int

WebhookEventID is the type for Trigger IDs.

type WebhookID

type WebhookID int

WebhookID is the type for Webhook IDs.

type WebhookManager

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

WebhookManager manages webhooks.

func New

func New(ctx context.Context) (*WebhookManager, error)

New creates a new webhook manager.

type WebhookMode

type WebhookMode string

WebhookMode is mode for the WebhookMode enum.

const (
	// WebhookModeWorkspace represents the webhook will be triggered by all experiment in the workspace.
	WebhookModeWorkspace WebhookMode = "WORKSPACE"
	// WebhookModeSpecific represents the webhook will only be triggered by experiment with
	// matching configuration in the same workspace as the web hook.
	WebhookModeSpecific WebhookMode = "SPECIFIC"
)

func WebhookModeFromProto

func WebhookModeFromProto(w webhookv1.WebhookMode) WebhookMode

WebhookModeFromProto returns a WebhookMode from a proto.

func (WebhookMode) Proto

func (w WebhookMode) Proto() webhookv1.WebhookMode

Proto returns a proto from a WebhookMode.

type WebhookType

type WebhookType string

WebhookType is type for the WebhookType enum.

const (
	// WebhookTypeDefault represents a default webhook.
	WebhookTypeDefault WebhookType = "DEFAULT"

	// WebhookTypeSlack represents a slack webhook.
	WebhookTypeSlack WebhookType = "SLACK"
)

func WebhookTypeFromProto

func WebhookTypeFromProto(w webhookv1.WebhookType) WebhookType

WebhookTypeFromProto returns a WebhookType from a proto.

func (WebhookType) Proto

func (w WebhookType) Proto() webhookv1.WebhookType

Proto returns a proto from a WebhookType.

type Webhooks

type Webhooks []Webhook

Webhooks is a slice of Webhook objects.

func (Webhooks) Proto

func (ws Webhooks) Proto() []*webhookv1.Webhook

Proto converts a slice of webhooks to its protobuf representation.

type WebhooksAPIServer

type WebhooksAPIServer struct{}

WebhooksAPIServer is an embedded api server struct.

func (*WebhooksAPIServer) DeleteWebhook

DeleteWebhook deletes a Webhook.

func (*WebhooksAPIServer) GetWebhooks

GetWebhooks returns all Webhooks.

func (*WebhooksAPIServer) PatchWebhook

PatchWebhook updates a webhook.

func (*WebhooksAPIServer) PostWebhook

PostWebhook creates a new Webhook.

func (*WebhooksAPIServer) PostWebhookEventData

PostWebhookEventData handles data for custom trigger.

func (*WebhooksAPIServer) TestWebhook

TestWebhook sends a test event for a Webhook.

Jump to

Keyboard shortcuts

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