hook

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: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ParamPreReceive is the parameter under which the pre-receive operation is registered.
	ParamPreReceive = "pre-receive"
	// ParamUpdate is the parameter under which the update operation is registered.
	ParamUpdate = "update"
	// ParamPostReceive is the parameter under which the post-receive operation is registered.
	ParamPostReceive = "post-receive"

	// CommandNamePreReceive is the command used by git for the pre-receive hook
	// (os.args[0] == "hooks/pre-receive").
	CommandNamePreReceive = "hooks/pre-receive"
	// CommandNameUpdate is the command used by git for the update hook
	// (os.args[0] == "hooks/update").
	CommandNameUpdate = "hooks/update"
	// CommandNamePostReceive is the command used by git for the post-receive hook
	// (os.args[0] == "hooks/post-receive").
	CommandNamePostReceive = "hooks/post-receive"
)

Variables

View Source
var (
	// ErrDisabled can be returned by the loading function to indicate the githook has been disabled.
	// Returning the error will cause the githook execution to be skipped (githook is noop and returns success).
	ErrDisabled = errors.New("githook disabled")
)
View Source
var (
	// ErrNotFound is returned in case resources related to a githook call aren't found.
	ErrNotFound = fmt.Errorf("not found")
)

Functions

func EnvVarsToMap

func EnvVarsToMap(in []string) (map[string]string, error)

TODO: move to single representation once we have our custom Git CLI wrapper.

func GenerateEnvironmentVariables

func GenerateEnvironmentVariables(payload any) (map[string]string, error)

GenerateEnvironmentVariables generates the environment variables that should be used when calling git to ensure the payload will be available to the githook cli.

func LoadPayloadFromEnvironment

func LoadPayloadFromEnvironment[T any]() (T, error)

LoadPayloadFromEnvironment loads the githook payload from the environment.

func LoadPayloadFromMap

func LoadPayloadFromMap[T any](envVars map[string]string) (T, error)

LoadPayloadFromMap loads the payload from a map containing environment variables in a map format.

func RegisterAll

func RegisterAll(cmd KingpinRegister, loadCoreFn LoadCLICoreFunc)

RegisterAll registers all githook commands.

func RegisterPostReceive

func RegisterPostReceive(cmd KingpinRegister, loadCoreFn LoadCLICoreFunc)

RegisterPostReceive registers the post-receive githook command.

func RegisterPreReceive

func RegisterPreReceive(cmd KingpinRegister, loadCoreFn LoadCLICoreFunc)

RegisterPreReceive registers the pre-receive githook command.

func RegisterUpdate

func RegisterUpdate(cmd KingpinRegister, loadCoreFn LoadCLICoreFunc)

RegisterUpdate registers the update githook command.

func SanitizeArgsForGit

func SanitizeArgsForGit(command string, args []string) ([]string, bool)

SanitizeArgsForGit sanitizes the command line arguments (os.Args) if the command indicates they are coming from git. Returns the santized args and true if the call comes from git, otherwise the original args are returned with false.

Types

type CLICore

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

CLICore implements the core of a githook cli. It uses the client and execution timeout to perform githook operations as part of a cli.

func NewCLICore

func NewCLICore(client Client, executionTimeout time.Duration) *CLICore

NewCLICore returns a new CLICore using the provided client and execution timeout.

func (*CLICore) PostReceive

func (c *CLICore) PostReceive(ctx context.Context) error

PostReceive executes the post-receive git hook.

func (*CLICore) PreReceive

func (c *CLICore) PreReceive(ctx context.Context) error

PreReceive executes the pre-receive git hook.

func (*CLICore) Update

func (c *CLICore) Update(ctx context.Context, ref string, oldSHARaw string, newSHARaw string) error

Update executes the update git hook.

type Client

type Client interface {
	PreReceive(ctx context.Context, in PreReceiveInput) (Output, error)
	Update(ctx context.Context, in UpdateInput) (Output, error)
	PostReceive(ctx context.Context, in PostReceiveInput) (Output, error)
}

Client is an abstraction of a githook client that can be used to trigger githook calls.

func NewNoopClient

func NewNoopClient(messages []string) Client

type ClientFactory

type ClientFactory interface {
	NewClient(envVars map[string]string) (Client, error)
}

ClientFactory is an abstraction of a factory that creates a new client based on the provided environment variables.

type Environment

type Environment struct {
	// AlternateObjectDirs contains any alternate object dirs required to access all objects of an operation.
	AlternateObjectDirs []string `json:"alternate_object_dirs,omitempty"`
}

Environment contains the information required to access a specific git environment.

type GitPushOptions

type GitPushOptions map[string]string

GitPushOptions is a wrapper around a map[string]string

type KingpinRegister

type KingpinRegister interface {
	Command(name, help string) *kingpin.CmdClause
}

KingpinRegister is an abstraction of an entity that allows to register commands. This is required to allow registering hook commands both on application and sub command level.

type LoadCLICoreFunc

type LoadCLICoreFunc func() (*CLICore, error)

LoadCLICoreFunc is a function that creates a new CLI core that's used for githook cli execution. This allows users to initialize their own CLI core with custom Client and configuration.

type NoopClient

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

NoopClient directly returns success with the provided messages, without any other impact.

func (*NoopClient) PostReceive

func (c *NoopClient) PostReceive(_ context.Context, _ PostReceiveInput) (Output, error)

func (*NoopClient) PreReceive

func (c *NoopClient) PreReceive(_ context.Context, _ PreReceiveInput) (Output, error)

func (*NoopClient) Update

func (c *NoopClient) Update(_ context.Context, _ UpdateInput) (Output, error)

type Output

type Output struct {
	// Messages contains standard user facing messages.
	Messages []string `json:"messages,omitempty"`

	// Error contains the user facing error (like "branch is protected", ...).
	Error *string `json:"error,omitempty"`
}

Output represents the output of server hook api calls.

type PostReceiveInput

type PostReceiveInput struct {
	// Environment contains the information required to access the git environment.
	Environment Environment `json:"environment"`

	// RefUpdates contains all references that got updated as part of the git operation.
	RefUpdates []ReferenceUpdate `json:"ref_updates"`
}

PostReceiveInput represents the input of the post-receive git hook.

type PreReceiveInput

type PreReceiveInput struct {
	// Environment contains the information required to access the git environment.
	Environment Environment `json:"environment"`

	// RefUpdates contains all references that are being updated as part of the git operation.
	RefUpdates []ReferenceUpdate `json:"ref_updates"`
}

PreReceiveInput represents the input of the pre-receive git hook.

type RefUpdater

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

RefUpdater is an entity that is responsible for update of a reference. It will call pre-receive hook prior to the update and post-receive hook after the update. It has a state machine to guarantee that methods are called in the correct order (Init, Pre, Update, Post).

func CreateRefUpdater

func CreateRefUpdater(
	hookClientFactory ClientFactory,
	envVars map[string]string,
	repoPath string,
	ref string,
) (*RefUpdater, error)

CreateRefUpdater creates new RefUpdater object using the provided git hook ClientFactory.

func (*RefUpdater) Do

func (u *RefUpdater) Do(ctx context.Context, oldValue, newValue sha.SHA) error

Do runs full ref update by executing all methods in the correct order.

func (*RefUpdater) Init

func (u *RefUpdater) Init(ctx context.Context, oldValue, newValue sha.SHA) error

func (*RefUpdater) InitNew

func (u *RefUpdater) InitNew(_ context.Context, newValue sha.SHA) error

func (*RefUpdater) InitOld

func (u *RefUpdater) InitOld(ctx context.Context, oldValue sha.SHA) error

func (*RefUpdater) Post

func (u *RefUpdater) Post(ctx context.Context, alternateDirs ...string) error

Post runs the pre-receive git hook.

func (*RefUpdater) Pre

func (u *RefUpdater) Pre(ctx context.Context, alternateDirs ...string) error

Pre runs the pre-receive git hook.

func (*RefUpdater) UpdateRef

func (u *RefUpdater) UpdateRef(ctx context.Context) error

UpdateRef updates the git reference.

type ReferenceUpdate

type ReferenceUpdate struct {
	// Ref is the full name of the reference that got updated.
	Ref string `json:"ref"`
	// Old is the old commmit hash (before the update).
	Old sha.SHA `json:"old"`
	// New is the new commit hash (after the update).
	New sha.SHA `json:"new"`
}

ReferenceUpdate represents an update of a git reference.

type UpdateInput

type UpdateInput struct {
	// Environment contains the information required to access the git environment.
	Environment Environment `json:"environment"`

	// RefUpdate contains information about the reference that is being updated.
	RefUpdate ReferenceUpdate `json:"ref_update"`
}

UpdateInput represents the input of the update git hook.

Jump to

Keyboard shortcuts

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