vcs

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: MPL-2.0 Imports: 31 Imported by: 0

Documentation

Overview

Package vcs handles version control system stuff.

templ: version: v0.3.906

Index

Constants

View Source
const (
	GithubAPIURL  = "https://api.github.com"
	GithubHTTPURL = "https://github.com"
)

Variables

View Source
var ErrAppNotFound = errors.New("github app not found")
View Source
var ErrInvalidRepo = errors.New("repository path is invalid")

Functions

func RepoURL added in v0.3.24

func RepoURL(provider *Provider, repo Repo) templ.SafeURL

Types

type Action

type Action string
const (
	ActionCreated Action = "created"
	ActionDeleted Action = "deleted"
	ActionMerged  Action = "merged"
	ActionUpdated Action = "updated"
)

type App added in v0.3.24

type App interface {
	// ListInstallations retrieves a list of installations.
	ListInstallations(context.Context) ([]Installation, error)
	// GetInstallation retrieves an installation by its ID.
	GetInstallation(context.Context, int64) (Installation, error)
	// InstallationLink is a link to the site where a user can create an
	// installation.
	InstallationLink() templ.SafeURL
}

type AppKind added in v0.3.24

type AppKind interface {
	GetApp(context.Context) (App, error)
}

type Broker

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

Broker is a brokerage for publishers and subscribers of VCS events.

func (*Broker) Publish

func (b *Broker) Publish(event Event)

func (*Broker) Subscribe

func (b *Broker) Subscribe(cb Callback)

type Callback

type Callback func(event Event)

type Client

type Client interface {
	// ListRepositories lists repositories accessible to the current user.
	ListRepositories(ctx context.Context, opts ListRepositoriesOptions) ([]Repo, error)
	GetDefaultBranch(ctx context.Context, identifier string) (string, error)
	// GetRepoTarball retrieves a .tar.gz tarball of a git repository
	GetRepoTarball(ctx context.Context, opts GetRepoTarballOptions) ([]byte, string, error)
	// CreateWebhook creates a webhook on the cloud provider, returning the
	// provider's unique ID for the webhook.
	CreateWebhook(ctx context.Context, opts CreateWebhookOptions) (string, error)
	UpdateWebhook(ctx context.Context, id string, opts UpdateWebhookOptions) error
	GetWebhook(ctx context.Context, opts GetWebhookOptions) (Webhook, error)
	DeleteWebhook(ctx context.Context, opts DeleteWebhookOptions) error
	SetStatus(ctx context.Context, opts SetStatusOptions) error
	// ListTags lists git tags on a repository. Each tag should be prefixed with
	// 'tags/'.
	ListTags(ctx context.Context, opts ListTagsOptions) ([]string, error)
	// ListPullRequestFiles returns the paths of files that are modified in the pull request
	ListPullRequestFiles(ctx context.Context, repo Repo, pull int) ([]string, error)
	// GetCommit retrieves commit from the repo with the given git ref
	GetCommit(ctx context.Context, repo Repo, ref string) (Commit, error)
}

type ClientConfig added in v0.4.0

type ClientConfig struct {
	Token        *string
	Installation *Installation
	BaseURL      *internal.WebURL
}

ClientConfig is configuration for the construction of a client.

type Commit

type Commit struct {
	SHA    string
	URL    string
	Author CommitAuthor
}

type CommitAuthor

type CommitAuthor struct {
	Username   string
	ProfileURL string
	AvatarURL  string
}

type CreateOptions added in v0.3.24

type CreateOptions struct {
	Organization organization.Name `schema:"organization_name,required"`
	Name         string
	KindID       KindID `schema:"kind,required"`
	// Token and InstallID are mutually exclusive.
	Token     *string
	InstallID *int64 `schema:"install_id"`
	// Optional.
	BaseURL *internal.WebURL `schema:"base_url,required"`
	// contains filtered or unexported fields
}

type CreateWebhookOptions

type CreateWebhookOptions struct {
	Repo     Repo   // repo identifier, <owner>/<repo>
	Secret   string // secret string for generating signature
	Endpoint string // otf's external-facing host[:port]
	Events   []EventType
}

type DeleteWebhookOptions

type DeleteWebhookOptions struct {
	Repo Repo   // Repository identifier, <owner>/<repo>
	ID   string // vcs' webhook ID
}

DeleteWebhookOptions are options for deleting a webhook.

type ErrIgnoreEvent added in v0.2.3

type ErrIgnoreEvent struct {
	Reason string
}

ErrIgnoreEvent informs an upstream vcs provider why an event it sent is ignored.

func NewErrIgnoreEvent added in v0.2.3

func NewErrIgnoreEvent(msg string, args ...any) ErrIgnoreEvent

func (ErrIgnoreEvent) Error added in v0.2.3

func (e ErrIgnoreEvent) Error() string

type Event

type Event struct {
	EventHeader
	EventPayload
}

Event is a VCS event received from a cloud, e.g. a commit event from github

type EventHeader

type EventHeader struct {
	// ID of vcs provider that generated this event.
	// event.
	VCSProviderID resource.TfeID
	// Source of vcs provider kind that generated this event.
	Source source.Source
}

type EventPayload

type EventPayload struct {
	Repo          Repo
	Type          EventType
	Action        Action
	Tag           string
	CommitSHA     string
	CommitURL     string
	Branch        string // head branch
	DefaultBranch string

	PullRequestNumber int
	PullRequestURL    string
	PullRequestTitle  string

	SenderUsername  string
	SenderAvatarURL string
	SenderHTMLURL   string

	// Paths of files that have been added/modified/removed. Only applicable
	// to Push and Tag events types.
	Paths []string

	// Only set if event is from a github app
	GithubAppInstallID *int64
}

func (EventPayload) Validate

func (e EventPayload) Validate() error

type EventType

type EventType string
const (
	EventTypePull         EventType = "pull"
	EventTypePush         EventType = "push"
	EventTypeTag          EventType = "tag"
	EventTypeInstallation EventType = "install" // github-app installation
)

type GetRepoTarballOptions

type GetRepoTarballOptions struct {
	Repo Repo    // repo identifier, <owner>/<repo>
	Ref  *string // branch/tag/SHA ref, nil means default branch
}

type GetWebhookOptions

type GetWebhookOptions struct {
	Repo Repo   // Repository identifier, <owner>/<repo>
	ID   string // vcs' webhook ID
}

GetWebhookOptions are options for retrieving a webhook.

type Installation added in v0.3.24

type Installation struct {
	ID           int64
	AppID        int64
	Username     *string
	Organization *string
	HTMLURL      string
}

func NewInstallation added in v0.3.24

func NewInstallation(ghInstall *github.Installation) (Installation, error)

func (Installation) String added in v0.3.24

func (i Installation) String() string

type Kind

type Kind struct {
	// ID distinguishes this kind from other kinds. NOTE: This must have first
	// been inserted into the vcs_kinds table via a database migration.
	ID KindID
	// DefaultURL is the default base URL for the provider.
	DefaultURL *internal.WebURL
	// Icon renders an icon identifying the VCS host kind.
	Icon templ.Component
	// TokenKind provides info about the token the provider expects. Mutually
	// exclusive with AppKind.
	TokenKind *TokenKind
	// AppKind provides info about installations for this ProviderKind.
	// Mutually exclusive with TokenKind.
	AppKind AppKind
	// NewClient constructs a client implementation.
	NewClient func(context.Context, ClientConfig) (Client, error)
	// EventHandler handles incoming events from the VCS host before relaying
	// them onwards for triggering actions like creating runs etc.
	EventHandler func(r *http.Request, secret string) (*EventPayload, error)
	// SkipRepohook if true skips the creation of a repository-level webhook.
	SkipRepohook bool
	// TFEServiceProviders registers the kind with the TFE API, permitting vcs
	// providers of this kind to be created via the TFE API. The value specified
	// here is the value that should be be provided by API clients via the
	// "service-provider" attribute:
	//
	// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/oauth-clients#request-body
	//
	// Must provide at least one type. A kind can register more than one type,
	// e.g. gitlab-ce and gitlab-ee, in which case if a client specifies either
	// of these types then OTF creates a provider of the gitlab kind. Note that
	// OTF makes no distinction between multiple types but it's merely there to
	// support the TFE API and the various values that clients might provide
	// according to the API documentation (though OTF may in future make a
	// distinction!).
	TFEServiceProviders []TFEServiceProviderType
	// Source sets the source identifier for this kind, to inform users which
	// kind is the source of a run configuration. By default the ID is used as
	// the source identifier but Source takes precedence if it is non-nil.
	Source *source.Source
}

Kind is a kind of vcs provider. Each kind represents a particular VCS hosting provider (e.g. github), and a way of interacting with the provider, including authentication, event handling. Typically there is one kind per VCS hosting provider, but providers sometimes offer more than one of interacting with it, e.g. GitHub uses both personal access tokens and a GitHub 'app' which is 'installed' via a private key.

func (Kind) GetSource added in v0.3.24

func (k Kind) GetSource() source.Source

type KindID added in v0.3.24

type KindID string

KindID of vcs hosting provider

type ListOptions added in v0.3.24

type ListOptions struct {
	resource.PageOptions
	Organization organization.Name `schema:"organization_name"`
}

type ListRepositoriesOptions

type ListRepositoriesOptions struct {
	PageSize int
}

type ListTagsOptions

type ListTagsOptions struct {
	Repo   Repo   // repo identifier, <owner>/<repo>
	Prefix string // only list tags that start with this string
}

ListTagsOptions are options for listing tags on a vcs repository

type NewTokenClientOptions

type NewTokenClientOptions struct {
	Token               string
	BaseURL             *internal.WebURL
	SkipTLSVerification bool
}

NewTokenClientOptions are options for creating a client using a personal access token (PAT).

type Options added in v0.3.24

type Options struct {
	*internal.HostnameService
	*sql.DB
	*tfeapi.Responder
	logr.Logger

	SourceIconRegistrar SourceIconRegistrar
	SkipTLSVerification bool
	Authorizer          *authz.Authorizer
}

type Provider added in v0.3.24

type Provider struct {
	ID           resource.TfeID
	Name         string
	CreatedAt    time.Time
	Organization organization.Name
	Token        *string
	Installation *Installation
	// The kind of provider
	Kind
	// Client is the actual client for interacting with the VCS host.
	Client
	// The base URL of the the provider.
	BaseURL *internal.WebURL
	// contains filtered or unexported fields
}

Provider is an identifiable client for interacting with a VCS host.

func (*Provider) LogValue added in v0.3.24

func (t *Provider) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (*Provider) String added in v0.3.24

func (t *Provider) String() string

String provides a human meaningful description of the vcs provider.

func (*Provider) Update added in v0.3.24

func (t *Provider) Update(opts UpdateOptions) error

type Publisher

type Publisher interface {
	Publish(Event)
}

type Repo added in v0.3.24

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

Repo is a hosted VCS repository.

func NewMustRepo added in v0.3.25

func NewMustRepo(owner, name string) Repo

func NewRandomModuleRepo added in v0.3.25

func NewRandomModuleRepo(provider, name string) Repo

func NewRandomRepo added in v0.3.25

func NewRandomRepo() Repo

func NewRepo added in v0.3.25

func NewRepo(owner, name string) (Repo, error)

func NewRepoFromString added in v0.3.25

func NewRepoFromString(s string) (Repo, error)

func (Repo) MarshalText added in v0.3.25

func (r Repo) MarshalText() ([]byte, error)

func (Repo) Name added in v0.3.24

func (r Repo) Name() string

Name is the actual name of the repo, not including the owner or namespace.

func (Repo) Owner added in v0.3.24

func (r Repo) Owner() string

Owner is the owner of the repo. On some kinds, e.g. Gitlab, the owner is the 'namespace', which may include forward slashes to indicate subgroups.

func (*Repo) Scan added in v0.3.25

func (r *Repo) Scan(text any) error

func (Repo) String added in v0.3.24

func (r Repo) String() string

func (*Repo) UnmarshalText added in v0.3.25

func (r *Repo) UnmarshalText(text []byte) error

func (*Repo) Value added in v0.3.25

func (r *Repo) Value() (driver.Value, error)

type Service added in v0.3.24

type Service struct {
	logr.Logger
	*authz.Authorizer

	*internal.HostnameService
	// contains filtered or unexported fields
}

func NewService added in v0.3.24

func NewService(opts Options) *Service

func (*Service) AddHandlers added in v0.3.24

func (a *Service) AddHandlers(r *mux.Router)

func (*Service) BeforeDeleteVCSProvider added in v0.3.24

func (a *Service) BeforeDeleteVCSProvider(hook func(context.Context, *Provider) error)

func (*Service) Create added in v0.3.24

func (a *Service) Create(ctx context.Context, opts CreateOptions) (*Provider, error)

func (*Service) Delete added in v0.3.24

func (a *Service) Delete(ctx context.Context, id resource.TfeID) (*Provider, error)

func (*Service) Get added in v0.3.24

func (a *Service) Get(ctx context.Context, id resource.TfeID) (*Provider, error)

func (Service) GetKind added in v0.3.24

func (db Service) GetKind(id KindID) (Kind, error)

func (Service) GetKindByTFEServiceProviderType added in v0.3.24

func (db Service) GetKindByTFEServiceProviderType(sp TFEServiceProviderType) (Kind, error)

GetKindByTFEServiceProviderType retrieves a kind by its TFE service provider type.

func (Service) GetKinds added in v0.3.24

func (db Service) GetKinds() []Kind

func (*Service) List added in v0.3.24

func (a *Service) List(ctx context.Context, organization organization.Name) ([]*Provider, error)

func (*Service) ListByInstall added in v0.3.24

func (a *Service) ListByInstall(ctx context.Context, installID int64) ([]*Provider, error)

func (Service) RegisterKind added in v0.3.24

func (db Service) RegisterKind(kind Kind)

func (*Service) Update added in v0.3.24

func (a *Service) Update(ctx context.Context, id resource.TfeID, opts UpdateOptions) (*Provider, error)

type SetStatusOptions

type SetStatusOptions struct {
	Workspace   string // workspace name
	Repo        Repo   // <owner>/<repo>
	Ref         string // git ref
	Status      Status
	TargetURL   string
	Description string
}

SetStatusOptions are options for setting a status on a VCS repo

type SourceIconRegistrar added in v0.3.25

type SourceIconRegistrar interface {
	RegisterSourceIcon(source source.Source, icon templ.Component)
}

type Status

type Status string
const (
	PendingStatus Status = "pending"
	SuccessStatus Status = "success"
	ErrorStatus   Status = "error"
	FailureStatus Status = "failure"
)

type Subscriber

type Subscriber interface {
	Subscribe(cb Callback)
}

type TFEOAuthClient added in v0.3.24

type TFEOAuthClient struct {
	ID                  resource.TfeID         `jsonapi:"primary,oauth-clients"`
	APIURL              string                 `jsonapi:"attribute" json:"api-url"`
	CallbackURL         string                 `jsonapi:"attribute" json:"callback-url"`
	ConnectPath         string                 `jsonapi:"attribute" json:"connect-path"`
	CreatedAt           time.Time              `jsonapi:"attribute" json:"created-at"`
	HTTPURL             string                 `jsonapi:"attribute" json:"http-url"`
	Key                 string                 `jsonapi:"attribute" json:"key"`
	RSAPublicKey        string                 `jsonapi:"attribute" json:"rsa-public-key"`
	Name                *string                `jsonapi:"attribute" json:"name"`
	Secret              string                 `jsonapi:"attribute" json:"secret"`
	ServiceProvider     TFEServiceProviderType `jsonapi:"attribute" json:"service-provider"`
	ServiceProviderName string                 `jsonapi:"attribute" json:"service-provider-display-name"`

	// Relations
	Organization *organization.TFEOrganization `jsonapi:"relationship" json:"organization"`
	OAuthTokens  []*TFEOAuthToken              `jsonapi:"relationship" json:"oauth-tokens"`
}

TFEOAuthClient represents a connection between an organization and a VCS provider.

type TFEOAuthClientCreateOptions added in v0.3.24

type TFEOAuthClientCreateOptions struct {
	// Type is a public field utilized by JSON:API to
	// set the resource type via the field tag.
	// It is not a user-defined value and does not need to be set.
	// https://jsonapi.org/format/#crud-creating
	Type string `jsonapi:"primary,oauth-clients"`

	// A display name for the OAuth Client.
	Name *string `jsonapi:"attribute" json:"name"`

	// Required: The base URL of your VCS provider's API.
	APIURL *internal.WebURL `jsonapi:"attribute" json:"api-url"`

	// Required: The homepage of your VCS provider.
	HTTPURL *internal.WebURL `jsonapi:"attribute" json:"http-url"`

	// Optional: The OAuth Client key.
	Key *string `jsonapi:"attribute" json:"key,omitempty"`

	// Optional: The token string you were given by your VCS provider.
	OAuthToken *string `jsonapi:"attribute" json:"oauth-token-string,omitempty"`

	// Optional: Private key associated with this vcs provider - only available for ado_server
	PrivateKey *string `jsonapi:"attribute" json:"private-key,omitempty"`

	// Optional: Secret key associated with this vcs provider - only available for ado_server
	Secret *string `jsonapi:"attribute" json:"secret,omitempty"`

	// Optional: RSAPublicKey the text of the SSH public key associated with your BitBucket
	// Server Application Link.
	RSAPublicKey *string `jsonapi:"attribute" json:"rsa-public-key,omitempty"`

	// Required: The VCS provider being connected with.
	ServiceProvider *TFEServiceProviderType `jsonapi:"attribute" json:"service-provider"`
}

TFEOAuthClientCreateOptions represents the options for creating an OAuth client.

type TFEOAuthToken added in v0.3.24

type TFEOAuthToken struct {
	ID                  resource.TfeID `jsonapi:"primary,oauth-tokens"`
	UID                 resource.TfeID `jsonapi:"attribute" json:"uid"`
	CreatedAt           time.Time      `jsonapi:"attribute" json:"created-at"`
	HasSSHKey           bool           `jsonapi:"attribute" json:"has-ssh-key"`
	ServiceProviderUser string         `jsonapi:"attribute" json:"service-provider-user"`

	// Relations
	OAuthClient *TFEOAuthClient `jsonapi:"relationship" json:"oauth-client"`
}

TFEOAuthToken represents a VCS configuration including the associated OAuth token

type TFEServiceProviderType added in v0.3.24

type TFEServiceProviderType string

TFEServiceProviderType represents a VCS type.

const (
	ServiceProviderAzureDevOpsServer   TFEServiceProviderType = "ado_server"
	ServiceProviderAzureDevOpsServices TFEServiceProviderType = "ado_services"
	ServiceProviderBitbucket           TFEServiceProviderType = "bitbucket_hosted"
	// Bitbucket Server v5.4.0 and above
	ServiceProviderBitbucketServer TFEServiceProviderType = "bitbucket_server"
	// Bitbucket Server v5.3.0 and below
	ServiceProviderBitbucketServerLegacy TFEServiceProviderType = "bitbucket_server_legacy"
	ServiceProviderForgejo               TFEServiceProviderType = "forgejo"
	ServiceProviderGithub                TFEServiceProviderType = "github"
	ServiceProviderGithubEE              TFEServiceProviderType = "github_enterprise"
	ServiceProviderGithubApp             TFEServiceProviderType = "github_app"
	ServiceProviderGitlab                TFEServiceProviderType = "gitlab_hosted"
	ServiceProviderGitlabCE              TFEServiceProviderType = "gitlab_community_edition"
	ServiceProviderGitlabEE              TFEServiceProviderType = "gitlab_enterprise_edition"
)

List of available VCS types.

type Table added in v0.3.24

type Table struct {
	Actions func(vcsProviderID resource.TfeID) templ.Component
}

func (Table) Header added in v0.3.24

func (t Table) Header() templ.Component

func (Table) Row added in v0.3.24

func (t Table) Row(provider *Provider) templ.Component

type TokenKind added in v0.3.24

type TokenKind struct {
	// TokenDescription renders a helpful description of what is expected of the
	// token, e.g. what permissions it should possess.
	Description templ.Component
}

type UpdateOptions added in v0.3.24

type UpdateOptions struct {
	Name    string
	Token   *string
	BaseURL *internal.WebURL
}

type UpdateWebhookOptions

type UpdateWebhookOptions CreateWebhookOptions

type Webhook

type Webhook struct {
	ID       string // cloud's webhook ID
	Repo     Repo   // identifier is <repo_owner>/<repo_name>
	Events   []EventType
	Endpoint string // the OTF URL that receives events
}

Webhook is a cloud's configuration for a webhook.

Jump to

Keyboard shortcuts

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