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
- Variables
- type Driver
- func (d *Driver) CanRead(ctx context.Context, baseURL, source string, creds mounts.Credentials) (bool, error)
- func (d *Driver) Delete(ctx context.Context, m mounts.Mount, creds mounts.Credentials, ...) (*mounts.WriteResult, error)
- func (d *Driver) DeleteWebhook(ctx context.Context, source, url string, creds mounts.Credentials) error
- func (d *Driver) EnsureWebhook(ctx context.Context, source string, req mounts.WebhookRequest, ...) (mounts.Webhook, error)
- func (d *Driver) FetchAt(ctx context.Context, m mounts.Mount, creds mounts.Credentials, commit string, ...) (blobs.Hash, bool, error)
- func (d *Driver) HostPath(m mounts.Mount, p string) (string, error)
- func (d *Driver) Kind() mounts.Kind
- func (d *Driver) ListAt(ctx context.Context, m mounts.Mount, creds mounts.Credentials, ...) ([]mounts.File, error)
- func (d *Driver) ListRefs(ctx context.Context, source string, req mounts.ListRefsRequest, ...) (mounts.RefPage, error)
- func (d *Driver) Modes() []mounts.Mode
- func (d *Driver) Moves(ctx context.Context, source, ref string, creds mounts.Credentials) (bool, error)
- func (d *Driver) Open(ctx context.Context, m mounts.Mount, creds mounts.Credentials) (fs.FS, error)
- func (d *Driver) Prepare(ctx context.Context, m mounts.Mount, creds mounts.Credentials) (mounts.Mount, error)
- func (d *Driver) Put(ctx context.Context, m mounts.Mount, creds mounts.Credentials, ...) (*mounts.WriteResult, error)
- func (d *Driver) Resolve(ctx context.Context, source, ref string, creds mounts.Credentials) (string, error)
- func (d *Driver) SameSource(a, b string) bool
- func (d *Driver) Stat(ctx context.Context, m mounts.Mount, creds mounts.Credentials, p string) (mounts.FileVersion, error)
- type Option
- type Push
Constants ¶
const DefaultBaseURL = "https://api.github.com"
DefaultBaseURL is GitHub's API root. GitHub Enterprise and tests set another with WithBaseURL.
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.
const MaxWebhookBytes = 25 << 20
MaxWebhookBytes caps the body ParsePush reads. GitHub caps a webhook payload at 25 MB.
Variables ¶
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 ¶
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 ¶
func (d *Driver) Delete(ctx context.Context, m mounts.Mount, creds mounts.Credentials, req mounts.DeleteRequest) (*mounts.WriteResult, error)
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 ¶
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) 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) 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 ¶
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 ¶
func (d *Driver) Put(ctx context.Context, m mounts.Mount, creds mounts.Credentials, req mounts.PutRequest) (*mounts.WriteResult, error)
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
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 ¶
func (d *Driver) Stat(ctx context.Context, m mounts.Mount, creds mounts.Credentials, p string) (mounts.FileVersion, error)
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 ¶
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 ¶
WithHTTPClient sets the client for every request. The default is http.DefaultClient.
func WithLFSURL ¶ added in v0.7.26
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 ¶
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
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.
type Push ¶ added in v0.7.16
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
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.
Source Files
¶
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. |