github

package
v1.111.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package github reads repository contents from the GitHub API, and searches public code.

It covers what a generator needs to track a file published upstream: find the commit that last touched it, list the tree at that commit, and fetch a file or the whole repository at it. Going through the API rather than cloning keeps the fetch to what is wanted, and yields a commit SHA to record alongside whatever was generated so a result can be traced back to what produced it.

SearchCode answers a different question from the rest: not what is in a repository already named, but which repositories mention something. It is the one call here that reaches across all of GitHub rather than into one project, and it is metered far more tightly than the others. LastCommitTimes dates what it finds: when each file was last committed to, many files to a request.

Index

Constants

View Source
const (
	DefaultHost        = "api.github.com"
	DefaultArchiveHost = "github.com"
)
View Source
const GraphqlPath = "/graphql"

GraphqlPath is where the GraphQL API is served.

View Source
const LastCommitBatchSize = 20

LastCommitBatchSize is how many files one request to LastCommitTimes asks about.

GitHub abandons a GraphQL query after ten seconds, answering 502, and walking a file's history takes a tenth of a second for an ordinary repository; fifty took five or six seconds, and one slow file took a batch past the limit. Twenty leaves room for a slow one.

View Source
const SearchCodeMaxPerPage = 100

SearchCodeMaxPerPage is the largest page the search serves. Asking for more is not an error; it simply returns this many.

View Source
const SearchCodePath = "/search/code"

SearchCodePath is where code search is served.

View Source
const TextMatchMediaType = "application/vnd.github.text-match+json"

TextMatchMediaType asks for the excerpt around each match. Without it a result names the file that matched but not what in it did.

Variables

View Source
var ErrGraphql = errors.New("graphql request refused")

ErrGraphql reports a GraphQL request the API refused as a whole, rather than answering in part.

View Source
var ErrUnexpectedCommitCount = errors.New("unexpected number of commits")

ErrUnexpectedCommitCount reports an answer that does not name exactly the one commit that was asked for.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(options ...github_config.Option) *Client

func (*Client) Blob

func (c *Client) Blob(
	ctx context.Context,
	owner string,
	repo string,
	fileSha string,
	options ...fetch_config.Option,
) (*blob.Blob, error)

Blob returns a file's contents.

func (*Client) CommitArchive

func (c *Client) CommitArchive(
	ctx context.Context,
	owner string,
	repo string,
	reference string,
	options ...fetch_config.Option,
) (*zip.Reader, error)

CommitArchive returns the repository at a revision, as an archive.

func (*Client) LastCommitTimes added in v1.110.0

func (c *Client) LastCommitTimes(
	ctx context.Context,
	files []*File,
	options ...fetch_config.Option,
) (map[File]*time.Time, []*File, error)

LastCommitTimes returns when each file was last committed to on its repository's default branch, which is the branch code search indexes, and the files GitHub could not date.

It is LatestCommit for many files at once. Asked through REST, every file is a request against an allowance of five thousand an hour, and the files a code search turns up for one organisation run to hundreds, spread over as many repositories. Through GraphQL a request asks about a batch of files, and costs about one point of that allowance however many it names.

A file the API could not resolve -- its repository deleted, renamed or made private since it was found, or a path no commit on the default branch touches -- is in neither answer. A batch GitHub could not run in the time it gives a query is split and asked again, until the file it could not run is asked about alone; one it cannot answer for even then is undated. That is a property of the file rather than a failure of the request: a mirror of a package repository has a history GitHub cannot walk for one path in ten seconds, through GraphQL or REST, however often it is asked. How many undated files are too many -- GitHub not answering at all, rather than a few files it cannot date -- is the caller's to say. Anything else fails the call: a request refused outright, or an error of any other kind.

func (*Client) LatestCommit

func (c *Client) LatestCommit(
	ctx context.Context,
	owner string,
	repo string,
	path string,
	options ...fetch_config.Option,
) (*commit.Commit, error)

LatestCommit returns the commit that last touched path.

Asking for the commit behind a single path, rather than the head of the branch, is what makes a generated file's provenance meaningful: the SHA names the revision that last changed the thing it was generated from.

func (*Client) SearchCode added in v1.40.0

func (c *Client) SearchCode(
	ctx context.Context,
	query string,
	page int,
	perPage int,
	options ...fetch_config.Option,
) (*code_search.Response, error)

SearchCode returns the public files matching the query.

The query is GitHub's own search syntax, so a caller narrows with qualifiers -- "example.com in:file language:yaml" -- rather than through parameters here. GitHub requires at least one term that is not a qualifier, and refuses a query that is all qualifiers with a 422.

Three things separate this from the rest of the client. It needs a token, and answers 401 without one. It is rate limited to ten requests a minute rather than the five thousand an hour the other calls get, so a caller sweeping many domains must pace itself. And the excerpt around each match arrives only under the text-match media type, which is asked for here because a result without it cannot be judged without opening the file.

perPage of zero asks for the server default; page of zero asks for the first.

func (*Client) Tree

func (c *Client) Tree(
	ctx context.Context,
	owner string,
	repo string,
	treeSha string,
	options ...fetch_config.Option,
) (*tree.Tree, error)

Tree returns the repository listing a tree sha names.

type File added in v1.110.0

type File struct {
	Owner string
	Repo  string
	Path  string
}

File names a file in a repository: the account the repository belongs to, its name, and the file's path in it.

Directories

Path Synopsis
types
blob
Package blob holds a file's contents as the GitHub API reports them.
Package blob holds a file's contents as the GitHub API reports them.
code_search
Package code_search holds what GitHub's code search answers with.
Package code_search holds what GitHub's code search answers with.
commit
Package commit holds a commit as the GitHub API reports one.
Package commit holds a commit as the GitHub API reports one.
tree
Package tree holds a repository listing as the GitHub API reports one.
Package tree holds a repository listing as the GitHub API reports one.

Jump to

Keyboard shortcuts

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