Documentation
¶
Overview ¶
Package github is kritika's forge. It holds the App's credentials and a transport that signs requests as the App with a short-lived JWT. It also holds a transport that mints, caches, and refreshes an installation token, the forge.Client over an installation, and the App manifest flow. The installation token authenticates API calls and git fetches alike, so an App-only installation needs no personal token.
Index ¶
- Variables
- func RetryAfter(err error, now time.Time) (time.Duration, bool)
- func Status(err error) int
- type AccountRepositories
- type App
- func (a *App) Client(tokens *InstallationTokens) (*gh.Client, error)
- func (a *App) DiscoverInstallation(ctx context.Context, owner, repo string) (int64, error)
- func (a *App) InstallationTokens(installationID int64) *InstallationTokens
- func (a *App) Installations(ctx context.Context) ([]Installation, error)
- func (a *App) Reach(ctx context.Context, accounts []string) ([]AccountRepositories, error)
- func (a *App) Repositories(ctx context.Context, id int64) ([]Repository, error)
- func (a *App) Slug(ctx context.Context) (string, error)
- func (a *App) Uninstall(ctx context.Context, id int64) error
- type Client
- func (c *Client) Approve(ctx context.Context, owner, repo string, number int, headSHA string) (bool, error)
- func (c *Client) BotLogin(ctx context.Context) (string, error)
- func (c *Client) BranchTip(ctx context.Context, owner, repo, branch string) (string, string, error)
- func (c *Client) ChangesRequested(ctx context.Context, owner, repo string, number int) (bool, error)
- func (c *Client) CloneURL(owner, repo string) string
- func (c *Client) Commit(ctx context.Context, owner, repo, sha string) (forge.CommitInfo, error)
- func (c *Client) CommitFiles(ctx context.Context, owner, repo, branch, parent, message string, ...) (string, error)
- func (c *Client) CommitSubject(ctx context.Context, owner, repo, sha string) (string, error)
- func (c *Client) CommitURL(owner, repo, sha string) string
- func (c *Client) CreateComment(ctx context.Context, owner, repo string, number int, body string) (int64, error)
- func (c *Client) CreateReview(ctx context.Context, owner, repo string, number int, headSHA string, ...) ([]int64, error)
- func (c *Client) DismissApprovals(ctx context.Context, owner, repo string, number int, message string) (int, error)
- func (c *Client) FileAt(ctx context.Context, owner, repo, ref, path string) ([]byte, error)
- func (c *Client) FileURL(owner, repo, sha, path string, line, endLine int) string
- func (c *Client) FindComment(ctx context.Context, owner, repo string, number int, login, marker string) (int64, error)
- func (c *Client) GetComment(ctx context.Context, owner, repo string, _ int, id int64, inline bool) (forge.Comment, error)
- func (c *Client) GitToken(ctx context.Context, repo string) (string, error)
- func (c *Client) Issue(ctx context.Context, owner, repo string, number int) (forge.Issue, error)
- func (c *Client) ListConversation(ctx context.Context, owner, repo string, number int) ([]forge.Comment, error)
- func (c *Client) ListInline(ctx context.Context, owner, repo string, number int) ([]forge.Comment, error)
- func (c *Client) ListOpenPullRequests(ctx context.Context, owner, repo string, since time.Time) ([]forge.OpenPullRequest, error)
- func (c *Client) MergeBase(ctx context.Context, owner, repo string, _ int, base, head string) (string, error)
- func (c *Client) OpenPullRequestsFrom(ctx context.Context, owner, repo, branch string) ([]int, error)
- func (c *Client) Permission(ctx context.Context, owner, repo, login string) (forge.Permission, error)
- func (c *Client) PullRequest(ctx context.Context, owner, repo string, number int) (forge.OpenPullRequest, error)
- func (c *Client) PullRequestDiff(ctx context.Context, owner, repo string, _ int, base, head string) (string, error)
- func (c *Client) PullRequestReaction(ctx context.Context, owner, repo string, number int, content string) (int64, error)
- func (c *Client) React(ctx context.Context, owner, repo string, to forge.Comment, content string) (int64, error)
- func (c *Client) ReactToPullRequest(ctx context.Context, owner, repo string, number int, content string) (int64, error)
- func (c *Client) Reaction(ctx context.Context, owner, repo string, on forge.Comment, content string) (int64, error)
- func (c *Client) ReplyInline(ctx context.Context, owner, repo string, number int, to forge.Comment, ...) (int64, error)
- func (c *Client) ResolveThread(ctx context.Context, owner, repo string, number int, id int64, onlyOwn bool) (bool, error)
- func (c *Client) SetStatus(ctx context.Context, owner, repo, sha string, state forge.StatusState, ...) error
- func (c *Client) StackBelow(ctx context.Context, owner, repo string, number int) ([]forge.StackEntry, error)
- func (c *Client) ThreadResolved(ctx context.Context, owner, repo string, number int, id int64) (bool, bool, error)
- func (c *Client) ThreadURL(owner, repo string, number int, id int64) string
- func (c *Client) ThreadsResolved(ctx context.Context, owner, repo string, number int) (map[int64]bool, error)
- func (c *Client) Unreact(ctx context.Context, owner, repo string, from forge.Comment, id int64) error
- func (c *Client) UnreactToPullRequest(ctx context.Context, owner, repo string, number int, id int64) error
- func (c *Client) UpdateComment(ctx context.Context, owner, repo string, id int64, body string) error
- type Installation
- type InstallationTokens
- type Repository
Constants ¶
This section is empty.
Variables ¶
var ErrRedirectRefused = errors.New("github: redirect refused")
ErrRedirectRefused is what a GitHub client refuses to follow a redirect with: one off the API's origin, or past the tenth. Go wraps it in a *url.Error, which is no failed connection to retry.
Functions ¶
func RetryAfter ¶
RetryAfter reports how long to wait before trying again after err, when err is GitHub's rate limit from REST or GraphQL. The wait lasts until the primary limit resets, with the second of grace that rateLimitWait gives. Or it lasts for the secondary limit's Retry-After, or tuning's GitHub.RateLimitMaxWait when that limit names none. A 403 or 429 that go-github does not know for a limit, as one whose body names no secondary limit, is one still when it names a Retry-After, and lasts for that. A job retried sooner only spends an attempt. The REST client also refuses requests locally until its limit resets.
Types ¶
type AccountRepositories ¶
type AccountRepositories struct {
Account string
Installed bool
Repositories []Repository
}
AccountRepositories is what an App reaches on one account: whether it is installed there, and the repositories its installation reaches.
type App ¶
type App struct {
// OnRateLimit, when set, is told of every response GitHub refused for a
// rate limit: the wait it asked for, and whether the request waited it
// out and was sent again. Set it before the App is used.
OnRateLimit func(wait time.Duration, waited bool)
// contains filtered or unexported fields
}
App is a GitHub App identity: the client id and private key of one App.
func NewApp ¶
NewApp parses the App's private key and builds the App-level client used for installation lookup and token minting. apiBase is empty for api.github.com. Tests point it at a server of their own.
func (*App) Client ¶
func (a *App) Client(tokens *InstallationTokens) (*gh.Client, error)
Client returns a go-github client authenticated as the installation.
func (*App) DiscoverInstallation ¶
DiscoverInstallation returns the id of the App's installation on the account that owns the repository.
func (*App) InstallationTokens ¶
func (a *App) InstallationTokens(installationID int64) *InstallationTokens
InstallationTokens returns the token source for one installation, as DiscoverInstallation finds it: the same one on every call, so the token is minted once an hour however often the installation is used.
func (*App) Installations ¶
func (a *App) Installations(ctx context.Context) ([]Installation, error)
Installations lists every account the App is installed on.
func (*App) Reach ¶
Reach lists, for each of accounts in order, whether the App is installed there and the repositories it reaches. A suspended installation reaches none: it can mint no token to list them with.
func (*App) Repositories ¶
Repositories lists every repository installation id reaches, with a token of that installation.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is one App installation's access to GitHub.
func (*Client) Approve ¶
func (c *Client) Approve(ctx context.Context, owner, repo string, number int, headSHA string) (bool, error)
Approve implements forge.Client. GitHub keeps every review that a user submits, so an approval of each head stacks up on a pull request that is pushed to often. The bot's reviews are listed first, and one already approving the head stands as it is. So does one of an older head while GitHub counts it (see approvalCounts). Where the base branch dismisses stale approvals none stands after a push, so the head gets a new approval. A head also gets one when approvalCounts fails, since the older approval may no longer count: the caller does not try again, and a base branch that wants the last push approved would stay blocked.
func (*Client) BotLogin ¶
BotLogin implements forge.Client. An App's comments are authored by the user "<slug>[bot]". The slug comes from the App itself, so nothing in the configuration has to repeat it.
func (*Client) ChangesRequested ¶
func (c *Client) ChangesRequested(ctx context.Context, owner, repo string, number int) (bool, error)
ChangesRequested implements forge.Client. GitHub keeps every review a user submits, oldest first: where a reviewer stands is their latest one that approved, requested changes or was dismissed, a comment changing nothing.
func (*Client) CommitFiles ¶
func (c *Client) CommitFiles( ctx context.Context, owner, repo, branch, parent, message string, files []forge.FileChange, ) (string, error)
CommitFiles implements forge.Client. An answer with an error that refusal names made no commit. The message's first line is the commit's headline. The commit ends with a Signed-off-by line of the App's bot, for a repository that checks for one (DCO).
func (*Client) CommitSubject ¶
CommitSubject implements forge.Client.
func (*Client) CreateComment ¶
func (c *Client) CreateComment(ctx context.Context, owner, repo string, number int, body string) (int64, error)
CreateComment implements forge.Client.
func (*Client) CreateReview ¶
func (c *Client) CreateReview( ctx context.Context, owner, repo string, number int, headSHA string, comments []forge.InlineComment, ) ([]int64, error)
CreateReview implements forge.Client with a COMMENT review: visible in the Files tab, never a required approval or a request for changes. The review's answer does not carry its comments, so they are listed after and matched to the ones sent by path and body, in order.
func (*Client) DismissApprovals ¶
func (c *Client) DismissApprovals(ctx context.Context, owner, repo string, number int, message string) (int, error)
DismissApprovals implements forge.Client.
func (*Client) FileAt ¶
FileAt implements forge.Client through the contents API. It reads a file up to the lower of maxContentsBytes and tuning's Forge.MaxFileBytes. A symlink or submodule is not a file.
func (*Client) FindComment ¶
func (c *Client) FindComment(ctx context.Context, owner, repo string, number int, login, marker string) (int64, error)
FindComment implements forge.Client.
func (*Client) GetComment ¶
func (c *Client) GetComment(ctx context.Context, owner, repo string, _ int, id int64, inline bool) (forge.Comment, error)
GetComment implements forge.Client. GitHub resolves a comment by id alone.
func (*Client) GitToken ¶
GitToken implements forge.Client with a read-only installation token for repo alone.
func (*Client) Issue ¶
Issue implements forge.Client. GitHub files pull requests among a repository's issues, so one is returned as such rather than as an error.
func (*Client) ListConversation ¶
func (c *Client) ListConversation(ctx context.Context, owner, repo string, number int) ([]forge.Comment, error)
ListConversation implements forge.Client.
func (*Client) ListInline ¶
func (c *Client) ListInline(ctx context.Context, owner, repo string, number int) ([]forge.Comment, error)
ListInline implements forge.Client.
func (*Client) ListOpenPullRequests ¶
func (c *Client) ListOpenPullRequests(ctx context.Context, owner, repo string, since time.Time) ([]forge.OpenPullRequest, error)
ListOpenPullRequests implements forge.Client. GitHub sorts by update time server-side, so the walk stops at the first page item older than since.
func (*Client) MergeBase ¶
func (c *Client) MergeBase(ctx context.Context, owner, repo string, _ int, base, head string) (string, error)
MergeBase implements forge.Client through the compare API, whose merge_base_commit is exactly what GitHub diffs a PR against. The pull request's number goes unused.
func (*Client) OpenPullRequestsFrom ¶
func (c *Client) OpenPullRequestsFrom(ctx context.Context, owner, repo, branch string) ([]int, error)
OpenPullRequestsFrom implements forge.Client. Two pull requests are enough to tell a branch that more than one shares, so it reads one page.
func (*Client) Permission ¶
func (c *Client) Permission(ctx context.Context, owner, repo, login string) (forge.Permission, error)
Permission implements forge.Client.
func (*Client) PullRequest ¶
func (c *Client) PullRequest(ctx context.Context, owner, repo string, number int) (forge.OpenPullRequest, error)
PullRequest implements forge.Client.
func (*Client) PullRequestDiff ¶
func (c *Client) PullRequestDiff(ctx context.Context, owner, repo string, _ int, base, head string) (string, error)
PullRequestDiff implements forge.Client from the compare of base and head, both commits, so the diff is of exactly those two.
func (*Client) PullRequestReaction ¶
func (c *Client) PullRequestReaction(ctx context.Context, owner, repo string, number int, content string) (int64, error)
PullRequestReaction implements forge.Client. A pull request's own reactions are its issue's.
func (*Client) React ¶
func (c *Client) React(ctx context.Context, owner, repo string, to forge.Comment, content string) (int64, error)
React implements forge.Client.
func (*Client) ReactToPullRequest ¶
func (c *Client) ReactToPullRequest(ctx context.Context, owner, repo string, number int, content string) (int64, error)
ReactToPullRequest implements forge.Client. A pull request's own reactions are its issue's.
func (*Client) Reaction ¶
func (c *Client) Reaction(ctx context.Context, owner, repo string, on forge.Comment, content string) (int64, error)
Reaction implements forge.Client.
func (*Client) ReplyInline ¶
func (c *Client) ReplyInline(ctx context.Context, owner, repo string, number int, to forge.Comment, body string) (int64, error)
ReplyInline implements forge.Client. The reply goes under the thread's top-level comment: GitHub takes no replies to replies.
func (*Client) ResolveThread ¶
func (c *Client) ResolveThread(ctx context.Context, owner, repo string, number int, id int64, onlyOwn bool) (bool, error)
ResolveThread implements forge.Client. GitHub lets an App resolve a thread only with write access to the repository's contents, whatever its access to pull requests. So an installation without it resolves nothing.
func (*Client) SetStatus ¶
func (c *Client) SetStatus(ctx context.Context, owner, repo, sha string, state forge.StatusState, description string) error
SetStatus implements forge.Client.
func (*Client) StackBelow ¶
func (c *Client) StackBelow(ctx context.Context, owner, repo string, number int) ([]forge.StackEntry, error)
StackBelow implements forge.Client. A GitHub Enterprise Server without stacked pull requests has no such endpoint and answers 404, which is no stack.
func (*Client) ThreadResolved ¶
func (c *Client) ThreadResolved(ctx context.Context, owner, repo string, number int, id int64) (bool, bool, error)
ThreadResolved implements forge.Client.
func (*Client) ThreadURL ¶
ThreadURL implements forge.Client: GitHub scrolls the conversation tab to a review comment's thread by its discussion anchor.
func (*Client) ThreadsResolved ¶
func (c *Client) ThreadsResolved(ctx context.Context, owner, repo string, number int) (map[int64]bool, error)
ThreadsResolved implements forge.Client, under the ids of all of a thread's comments: GraphQL knows the thread, REST the comments.
func (*Client) Unreact ¶
func (c *Client) Unreact(ctx context.Context, owner, repo string, from forge.Comment, id int64) error
Unreact implements forge.Client.
type Installation ¶
type Installation struct {
ID int64
// Account is the login of the account, AccountType "User" or
// "Organization".
Account, AccountType string
// AllRepositories is whether it covers every repository of the account
// rather than those selected.
AllRepositories bool
Suspended bool
// HTMLURL is the installation's settings page on GitHub.
HTMLURL string
}
Installation is one account the App is installed on.
type InstallationTokens ¶
type InstallationTokens struct {
// contains filtered or unexported fields
}
InstallationTokens mints an installation token on first use, caches it, and refreshes it before expiry. One instance per installation.
func (*InstallationTokens) CanWriteContents ¶
func (t *InstallationTokens) CanWriteContents(ctx context.Context) (bool, error)
CanWriteContents reports whether the installation was granted write access to repository contents. A token that GitHub minted without saying what it can do counts as one that can.
type Repository ¶
type Repository struct {
// Name is the repository's name, FullName "owner/name".
Name, FullName, DefaultBranch string
Archived, Fork bool
// Visibility is public, private or internal, as GitHub reports it.
Visibility string
}
Repository is one repository an installation reaches.