github

package
v0.45.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 25 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 creates the authenticated GitHub API client marge uses.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoCallerToken = errors.New("not signed in: this server acts as the person calling it and the request carried no GitHub token. Sign in to the marge MCP server (core_auth_login in muster) and retry")

ErrNoCallerToken is returned when a request carried no bearer token. It is never an invitation to authenticate as something else: a served call belongs to the person who made it, and no request is answered with the App's identity or with a token the process happens to hold.

Functions

func APITransport added in v0.30.1

func APITransport() *http.Transport

APITransport returns the connection pool every GitHub client shares. One pool across the process is what lets a second tool call reuse the connections the first one opened.

func AppWriteAccess added in v0.19.2

func AppWriteAccess(client *github.Client) func(ctx context.Context, owner, repo string) (bool, error)

AppWriteAccess returns a check of whether the App installation behind the client may write to a repository, or nil when the client authenticates with a person's token. A nil result is the signal to keep reading permissions.push from the repository, which is the only answer a user token has.

The check reads the permissions GitHub reports when it mints the token the call would carry, so it describes that token and nothing else. The token is already minted for every repository the sweep touches, so the check costs no extra request.

func AuthenticatedLogin added in v0.19.0

func AuthenticatedLogin(ctx context.Context, client *github.Client) (string, error)

AuthenticatedLogin returns the login the client acts as. An installation token has no user behind it, so GET /user answers 403 and the App's own slug names the bot instead.

The answer comes from the client, not from the environment: a process may hold the App credential and still serve a call with a person's token, and that call is the person's.

func CallerToken added in v0.21.0

func CallerToken(ctx context.Context) string

CallerToken returns the caller's token, or "" when the request carried none.

func CheckRateBudget added in v0.44.2

func CheckRateBudget(ctx context.Context, client *github.Client, needed int) error

CheckRateBudget refuses a run that needs more REST requests than the client's token has left. GitHub does not count the read of the budget against it. A budget that cannot be read refuses nothing: the run then stops where GitHub refuses it, as it would without the check.

func ContextWithBearer added in v0.21.0

func ContextWithBearer(ctx context.Context, r *http.Request) context.Context

ContextWithBearer carries the request's bearer token on ctx. It is the mcp-go HTTP context hook of the streamable transport, so every tool call of a session sees the token that arrived with it.

func ContextWithCallerToken added in v0.21.0

func ContextWithCallerToken(ctx context.Context, token string) context.Context

ContextWithCallerToken carries token on ctx. An empty token is left off, so a later read cannot tell it from no header at all.

func GraphQLURL added in v0.30.1

func GraphQLURL(client *github.Client) (string, error)

GraphQLURL returns the GraphQL endpoint of the API a client talks to. go-github speaks REST only, so the endpoint is derived from the REST root: github.com serves it at /graphql, and GitHub Enterprise Server next to the /api/v3 root it was given.

func ListOpenPRs added in v0.30.1

func ListOpenPRs(ctx context.Context, client *github.Client, refs []RepoRef) ([]OpenPR, []RepoFailure)

ListOpenPRs returns the open pull requests of every repository named, and the repositories it could not read.

One GraphQL request covers reposPerQuery repositories, so a scope of a few hundred repositories costs a handful of requests instead of one per repository. The order of the answer follows the order of refs, so the same scope lists the same way twice.

func LoadToken

func LoadToken(ctx context.Context) string

LoadToken returns the GitHub token from GITHUB_TOKEN, GH_TOKEN or, failing both, the GitHub CLI's own login (`gh auth token`). It returns "" when none of them yields a token.

func NewAppClient added in v0.19.0

func NewAppClient(app *App, baseURL string, httpClient *http.Client) (*github.Client, error)

NewAppClient returns a client that authenticates as the App against baseURL. A nil httpClient uses the default transport. Only the tests pass either argument; NewClient supplies GitHub's own.

func NewCallerClient added in v0.21.0

func NewCallerClient(ctx context.Context) (*github.Client, error)

NewCallerClient returns a GitHub client authenticated with the token the caller presented, so every read and write lands as that person. It reads no environment variable and no App credential: the process may hold both, and either would make a write the bot's rather than the caller's.

func NewClient

func NewClient(ctx context.Context) (*github.Client, error)

NewClient returns a GitHub API client. It authenticates as the sweep App when the environment carries the App credential, and with the token LoadToken finds otherwise.

func RequireBearer added in v0.25.3

func RequireBearer(next http.Handler) http.Handler

RequireBearer refuses a request that carries no bearer token with 401 and a Bearer challenge, before next sees it. Without the challenge muster's connect-time probe, which carries no token, succeeds, and muster then serves the endpoint as a shared unauthenticated server: no sign-in is ever offered and every tool call fails for want of a caller. The 401 is what makes muster hold the server at "Auth Required" until a person completes core_auth_login.

func SharedTransport added in v0.38.1

func SharedTransport() http.RoundTripper

SharedTransport returns the round tripper every GitHub client in the process uses: the pool, behind the rate gate. It is one gate because a refusal for rate is about the token, not about the request, so a sweep that earned one must hold every call it has in flight and not only the one that was refused.

func StopOnRateLimit added in v0.44.2

func StopOnRateLimit(ctx context.Context) (context.Context, context.CancelFunc)

StopOnRateLimit returns a context that is cancelled, with a *RateLimitExhausted as its cause, the first time a request made under it is refused for rate for longer than the transport waits.

func WithWriteBound added in v0.44.7

func WithWriteBound(ctx context.Context, n int) context.Context

WithWriteBound returns a context under which at most n GitHub writes are in flight at once, however many reads run beside them. GitHub answers a burst of writes with a secondary rate limit, so a sweep reads as wide as GitHub answers and holds its writes to the policy's ceiling. The bound is the run's, not the process's: two sweeps under one served process each keep their own.

Types

type App added in v0.19.0

type App struct {
	ID             int64
	InstallationID int64
	// contains filtered or unexported fields
}

App is the sweep GitHub App's credential. It mints installation tokens on demand and holds no token beyond the life of the process.

func LoadApp added in v0.19.0

func LoadApp() (*App, error)

LoadApp returns the App credential the environment carries, or nil when it carries none, which is not an error: the token path serves a person running the CLI. A partial credential is an error, because it is a misconfigured unattended run and not a person's shell.

func (*App) JWT added in v0.19.0

func (a *App) JWT(now time.Time) (string, error)

JWT returns the App JWT that authenticates marge as the App itself, which is what the token mint and GET /app accept.

func (*App) MintToken added in v0.19.0

func (a *App) MintToken(ctx context.Context, httpClient *http.Client, baseURL string, repos []string) (InstallationToken, error)

MintToken returns an installation token for the named repositories. GitHub scopes the token to those repositories alone: every other repository of the installation answers 404 under it.

type InstallationToken added in v0.19.2

type InstallationToken struct {
	Value       string
	Expires     time.Time
	Permissions map[string]string
}

InstallationToken is one minted token, the moment it stops working, and the permissions GitHub reports for it. The permissions are the token's own authority, which is the only trustworthy answer to what an installation may do: GET /repos reports permissions for the authenticated user, and an installation token has no user behind it.

type OpenPR added in v0.30.1

type OpenPR struct {
	Owner     string
	Repo      string
	Number    int
	Title     string
	URL       string
	Author    string
	BaseRef   string
	HeadRef   string
	CreatedAt time.Time
	Labels    []string
}

OpenPR is one open pull request as a repository listing reports it.

type RateBudgetShort added in v0.44.2

type RateBudgetShort struct {
	Remaining int
	Needed    int
	Reset     time.Time
}

RateBudgetShort is a run refused before it started: the token has fewer requests left than the run spends at the least.

func (*RateBudgetShort) Error added in v0.44.2

func (e *RateBudgetShort) Error() string

type RateLimitExhausted added in v0.44.2

type RateLimitExhausted struct {
	Reset time.Time
}

RateLimitExhausted is GitHub refusing a token for rate until Reset: its primary limit is spent, or a secondary limit asks for a wait longer than the transport sits out. Every call on the token is refused until then, so the run that met it stops instead of reading on without evidence.

func AsRateLimit added in v0.44.2

func AsRateLimit(ctx context.Context, err error) (*RateLimitExhausted, bool)

AsRateLimit reports whether the run under ctx, or the error it returned, ended on a token refused for rate, and until when.

func (*RateLimitExhausted) Error added in v0.44.2

func (e *RateLimitExhausted) Error() string

type RepoFailure added in v0.30.1

type RepoFailure struct {
	Repo string
	Err  string
}

RepoFailure names a repository whose pull requests could not be listed, and why. A repository that fails to list is reported, never silently dropped from a sweep.

type RepoRef added in v0.30.1

type RepoRef struct {
	Owner string
	Name  string
}

RepoRef is one repository to list, as owner and name.

func ParseRepoRefs added in v0.30.1

func ParseRepoRefs(repos []string) []RepoRef

ParseRepoRefs reads "owner/name" entries. An entry that does not name both is dropped, exactly as the repository list of a team file is read today.

func (RepoRef) String added in v0.30.1

func (r RepoRef) String() string

Jump to

Keyboard shortcuts

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