Documentation
¶
Overview ¶
Package github is vx's client for the GitHub REST API as a GitHub App: the manifest flow, installations, installation tokens, OAuth sign-in, repository permissions and organization membership. It implements service.GitHubAPI. Base URLs are configurable for GitHub Enterprise and for tests, which run it against the fake in internal/github/githubtest.
Errors never contain tokens, keys, client secrets or OAuth codes.
Index ¶
- Constants
- Variables
- func ParsePrivateKey(pemText string) (*rsa.PrivateKey, error)
- type Client
- func (c *Client) APIURL() string
- func (c *Client) AppJWT() (string, error)
- func (c *Client) AppSettings(ctx context.Context) (service.GitHubAppSettings, error)
- func (c *Client) CreateAppFromManifest(ctx context.Context, code string) (service.GitHubAppCredentials, error)
- func (c *Client) CreateCheckRun(ctx context.Context, installationID int64, owner, repo string, ...) error
- func (c *Client) ExchangeOAuthCode(ctx context.Context, code, redirectURI, codeVerifier string) (string, error)
- func (c *Client) ForgetInstallationToken(id int64)
- func (c *Client) Installation(ctx context.Context, id int64) (service.GitHubInstallation, error)
- func (c *Client) InstallationRepos(ctx context.Context, id int64) ([]service.GitHubRepo, error)
- func (c *Client) InstallationToken(ctx context.Context, id int64) (string, time.Time, error)
- func (c *Client) OrgMembership(ctx context.Context, installationID int64, org, login string) (string, error)
- func (c *Client) RepoPermission(ctx context.Context, installationID int64, owner, repo, login string) (string, error)
- func (c *Client) User(ctx context.Context, userToken string) (service.GitHubUser, error)
- func (c *Client) WebURL() string
- type Config
- type Error
Constants ¶
const ( DefaultAPIURL = "https://api.github.com" DefaultWebURL = "https://github.com" )
Default base URLs of github.com.
Variables ¶
var ErrNotFound = errors.New("github: not found")
ErrNotFound matches (errors.Is) GitHub 404 responses.
var ErrRateLimited = errors.New("github: rate limited")
ErrRateLimited matches (errors.Is) rate-limited responses.
ErrUnauthorized matches (errors.Is) GitHub 401 responses.
Functions ¶
func ParsePrivateKey ¶
func ParsePrivateKey(pemText string) (*rsa.PrivateKey, error)
ParsePrivateKey parses an App private key in PEM.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to GitHub as the App. It is safe for concurrent use.
func FromCredentials ¶
func FromCredentials(creds service.GitHubAppCredentials, apiURL, webURL string) (*Client, error)
FromCredentials returns a client for an App created by the manifest flow.
func New ¶
New returns a client. A private key, when given, must be an RSA key in PEM (PKCS#1 or PKCS#8), as GitHub hands it out.
func (*Client) AppJWT ¶
AppJWT returns a JSON Web Token that authenticates as the App, valid for at most ten minutes (RS256, issued a minute in the past for clock skew).
func (*Client) AppSettings ¶ added in v0.4.0
AppSettings reads the App's permissions and webhook events (GET /app).
func (*Client) CreateAppFromManifest ¶
func (c *Client) CreateAppFromManifest(ctx context.Context, code string) (service.GitHubAppCredentials, error)
CreateAppFromManifest completes the manifest flow: it converts the one-time code GitHub sent back into the new App's credentials.
func (*Client) CreateCheckRun ¶ added in v0.4.0
func (c *Client) CreateCheckRun(ctx context.Context, installationID int64, owner, repo string, run service.GitHubCheckRun) error
CreateCheckRun posts a completed check run on a commit of owner/repo. A refusal for want of the checks: write permission matches service.ErrGitHubForbidden.
func (*Client) ExchangeOAuthCode ¶
func (c *Client) ExchangeOAuthCode(ctx context.Context, code, redirectURI, codeVerifier string) (string, error)
ExchangeOAuthCode turns a sign-in code into a user access token (PKCE).
func (*Client) ForgetInstallationToken ¶
ForgetInstallationToken drops a cached token, for example after GitHub refused it or the installation was deleted.
func (*Client) Installation ¶
Installation reads an installation of the App.
func (*Client) InstallationRepos ¶
InstallationRepos lists every repository an installation can reach, following pagination.
func (*Client) InstallationToken ¶
InstallationToken returns a token for an installation, cached until shortly before it expires.
func (*Client) OrgMembership ¶
func (c *Client) OrgMembership(ctx context.Context, installationID int64, org, login string) (string, error)
OrgMembership returns login's role in org: admin, member or none (pending invitations are none).
func (*Client) RepoPermission ¶
func (c *Client) RepoPermission(ctx context.Context, installationID int64, owner, repo, login string) (string, error)
RepoPermission returns login's permission on owner/repo: admin, maintain, write, triage, read or none. Unknown users and repositories the installation cannot see are "none".
type Config ¶
type Config struct {
// APIURL is the REST API base: https://api.github.com, or
// https://HOST/api/v3 for GitHub Enterprise Server. Empty: github.com.
APIURL string
// WebURL is the browser base: https://github.com or https://HOST.
// OAuth code exchange goes here. Empty: github.com.
WebURL string
AppID int64
PrivateKeyPEM string
ClientID string
ClientSecret string
// HTTPClient defaults to a client with a 30 second timeout.
HTTPClient *http.Client
// Now defaults to time.Now.
Now func() time.Time
}
Config configures a Client. Only the base URLs are needed to complete the manifest flow; everything else needs the App's credentials.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package githubtest is a fake GitHub for tests: an httptest server with the parts of the REST API and the browser flows vx uses, plus helpers to create users, organizations and repositories, set permissions, install the App and fire signed webhook deliveries.
|
Package githubtest is a fake GitHub for tests: an httptest server with the parts of the REST API and the browser flows vx uses, plus helpers to create users, organizations and repositories, set permissions, install the App and fire signed webhook deliveries. |