webhook

package
v0.0.0-...-4c964c4 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: Apache-2.0 Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeReviewTemplate = `` /* 302-byte string literal not displayed */

	DefaultDiffUnified = 3
	MaxCommentLength   = 64 << 10
	DefaultAITimeout   = 30 * time.Second
)
View Source
const MaxWebhookCommitFileStats = 20

Variables

View Source
var ErrInternalWebhookOperationNotAllowed = errors.Forbidden("changes to internal webhooks are not allowed")
View Source
var (
	// ErrWebhookNotRetriggerable is returned in case the webhook can't be retriggered due to an incomplete execution.
	// This should only occur if we failed to generate the request body (most likely out of memory).
	ErrWebhookNotRetriggerable = errors.New("webhook execution is incomplete and can't be retriggered")
)

WireSet provides a wire set for this package.

Functions

func CheckSecret

func CheckSecret(secret string) error

checkSecret validates the secret of a webhook.

func CheckTriggers

func CheckTriggers(triggers []enum.WebhookTrigger) error

CheckTriggers validates the triggers of a webhook.

func CheckURL

func CheckURL(rawURL string, allowLoopback bool, allowPrivateNetwork bool, internal bool) error

CheckURL validates the url of a webhook.

func ConvertTriggers

func ConvertTriggers(vals []string) []enum.WebhookTrigger

func DeduplicateTriggers

func DeduplicateTriggers(in []enum.WebhookTrigger) []enum.WebhookTrigger

DeduplicateTriggers de-duplicates the triggers provided by the user.

Types

type BaseSegment

type BaseSegment struct {
	Trigger   enum.WebhookTrigger `json:"trigger"`
	Repo      RepositoryInfo      `json:"repo"`
	Principal PrincipalInfo       `json:"principal"`
}

BaseSegment contains base info of all payloads for webhooks.

type CodeCommentInfo

type CodeCommentInfo struct {
	Outdated     bool   `json:"outdated"`
	MergeBaseSHA string `json:"merge_base_sha"`
	SourceSHA    string `json:"source_sha"`
	Path         string `json:"path"`
	LineNew      int    `json:"line_new"`
	SpanNew      int    `json:"span_new"`
	LineOld      int    `json:"line_old"`
	SpanOld      int    `json:"span_old"`
}

type CommentInfo

type CommentInfo struct {
	ID       int64                    `json:"id"`
	ParentID *int64                   `json:"parent_id,omitempty"`
	Text     string                   `json:"text"`
	Created  int64                    `json:"created"`
	Updated  int64                    `json:"updated"`
	Kind     enum.PullReqActivityKind `json:"kind"`
}

type CommitInfo

type CommitInfo struct {
	SHA       string        `json:"sha"`
	Message   string        `json:"message"`
	Author    SignatureInfo `json:"author"`
	Committer SignatureInfo `json:"committer"`

	Added    []string `json:"added"`
	Removed  []string `json:"removed"`
	Modified []string `json:"modified"`
}

CommitInfo describes the commit related info for a webhook payload. NOTE: don't use types package as we want webhook payload to be independent from API calls.

type Config

type Config struct {
	// UserAgentIdentity specifies the identity used for the user agent header
	// IMPORTANT: do not include version.
	UserAgentIdentity string
	// HeaderIdentity specifies the identity used for headers in webhook calls (e.g. X-Gitness-Trigger, ...).
	// NOTE: If no value is provided, the UserAgentIdentity will be used.
	HeaderIdentity      string
	EventReaderName     string
	Concurrency         int
	MaxRetries          int
	AllowPrivateNetwork bool
	AllowLoopback       bool

	// InternalWebhooksURL specifies the internal webhook URL which will be used if webhook is marked internal
	InternalWebhooksURL string
}

func (*Config) Prepare

func (c *Config) Prepare() error

type GitnessURLProvider

type GitnessURLProvider struct{}

func NewURLProvider

func NewURLProvider(_ context.Context) *GitnessURLProvider

func (*GitnessURLProvider) GetWebhookURL

func (u *GitnessURLProvider) GetWebhookURL(_ context.Context, webhook *types.Webhook) (string, error)

type IdentityInfo

type IdentityInfo struct {
	Name  string `json:"name"`
	Email string `json:"email"`
}

IdentityInfo describes the signature identity related info for a webhook payload. NOTE: don't use types package as we want webhook payload to be independent from API calls.

type LabelInfo

type LabelInfo struct {
	ID      int64   `json:"id"`
	Key     string  `json:"key"`
	ValueID *int64  `json:"value_id,omitempty"`
	Value   *string `json:"value,omitempty"`
}

type PrincipalInfo

type PrincipalInfo struct {
	ID          int64              `json:"id"`
	UID         string             `json:"uid"`
	DisplayName string             `json:"display_name"`
	Email       string             `json:"email"`
	Type        enum.PrincipalType `json:"type"`
	Created     int64              `json:"created"`
	Updated     int64              `json:"updated"`
}

PrincipalInfo describes the principal related info for a webhook payload. NOTE: don't use types package as we want webhook payload to be independent from API calls.

type PullReqBranchUpdatedPayload

PullReqBranchUpdatedPayload describes the body of the pullreq branch updated trigger. TODO: move in separate package for small import?

type PullReqClosedPayload

PullReqClosedPayload describes the body of the pullreq closed trigger.

type PullReqCommentPayload

PullReqCommentPayload describes the body of the pullreq comment create trigger.

type PullReqCommentSegment

type PullReqCommentSegment struct {
	CommentInfo CommentInfo `json:"comment"`
	*CodeCommentInfo
}

PullReqCommentSegment contains details for all pull req comment related payloads for webhooks.

type PullReqCommentUpdatedPayload

PullReqCommentUpdatedPayload describes the body of the pullreq comment create trigger.

type PullReqCommentUpdatedSegment

type PullReqCommentUpdatedSegment struct {
	CommentInfo
	*CodeCommentInfo
}

PullReqCommentUpdatedSegment contains details for pullreq text comment edited payloads for webhooks.

type PullReqCreatedPayload

PullReqCreatedPayload describes the body of the pullreq created trigger. TODO: move in separate package for small import?

type PullReqInfo

type PullReqInfo struct {
	Number        int64             `json:"number"`
	State         enum.PullReqState `json:"state"`
	IsDraft       bool              `json:"is_draft"`
	Title         string            `json:"title"`
	Description   string            `json:"description"`
	SourceRepoID  int64             `json:"source_repo_id"`
	SourceBranch  string            `json:"source_branch"`
	TargetRepoID  int64             `json:"target_repo_id"`
	TargetBranch  string            `json:"target_branch"`
	MergeStrategy *enum.MergeMethod `json:"merge_strategy,omitempty"`
	Author        PrincipalInfo     `json:"author"`
	PrURL         string            `json:"pr_url"`
}

PullReqInfo describes the pullreq related info for a webhook payload. NOTE: don't use types package as we want pullreq payload to be independent from API calls.

type PullReqLabelAssignedPayload

type PullReqLabelAssignedPayload struct {
	BaseSegment
	PullReqSegment
	PullReqLabelSegment
}

PullReqLabelAssignedPayload describes the body of the pullreq label assignment trigger.

type PullReqLabelSegment

type PullReqLabelSegment struct {
	LabelInfo LabelInfo `json:"label"`
}

PullReqLabelSegment contains details for all pull req label related payloads for webhooks.

type PullReqMergedPayload

PullReqMergedPayload describes the body of the pullreq merged trigger.

type PullReqReopenedPayload

type PullReqReopenedPayload PullReqCreatedPayload

PullReqReopenedPayload describes the body of the pullreq reopened trigger. Note: same as payload for created.

type PullReqReviewSegment

type PullReqReviewSegment struct {
	ReviewDecision enum.PullReqReviewDecision `json:"review_decision"`
	ReviewerInfo   PrincipalInfo              `json:"reviewer"`
}

type PullReqReviewSubmittedPayload

PullReqReviewSubmittedPayload describes the body of the pullreq review submitted trigger.

type PullReqReviewerChangedPayload

type PullReqReviewerChangedPayload struct {
	BaseSegment
	PullReqSegment
	ReviewerSegment
}

type PullReqSegment

type PullReqSegment struct {
	PullReq PullReqInfo `json:"pull_req"`
}

PullReqSegment contains details for all pull req related payloads for webhooks.

type PullReqTargetReferenceSegment

type PullReqTargetReferenceSegment struct {
	TargetRef ReferenceInfo `json:"target_ref"`
}

PullReqTargetReferenceSegment contains details for the pull req target reference for webhooks.

type PullReqUpdateSegment

type PullReqUpdateSegment struct {
	TitleChanged       bool   `json:"title_changed"`
	TitleOld           string `json:"title_old"`
	TitleNew           string `json:"title_new"`
	DescriptionChanged bool   `json:"description_changed"`
	DescriptionOld     string `json:"description_old"`
	DescriptionNew     string `json:"description_new"`
}

PullReqUpdateSegment contains details what has been updated in the pull request.

type PullReqUpdatedPayload

PullReqUpdatedPayload describes the body of the pullreq updated trigger.

type ReferenceDetailsSegment

type ReferenceDetailsSegment struct {
	SHA string `json:"sha"`

	HeadCommit *CommitInfo `json:"head_commit,omitempty"`

	Commits           *[]CommitInfo `json:"commits,omitempty"`
	TotalCommitsCount int           `json:"total_commits_count,omitempty"`

	// Deprecated
	Commit *CommitInfo `json:"commit,omitempty"`
}

ReferenceDetailsSegment contains extra details for reference related payloads for webhooks.

type ReferenceInfo

type ReferenceInfo struct {
	Name string         `json:"name"`
	Repo RepositoryInfo `json:"repo"`
}

ReferenceInfo describes a unique reference in Harness. It contains both the reference name as well as the repo the reference belongs to.

type ReferencePayload

ReferencePayload describes the payload of Reference related webhook triggers. Note: Use same payload for all reference operations to make it easier for consumers.

type ReferenceSegment

type ReferenceSegment struct {
	Ref ReferenceInfo `json:"ref"`
}

ReferenceSegment contains the reference info for webhooks.

type ReferenceUpdateSegment

type ReferenceUpdateSegment struct {
	OldSHA string `json:"old_sha"`
	Forced bool   `json:"forced"`
}

ReferenceUpdateSegment contains extra details for reference update related payloads for webhooks.

type RepositoryInfo

type RepositoryInfo struct {
	ID            int64  `json:"id"`
	Path          string `json:"path"`
	Identifier    string `json:"identifier"`
	Description   string `json:"description"`
	DefaultBranch string `json:"default_branch"`
	URL           string `json:"url"`
	GitURL        string `json:"git_url"`
	GitSSHURL     string `json:"git_ssh_url"`
}

RepositoryInfo describes the repo related info for a webhook payload. NOTE: don't use types package as we want webhook payload to be independent from API calls.

func (RepositoryInfo) MarshalJSON

func (r RepositoryInfo) MarshalJSON() ([]byte, error)

TODO [CODE-1363]: remove after identifier migration.

type ReviewerSegment

type ReviewerSegment struct {
	Reviewer PrincipalInfo `json:"reviewer"`
}

ReviewerSegment contains details for all reviewer related payloads for webhooks.

type Service

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

Service is responsible for processing webhook events.

func NewService

func NewService(
	ctx context.Context,
	config Config,
	tx dbtx.Transactor,
	gitReaderFactory *events.ReaderFactory[*gitevents.Reader],
	prReaderFactory *events.ReaderFactory[*pullreqevents.Reader],
	webhookStore store.WebhookStore,
	webhookExecutionStore store.WebhookExecutionStore,
	spaceStore store.SpaceStore,
	aiStore store.AIStore,
	repoStore store.RepoStore,
	pullreqStore store.PullReqStore,
	activityStore store.PullReqActivityStore,
	urlProvider url.Provider,
	principalStore store.PrincipalStore,
	git git.Interface,
	encrypter encrypt.Encrypter,
	labelStore store.LabelStore,
	settings *settings.Service,
	webhookURLProvider URLProvider,
	labelValueStore store.LabelValueStore,
) (*Service, error)

func ProvideService

func ProvideService(
	ctx context.Context,
	config Config,
	tx dbtx.Transactor,
	gitReaderFactory *events.ReaderFactory[*gitevents.Reader],
	prReaderFactory *events.ReaderFactory[*pullreqevents.Reader],
	webhookStore store.WebhookStore,
	webhookExecutionStore store.WebhookExecutionStore,
	spaceStore store.SpaceStore,
	aiStore store.AIStore,
	repoStore store.RepoStore,
	pullreqStore store.PullReqStore,
	activityStore store.PullReqActivityStore,
	urlProvider url.Provider,
	principalStore store.PrincipalStore,
	git git.Interface,
	encrypter encrypt.Encrypter,
	labelStore store.LabelStore,
	settings *settings.Service,
	webhookURLProvider URLProvider,
	labelValueStore store.LabelValueStore,
) (*Service, error)

func (*Service) Create

func (s *Service) Create(
	ctx context.Context,
	principalID int64,
	parentID int64,
	parentType enum.WebhookParent,
	internal bool,
	in *types.WebhookCreateInput,
) (*types.Webhook, error)

func (*Service) Delete

func (s *Service) Delete(
	ctx context.Context,
	parentID int64,
	parentType enum.WebhookParent,
	webhookIdentifier string,
	allowDeletingInternal bool,
) error

Delete deletes an existing webhook.

func (*Service) Find

func (s *Service) Find(
	ctx context.Context,
	parentID int64,
	parentType enum.WebhookParent,
	webhookIdentifier string,
) (*types.Webhook, error)

func (*Service) FindExecution

func (s *Service) FindExecution(
	ctx context.Context,
	parentID int64,
	parentType enum.WebhookParent,
	webhookIdentifier string,
	webhookExecutionID int64,
) (*types.WebhookExecution, error)

FindExecution finds a webhook execution.

func (*Service) GetWebhookExecutionVerifyOwnership

func (s *Service) GetWebhookExecutionVerifyOwnership(
	ctx context.Context,
	webhookID int64,
	webhookExecutionID int64,
) (*types.WebhookExecution, error)

GetWebhookExecutionVerifyOwnership gets the webhook execution and ensures it belongs to the webhook with the specified id.

func (*Service) GetWebhookVerifyOwnership

func (s *Service) GetWebhookVerifyOwnership(
	ctx context.Context,
	parentID int64,
	parentType enum.WebhookParent,
	webhookIdentifier string,
) (*types.Webhook, error)

GetWebhookVerifyOwnership gets the webhook and ensures it belongs to the scope with the specified id and type.

func (*Service) List

func (s *Service) List(
	ctx context.Context,
	parentID int64,
	parentType enum.WebhookParent,
	inherited bool,
	filter *types.WebhookFilter,
) ([]*types.Webhook, int64, error)

Listreturns the webhooks from the provided scope.

func (*Service) ListExecutions

func (s *Service) ListExecutions(
	ctx context.Context,
	parentID int64,
	parentType enum.WebhookParent,
	webhookIdentifier string,
	filter *types.WebhookExecutionFilter,
) ([]*types.WebhookExecution, int64, error)

ListExecutions returns the executions of the webhook.

func (*Service) RetriggerExecution

func (s *Service) RetriggerExecution(
	ctx context.Context,
	parentID int64,
	parentType enum.WebhookParent,
	webhookIdentifier string,
	webhookExecutionID int64,
) (*types.WebhookExecution, error)

RetriggerExecution retriggers an existing webhook execution.

func (*Service) RetriggerWebhookExecution

func (s *Service) RetriggerWebhookExecution(ctx context.Context, webhookExecutionID int64) (*TriggerResult, error)

func (*Service) Update

func (s *Service) Update(
	ctx context.Context,
	parentID int64,
	parentType enum.WebhookParent,
	webhookIdentifier string,
	allowModifyingInternal bool,
	in *types.WebhookUpdateInput,
) (*types.Webhook, error)

type SignatureInfo

type SignatureInfo struct {
	Identity IdentityInfo `json:"identity"`
	When     time.Time    `json:"when"`
}

SignatureInfo describes the commit signature related info for a webhook payload. NOTE: don't use types package as we want webhook payload to be independent from API calls.

type TriggerResult

type TriggerResult struct {
	TriggerID   string
	TriggerType enum.WebhookTrigger
	Webhook     *types.Webhook
	Execution   *types.WebhookExecution
	Err         error
}

func (*TriggerResult) Skipped

func (r *TriggerResult) Skipped() bool

type URLProvider

type URLProvider interface {
	GetWebhookURL(ctx context.Context, webhook *types.Webhook) (string, error)
}

func ProvideURLProvider

func ProvideURLProvider(ctx context.Context) URLProvider

Jump to

Keyboard shortcuts

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