githubapp

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 21 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 githubapp makes the tokens of a GitHub App: a short JWT signed with the App's private key asks GitHub for an installation token limited to one repository and to what one operation needs, kept in memory until shortly before it expires.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPending is a code not entered yet: ask again after the interval.
	ErrPending = errors.New("the code has not been entered yet")
	// ErrSlowDown is asking too often: ask again after a longer interval.
	ErrSlowDown = errors.New("asked too often")
)

What waiting for a person to enter a code may answer besides a token.

View Source
var ErrNotInstalled = fmt.Errorf("%w: the GitHub App is not installed on it", credentials.ErrUnreachable)

ErrNotInstalled is a repository the App cannot reach: not installed on its owner, or not on that repository.

Functions

func APIBase

func APIBase(host string, httpsPort int) string

APIBase is the address of the API of a GitHub host: api.github.com for github.com, and `/api/v3` on the host, on its HTTPS port, for a GitHub Enterprise Server.

func FormAction

func FormAction(webBase, organization, state string) string

FormAction is where a browser posts a manifest to create an App owned by organization, or by the person signed in when organization is empty, carrying state back.

func Manifest

func Manifest(name, redirectURL string) (string, error)

Manifest is the manifest of an App named name, which GitHub sends back to redirectURL with a code: private, no webhook, only the permissions publishing takes.

func WebBase

func WebBase(host string, httpsPort int) string

WebBase is the address of a GitHub host's pages.

Types

type API

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

API calls GitHub's REST API at one address: api.github.com, or a GitHub Enterprise Server's. A redirect is never followed, so a token is sent only where it was meant for.

func NewAPI

func NewAPI(base, userAgent string) *API

NewAPI returns the API at base, calling as userAgent (gitd and its version).

func (*API) AskToReview

func (api *API) AskToReview(ctx context.Context, repository Repository, number int64, reviewers []string) error

AskToReview asks people to review a pull request.

func (*API) Convert

func (api *API) Convert(ctx context.Context, code string) (Created, error)

Convert exchanges the code GitHub sent back from a manifest for the App it created. The code alone is the proof: the call carries no other credential.

func (*API) DefaultBranch

func (api *API) DefaultBranch(ctx context.Context, repository Repository) (string, error)

DefaultBranch is the branch a repository's pull requests merge into unless they say.

func (*API) Edit

func (api *API) Edit(ctx context.Context, repository Repository, number int64, edit Edit) error

Edit writes a pull request's title and body over what they were.

func (*API) Label

func (api *API) Label(ctx context.Context, repository Repository, number int64, labels []string) error

Label adds labels to a pull request.

func (*API) Open

func (api *API) Open(ctx context.Context, repository Repository, request PullRequest) (Opened, error)

Open opens a pull request.

func (*API) OpenFrom

func (api *API) OpenFrom(ctx context.Context, repository Repository, branch string) (Opened, bool, error)

OpenFrom is the pull request open from a branch of the repository itself, and false when none is.

func (*API) User

func (api *API) User(ctx context.Context, accessToken string) (string, error)

User is the login of the person a token is theirs.

type APIError

type APIError struct {
	Status  int
	Message string
}

APIError is an answer GitHub gave other than success: its status and its message.

func (*APIError) Error

func (failure *APIError) Error() string

type App

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

App makes the installation tokens of one GitHub App, each for one repository and one purpose, kept in memory until shortly before they expire and never written anywhere.

func NewApp

func NewApp(signer *Signer, api *API) *App

NewApp returns the App that signer signs for, calling GitHub's API at api.

func (*App) Identity

func (app *App) Identity(ctx context.Context) (string, string, error)

Identity is who the App's tokens act as: its bot, e.g. `acme-agents[bot]`, and where its installations are managed.

func (*App) Token

func (app *App) Token(ctx context.Context, repository repo.Repo, purpose credentials.Purpose) (string, error)

Token is a token for one repository and one purpose: the one kept, or a new one when the one kept is near its end.

type Created

type Created struct {
	ID      int64  `json:"id"`
	Slug    string `json:"slug"`
	HTMLURL string `json:"html_url"`
	PEM     string `json:"pem"`
	Owner   struct {
		Login string `json:"login"`
	} `json:"owner"`
}

Created is an App GitHub created from a manifest: its ID, its name in addresses, its page, who owns it, and its private key.

type DeviceCode

type DeviceCode struct {
	DeviceCode      string `json:"device_code"`
	UserCode        string `json:"user_code"`
	VerificationURI string `json:"verification_uri"`
	ExpiresIn       int    `json:"expires_in"`
	Interval        int    `json:"interval"`
}

DeviceCode is what a person enters, where, and how to wait for them.

type Edit

type Edit struct {
	Title string `json:"title"`
	Body  string `json:"body"`
}

Edit is what editing a pull request writes over: its title and body.

type KeyLoader

type KeyLoader func() ([]byte, error)

KeyLoader reads an App's private key, in PEM. It is called each time a JWT is signed, so a key replaced where it is kept is used from then on.

type OAuth

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

OAuth signs a person in through a GitHub App by GitHub's device flow, and renews their tokens, with the App's client ID alone: neither takes a secret.

func NewOAuth

func NewOAuth(web, clientID string) *OAuth

NewOAuth returns the device flow of the App whose client ID is clientID, on the GitHub whose pages are at web.

func (*OAuth) Poll

func (oauth *OAuth) Poll(ctx context.Context, deviceCode string) (UserTokens, error)

Poll asks once whether the person entered the code: their tokens when they did, ErrPending or ErrSlowDown while they have not, and another error when they never will.

func (*OAuth) Renew

func (oauth *OAuth) Renew(ctx context.Context, refreshToken string) (UserTokens, error)

Renew exchanges a renewal token for new tokens; the renewal token is replaced too.

func (*OAuth) Start

func (oauth *OAuth) Start(ctx context.Context) (DeviceCode, error)

Start asks GitHub for a code for a person to enter.

type Opened

type Opened struct {
	URL    string `json:"html_url"`
	Number int64  `json:"number"`
}

Opened is a pull request GitHub opened.

type PullRequest

type PullRequest struct {
	Title string `json:"title"`
	Body  string `json:"body"`
	Head  string `json:"head"`
	Base  string `json:"base"`
	Draft bool   `json:"draft"`
}

PullRequest is a pull request to open: from Head into Base.

type Repository

type Repository struct {
	Owner string
	Name  string
	Token string
}

Repository is a repository on GitHub by its owner and name, and the token that reaches it.

type Signer

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

Signer signs the JWTs an App authenticates to GitHub with.

func NewSigner

func NewSigner(appID int64, key KeyLoader) (*Signer, error)

NewSigner returns the signer of an App, checking that its key can be read now.

func (*Signer) JWT

func (signer *Signer) JWT(now time.Time) (string, error)

JWT is a token that says, until it expires, that this App is asking.

type UserTokens

type UserTokens struct {
	AccessToken           string `json:"access_token"`
	ExpiresIn             int    `json:"expires_in"`
	RefreshToken          string `json:"refresh_token"`
	RefreshTokenExpiresIn int    `json:"refresh_token_expires_in"`
	Error                 string `json:"error"`
	ErrorDescription      string `json:"error_description"`
}

UserTokens are a person's tokens: one that works for eight hours, and one that renews it for six months.

func (UserTokens) AccessExpires

func (tokens UserTokens) AccessExpires(now time.Time) time.Time

AccessExpires and RefreshExpires are when the tokens stop working, counted from now.

func (UserTokens) RefreshExpires

func (tokens UserTokens) RefreshExpires(now time.Time) time.Time

Jump to

Keyboard shortcuts

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