github

package
v0.7.31 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 28 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package github is the mounts driver for GitHub repositories, read and written through GitHub's REST API. A mount's Source is "github://owner/repo".

A snapshot mount pins a commit. When the mount is made, the driver downloads the commit's tarball into a blob store and writes a manifest, so the mount serves the same bytes for its whole life, and after a restart, without asking GitHub again. A live mount reads a branch through the Contents API, and each Put or Delete is one commit on it.

The driver sends each request with the bearer token of the mounts.BearerCredentials the registry hands it, which come from the mount's connection. Other kinds of credentials return ErrInvalidArgument. A private repo answers 404 to a request without a valid token, so the driver reports ErrNotFound for it, as GitHub intends.

Index

Constants

View Source
const DefaultBaseURL = "https://api.github.com"

DefaultBaseURL is GitHub's API root. GitHub Enterprise and tests set another with WithBaseURL.

View Source
const DefaultRawURL = "https://raw.githubusercontent.com"

DefaultRawURL is the host that serves a repo's files by commit and path. Its downloads don't count against the REST API's hourly limit, which is 60 requests for a caller with no token.

View Source
const MaxWebhookBytes = 25 << 20

MaxWebhookBytes caps the body ParsePush reads. GitHub caps a webhook payload at 25 MB.

Variables

View Source
var (
	// ErrBadSignature means a webhook delivery's X-Hub-Signature-256 is missing, malformed, or
	// isn't the HMAC-SHA256 of the body under the secret. A handler answers 401.
	ErrBadSignature = errors.New("github: bad webhook signature")

	// ErrPing means the delivery is the ping GitHub sends when a webhook is made. Its signature
	// checked out. A handler answers 200.
	ErrPing = errors.New("github: webhook ping")

	// ErrNotPush means the delivery is a signed event other than a push or a ping. A handler
	// answers 200 or 204 and ignores it.
	ErrNotPush = errors.New("github: webhook event isn't a push")
)

Functions

This section is empty.

Types

type Driver

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

Driver serves mounts.KindGitHub mounts. It implements mounts.Preparer, mounts.Writer, mounts.Localizer and mounts.VersionReader. Share one Driver per process, since it caches manifests and builds each snapshot's folder view once.

func New

func New(store blobs.Store, opts ...Option) *Driver

New returns a GitHub driver keeping snapshots in store. A nil store serves live mounts only, and Prepare refuses a snapshot with ErrInvalidArgument.

func (*Driver) CanRead

func (d *Driver) CanRead(ctx context.Context, baseURL, source string, creds mounts.Credentials) (bool, error)

CanRead reports whether creds can read the repo at source. It fits connections.ReadCheck, so an app passes it to connections.WithReadCheck.

baseURL is the connection's API root, empty for github.com. A connection to another host than this driver serves gets false with no request, since its token means nothing here. A 404 is a definite no, and it is also how GitHub answers a private repo the token can't see. Any other failure, a rejected token included, is an error, since it says nothing about access.

func (*Driver) Delete

Delete removes req.Path as one commit on the mount's branch. IfMatch must be the file's current version or AnyVersion; a stale one returns ErrConflict, and an empty one ErrInvalidArgument.

func (*Driver) DeleteWebhook added in v0.7.18

func (d *Driver) DeleteWebhook(ctx context.Context, source, url string, creds mounts.Credentials) error

DeleteWebhook removes the repo's hooks that send to url. It lists the hooks first, so a 404 means the token can't manage them, which returns mounts.ErrPermissionDenied, and a hook that is already gone isn't an error.

func (*Driver) EnsureWebhook added in v0.7.18

func (d *Driver) EnsureWebhook(ctx context.Context, source string, req mounts.WebhookRequest, creds mounts.Credentials) (mounts.Webhook, error)

EnsureWebhook makes a hook on the repo that sends req.Events to req.URL as JSON, signed with req.Secret, or updates the hook already sending to req.URL. It always sends the update, since GitHub never shows a hook's secret, so the driver can't tell whether it changed. The JSON content type lets ParsePush read each delivery as sent.

GitHub lets a token manage a repo's hooks only with admin rights on the repo, or a fine-grained token's webhooks permission. It answers 404 to a token that can read the repo but can't manage its hooks, so 404 and 403 return mounts.ErrPermissionDenied. 422, such as for a URL GitHub won't send to, returns mounts.ErrInvalidArgument with GitHub's message.

func (*Driver) FetchAt added in v0.7.14

func (d *Driver) FetchAt(ctx context.Context, m mounts.Mount, creds mounts.Credentials, commit string, f mounts.File) (blobs.Hash, bool, error)

FetchAt keeps f's contents in the blob store. When the store is a blobs.Aliases that maps f.ID to a blob it holds, nothing is downloaded. A download is checked against f.ID before it's kept. A Git LFS pointer is swapped for its object, and f.ID then maps to the object's blob.

func (*Driver) HostPath

func (d *Driver) HostPath(m mounts.Mount, p string) (string, error)

HostPath returns where p of a snapshot mount is on disk, in the folder WithViews set. The first call for a mount builds its view there, as hard links to the blob store (copies where the filesystem refuses links), so a restart that lost the folder rebuilds it from the manifest without asking GitHub. A live mount, or a driver without WithViews or a linking blob store, returns ErrNotLocal.

func (*Driver) Kind

func (d *Driver) Kind() mounts.Kind

Kind returns mounts.KindGitHub.

func (*Driver) ListAt added in v0.7.14

func (d *Driver) ListAt(ctx context.Context, m mounts.Mount, creds mounts.Credentials, commit, under string) ([]mounts.File, error)

ListAt lists the files at commit under the path under, from one call to the trees API per commit. A tree GitHub truncates returns ErrTooLarge, since a partial list would hide files.

func (*Driver) ListRefs added in v0.7.16

func (d *Driver) ListRefs(ctx context.Context, source string, req mounts.ListRefsRequest, creds mounts.Credentials) (mounts.RefPage, error)

ListRefs lists the repo's tags, or a branch's commits, newest first.

With a connection, tags come from one GraphQL query per page, ordered by their commit's date. GitHub orders by the commit's day only, then by name, so the driver sorts each page by time. Two tags made the same day on either side of a page break can still come out of order. A tag on something other than a commit, such as a tree, is left out.

GitHub's GraphQL API refuses a request with no token, so without a connection tags come from the REST tags API, which GitHub orders by version, newest first (v1.10 before v1.9). It gives no dates, so the driver reads each tag's commit too. Every request counts against GitHub's limit of 60 an hour without a token, so a page holds at most 10 tags, and a refusal for the limit returns ErrUnavailable saying a connection lifts it.

Commits come from the REST commits API, following parents from the branch's head. An empty Branch is the default branch. A missing branch returns ErrNotFound.

func (*Driver) Modes

func (d *Driver) Modes() []mounts.Mode

Modes returns snapshot first, so a mount made with no mode is pinned.

func (*Driver) Moves added in v0.7.14

func (d *Driver) Moves(ctx context.Context, source, ref string, creds mounts.Credentials) (bool, error)

Moves reports whether ref moves: an empty ref, which is the default branch, or a branch. A full commit sha doesn't, and neither does a tag.

func (*Driver) Open

func (d *Driver) Open(ctx context.Context, m mounts.Mount, creds mounts.Credentials) (fs.FS, error)

Open returns the mount's files: a snapshot's from its manifest and the blob store, or a live mount's from the Contents API at its branch. A live mount's fs.FS keeps ctx and creds for the requests it makes later.

func (*Driver) Prepare

func (d *Driver) Prepare(ctx context.Context, m mounts.Mount, creds mounts.Credentials) (mounts.Mount, error)

Prepare finishes a new mount after Resolve. For a snapshot it downloads m.Version's tarball into the blob store and sets m.Manifest. For a live mount it sets m.Ref to the default branch when it is empty, so later writes name the branch. A live mount's Ref must be a branch, since GitHub commits a write onto a branch.

func (*Driver) Put

Put writes req.Data to req.Path as one commit on the mount's branch. An empty IfMatch sends no sha, so GitHub refuses to replace an existing file and Put returns ErrConflict. A stale IfMatch returns ErrConflict too. The result names the new commit and the file's new blob sha.

func (*Driver) Resolve

func (d *Driver) Resolve(ctx context.Context, source, ref string, creds mounts.Credentials) (string, error)

Resolve checks the repo and returns the commit ref points at. An empty ref means the repo's default branch. A missing repo or ref returns ErrNotFound, and so does a private repo the token can't read.

func (*Driver) SameSource added in v0.7.16

func (d *Driver) SameSource(a, b string) bool

SameSource reports whether a and b name one repo. GitHub ignores case in owner and repo names, and a trailing ".git" names the same repo.

func (*Driver) Stat

Stat returns the file's git blob sha, the version a Put or Delete passes as IfMatch. A folder returns ErrInvalidArgument, and a missing file ErrNotFound. A snapshot mount returns ErrReadOnly, since its files have no versions to write against.

type Option

type Option func(*Driver)

Option configures a Driver.

func WithBaseURL

func WithBaseURL(u string) Option

WithBaseURL points the driver at another API root, such as a GitHub Enterprise host ("https://ghe.example.com/api/v3") or a test's fake.

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

WithHTTPClient sets the client for every request. The default is http.DefaultClient.

func WithLFSURL added in v0.7.26

func WithLFSURL(u string) Option

WithLFSURL sets the web host whose "/{owner}/{repo}.git/info/lfs" serves the repo's LFS batch API, such as a test's fake. The default follows the API base: https://github.com for GitHub's own API, and the base without "/api/v3" for a GitHub Enterprise Server host.

func WithLimits

func WithLimits(l mounts.Limits) Option

WithLimits caps a snapshot's size and file count. A zero field keeps mounts.DefaultLimits' value. Past either, Prepare returns ErrTooLarge and stores no manifest.

func WithRawURL added in v0.7.14

func WithRawURL(u string) Option

WithRawURL sets the host FetchAt downloads files from, by commit and path, such as a test's fake. The default is DefaultRawURL when the API base is GitHub's own. With another API base, such as GitHub Enterprise, and no raw URL, FetchAt uses the git blobs API instead.

func WithViews

func WithViews(dir string) Option

WithViews sets the folder where HostPath builds each snapshot's view, as hard links to its blobs, at <dir>/<mount name>/<commit>. It needs a blob store that is a blobs.Linker. Without it, HostPath returns ErrNotLocal, and the snapshot is served only as an fs.FS.

type Push added in v0.7.16

type Push struct {
	mounts.Change
	Delivery string
}

Push is one push event a GitHub webhook sent. Change is the push in the form every driver shares, for Registry.Following. Delivery is GitHub's id for the delivery, the same on a redelivery, so an app can drop a duplicate.

func ParsePush added in v0.7.16

func ParsePush(r *http.Request, secret []byte) (Push, error)

ParsePush checks a GitHub webhook delivery's signature and reads the push it carries (goapplib issue 179). secret is the webhook's secret, which the app resolves from a secret reference, as connections do. An empty secret refuses every delivery, since anyone could sign under it.

It reads at most MaxWebhookBytes of the body, and checks X-Hub-Signature-256 as an HMAC-SHA256 of the raw body in constant time before reading anything from it. A body sent as application/x-www-form-urlencoded carries the JSON in its payload field, as GitHub sends it when the webhook's content type is set that way.

It returns ErrBadSignature for a missing or wrong signature, ErrPing for a ping, and ErrNotPush for any other event. A push's Source is "github://owner/repo", its Ref the branch or tag name with "refs/heads/" or "refs/tags/" taken off, and its Version the new commit. A deleted ref has Deleted set and no Version. A push to anything but a branch or a tag returns ErrNotPush.

Directories

Path Synopsis
Package githubtest is a fake GitHub for tests of code that reads and writes repos: the mounts GitHub driver, the storage example app, and consumers' own tests.
Package githubtest is a fake GitHub for tests of code that reads and writes repos: the mounts GitHub driver, the storage example app, and consumers' own tests.
cmd/fakegithub command
Command fakegithub serves githubtest's fake as a GitHub Enterprise Server built from a seed file, for an exercise that runs an app outside the Go test process, such as one in a container.
Command fakegithub serves githubtest's fake as a GitHub Enterprise Server built from a seed file, for an exercise that runs an app outside the Go test process, such as one in a container.

Jump to

Keyboard shortcuts

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