github

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	DefaultAPIURL = "https://api.github.com"
	DefaultWebURL = "https://github.com"
)

Default base URLs of github.com.

Variables

View Source
var ErrNotFound = errors.New("github: not found")

ErrNotFound matches (errors.Is) GitHub 404 responses.

View Source
var ErrRateLimited = errors.New("github: rate limited")

ErrRateLimited matches (errors.Is) rate-limited responses.

View Source
var ErrUnauthorized = errors.New("github: unauthorized")

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

func New(cfg Config) (*Client, error)

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) APIURL

func (c *Client) APIURL() string

APIURL and WebURL return the configured base URLs.

func (*Client) AppJWT

func (c *Client) AppJWT() (string, error)

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

func (c *Client) AppSettings(ctx context.Context) (service.GitHubAppSettings, error)

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

func (c *Client) ForgetInstallationToken(id int64)

ForgetInstallationToken drops a cached token, for example after GitHub refused it or the installation was deleted.

func (*Client) Installation

func (c *Client) Installation(ctx context.Context, id int64) (service.GitHubInstallation, error)

Installation reads an installation of the App.

func (*Client) InstallationRepos

func (c *Client) InstallationRepos(ctx context.Context, id int64) ([]service.GitHubRepo, error)

InstallationRepos lists every repository an installation can reach, following pagination.

func (*Client) InstallationToken

func (c *Client) InstallationToken(ctx context.Context, id int64) (string, time.Time, error)

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".

func (*Client) User

func (c *Client) User(ctx context.Context, userToken string) (service.GitHubUser, error)

User returns the user a user access token belongs to. Email is the primary verified email when GitHub shares it, else empty.

func (*Client) WebURL

func (c *Client) WebURL() string

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.

type Error

type Error struct {
	Method, Path string
	Status       int
	Message      string
	// RateLimited is set when GitHub refused for a rate limit; Reset is
	// when the limit resets, if GitHub said.
	RateLimited bool
	Reset       time.Time
}

Error is a failed GitHub request. Path never contains secrets.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL