webhook

package
v1.18.4 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2023 License: MIT Imports: 13 Imported by: 13

Documentation

Index

Constants

View Source
const (
	HookStatusNone = iota
	HookStatusSucceed
	HookStatusFail
)

Possible statuses of a web hook

Variables

This section is empty.

Functions

func CleanupHookTaskTable

func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType, olderThan time.Duration, numberToKeep int) error

CleanupHookTaskTable deletes rows from hook_task as needed.

func CopyDefaultWebhooksToRepo

func CopyDefaultWebhooksToRepo(ctx context.Context, repoID int64) error

CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo

func CountWebhooksByOpts

func CountWebhooksByOpts(opts *ListWebhookOptions) (int64, error)

CountWebhooksByOpts count webhooks based on options and ignore pagination

func CreateWebhook

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

CreateWebhook creates a new web hook.

func CreateWebhooks added in v1.17.0

func CreateWebhooks(ctx context.Context, ws []*Webhook) error

CreateWebhooks creates multiple web hooks

func DeleteDefaultSystemWebhook

func DeleteDefaultSystemWebhook(id int64) error

DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)

func DeleteWebhookByOrgID

func DeleteWebhookByOrgID(orgID, id int64) error

DeleteWebhookByOrgID deletes webhook of organization by given ID.

func DeleteWebhookByRepoID

func DeleteWebhookByRepoID(repoID, id int64) error

DeleteWebhookByRepoID deletes webhook of repository by given ID.

func IsErrHookTaskNotExist

func IsErrHookTaskNotExist(err error) bool

IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist.

func IsErrWebhookNotExist

func IsErrWebhookNotExist(err error) bool

IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist.

func IsValidHookContentType

func IsValidHookContentType(name string) bool

IsValidHookContentType returns true if given name is a valid hook content type.

func UpdateHookTask

func UpdateHookTask(t *HookTask) error

UpdateHookTask updates information of hook task.

func UpdateWebhook

func UpdateWebhook(w *Webhook) error

UpdateWebhook updates information of webhook.

func UpdateWebhookLastStatus

func UpdateWebhookLastStatus(w *Webhook) error

UpdateWebhookLastStatus updates last status of webhook.

Types

type ErrHookTaskNotExist

type ErrHookTaskNotExist struct {
	TaskID int64
	HookID int64
	UUID   string
}

ErrHookTaskNotExist represents a "HookTaskNotExist" kind of error.

func (ErrHookTaskNotExist) Error

func (err ErrHookTaskNotExist) Error() string

func (ErrHookTaskNotExist) Unwrap added in v1.17.4

func (err ErrHookTaskNotExist) Unwrap() error

type ErrWebhookNotExist

type ErrWebhookNotExist struct {
	ID int64
}

ErrWebhookNotExist represents a "WebhookNotExist" kind of error.

func (ErrWebhookNotExist) Error

func (err ErrWebhookNotExist) Error() string

func (ErrWebhookNotExist) Unwrap added in v1.17.4

func (err ErrWebhookNotExist) Unwrap() error

type HookContentType

type HookContentType int

HookContentType is the content type of a web hook

const (
	// ContentTypeJSON is a JSON payload for web hooks
	ContentTypeJSON HookContentType = iota + 1
	// ContentTypeForm is an url-encoded form payload for web hook
	ContentTypeForm
)

func ToHookContentType

func ToHookContentType(name string) HookContentType

ToHookContentType returns HookContentType by given name.

func (HookContentType) Name

func (t HookContentType) Name() string

Name returns the name of a given web hook's content type

type HookEvent

type HookEvent struct {
	PushOnly       bool   `json:"push_only"`
	SendEverything bool   `json:"send_everything"`
	ChooseEvents   bool   `json:"choose_events"`
	BranchFilter   string `json:"branch_filter"`

	HookEvents `json:"events"`
}

HookEvent represents events that will delivery hook.

type HookEventType

type HookEventType string

HookEventType is the type of an hook event

const (
	HookEventCreate                    HookEventType = "create"
	HookEventDelete                    HookEventType = "delete"
	HookEventFork                      HookEventType = "fork"
	HookEventPush                      HookEventType = "push"
	HookEventIssues                    HookEventType = "issues"
	HookEventIssueAssign               HookEventType = "issue_assign"
	HookEventIssueLabel                HookEventType = "issue_label"
	HookEventIssueMilestone            HookEventType = "issue_milestone"
	HookEventIssueComment              HookEventType = "issue_comment"
	HookEventPullRequest               HookEventType = "pull_request"
	HookEventPullRequestAssign         HookEventType = "pull_request_assign"
	HookEventPullRequestLabel          HookEventType = "pull_request_label"
	HookEventPullRequestMilestone      HookEventType = "pull_request_milestone"
	HookEventPullRequestComment        HookEventType = "pull_request_comment"
	HookEventPullRequestReviewApproved HookEventType = "pull_request_review_approved"
	HookEventPullRequestReviewRejected HookEventType = "pull_request_review_rejected"
	HookEventPullRequestReviewComment  HookEventType = "pull_request_review_comment"
	HookEventPullRequestSync           HookEventType = "pull_request_sync"
	HookEventWiki                      HookEventType = "wiki"
	HookEventRepository                HookEventType = "repository"
	HookEventRelease                   HookEventType = "release"
	HookEventPackage                   HookEventType = "package"
)

Types of hook events

func (HookEventType) Event

func (h HookEventType) Event() string

Event returns the HookEventType as an event string

type HookEvents

type HookEvents struct {
	Create               bool `json:"create"`
	Delete               bool `json:"delete"`
	Fork                 bool `json:"fork"`
	Issues               bool `json:"issues"`
	IssueAssign          bool `json:"issue_assign"`
	IssueLabel           bool `json:"issue_label"`
	IssueMilestone       bool `json:"issue_milestone"`
	IssueComment         bool `json:"issue_comment"`
	Push                 bool `json:"push"`
	PullRequest          bool `json:"pull_request"`
	PullRequestAssign    bool `json:"pull_request_assign"`
	PullRequestLabel     bool `json:"pull_request_label"`
	PullRequestMilestone bool `json:"pull_request_milestone"`
	PullRequestComment   bool `json:"pull_request_comment"`
	PullRequestReview    bool `json:"pull_request_review"`
	PullRequestSync      bool `json:"pull_request_sync"`
	Wiki                 bool `json:"wiki"`
	Repository           bool `json:"repository"`
	Release              bool `json:"release"`
	Package              bool `json:"package"`
}

HookEvents is a set of web hook events

type HookRequest

type HookRequest struct {
	URL        string            `json:"url"`
	HTTPMethod string            `json:"http_method"`
	Headers    map[string]string `json:"headers"`
}

HookRequest represents hook task request information.

type HookResponse

type HookResponse struct {
	Status  int               `json:"status"`
	Headers map[string]string `json:"headers"`
	Body    string            `json:"body"`
}

HookResponse represents hook task response information.

type HookStatus

type HookStatus int

HookStatus is the status of a web hook

type HookTask

type HookTask struct {
	ID              int64 `xorm:"pk autoincr"`
	HookID          int64
	UUID            string
	api.Payloader   `xorm:"-"`
	PayloadContent  string `xorm:"LONGTEXT"`
	EventType       HookEventType
	IsDelivered     bool
	Delivered       int64
	DeliveredString string `xorm:"-"`

	// History info.
	IsSucceed       bool
	RequestContent  string        `xorm:"LONGTEXT"`
	RequestInfo     *HookRequest  `xorm:"-"`
	ResponseContent string        `xorm:"LONGTEXT"`
	ResponseInfo    *HookResponse `xorm:"-"`
}

HookTask represents a hook task.

func CreateHookTask

func CreateHookTask(ctx context.Context, t *HookTask) (*HookTask, error)

CreateHookTask creates a new hook task, it handles conversion from Payload to PayloadContent.

func FindUndeliveredHookTasks

func FindUndeliveredHookTasks(ctx context.Context) ([]*HookTask, error)

FindUndeliveredHookTasks represents find the undelivered hook tasks

func GetHookTaskByID added in v1.17.4

func GetHookTaskByID(ctx context.Context, id int64) (*HookTask, error)

func HookTasks

func HookTasks(hookID int64, page int) ([]*HookTask, error)

HookTasks returns a list of hook tasks by given conditions.

func ReplayHookTask

func ReplayHookTask(ctx context.Context, hookID int64, uuid string) (*HookTask, error)

ReplayHookTask copies a hook task to get re-delivered

func (*HookTask) AfterLoad

func (t *HookTask) AfterLoad()

AfterLoad updates the webhook object upon setting a column

func (*HookTask) BeforeUpdate

func (t *HookTask) BeforeUpdate()

BeforeUpdate will be invoked by XORM before updating a record representing this object

type HookTaskCleanupType

type HookTaskCleanupType int

HookTaskCleanupType is the type of cleanup to perform on hook_task

const (
	// OlderThan hook_task rows will be cleaned up by the age of the row
	OlderThan HookTaskCleanupType = iota
	// PerWebhook hook_task rows will be cleaned up by leaving the most recent deliveries for each webhook
	PerWebhook
)

func ToHookTaskCleanupType

func ToHookTaskCleanupType(name string) HookTaskCleanupType

ToHookTaskCleanupType returns HookTaskCleanupType by given name.

type HookType

type HookType = string

HookType is the type of a webhook

const (
	GITEA      HookType = "gitea"
	GOGS       HookType = "gogs"
	SLACK      HookType = "slack"
	DISCORD    HookType = "discord"
	DINGTALK   HookType = "dingtalk"
	TELEGRAM   HookType = "telegram"
	MSTEAMS    HookType = "msteams"
	FEISHU     HookType = "feishu"
	MATRIX     HookType = "matrix"
	WECHATWORK HookType = "wechatwork"
	PACKAGIST  HookType = "packagist"
)

Types of webhooks

type ListWebhookOptions

type ListWebhookOptions struct {
	db.ListOptions
	RepoID   int64
	OrgID    int64
	IsActive util.OptionalBool
}

ListWebhookOptions are options to filter webhooks on ListWebhooksByOpts

type Webhook

type Webhook struct {
	ID              int64 `xorm:"pk autoincr"`
	RepoID          int64 `xorm:"INDEX"` // An ID of 0 indicates either a default or system webhook
	OrgID           int64 `xorm:"INDEX"`
	IsSystemWebhook bool
	URL             string `xorm:"url TEXT"`
	HTTPMethod      string `xorm:"http_method"`
	ContentType     HookContentType
	Secret          string `xorm:"TEXT"`
	Events          string `xorm:"TEXT"`
	*HookEvent      `xorm:"-"`
	IsActive        bool       `xorm:"INDEX"`
	Type            HookType   `xorm:"VARCHAR(16) 'type'"`
	Meta            string     `xorm:"TEXT"` // store hook-specific attributes
	LastStatus      HookStatus // Last delivery status

	CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
	UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
}

Webhook represents a web hook object.

func GetDefaultWebhooks

func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error)

GetDefaultWebhooks returns all admin-default webhooks.

func GetSystemOrDefaultWebhook

func GetSystemOrDefaultWebhook(id int64) (*Webhook, error)

GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.

func GetSystemWebhooks

func GetSystemWebhooks(ctx context.Context, isActive util.OptionalBool) ([]*Webhook, error)

GetSystemWebhooks returns all admin system webhooks.

func GetWebhookByID

func GetWebhookByID(id int64) (*Webhook, error)

GetWebhookByID returns webhook of repository by given ID.

func GetWebhookByOrgID

func GetWebhookByOrgID(orgID, id int64) (*Webhook, error)

GetWebhookByOrgID returns webhook of organization by given ID.

func GetWebhookByRepoID

func GetWebhookByRepoID(repoID, id int64) (*Webhook, error)

GetWebhookByRepoID returns webhook of repository by given ID.

func ListWebhooksByOpts

func ListWebhooksByOpts(ctx context.Context, opts *ListWebhookOptions) ([]*Webhook, error)

ListWebhooksByOpts return webhooks based on options

func (*Webhook) AfterLoad

func (w *Webhook) AfterLoad()

AfterLoad updates the webhook object upon setting a column

func (*Webhook) EventCheckers

func (w *Webhook) EventCheckers() []struct {
	Has  func() bool
	Type HookEventType
}

EventCheckers returns event checkers

func (*Webhook) EventsArray

func (w *Webhook) EventsArray() []string

EventsArray returns an array of hook events

func (*Webhook) HasCreateEvent

func (w *Webhook) HasCreateEvent() bool

HasCreateEvent returns true if hook enabled create event.

func (*Webhook) HasDeleteEvent

func (w *Webhook) HasDeleteEvent() bool

HasDeleteEvent returns true if hook enabled delete event.

func (*Webhook) HasForkEvent

func (w *Webhook) HasForkEvent() bool

HasForkEvent returns true if hook enabled fork event.

func (*Webhook) HasIssueCommentEvent

func (w *Webhook) HasIssueCommentEvent() bool

HasIssueCommentEvent returns true if hook enabled issue_comment event.

func (*Webhook) HasIssuesAssignEvent

func (w *Webhook) HasIssuesAssignEvent() bool

HasIssuesAssignEvent returns true if hook enabled issues assign event.

func (*Webhook) HasIssuesEvent

func (w *Webhook) HasIssuesEvent() bool

HasIssuesEvent returns true if hook enabled issues event.

func (*Webhook) HasIssuesLabelEvent

func (w *Webhook) HasIssuesLabelEvent() bool

HasIssuesLabelEvent returns true if hook enabled issues label event.

func (*Webhook) HasIssuesMilestoneEvent

func (w *Webhook) HasIssuesMilestoneEvent() bool

HasIssuesMilestoneEvent returns true if hook enabled issues milestone event.

func (*Webhook) HasPackageEvent added in v1.17.0

func (w *Webhook) HasPackageEvent() bool

HasPackageEvent returns if hook enabled package event.

func (*Webhook) HasPullRequestApprovedEvent

func (w *Webhook) HasPullRequestApprovedEvent() bool

HasPullRequestApprovedEvent returns true if hook enabled pull request review event.

func (*Webhook) HasPullRequestAssignEvent

func (w *Webhook) HasPullRequestAssignEvent() bool

HasPullRequestAssignEvent returns true if hook enabled pull request assign event.

func (*Webhook) HasPullRequestCommentEvent

func (w *Webhook) HasPullRequestCommentEvent() bool

HasPullRequestCommentEvent returns true if hook enabled pull_request_comment event.

func (*Webhook) HasPullRequestEvent

func (w *Webhook) HasPullRequestEvent() bool

HasPullRequestEvent returns true if hook enabled pull request event.

func (*Webhook) HasPullRequestLabelEvent

func (w *Webhook) HasPullRequestLabelEvent() bool

HasPullRequestLabelEvent returns true if hook enabled pull request label event.

func (*Webhook) HasPullRequestMilestoneEvent

func (w *Webhook) HasPullRequestMilestoneEvent() bool

HasPullRequestMilestoneEvent returns true if hook enabled pull request milestone event.

func (*Webhook) HasPullRequestRejectedEvent

func (w *Webhook) HasPullRequestRejectedEvent() bool

HasPullRequestRejectedEvent returns true if hook enabled pull request review event.

func (*Webhook) HasPullRequestReviewCommentEvent

func (w *Webhook) HasPullRequestReviewCommentEvent() bool

HasPullRequestReviewCommentEvent returns true if hook enabled pull request review event.

func (*Webhook) HasPullRequestSyncEvent

func (w *Webhook) HasPullRequestSyncEvent() bool

HasPullRequestSyncEvent returns true if hook enabled pull request sync event.

func (*Webhook) HasPushEvent

func (w *Webhook) HasPushEvent() bool

HasPushEvent returns true if hook enabled push event.

func (*Webhook) HasReleaseEvent

func (w *Webhook) HasReleaseEvent() bool

HasReleaseEvent returns if hook enabled release event.

func (*Webhook) HasRepositoryEvent

func (w *Webhook) HasRepositoryEvent() bool

HasRepositoryEvent returns if hook enabled repository event.

func (*Webhook) HasWikiEvent added in v1.17.4

func (w *Webhook) HasWikiEvent() bool

HasWikiEvent returns true if hook enabled wiki event.

func (*Webhook) History

func (w *Webhook) History(page int) ([]*HookTask, error)

History returns history of webhook by given conditions.

func (*Webhook) UpdateEvent

func (w *Webhook) UpdateEvent() error

UpdateEvent handles conversion from HookEvent to Events.

Jump to

Keyboard shortcuts

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