Documentation
¶
Overview ¶
Package ghclient is patchy's GitHub REST client: GitHub App and personal-access-token authentication, installation-scoped clients and short-lived scoped tokens, and a narrow, fake-able surface (IssueStore, AlertStore, RepoStore) over the handful of endpoints the controllers use. Consumers depend on the per-concern interfaces, never on go-github types, so tests fake them with a struct.
Every client shares a rate-limit-aware transport that waits out GitHub's advertised primary and secondary rate-limit delays before retrying, so callers never see a bare 403/429 that a short pause would have cured.
Index ¶
- type Alert
- type AlertStore
- type App
- func (a *App) All(ctx context.Context) ([]*Client, error)
- func (a *App) For(ctx context.Context, repo Repo) (*Client, error)
- func (a *App) Installation(ctx context.Context, repo Repo) (*Client, error)
- func (a *App) Installations(ctx context.Context) ([]*Client, error)
- func (a *App) ScopedToken(ctx context.Context, repo Repo, perms TokenPerms) (string, time.Time, error)
- type AppConfig
- type BranchPush
- type Client
- func (c *Client) AddLabels(ctx context.Context, repo Repo, number int, add []string) error
- func (c *Client) All(context.Context) ([]*Client, error)
- func (c *Client) Assign(ctx context.Context, repo Repo, number int, logins []string) error
- func (c *Client) Close(ctx context.Context, repo Repo, number int) error
- func (c *Client) Comment(ctx context.Context, repo Repo, number int, body string) error
- func (c *Client) Create(ctx context.Context, repo Repo, req IssueRequest) (*Issue, error)
- func (c *Client) CreatePR(ctx context.Context, repo Repo, req PRRequest) (*PR, error)
- func (c *Client) DefaultBranch(ctx context.Context, repo Repo) (string, error)
- func (c *Client) DismissAlert(ctx context.Context, repo Repo, number int, reason, comment string) error
- func (c *Client) EditBody(ctx context.Context, repo Repo, number int, body string) error
- func (c *Client) For(context.Context, Repo) (*Client, error)
- func (c *Client) GetAlert(ctx context.Context, repo Repo, number int) (*Alert, error)
- func (c *Client) HeadSHA(ctx context.Context, repo Repo, branch string) (string, error)
- func (c *Client) ListComments(ctx context.Context, repo Repo, number int) ([]*Comment, error)
- func (c *Client) ListOpen(ctx context.Context, repo Repo, labels []string) ([]*Issue, error)
- func (c *Client) PushBranch(ctx context.Context, repo Repo, req BranchPush) error
- func (c *Client) RemoveLabel(ctx context.Context, repo Repo, number int, name string) error
- func (c *Client) ScopedToken(context.Context, Repo, TokenPerms) (string, time.Time, error)
- func (c *Client) SearchIssues(ctx context.Context, query string) ([]*Issue, error)
- type Comment
- type CommitFile
- type Issue
- type IssueRequest
- type IssueStore
- type PR
- type PRRequest
- type Repo
- type RepoStore
- type Resolver
- type TokenPerms
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Alert ¶
type Alert struct {
Number int
RuleID string
RuleName string
RuleDescription string
RuleHelp string
Tags []string
Severity string
State string
HTMLURL string
Path string
StartLine int
EndLine int
Snippet string
MostRecentSHA string
}
Alert is patchy's view of a code-scanning alert: the rule, the severity (security_severity_level falling back to the rule severity), and the most recent instance's location, message snippet, and commit.
type AlertStore ¶
type AlertStore interface {
// GetAlert fetches one code-scanning alert.
GetAlert(ctx context.Context, repo Repo, number int) (*Alert, error)
// DismissAlert dismisses an alert; reason must be one of GitHub's
// "false positive", "won't fix", or "used in tests".
DismissAlert(ctx context.Context, repo Repo, number int, reason, comment string) error
}
AlertStore is the code-scanning surface.
type App ¶
type App struct {
// contains filtered or unexported fields
}
App mints installation-scoped clients and tokens for a GitHub App.
func (*App) Installation ¶
Installation returns a Client authenticated as the installation covering repo. Both the repo → installation lookup and the per-installation client are cached, so repeated calls cost no API requests.
func (*App) Installations ¶
Installations returns one client per installation of the App — the fan-out for cross-repository sweeps like the reconcile searches.
type AppConfig ¶
type AppConfig struct {
AppID int64
PrivateKey []byte // PEM-encoded RSA private key
BaseURL string // default https://api.github.com/ ; tests and GHES override
}
AppConfig configures GitHub App authentication.
type BranchPush ¶ added in v0.3.0
type BranchPush struct {
Branch string
BaseSHA string
Message string
Files []CommitFile
Deletes []string
}
BranchPush describes one commit to create on top of BaseSHA and the branch to point at it.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps the GitHub REST API with patchy's narrow, fake-able surface. Construct one with NewToken or App.Installation.
func NewToken ¶
NewToken returns a Client authenticated with a personal access token — the dev-mode fallback. baseURL "" means api.github.com.
func (*Client) DefaultBranch ¶
DefaultBranch returns the repository's default branch name.
func (*Client) DismissAlert ¶
func (c *Client) DismissAlert(ctx context.Context, repo Repo, number int, reason, comment string) error
DismissAlert dismisses a code-scanning alert. reason must be one of GitHub's "false positive", "won't fix", or "used in tests".
func (*Client) HeadSHA ¶ added in v0.3.0
HeadSHA resolves the current commit SHA of refs/heads/<branch>.
func (*Client) ListComments ¶
ListComments returns every comment on the issue, following pagination.
func (*Client) ListOpen ¶
ListOpen returns every open issue in repo carrying all of labels, following pagination. Pull requests (which the issues API also returns) are skipped.
func (*Client) PushBranch ¶ added in v0.3.0
PushBranch creates one commit on top of req.BaseSHA carrying req's file changes and points refs/heads/<Branch> at it, force-updating an existing branch so a retried event re-applies cleanly. It is the git-less push: blob → tree → commit → ref through the Git Data API.
func (*Client) RemoveLabel ¶
RemoveLabel removes one label from the issue. A 404 — the label is already absent — is not an error, so removals are idempotent.
func (*Client) ScopedToken ¶
ScopedToken implements Resolver for the dev-mode token client: it returns the configured PAT. A PAT cannot be narrowed per repository — that is precisely why App auth is the production mode.
type CommitFile ¶ added in v0.3.0
type CommitFile struct {
Path string
// Mode is the git file mode: "100644", "100755", or "120000".
Mode string
// Content is the raw blob; for a symlink ("120000"), the link target.
Content []byte
}
CommitFile is one file created or modified by a BranchPush.
type Issue ¶
type Issue struct {
// Repo is the issue's repository, populated from the list/search
// response so cross-repository sweeps can act on their results.
Repo Repo
Number int
Title string
Body string
State string
Labels []string
Assignees []string
CreatedAt time.Time
}
Issue is patchy's thin view of a GitHub issue, decoupled from go-github.
type IssueRequest ¶
IssueRequest is the payload for creating an issue.
type IssueStore ¶
type IssueStore interface {
// ListOpen returns every open issue in repo carrying all of labels.
ListOpen(ctx context.Context, repo Repo, labels []string) ([]*Issue, error)
// Create opens a new issue.
Create(ctx context.Context, repo Repo, req IssueRequest) (*Issue, error)
// Comment adds a comment to the issue.
Comment(ctx context.Context, repo Repo, number int, body string) error
// ListComments returns every comment on the issue, oldest first.
ListComments(ctx context.Context, repo Repo, number int) ([]*Comment, error)
// EditBody replaces the issue body.
EditBody(ctx context.Context, repo Repo, number int, body string) error
// AddLabels adds labels to the issue, creating them as needed.
AddLabels(ctx context.Context, repo Repo, number int, add []string) error
// RemoveLabel removes one label; a label already absent is not an error.
RemoveLabel(ctx context.Context, repo Repo, number int, name string) error
// Assign adds assignees to the issue.
Assign(ctx context.Context, repo Repo, number int, logins []string) error
// Close closes the issue.
Close(ctx context.Context, repo Repo, number int) error
}
IssueStore is the issue surface the controllers use; *Client implements it, controller tests fake it.
type RepoStore ¶
type RepoStore interface {
// DefaultBranch returns the repository's default branch name.
DefaultBranch(ctx context.Context, repo Repo) (string, error)
// HeadSHA resolves the current commit SHA of refs/heads/<branch>.
HeadSHA(ctx context.Context, repo Repo, branch string) (string, error)
// CreatePR opens a pull request.
CreatePR(ctx context.Context, repo Repo, req PRRequest) (*PR, error)
// SearchIssues runs an issue search query (cross-repo reconcile
// queries) and returns every matching issue.
SearchIssues(ctx context.Context, query string) ([]*Issue, error)
}
RepoStore is the repository/PR/search surface.
type Resolver ¶
type Resolver interface {
// For returns the client covering repo.
For(ctx context.Context, repo Repo) (*Client, error)
// All returns one client per installation (for cross-repo sweeps).
All(ctx context.Context) ([]*Client, error)
// ScopedToken mints a credential for git operations against one
// repository. Under App auth it is short-lived and single-repository;
// in dev-token mode it is the configured PAT, which is as scoped as
// that mode can be.
ScopedToken(ctx context.Context, repo Repo, perms TokenPerms) (string, time.Time, error)
}
Resolver yields clients regardless of auth mode: an App resolves per-repo installations and fans out across all of them; a PAT Client is its own single-tenant resolver. Controllers depend on this seam so both modes wire identically.
type TokenPerms ¶
type TokenPerms struct {
Contents string
}
TokenPerms is the permission set for a scoped token; string values are "read"/"write" per the GitHub API; empty means not requested.