shamhub

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2024 License: GPL-3.0 Imports: 31 Imported by: 0

Documentation

Overview

Package shamhub implements a fake GitHub-like Forge.

It stores Git repositories in a temporary directory, and provides a REST-like API for interacting with them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthenticationToken

type AuthenticationToken struct {
	forge.AuthenticationToken
	// contains filtered or unexported fields
}

AuthenticationToken defines the token returned by the ShamHub forge.

type Change

type Change struct {
	Number int    `json:"number"`
	URL    string `json:"html_url"`

	Draft  bool   `json:"draft,omitempty"`
	State  string `json:"state"`
	Merged bool   `json:"merged,omitempty"`

	Subject string `json:"title"`
	Body    string `json:"body"`

	Base *ChangeBranch `json:"base"`
	Head *ChangeBranch `json:"head"`
}

Change is a change proposal against a repository.

type ChangeBranch

type ChangeBranch struct {
	Name string `json:"ref"`
	Hash string `json:"sha"`
}

ChangeBranch is a branch in a change proposal.

type ChangeComment

type ChangeComment struct {
	ID     int
	Change int
	Body   string
}

ChangeComment is a comment made on ShamHub.

type ChangeCommentID

type ChangeCommentID int

ChangeCommentID uniquely identifies a comment on a change in ShamHub.

func (ChangeCommentID) String

func (id ChangeCommentID) String() string

type ChangeID

type ChangeID int

ChangeID is a unique identifier for a change on a ShamHub server.

func (ChangeID) String

func (id ChangeID) String() string

type ChangeMetadata

type ChangeMetadata struct {
	Number int `json:"number"`

	NavigationComment int `json:"nav_comment"`
}

ChangeMetadata records the metadata for a change on a ShamHub server.

func (*ChangeMetadata) ChangeID

func (m *ChangeMetadata) ChangeID() forge.ChangeID

ChangeID reports the change ID of the change.

func (*ChangeMetadata) ForgeID

func (*ChangeMetadata) ForgeID() string

ForgeID reports the forge ID that owns this metadata.

func (*ChangeMetadata) NavigationCommentID added in v0.5.0

func (m *ChangeMetadata) NavigationCommentID() forge.ChangeCommentID

NavigationCommentID reports the comment ID of the navigation comment.

func (*ChangeMetadata) SetNavigationCommentID added in v0.5.0

func (m *ChangeMetadata) SetNavigationCommentID(id forge.ChangeCommentID)

SetNavigationCommentID sets the comment ID of the navigation comment. id may be nil.

type Cmd

type Cmd struct{}

Cmd implements the 'shamhub' command for test scripts.

To install it, run Cmd.Setup in your testscript.Params.Setup, and add Cmd.Run as a command in the testscript.Params.Cmds.

func (*Cmd) Run

func (c *Cmd) Run(ts *testscript.TestScript, neg bool, args []string)

Run implements the 'shamhub' command for test scripts. The script MUST have called Cmd.Setup first.

func (*Cmd) Setup

func (c *Cmd) Setup(t testing.TB, e *testscript.Env)

Setup installs per-script state for the 'shamhub' command into the script's environment.

type Config

type Config struct {
	// Git is the path to the git binary.
	// If not set, we'll look for it in the PATH.
	Git string

	Log *log.Logger
}

Config configures a ShamHub server.

type Forge

type Forge struct {
	Options

	// Log is the logger to use for logging.
	Log *log.Logger
}

Forge provides an implementation of forge.Forge backed by a ShamHub server.

func (*Forge) AuthenticationFlow

func (f *Forge) AuthenticationFlow(ctx context.Context) (forge.AuthenticationToken, error)

AuthenticationFlow initiates the authentication flow for the ShamHub forge. The flow is optimized for ease of use from test scripts and is not representative of a real-world authentication flow.

To authenticate, the user must set the SHAMHUB_USERNAME environment variable before attempting to authenticate. The flow will fail if these variables are not set. The flow will also fail if the user is already authenticated.

func (*Forge) CLIPlugin

func (f *Forge) CLIPlugin() any

CLIPlugin registers additional CLI flags for the ShamHub forge.

func (*Forge) ChangeTemplatePaths

func (f *Forge) ChangeTemplatePaths() []string

ChangeTemplatePaths reports the case-insensitive paths at which it's possible to define change templates in the repository.

func (*Forge) ClearAuthenticationToken

func (f *Forge) ClearAuthenticationToken(stash secret.Stash) error

ClearAuthenticationToken removes the authentication token from the stash.

func (*Forge) ID

func (*Forge) ID() string

ID reports a unique identifier for this forge.

func (*Forge) LoadAuthenticationToken

func (f *Forge) LoadAuthenticationToken(stash secret.Stash) (forge.AuthenticationToken, error)

LoadAuthenticationToken loads the authentication token from the stash.

func (*Forge) MarshalChangeMetadata

func (f *Forge) MarshalChangeMetadata(md forge.ChangeMetadata) (json.RawMessage, error)

MarshalChangeMetadata marshals the given change metadata to JSON.

func (*Forge) MatchURL

func (f *Forge) MatchURL(remoteURL string) bool

MatchURL reports whether the given URL is a ShamHub URL.

func (*Forge) OpenURL

func (f *Forge) OpenURL(ctx context.Context, token forge.AuthenticationToken, remoteURL string) (forge.Repository, error)

OpenURL opens a repository hosted on the forge with the given remote URL.

func (*Forge) SaveAuthenticationToken

func (f *Forge) SaveAuthenticationToken(stash secret.Stash, t forge.AuthenticationToken) error

SaveAuthenticationToken saves the given authentication token to the stash.

func (*Forge) UnmarshalChangeMetadata

func (f *Forge) UnmarshalChangeMetadata(data json.RawMessage) (forge.ChangeMetadata, error)

UnmarshalChangeMetadata unmarshals the given JSON data to change metadata.

type MergeChangeRequest

type MergeChangeRequest struct {
	Owner, Repo string
	Number      int

	// Optional fields:
	Time                          time.Time
	CommitterName, CommitterEmail string

	// DeleteBranch indicates that the merged branch
	// should be deleted after the merge.
	DeleteBranch bool
}

MergeChangeRequest is a request to merge an open change proposed against this forge.

type Options

type Options struct {
	// URL is the base URL for Git repositories
	// hosted on the ShamHub server.
	// URLs under this must implement the Git HTTP protocol.
	URL string `name:"shamhub-url" hidden:"" env:"SHAMHUB_URL" help:"Base URL for ShamHub requests"`

	// APIURL is the base URL for the ShamHub API.
	APIURL string `name:"shamhub-api-url" hidden:"" env:"SHAMHUB_API_URL" help:"Base URL for ShamHub API requests"`
}

Options defines CLI options for the ShamHub forge.

type RejectChangeRequest added in v0.6.0

type RejectChangeRequest struct {
	Owner, Repo string
	Number      int
}

RejectChangeRequest is a request to reject a change.

type ShamHub

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

ShamHub is a fake GitHub-like Forge. The ShamHub type provides control of the forge, with direct access to Git repositories and change proposals.

It provides two HTTP endpoints: one for the API server implementing the Forge API, and one for the Git server implementing the Git HTTP protocol. Note that the HTTP API provided by ShamHub is not the same as the GitHub API.

func New

func New(cfg Config) (*ShamHub, error)

New creates a new ShamHub server and returns an ShamHub to control it. The server should be closed with the Close method when done.

func (*ShamHub) APIURL

func (sh *ShamHub) APIURL() string

APIURL returns the URL to which API requests should be sent. Configure the shamhub.Forge to use this as the API URL.

func (*ShamHub) Close

func (sh *ShamHub) Close() error

Close closes the ShamHub server.

func (*ShamHub) GitRoot

func (sh *ShamHub) GitRoot() string

GitRoot returns the path to the root directory of the Git repositories.

func (*ShamHub) GitURL

func (sh *ShamHub) GitURL() string

GitURL returns the URL for the Git HTTP server. Append <owner>/<repo>.git to these to get the Git remote. Configure the shamhub.Forge to use this as the Base URL.

func (*ShamHub) ListChangeComments

func (sh *ShamHub) ListChangeComments() ([]*ChangeComment, error)

ListChangeComments returns all comments on all changes in ShamHub.

func (*ShamHub) ListChanges

func (sh *ShamHub) ListChanges() ([]*Change, error)

ListChanges reports all changes known to the forge.

func (*ShamHub) MergeChange

func (sh *ShamHub) MergeChange(req MergeChangeRequest) error

MergeChange merges an open change against this forge.

func (*ShamHub) NewRepository

func (sh *ShamHub) NewRepository(owner, repo string) (string, error)

NewRepository creates a new Git repository with the given owner and repo name and returns the URL to the repository.

func (*ShamHub) RegisterUser

func (sh *ShamHub) RegisterUser(username string) error

RegisterUser registers a new user against the Forge with the given username and password.

func (*ShamHub) RejectChange added in v0.6.0

func (sh *ShamHub) RejectChange(req RejectChangeRequest) error

RejectChange closes a CR without merging it.

func (*ShamHub) RepoURL

func (sh *ShamHub) RepoURL(owner, repo string) string

RepoURL returns the URL for the Git repository with the given owner and repo name.

Jump to

Keyboard shortcuts

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