gitlab

package
v14.6.7 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2022 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// MockAllowed is a callback for the MockClient's `Allowed()` function which always allows a
	// change.
	MockAllowed = func(context.Context, AllowedParams) (bool, string, error) {
		return true, "", nil
	}
	// MockPreReceive is a callback for the MockClient's `PreReceive()` function which always
	// allows a change.
	MockPreReceive = func(context.Context, string) (bool, error) {
		return true, nil
	}
	// MockPostReceive is a callback for the MockCLient's `PostReceive()` function which always
	// allows a change.
	MockPostReceive = func(context.Context, string, string, string, ...string) (bool, []PostReceiveMessage, error) {
		return true, nil, nil
	}
)

Functions

func NewTestServer added in v14.2.0

func NewTestServer(t testing.TB, options TestServerOptions) (url string, cleanup func())

NewTestServer returns a mock gitlab server that responds to the hook api endpoints

func SetupAndStartGitlabServer added in v14.2.0

func SetupAndStartGitlabServer(t testing.TB, shellDir string, c *TestServerOptions) (string, func())

SetupAndStartGitlabServer creates a new GitlabTestServer, starts it and sets up the gitlab-shell secret.

func WriteShellSecretFile added in v14.2.0

func WriteShellSecretFile(t testing.TB, dir, secretToken string) string

WriteShellSecretFile writes a .gitlab_shell_secret file in the specified directory

Types

type AllowedParams

type AllowedParams struct {
	// RepoPath is an absolute path to the repository.
	RepoPath string
	// GitObjectDirectory is a path to git object directory.
	GitObjectDirectory string
	// GitAlternateObjectDirectories are the paths to alternate object directories.
	GitAlternateObjectDirectories []string
	// GLRepository is a name of the repository.
	GLRepository string
	// GLID is an identifier of the repository.
	GLID string
	// GLProtocol is a protocol used for operation.
	GLProtocol string
	// Changes is a set of changes to be applied.
	Changes string
}

AllowedParams compose set of parameters required to call 'GitlabAPI.Allowed' method.

type CheckInfo

type CheckInfo struct {
	// Version of the GitLab Rails component
	Version string `json:"gitlab_version"`
	// Revision of the Git object of the running GitLab
	Revision string `json:"gitlab_revision"`
	// APIVersion of GitLab, expected to be v4
	APIVersion string `json:"api_version"`
	// RedisReachable shows if GitLab can reach Redis. This can be false
	// while the check itself succeeds. Normal hook API calls will likely
	// fail.
	RedisReachable bool `json:"redis"`
}

CheckInfo represents the response of GitLabs `check` API endpoint

type Client

type Client interface {
	// Allowed queries the gitlab internal api /allowed endpoint to determine if a ref change for a given repository and user is allowed
	Allowed(ctx context.Context, params AllowedParams) (bool, string, error)
	// Check verifies that GitLab can be reached, and authenticated to
	Check(ctx context.Context) (*CheckInfo, error)
	// PreReceive queries the gitlab internal api /pre_receive to increase the reference counter
	PreReceive(ctx context.Context, glRepository string) (bool, error)
	// PostReceive queries the gitlab internal api /post_receive to decrease the reference counter
	PostReceive(ctx context.Context, glRepository, glID, changes string, pushOptions ...string) (bool, []PostReceiveMessage, error)
}

Client is an interface for accessing the GitLab internal API

func NewMockClient

func NewMockClient(
	tb testing.TB,
	allowed func(context.Context, AllowedParams) (bool, string, error),
	preReceive func(context.Context, string) (bool, error),
	postReceive func(context.Context, string, string, string, ...string) (bool, []PostReceiveMessage, error),
) Client

NewMockClient returns a new mock client for the internal GitLab API.

type HTTPClient

type HTTPClient struct {
	*client.GitlabNetClient
	// contains filtered or unexported fields
}

HTTPClient is an HTTP client used to talk to the internal GitLab Rails API.

func NewHTTPClient

func NewHTTPClient(
	logger logrus.FieldLogger,
	gitlabCfg config.Gitlab,
	tlsCfg config.TLS,
	promCfg gitalycfgprom.Config,
) (*HTTPClient, error)

NewHTTPClient creates an HTTP client to talk to the Rails internal API

func (*HTTPClient) Allowed

func (c *HTTPClient) Allowed(ctx context.Context, params AllowedParams) (bool, string, error)

Allowed checks if a ref change for a given repository is allowed through the gitlab internal api /allowed endpoint

func (*HTTPClient) Check

func (c *HTTPClient) Check(ctx context.Context) (*CheckInfo, error)

Check performs an HTTP request to the internal/check API endpoint to verify the connection and tokens. It returns basic information of the installed GitLab

func (*HTTPClient) Collect

func (c *HTTPClient) Collect(metrics chan<- prometheus.Metric)

Collect collects Prometheus metrics exposed by the HTTPClient.

func (*HTTPClient) Describe

func (c *HTTPClient) Describe(descs chan<- *prometheus.Desc)

Describe describes Prometheus metrics exposed by the HTTPClient.

func (*HTTPClient) PostReceive

func (c *HTTPClient) PostReceive(ctx context.Context, glRepository, glID, changes string, pushOptions ...string) (bool, []PostReceiveMessage, error)

PostReceive decreases the reference counter for a push for a given gl_repository through the gitlab internal API /post_receive endpoint

func (*HTTPClient) PreReceive

func (c *HTTPClient) PreReceive(ctx context.Context, glRepository string) (bool, error)

PreReceive increases the reference counter for a push for a given gl_repository through the gitlab internal API /pre_receive endpoint

type MockClient

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

MockClient is a mock client of the internal GitLab API.

func (*MockClient) Allowed

func (m *MockClient) Allowed(ctx context.Context, params AllowedParams) (bool, string, error)

Allowed does nothing and always returns true.

func (*MockClient) Check

func (m *MockClient) Check(ctx context.Context) (*CheckInfo, error)

Check does nothing and always returns a CheckInfo prepopulated with static data.

func (*MockClient) PostReceive

func (m *MockClient) PostReceive(ctx context.Context, glRepository, glID, changes string, gitPushOptions ...string) (bool, []PostReceiveMessage, error)

PostReceive does nothing and always returns true.

func (*MockClient) PreReceive

func (m *MockClient) PreReceive(ctx context.Context, glRepository string) (bool, error)

PreReceive does nothing and always return true.

type PostReceiveMessage

type PostReceiveMessage struct {
	Message string `json:"message"`
	Type    string `json:"type"`
}

PostReceiveMessage encapsulates a message from the /post_receive endpoint that gets printed to stdout

type TestServerOptions added in v14.2.0

type TestServerOptions struct {
	User, Password, SecretToken string
	GLID                        string
	GLRepository                string
	Changes                     string
	PostReceiveMessages         []string
	PostReceiveAlerts           []string
	PostReceiveCounterDecreased bool
	UnixSocket                  bool
	LfsStatusCode               int
	LfsOid                      string
	LfsBody                     string
	Protocol                    string
	GitPushOptions              []string
	GitObjectDir                string
	GitAlternateObjectDirs      []string
	RepoPath                    string
	RelativeURLRoot             string
	GlRepository                string
	ClientCACertPath            string // used to verify client certs are valid
	ServerCertPath              string
	ServerKeyPath               string
}

TestServerOptions is a config for a mock gitlab server containing expected values

Jump to

Keyboard shortcuts

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