forge

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package forge provides an abstraction layer between git-spice and the underlying forge (e.g. GitHub, GitLab, Bitbucket).

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedURL = errors.New("unsupported URL")

ErrUnsupportedURL indicates that the given remote URL does not match any registered forge.

Functions

This section is empty.

Types

type AuthenticationToken

type AuthenticationToken interface {
	// contains filtered or unexported methods
}

AuthenticationToken is a secret that results from a successful login. It will be persisted in a safe place, and re-used for future authentication with the forge.

Implementations must embed this interface.

type ChangeCommentID

type ChangeCommentID interface {
	String() string
}

ChangeCommentID is a unique identifier for a comment on a change.

type ChangeID

type ChangeID interface {
	String() string
}

ChangeID is a unique identifier for a change in a repository.

type ChangeMetadata

type ChangeMetadata interface {
	ForgeID() string

	// ChangeID is a human-readable identifier for the change.
	// This is presented to the user in the UI.
	ChangeID() ChangeID

	// NavigationCommentID is a comment left on the Change
	// that contains a visualization of the stack.
	NavigationCommentID() ChangeCommentID

	// SetNavigationCommentID sets the ID of the navigation comment
	// on the chnage metadata to persist it later.
	//
	// The ID may be nil to indicate that there is no navigation comment.
	SetNavigationCommentID(ChangeCommentID)
}

ChangeMetadata defines Forge-specific per-change metadata. This metadata is persisted to the state store alongside the branch state. It is used to track the relationship between a branch and its corresponding change in the forge.

The implementation is per-forge, and should contain enough information for the forge to uniquely identify a change within a repository.

The metadata must be JSON-serializable (as defined by methods on Forge).

type ChangeState

type ChangeState int

ChangeState is the current state of a change.

const (
	// ChangeOpen specifies that a change is open.
	ChangeOpen ChangeState = iota + 1

	// ChangeMerged specifies that a change has been merged.
	ChangeMerged

	// ChangeClosed specifies that a change has been closed.
	ChangeClosed
)

func (ChangeState) MarshalText

func (s ChangeState) MarshalText() ([]byte, error)

MarshalText serialize the change state to text. This implements encoding.TextMarshaler.

func (ChangeState) String

func (s ChangeState) String() string

func (*ChangeState) UnmarshalText

func (s *ChangeState) UnmarshalText(b []byte) error

UnmarshalText parses the change state from text. This implements encoding.TextUnmarshaler.

type ChangeTemplate

type ChangeTemplate struct {
	// Filename is the name of the template file.
	//
	// This is NOT a path.
	Filename string

	// Body is the content of the template file.
	Body string
}

ChangeTemplate is a template for a new change proposal.

type EditChangeOptions

type EditChangeOptions struct {
	// Base specifies the name of the base branch.
	//
	// If unset, the base branch is not changed.
	Base string

	// Draft specifies whether the change should be marked as a draft.
	// If unset, the draft status is not changed.
	Draft *bool
}

EditChangeOptions specifies options for an operation to edit an existing change.

type FindChangeItem

type FindChangeItem struct {
	// ID is a unique identifier for the change.
	ID ChangeID // required

	// URL is the web URL at which the change can be viewed.
	URL string // required

	// State is the current state of the change.
	State ChangeState // required

	// Subject is the title of the change.
	Subject string // required

	// HeadHash is the hash of the commit at the top of the change.
	HeadHash git.Hash // required

	// BaseName is the name of the base branch
	// that this change is proposed against.
	BaseName string // required

	// Draft is true if the change is not yet ready to be reviewed.
	Draft bool // required
}

FindChangeItem is a single result from searching for changes in the repository.

type FindChangesOptions

type FindChangesOptions struct {
	State ChangeState // 0 = all

	// Limit specifies the maximum number of changes to return.
	// Changes are sorted by most recently updated.
	// Defaults to 10.
	Limit int
}

FindChangesOptions specifies filtering options for searching for changes.

type Forge

type Forge interface {
	// ID reports a unique identifier for the forge, e.g. "github".
	ID() string

	// CLIPlugin returns a Kong plugin for this Forge.
	// Return nil if the forge does not require any extra CLI flags.
	CLIPlugin() any

	// MatchURL reports whether the given remote URL is hosted on the forge.
	MatchURL(remoteURL string) bool

	// OpenURL opens a repository hosted on the Forge
	// with the given remote URL.
	//
	// This will only be called if MatchURL reports true.
	OpenURL(ctx context.Context, tok AuthenticationToken, remoteURL string) (Repository, error)

	// ChangeTemplatePaths reports the case-insensitive paths at which
	// it's possible to define change templates in the repository.
	ChangeTemplatePaths() []string

	// MarshalChangeID serializes the given change ID into a valid JSON blob.
	MarshalChangeID(ChangeID) (json.RawMessage, error)

	// UnmarshalChangeID deserializes the given JSON blob into a change ID.
	UnmarshalChangeID(json.RawMessage) (ChangeID, error)

	// MarshalChangeMetadata serializes the given change metadata
	// into a valid JSON blob.
	MarshalChangeMetadata(ChangeMetadata) (json.RawMessage, error)

	// UnmarshalChangeMetadata deserializes the given JSON blob
	// into change metadata.
	UnmarshalChangeMetadata(json.RawMessage) (ChangeMetadata, error)

	// AuthenticationFlow runs the authentication flow for the forge.
	// This may prompt the user, perform network requests, etc.
	//
	// The implementation should return a secret that the Forge
	// can serialize and store for future use.
	AuthenticationFlow(ctx context.Context, view ui.View) (AuthenticationToken, error)

	// SaveAuthenticationToken saves the given authentication token
	// to the secret stash.
	SaveAuthenticationToken(secret.Stash, AuthenticationToken) error

	// LoadAuthenticationToken loads the authentication token
	// from the secret stash.
	LoadAuthenticationToken(secret.Stash) (AuthenticationToken, error)

	// ClearAuthenticationToken removes the authentication token
	// from the secret stash.
	ClearAuthenticationToken(secret.Stash) error
}

Forge is a forge that hosts Git repositories.

func MatchForgeURL

func MatchForgeURL(r *Registry, remoteURL string) (forge Forge, ok bool)

MatchForgeURL attempts to match the given remote URL with a registered forge. Returns the matched forge and true if a match was found.

type ListChangeCommentItem added in v0.5.0

type ListChangeCommentItem struct {
	ID   ChangeCommentID
	Body string
}

ListChangeCommentItem is a single result from listing comments on a change.

type ListChangeCommentsOptions added in v0.5.0

type ListChangeCommentsOptions struct {
	// BodyMatchesAll specifies zero or more regular expressions
	// that must all match the comment body.
	//
	// If empty, all comments are returned.
	BodyMatchesAll []*regexp.Regexp

	// CanUpdate specifies whether only comments that can be updated
	// by the current user should be returned.
	//
	// If false, all comments are returned.
	CanUpdate bool
}

ListChangeCommentsOptions specifies options for filtering and limiting comments listed by ListChangeComments.

Conditions specified here are combined with AND.

type Registry added in v0.12.0

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

Registry is a collection of known code forges.

func (*Registry) All added in v0.12.0

func (r *Registry) All() iter.Seq[Forge]

All returns an iterator over items in the Forge in an unspecified order.

func (*Registry) Lookup added in v0.12.0

func (r *Registry) Lookup(id string) (Forge, bool)

Lookup searches for a registered Forge by ID. It returns false if a forge with that ID is not known.

func (*Registry) Register added in v0.12.0

func (r *Registry) Register(f Forge) (unregister func())

Register registers a Forge with the Registry. The Forge may be unregistered by calling the returned function.

type Repository

type Repository interface {
	Forge() Forge

	SubmitChange(ctx context.Context, req SubmitChangeRequest) (SubmitChangeResult, error)
	EditChange(ctx context.Context, id ChangeID, opts EditChangeOptions) error
	FindChangesByBranch(ctx context.Context, branch string, opts FindChangesOptions) ([]*FindChangeItem, error)
	FindChangeByID(ctx context.Context, id ChangeID) (*FindChangeItem, error)
	ChangesAreMerged(ctx context.Context, ids []ChangeID) ([]bool, error)

	// Post and update comments on changes.
	PostChangeComment(context.Context, ChangeID, string) (ChangeCommentID, error)
	UpdateChangeComment(context.Context, ChangeCommentID, string) error

	// List comments on a CR, optionally filtered per the given options.
	ListChangeComments(context.Context, ChangeID, *ListChangeCommentsOptions) iter.Seq2[*ListChangeCommentItem, error]

	// NewChangeMetadata builds a ChangeMetadata for the given change ID.
	//
	// This may perform network requests to fetch additional information
	// if necessary.
	NewChangeMetadata(ctx context.Context, id ChangeID) (ChangeMetadata, error)

	// ListChangeTemplates returns templates defined in the repository
	// for new change proposals.
	//
	// Returns an empty list if no templates are found.
	ListChangeTemplates(context.Context) ([]*ChangeTemplate, error)
}

Repository is a Git repository hosted on a forge.

type SubmitChangeRequest

type SubmitChangeRequest struct {
	// Subject is the title of the change.
	Subject string // required

	// Body is the description of the change.
	Body string

	// Base is the name of the base branch
	// that this change is proposed against.
	Base string // required

	// Head is the name of the branch containing the change.
	//
	// This must have already been pushed to the remote.
	Head string // required

	// Draft specifies whether the change should be marked as a draft.
	Draft bool
}

SubmitChangeRequest is a request to submit a new change in a repository. The change must have already been pushed to the remote.

type SubmitChangeResult

type SubmitChangeResult struct {
	ID  ChangeID
	URL string
}

SubmitChangeResult is the result of creating a new change in a repository.

Directories

Path Synopsis
Package forgetest is a generated GoMock package.
Package forgetest is a generated GoMock package.
Package github provides a wrapper around GitHub's APIs in a manner compliant with the forge.Forge interface.
Package github provides a wrapper around GitHub's APIs in a manner compliant with the forge.Forge interface.
Package gitlab provides a wrapper around GitLab's APIs in a manner compliant with the forge.Forge interface.
Package gitlab provides a wrapper around GitLab's APIs in a manner compliant with the forge.Forge interface.
Package shamhub implements a fake GitHub-like Forge.
Package shamhub implements a fake GitHub-like Forge.
Package stacknav provides support for creating stack navigation comments and descriptions.
Package stacknav provides support for creating stack navigation comments and descriptions.

Jump to

Keyboard shortcuts

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