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 ¶
- Variables
- func IsNotFound(err error) bool
- type Alert
- type AlertStore
- type App
- func (a *App) Deliveries(ctx context.Context, visit func(Delivery) bool) (complete bool, err error)
- func (a *App) Installation(ctx context.Context, repo Repo) (*Client, error)
- func (a *App) Installations(ctx context.Context) ([]*Client, error)
- func (a *App) Redeliver(ctx context.Context, deliveryID int64) 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) 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) DeleteIssue(ctx context.Context, repo Repo, number int) 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) EditComment(ctx context.Context, repo Repo, commentID int64, body string) error
- func (c *Client) FindPRByHead(ctx context.Context, repo Repo, branch string) (*PR, error)
- func (c *Client) GetAlert(ctx context.Context, repo Repo, number int) (*Alert, error)
- func (c *Client) GetIssue(ctx context.Context, repo Repo, number int) (*Issue, 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) OpenAlert(ctx context.Context, repo Repo, number int) 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) SearchIssues(ctx context.Context, query string) ([]*Issue, error)
- func (c *Client) Tarball(ctx context.Context, repo Repo, ref string) (io.ReadCloser, error)
- type Comment
- type CommitFile
- type Delivery
- type Issue
- type IssueRequest
- type IssueStore
- type PR
- type PRRequest
- type Repo
- type RepoStore
- type TokenPerms
Constants ¶
This section is empty.
Variables ¶
ErrDeleteUnauthorized reports the credential cannot delete issues: GitHub restricts the deleteIssue mutation to admin-permission user tokens — App installation tokens and fine-grained PATs are always refused ("Viewer not authorized to delete"). Callers may fall back to closing the issue.
Functions ¶
func IsNotFound ¶ added in v0.4.0
IsNotFound reports whether err (anywhere in its chain) is a GitHub API 404 — the projected object no longer exists on the forge.
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) Deliveries ¶ added in v0.5.4
Deliveries walks the App webhook's delivery log newest-first, calling visit for each attempt until visit returns false or the log is exhausted. complete is false when the page cap ended the walk instead.
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) DeleteIssue ¶ added in v0.5.7
DeleteIssue permanently deletes an issue. The REST API cannot delete issues at all — only the GraphQL deleteIssue mutation can, and it needs the issue's node ID, so this fetches the issue first. Deleting requires admin-level repository permission; it exists for the demo reset, which removes the tracking issues a replayed demo would otherwise duplicate. A missing issue is success: deletion is idempotent.
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) EditComment ¶ added in v0.5.3
EditComment replaces the body of an existing issue comment.
func (*Client) FindPRByHead ¶ added in v0.4.0
FindPRByHead returns the open pull request whose head is branch, or nil — the idempotency check before creating a remediation PR (a crash between push and status write must not open a duplicate).
func (*Client) GetIssue ¶ added in v0.4.0
GetIssue fetches one issue — the projection reconciler's current-state read before diffing labels and body.
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) OpenAlert ¶ added in v0.5.7
OpenAlert reopens a code-scanning alert (undoes a dismissal). Only a dismissed alert can be reopened, so the current state is read first and anything else is success: an alert that is already open, fixed, or gone with the repository that held it has no dismissal left to undo. Reopening is therefore convergent rather than authoritative — the retried cleanup of a repository that was deleted or recreated since the dismissal still succeeds. GitHub rejects every illegal transition with the same opaque 400 ("There was an issue creating the request. Please try again."), which is why the state is read rather than sniffed out of the error.
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) SearchIssues ¶
SearchIssues runs an issue search query and returns every matching issue, following pagination.
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 Delivery ¶ added in v0.5.4
type Delivery struct {
ID int64
GUID string
DeliveredAt time.Time
// Redelivery marks a redelivered attempt.
Redelivery bool
// OK reports whether the receiver answered 2xx.
OK bool
Event string
}
Delivery is one attempt from the App webhook's delivery log. Attempts of the same logical delivery (the original and its redeliveries) share a GUID.
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 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.