webhook

package
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2023 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidContentType = errors.New("invalid content type")

ErrInvalidContentType is returned when the content type is invalid.

View Source
var ErrInvalidEvent = errors.New("invalid event")

ErrInvalidEvent is returned when the event is invalid.

Functions

func SendEvent

func SendEvent(ctx context.Context, payload EventPayload) error

SendEvent sends a webhook event.

func SendWebhook

func SendWebhook(ctx context.Context, w models.Webhook, event Event, payload interface{}) error

SendWebhook sends a webhook event.

Types

type Author

type Author struct {
	// Name is the author name.
	Name string `json:"name" url:"name"`
	// Email is the author email.
	Email string `json:"email" url:"email"`
	// Date is the author date.
	Date time.Time `json:"date" url:"date"`
}

Author is a commit author.

type BranchTagEvent

type BranchTagEvent struct {
	Common

	// Ref is the branch or tag name.
	Ref string `json:"ref" url:"ref"`
	// Before is the previous commit SHA.
	Before string `json:"before" url:"before"`
	// After is the current commit SHA.
	After string `json:"after" url:"after"`
	// Created is whether the branch or tag was created.
	Created bool `json:"created" url:"created"`
	// Deleted is whether the branch or tag was deleted.
	Deleted bool `json:"deleted" url:"deleted"`
}

BranchTagEvent is a branch or tag event.

func NewBranchTagEvent

func NewBranchTagEvent(ctx context.Context, user proto.User, repo proto.Repository, ref, before, after string) (BranchTagEvent, error)

NewBranchTagEvent sends a branch or tag event.

type CollaboratorEvent

type CollaboratorEvent struct {
	Common

	// Action is the collaborator event action.
	Action CollaboratorEventAction `json:"action" url:"action"`
	// AccessLevel is the collaborator access level.
	AccessLevel access.AccessLevel `json:"access_level" url:"access_level"`
	// Collaborator is the collaborator.
	Collaborator User `json:"collaborator" url:"collaborator"`
}

CollaboratorEvent is a collaborator event.

func NewCollaboratorEvent

func NewCollaboratorEvent(ctx context.Context, user proto.User, repo proto.Repository, collabUsername string, action CollaboratorEventAction) (CollaboratorEvent, error)

NewCollaboratorEvent sends a collaborator event.

type CollaboratorEventAction

type CollaboratorEventAction string

CollaboratorEventAction is a collaborator event action.

const (
	// CollaboratorEventAdded is a collaborator added event.
	CollaboratorEventAdded CollaboratorEventAction = "added"
	// CollaboratorEventRemoved is a collaborator removed event.
	CollaboratorEventRemoved CollaboratorEventAction = "removed"
)

type Commit

type Commit struct {
	// ID is the commit ID.
	ID string `json:"id" url:"id"`
	// Message is the commit message.
	Message string `json:"message" url:"message"`
	// Title is the commit title.
	Title string `json:"title" url:"title"`
	// Author is the commit author.
	Author Author `json:"author" url:"author"`
	// Committer is the commit committer.
	Committer Author `json:"committer" url:"committer"`
	// Timestamp is the commit timestamp.
	Timestamp time.Time `json:"timestamp" url:"timestamp"`
}

Commit represents a Git commit.

type Common

type Common struct {
	// EventType is the event type.
	EventType Event `json:"event" url:"event"`
	// Repository is the repository payload.
	Repository Repository `json:"repository" url:"repository"`
	// Sender is the sender payload.
	Sender User `json:"sender" url:"sender"`
}

Common is a common payload.

func (Common) Event

func (c Common) Event() Event

Event returns the event type. Implements EventPayload.

func (Common) RepositoryID

func (c Common) RepositoryID() int64

RepositoryID returns the repository ID. Implements EventPayload.

type ContentType

type ContentType int8

ContentType is the type of content that will be sent in a webhook request.

const (
	// ContentTypeJSON is the JSON content type.
	ContentTypeJSON ContentType = iota
	// ContentTypeForm is the form content type.
	ContentTypeForm
)

func ParseContentType

func ParseContentType(s string) (ContentType, error)

ParseContentType parses a content type string and returns the content type.

func (ContentType) MarshalText

func (c ContentType) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler.

func (ContentType) String

func (c ContentType) String() string

String returns the string representation of the content type.

func (*ContentType) UnmarshalText

func (c *ContentType) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Delivery

type Delivery struct {
	models.WebhookDelivery
	Event Event
}

Delivery is a webhook delivery.

type Event

type Event int

Event is a webhook event.

const (
	// EventBranchTagCreate is a branch or tag create event.
	EventBranchTagCreate Event = 1

	// EventBranchTagDelete is a branch or tag delete event.
	EventBranchTagDelete Event = 2

	// EventCollaborator is a collaborator change event.
	EventCollaborator Event = 3

	// EventPush is a push event.
	EventPush Event = 4

	// EventRepository is a repository create, delete, rename event.
	EventRepository Event = 5

	// EventRepositoryVisibilityChange is a repository visibility change event.
	EventRepositoryVisibilityChange Event = 6
)

func Events

func Events() []Event

Events return all events.

func ParseEvent

func ParseEvent(s string) (Event, error)

ParseEvent parses an event string and returns the event.

func (Event) MarshalText

func (e Event) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler.

func (Event) String

func (e Event) String() string

String returns the string representation of the event.

func (*Event) UnmarshalText

func (e *Event) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type EventPayload

type EventPayload interface {
	// Event returns the event type.
	Event() Event
	// RepositoryID returns the repository ID.
	RepositoryID() int64
}

EventPayload is a webhook event payload.

type Hook

type Hook struct {
	models.Webhook
	ContentType ContentType
	Events      []Event
}

Hook is a repository webhook.

type PushEvent

type PushEvent struct {
	Common

	// Ref is the branch or tag name.
	Ref string `json:"ref" url:"ref"`
	// Before is the previous commit SHA.
	Before string `json:"before" url:"before"`
	// After is the current commit SHA.
	After string `json:"after" url:"after"`
	// Commits is the list of commits.
	Commits []Commit `json:"commits" url:"commits"`
}

PushEvent is a push event.

func NewPushEvent

func NewPushEvent(ctx context.Context, user proto.User, repo proto.Repository, ref, before, after string) (PushEvent, error)

NewPushEvent sends a push event.

type Repository

type Repository struct {
	// ID is the repository ID.
	ID int64 `json:"id" url:"id"`
	// Name is the repository name.
	Name string `json:"name" url:"name"`
	// ProjectName is the repository project name.
	ProjectName string `json:"project_name" url:"project_name"`
	// Description is the repository description.
	Description string `json:"description" url:"description"`
	// DefaultBranch is the repository default branch.
	DefaultBranch string `json:"default_branch" url:"default_branch"`
	// Private is whether the repository is private.
	Private bool `json:"private" url:"private"`
	// Owner is the repository owner.
	Owner User `json:"owner" url:"owner"`
	// HTTPURL is the repository HTTP URL.
	HTTPURL string `json:"http_url" url:"http_url"`
	// SSHURL is the repository SSH URL.
	SSHURL string `json:"ssh_url" url:"ssh_url"`
	// GitURL is the repository Git URL.
	GitURL string `json:"git_url" url:"git_url"`
	// CreatedAt is the repository creation time.
	CreatedAt time.Time `json:"created_at" url:"created_at"`
	// UpdatedAt is the repository last update time.
	UpdatedAt time.Time `json:"updated_at" url:"updated_at"`
}

Repository represents an event repository.

type RepositoryEvent

type RepositoryEvent struct {
	Common

	// Action is the repository event action.
	Action RepositoryEventAction `json:"action" url:"action"`
}

RepositoryEvent is a repository payload.

func NewRepositoryEvent

func NewRepositoryEvent(ctx context.Context, user proto.User, repo proto.Repository, action RepositoryEventAction) (RepositoryEvent, error)

NewRepositoryEvent sends a repository event.

type RepositoryEventAction

type RepositoryEventAction string

RepositoryEventAction is a repository event action.

const (
	// RepositoryEventActionDelete is a repository deleted event.
	RepositoryEventActionDelete RepositoryEventAction = "delete"
	// RepositoryEventActionRename is a repository renamed event.
	RepositoryEventActionRename RepositoryEventAction = "rename"
	// RepositoryEventActionVisibilityChange is a repository visibility changed event.
	RepositoryEventActionVisibilityChange RepositoryEventAction = "visibility_change"
	// RepositoryEventActionDefaultBranchChange is a repository default branch changed event.
	RepositoryEventActionDefaultBranchChange RepositoryEventAction = "default_branch_change"
)

type User

type User struct {
	// ID is the owner ID.
	ID int64 `json:"id" url:"id"`
	// Username is the owner username.
	Username string `json:"username" url:"username"`
}

User represents a user in an event.

Jump to

Keyboard shortcuts

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