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
- Variables
- type Client
- func (c *Client) Blob(ctx context.Context, owner string, repo string, fileSha string, ...) (*blob.Blob, error)
- func (c *Client) CommitArchive(ctx context.Context, owner string, repo string, reference string, ...) (*zip.Reader, error)
- func (c *Client) LastCommitTimes(ctx context.Context, files []*File, options ...fetch_config.Option) (map[File]*time.Time, error)
- func (c *Client) LatestCommit(ctx context.Context, owner string, repo string, path string, ...) (*commit.Commit, error)
- func (c *Client) SearchCode(ctx context.Context, query string, page int, perPage int, ...) (*code_search.Response, error)
- func (c *Client) Tree(ctx context.Context, owner string, repo string, treeSha string, ...) (*tree.Tree, error)
- type File
Constants ¶
const ( DefaultHost = "api.github.com" DefaultArchiveHost = "github.com" )
const GraphqlPath = "/graphql"
GraphqlPath is where the GraphQL API is served.
const LastCommitBatchSize = 50
LastCommitBatchSize is how many files one request to LastCommitTimes asks about. It keeps a request well inside the time GitHub gives a GraphQL query before abandoning it.
const SearchCodeMaxPerPage = 100
SearchCodeMaxPerPage is the largest page the search serves. Asking for more is not an error; it simply returns this many.
const SearchCodePath = "/search/code"
SearchCodePath is where code search is served.
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 ¶
var ErrGraphql = errors.New("graphql request refused")
ErrGraphql reports a GraphQL request the API refused as a whole, rather than answering in part.
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, error)
LastCommitTimes returns when each file was last committed to on its repository's default branch, which is the branch code search indexes.
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 absent from the result rather than failing the rest. Any other error is: a request the API refused outright, and a part of one it could not run, which would otherwise read as a file nobody has committed to.
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.
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. |