github

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 31 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 supplies the GitHub client and authentication shared by forge and verification adapters.

It selects credentials, constructs go-github clients lazily, implements device authorization and identity checks, and applies redirect and rate-limit handling. The adapters own repository, pull-request, and Actions operations; callers own credential persistence and user interaction.

It also reads and writes GitHub's addresses: the repository a Git remote or a web page names, a repository's remote, and a pull request's pages.

Index

Constants

This section is empty.

Variables

View Source
var CredentialKey = credential.Key{Service: "github.com/herbygillot/dockhand", Account: "github.com"}

CredentialKey is where dockhand keeps its GitHub login in the system credential store.

View Source
var DefaultOAuthClientID = "Ov23lixRrrqO0uEzU2GA"

DefaultOAuthClientID is the public client ID of dockhand's GitHub OAuth application, which has the device flow enabled. No client secret exists. The Makefile's GITHUB_OAUTH_CLIENT_ID replaces it at link time for development and alternate registrations.

View Source
var ErrAuthentication = forge.ErrAuthentication
View Source
var ErrLoginEnded = errors.New("github: the saved login can no longer be renewed")

errOldLogin and errExpiredLogin are a saved login that can't renew itself: one an earlier dockhand kept, a bare token, or one unused six months. ErrLoginEnded is a saved login that can't renew itself any more, which only dockhand setup github answers: serve says it once, rather than a failure for each pull request.

View Source
var ErrNoCredentials error = refusal.Wrap(refusal.Auth, ErrAuthentication, "no GitHub login was found; dockhand setup github signs in").
	Then(refusal.Next{Verb: refusal.VerbSetup, Args: []string{"github"}})

ErrNoCredentials permits anonymous public reads; invalid or inaccessible credentials do not. It says what's missing and the way on, where serve's line read "forge: authentication is required" (D-C3 on rc8).

View Source
var ErrNotGitHub = errors.New("github: not a github.com address")

ErrNotGitHub is an address somewhere other than github.com.

Functions

func LockedLogin added in v0.6.0

func LockedLogin(locked error) *refusal.Refusal

LockedLogin is the refusal of a login, or a sign-in, while the login Keychain that keeps dockhand's login is locked: the way on is to unlock it, never to sign in again, which couldn't be saved. It wraps the store's error, credential.ErrLocked.

func Login added in v0.5.0

Login signs in to GitHub with flow's one-time code for the OAuth app clientID, and keeps the login in store. present shows the person the code and where to enter it, which is only ever GitHub's address over https; nothing is kept where the sign-in doesn't finish.

func Logout added in v0.5.0

func Logout(ctx context.Context, store LoginStore) (revoke string, err error)

Logout removes dockhand's login from store, and says where at GitHub the access its OAuth app was given is revoked (RevocationPage): the saved login's app, else dockhand's. It's store's ErrNotFound where store keeps none.

func PageRepository

func PageRepository(address string) (string, error)

PageRepository is the owner/name of the repository a github.com page belongs to, as a person copies its address from a browser: with or without its scheme, on www.github.com too, and perhaps a page beneath the repository's own, such as https://github.com/owner/name/releases.

func PullRequestChecksURL

func PullRequestChecksURL(repository string, number int) string

PullRequestChecksURL is the page of a pull request's checks on github.com.

func PullRequestURL

func PullRequestURL(repository string, number int) string

PullRequestURL is a pull request's page on github.com.

func RateLimitError

func RateLimitError(err error) error

func Remote

func Remote(repository string, ssh bool) string

Remote is a repository's Git address on github.com: over SSH, git@github.com:owner/name.git, or else HTTPS, https://github.com/owner/name.git.

func RemoteRepository

func RemoteRepository(remote string) (string, error)

RemoteRepository is the owner/name of the github.com repository a Git remote's URL names, over HTTPS or SSH: https://github.com/owner/name, ssh://git@github.com/owner/name, or git@github.com:owner/name, each perhaps ending in .git or a slash. It names the repository exactly: nothing beneath it, no query, and no credentials in an HTTPS address.

func RevocationPage

func RevocationPage(clientID string) string

RevocationPage is where the person revokes the login an OAuth app was given, at GitHub.

func SavedLogin

func SavedLogin(ctx context.Context, store credential.Store) (credential.Login, error)

SavedLogin is the login the store keeps, as the person sees it: whose, and until when it renews itself; ErrLoginEnded, wrapped, where it's from an earlier dockhand or expired, and the store's ErrNotFound where it keeps none. It asks GitHub nothing.

func Used

func Used() int64

Used is the last X-RateLimit-Used GitHub answered this process with.

func ValidRepositoryName

func ValidRepositoryName(value string) bool

ValidRepositoryName reports whether value is a repository's owner/name, each in the characters GitHub allows.

Types

type Allowance added in v0.5.0

type Allowance struct {
	Remaining, Limit int
	// Reset is when the hour's count starts again.
	Reset time.Time
}

Allowance is what's left of the hour's requests to GitHub's API for the runtime's login, or for requests without one, as GitHub says it.

type CLI

type CLI struct {
	// Path is gh's; found on the PATH when empty.
	Path string
}

CLI is the GitHub CLI, gh, which acts on GitHub as its own app with its own login, which dockhand doesn't read here. An organization that refuses dockhand's app may take it (D8). Only its documented commands are run.

func (CLI) Login

func (c CLI) Login(ctx context.Context) (string, error)

Login is the account gh is signed in as on github.com.

func (CLI) MarkDraft added in v0.7.0

func (c CLI) MarkDraft(ctx context.Context, ref forge.PullRequestRef) error

MarkDraft returns a pull request to draft, with gh pr ready --undo.

func (CLI) MarkReady

func (c CLI) MarkReady(ctx context.Context, ref forge.PullRequestRef) error

MarkReady takes a draft pull request out of draft, with gh pr ready.

type Client

type Client struct {
	HTTP        *http.Client
	Config      Config
	Credentials TokenSource
	// Conditional keeps reads' answers with their ETags, for reads asked
	// again to cost nothing where nothing moved; nil keeps none.
	Conditional *ConditionalCache
	// contains filtered or unexported fields
}

Configure a Client before its first API operation; it is then safe to share.

func SystemClient

func SystemClient(signin Signin) *Client

SystemClient is GitHub as the person's login reaches it (Signin). It is the one way dockhand makes that client.

func (*Client) API

func (c *Client) API(ctx context.Context) (*gh.Client, error)

API uses available credentials for public reads, allowing anonymous reads only when none exist.

func (*Client) Allowance added in v0.6.0

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

Allowance asks GitHub what's left of the hour's requests for the client's login, or for requests without one (Runtime.Allowance).

func (*Client) AuthenticatedAPI

func (c *Client) AuthenticatedAPI(ctx context.Context) (*gh.Client, error)

AuthenticatedAPI shares one initialized credential and SDK client between adapters.

func (*Client) AuthenticatedUser

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

func (*Client) CredentialSource

func (c *Client) CredentialSource() CredentialSource

CredentialSource is where the token the client last carried came from; empty before it has asked for one.

func (*Client) LoginStatus added in v0.4.0

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

LoginStatus asks GitHub whose the client's token is, and says where the token came from; ErrNoCredentials where there is none.

type ConditionalCache added in v0.6.0

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

ConditionalCache keeps GitHub's answers to reads with their ETags, so a read asked again goes with If-None-Match, and GitHub's 304 for one that hasn't moved, which it doesn't count against the hour's allowance, is answered with what was kept (Then's item 5): the M1's run at 1da4fdbf spent 2,166 requests on one outdated --mine of 835 ports, and a second look spent as many. It's kept on disk where a directory is given, so the next command's look is nearly free too, and in the process only where the disk won't take it: serve kept every look's answers in memory, about 30 MB of a 37 MB heap after a look of 1,116 ports (the roadmap's "serve gives back a look's memory once its work ends"). Each answer is kept by the request's address, what it accepts, and a digest of its credential, so one login's answers are never another's.

func NewConditionalCache added in v0.6.0

func NewConditionalCache(directory string) *ConditionalCache

NewConditionalCache keeps answers in the process, and in directory where it isn't empty.

type Config

type Config struct {
	BaseURL string
	Token   string
	// CloneURL is where repositories are read with plain git, when the API
	// cannot answer; empty is https://github.com.
	CloneURL string
}

type CredentialSource

type CredentialSource string
const (
	SourceExplicit          CredentialSource = "explicit credential"
	SourceGHEnvironment     CredentialSource = "GH_TOKEN"
	SourceGitHubEnvironment CredentialSource = "GITHUB_TOKEN"
	SourceKeychain          CredentialSource = "Dockhand macOS Keychain"
	SourceGitHubCLI         CredentialSource = "GitHub CLI"
)
const LoginLocked CredentialSource = "the locked login Keychain"

LoginLocked is LoginSource's word for a login the Keychain keeps but can't give now, locked, as over SSH: setup read it as no login, offered one, and named dockhand setup github, while its Publishing line said the Keychain was locked (F5 on rc5).

func LoginSource added in v0.5.0

func LoginSource(ctx context.Context, signin Signin) CredentialSource

LoginSource is where a login comes from, as dockhand tells without asking GitHub: the token that overrides dockhand's login, else the store, where it keeps a login that hasn't ended (SavedLogin), or LoginLocked where the store keeps one it can't give now; else empty, which setup offers to fix.

func (CredentialSource) Words added in v0.4.0

func (s CredentialSource) Words() string

Words say where a token comes from, as a person reads it: "GH_TOKEN", "dockhand's login in the Keychain", "the GitHub CLI's login".

type DeviceFlow

type DeviceFlow struct {
	HTTP       *http.Client
	Endpoint   oauth2.Endpoint
	APIBaseURL string
	// Now is the clock a refresh token's expiry is reckoned by; time.Now
	// where nil.
	Now func() time.Time
}

DeviceFlow signs in through GitHub's device flow. HTTP is fetch.Client when nil, whose wait for a response is bounded.

func (*DeviceFlow) Authorize

func (f *DeviceFlow) Authorize(ctx context.Context, clientID string, present func(credential.DeviceAuthorization) error) (credential.Login, error)

type Identity

type Identity struct {
	Account string
	Source  CredentialSource
}

Identity is who the person's GitHub login acts as now, and where its token comes from.

type IdentityWatcher

type IdentityWatcher struct {
	Credentials TokenSource
	HTTP        *http.Client
	BaseURL     string
	// Recheck is how long an account read stands for the same token; ten
	// minutes when zero.
	Recheck time.Duration
	// contains filtered or unexported fields
}

IdentityWatcher says who the login acts as each time it's asked, reading the credential chain afresh, as a client holding a token doesn't: a long-running serve kept acting with a token after a logout, and could have gone on with the GitHub CLI's, another account's, once it expired (the rc6 full stage, D-C2). The account is asked of GitHub when the token changes, and again each Recheck, so a token GitHub stopped honouring, as a revoked login's, is found: its error is then ErrAuthentication's. With no credential at all it returns ErrNoCredentials.

func (*IdentityWatcher) Current

func (w *IdentityWatcher) Current(ctx context.Context) (Identity, error)

type LoginStatus added in v0.4.0

type LoginStatus struct {
	Identity
	RenewsUntil time.Time
}

LoginStatus is who a client acts as, where its token comes from, and, for dockhand's own login in the Keychain, until when it renews itself unused (zero for any other): what setup and submit's preview say of the login (the rc9 full stage's D-C4, where GH_TOKEN held another account's token and the preview named none).

type LoginStore added in v0.5.0

type LoginStore interface {
	credential.Store
	credential.Remover
}

LoginStore is where dockhand's login is kept, which logging out removes it from.

type Renewer

type Renewer interface {
	Renew(ctx context.Context, rejected Token) (Token, error)
}

Renewer is a TokenSource that, told the token GitHub rejected, can find another: a login it can refresh. A source that isn't one is asked again as it was, which finds a login saved since.

type Runtime added in v0.5.0

type Runtime struct {
	Client *Client
}

Runtime is GitHub as one process reaches it: one client, its login, and the rate limits GitHub's answers report, which the forge, upstream discovery, the source archiver, project inspection, Actions, and setup's login line all share, where each built a client of its own (the architecture review of 5897a82, CX-11). It's built once, where the process is composed.

func NewRuntime added in v0.5.0

func NewRuntime(signin Signin) *Runtime

NewRuntime is GitHub as the person's login reaches it (SystemClient).

func (*Runtime) AccountChanged added in v0.5.0

func (r *Runtime) AccountChanged()

AccountChanged lets go of the login the client holds, and the API client carrying it, so its next use reads the login as it is now: serve's, when the login changed under it, in one place for everything that shares the client.

func (*Runtime) Allowance added in v0.5.0

func (r *Runtime) Allowance(ctx context.Context) (Allowance, error)

Allowance asks GitHub what's left of the hour's requests, which its rate_limit endpoint answers without counting against it, for a look to weigh its estimate against what other sessions sharing the login have spent.

func (*Runtime) Identity added in v0.5.0

func (r *Runtime) Identity() *IdentityWatcher

Identity watches who the runtime's login acts as, from the client's own credentials, so a renewal the store didn't take is the watcher's too.

type Signin added in v0.5.0

type Signin struct {
	Store credential.Store
	Token Token
	Lock  string
	CLI   string
}

Signin is how the person's GitHub login is reached: a token that overrides dockhand's own login, such as GH_TOKEN's, where one is given; else the login kept in Store, which processes renewing it take the file at Lock in turn for; else the GitHub CLI's, at CLI, where it's installed. compose reads them from the command line's environment.

type Spend added in v0.4.0

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

Spend counts the requests a span of this process sends GitHub's API: a command's, for its -v line and its envelope (the M1's run at 10aac0c3).

func StartSpend added in v0.4.0

func StartSpend() Spend

StartSpend starts counting.

func (Spend) Log added in v0.4.0

func (s Spend) Log(path string, args []string)

Log appends a command's line to the spend log at path, $DOCKHAND_GITHUB_LOG, where there's one, --json or not: how many requests it sent GitHub's API, how much of the hour's allowance GitHub last said was spent, what it was asked, and the first of the requests the cache didn't answer, tab-separated, so a run can say which command spent it, and on what. One that can't be written is skipped, as a log is.

func (Spend) Requests added in v0.4.0

func (s Spend) Requests() int64

Requests is how many requests sent since StartSpend counted against the hour's allowance: those GitHub answered 304, as nothing moved since an answer kept, count nothing (ConditionalCache).

type SystemCredentials

type SystemCredentials struct {
	Store credential.Store
	Key   credential.Key
	// Override is a token that takes precedence over the login in Store
	// (Signin.Token), where it has a secret.
	Override Token
	// Lock is the file processes renewing the login take in turn; a
	// renewal is refused without one.
	Lock string
	// CLI is the GitHub CLI, whose login is read last; none where empty.
	CLI string
	// Flow renews the login; GitHub's device flow where nil.
	Flow *DeviceFlow
	// contains filtered or unexported fields
}

func (SystemCredentials) Renew

func (s SystemCredentials) Renew(ctx context.Context, rejected Token) (Token, error)

Renew is a token after GitHub rejected one: the login renewed, where the rejected token was its own, else whatever Token gives now.

func (SystemCredentials) Token

func (s SystemCredentials) Token(ctx context.Context) (Token, error)

func (SystemCredentials) WithRenewal

func (s SystemCredentials) WithRenewal(flow *DeviceFlow, path string) SystemCredentials

WithRenewal is the credentials renewing their login through flow, under the lock at path, for tests and other GitHubs; flow's endpoint is GitHub's where it's empty.

type Token

type Token struct {
	Secret string           `json:"-"`
	Source CredentialSource `json:"source"`
	// Expiry is when the token stops working; zero for one that doesn't
	// expire, or whose source doesn't say.
	Expiry time.Time `json:"-"`
}

type TokenSource

type TokenSource interface {
	Token(context.Context) (Token, error)
}

type TokenSourceFunc

type TokenSourceFunc func(context.Context) (Token, error)

TokenSourceFunc is a TokenSource written as a function, for tests that script a credential.

func (TokenSourceFunc) Token

func (f TokenSourceFunc) Token(ctx context.Context) (Token, error)

Jump to

Keyboard shortcuts

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