github

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package github adapts GitHub App REST, OAuth and webhook wire formats. It holds no storage and no diffx policy.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnauthorized matches 401 responses and rejected OAuth codes or refresh tokens.
	ErrUnauthorized      = errors.New("github: unauthorized")
	ErrInvalidRepository = errors.New("github: repository must be owner/name")
)

Functions

func ParseEvent

func ParseEvent(event string, body []byte) (any, error)

ParseEvent decodes a verified webhook body by X-GitHub-Event name. It returns PullRequestEvent, InstallationEvent, InstallationRepositoriesEvent or AuthorizationEvent, and (nil, nil) for events diffx ignores, including ping.

func ParsePrivateKey

func ParsePrivateKey(pemBytes []byte) (*rsa.PrivateKey, error)

ParsePrivateKey accepts PKCS#1 ("RSA PRIVATE KEY") or PKCS#8 ("PRIVATE KEY") PEM.

Types

type Account

type Account struct {
	ID      int64
	Login   string
	HTMLURL string
}

type App

type App struct {
	ID            int64
	ClientID      string
	ClientSecret  string
	PrivateKey    *rsa.PrivateKey
	WebhookSecret []byte
}

type AuthorizationEvent

type AuthorizationEvent struct {
	Action string
	UserID int64
}

AuthorizationEvent reports a user revoking the app (github_app_authorization).

type Client

type Client struct {
	// contains filtered or unexported fields
}

func New

func New(app App, options ...Option) (*Client, error)

func (*Client) AppSlug

func (c *Client) AppSlug(ctx context.Context) (string, error)

func (*Client) AuthorizeURL

func (c *Client) AuthorizeURL(state, redirectURI string) string

func (*Client) Exchange

func (c *Client) Exchange(ctx context.Context, code, redirectURI string) (UserToken, error)

func (*Client) InstallationToken

func (c *Client) InstallationToken(ctx context.Context, installationID int64, repositoryIDs []int64, permissions map[string]string) (Token, error)

InstallationToken mints a token narrowed to repositoryIDs and permissions when given.

func (*Client) OpenPullRequests

func (c *Client) OpenPullRequests(ctx context.Context, installationID int64, fullName string) ([]PullRequest, error)

OpenPullRequests returns at most 3000 open pull requests.

func (*Client) PullRequest

func (c *Client) PullRequest(ctx context.Context, installationID int64, fullName string, number int) (PullRequest, error)

func (*Client) Refresh

func (c *Client) Refresh(ctx context.Context, refreshToken string) (UserToken, error)

func (*Client) User

func (c *Client) User(ctx context.Context, accessToken string) (Account, error)

func (*Client) UserRepositories

func (c *Client) UserRepositories(ctx context.Context, accessToken string) ([]Repository, error)

UserRepositories lists repositories both the user and the app can access, tagged with their installation and capped at 10000.

func (*Client) VerifyWebhook

func (c *Client) VerifyWebhook(body []byte, signature256 string) bool

VerifyWebhook checks an X-Hub-Signature-256 header in constant time.

type Error

type Error struct {
	Status  int
	Message string
}

Error is a non-2xx GitHub API response. Rate-limited 403s report Status 429.

func (*Error) Error

func (err *Error) Error() string

func (*Error) Is

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

type InstallationEvent

type InstallationEvent struct {
	Action         string
	InstallationID int64
}

InstallationEvent.Action is created, deleted, suspend, unsuspend or new_permissions_accepted.

type InstallationRepositoriesEvent

type InstallationRepositoriesEvent struct {
	Action         string
	InstallationID int64
	Added          []Repository
	Removed        []Repository
}

type Option

type Option func(*Client)

func WithAPIURL

func WithAPIURL(url string) Option

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

func WithWebURL

func WithWebURL(url string) Option

WithWebURL sets the origin of the OAuth authorize and token endpoints.

type PullRequest

type PullRequest struct {
	Number    int
	State     string
	Merged    bool
	Draft     bool
	Title     string
	Body      string
	HTMLURL   string
	Author    Account
	Base      Ref
	Head      Ref
	CreatedAt time.Time
	UpdatedAt time.Time
}

PullRequest.Head.Repository is zero when the head fork was deleted.

type PullRequestEvent

type PullRequestEvent struct {
	Action         string
	InstallationID int64
	Repository     Repository
	Number         int
}

type Ref

type Ref struct {
	Ref        string
	SHA        string
	Repository Repository
}

type Repository

type Repository struct {
	ID             int64
	FullName       string
	CloneURL       string
	Private        bool
	InstallationID int64
}

Repository.InstallationID is set only by installation-scoped listings and webhooks.

type Token

type Token struct {
	Value     string
	ExpiresAt time.Time
}

type UserToken

type UserToken struct {
	AccessToken      string
	ExpiresAt        time.Time
	RefreshToken     string
	RefreshExpiresAt time.Time
}

UserToken times are zero when GitHub omits the matching expiry.

Jump to

Keyboard shortcuts

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