Documentation
¶
Index ¶
- Constants
- Variables
- func EnvVarsToMap(in []string) (map[string]string, error)
- func GenerateEnvironmentVariables(payload any) (map[string]string, error)
- func LoadPayloadFromEnvironment[T any]() (T, error)
- func LoadPayloadFromMap[T any](envVars map[string]string) (T, error)
- func RegisterAll(cmd KingpinRegister, loadCoreFn LoadCLICoreFunc)
- func RegisterPostReceive(cmd KingpinRegister, loadCoreFn LoadCLICoreFunc)
- func RegisterPreReceive(cmd KingpinRegister, loadCoreFn LoadCLICoreFunc)
- func RegisterUpdate(cmd KingpinRegister, loadCoreFn LoadCLICoreFunc)
- func SanitizeArgsForGit(command string, args []string) ([]string, bool)
- type CLICore
- type Client
- type ClientFactory
- type Environment
- type GitPushOptions
- type KingpinRegister
- type LoadCLICoreFunc
- type NoopClient
- type Output
- type PostReceiveInput
- type PreReceiveInput
- type RefUpdater
- func (u *RefUpdater) Do(ctx context.Context, oldValue, newValue sha.SHA) error
- func (u *RefUpdater) Init(ctx context.Context, oldValue, newValue sha.SHA) error
- func (u *RefUpdater) InitNew(_ context.Context, newValue sha.SHA) error
- func (u *RefUpdater) InitOld(ctx context.Context, oldValue sha.SHA) error
- func (u *RefUpdater) Post(ctx context.Context, alternateDirs ...string) error
- func (u *RefUpdater) Pre(ctx context.Context, alternateDirs ...string) error
- func (u *RefUpdater) UpdateRef(ctx context.Context) error
- type ReferenceUpdate
- type UpdateInput
Constants ¶
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 ¶
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") )
var ( // ErrNotFound is returned in case resources related to a githook call aren't found. ErrNotFound = fmt.Errorf("not found") )
Functions ¶
func EnvVarsToMap ¶
TODO: move to single representation once we have our custom Git CLI wrapper.
func GenerateEnvironmentVariables ¶
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 ¶
LoadPayloadFromEnvironment loads the githook payload from the environment.
func LoadPayloadFromMap ¶
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 ¶
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 ¶
NewCLICore returns a new CLICore using the provided client and execution timeout.
func (*CLICore) PostReceive ¶
PostReceive executes the post-receive git hook.
func (*CLICore) PreReceive ¶
PreReceive executes the pre-receive 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 ¶
type ClientFactory ¶
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 ¶
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 ¶
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) Post ¶
func (u *RefUpdater) Post(ctx context.Context, alternateDirs ...string) error
Post runs the pre-receive git hook.
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.