github

package
v0.3.0-rc11 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 24 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't renew itself")

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 = fmt.Errorf("%w: no credential available", ErrAuthentication)

ErrNoCredentials permits anonymous public reads; invalid or inaccessible credentials do not.

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

ErrNotGitHub is an address somewhere other than github.com.

View Source
var LoginLock = func() string {
	home, err := os.UserHomeDir()
	if err != nil {
		return filepath.Join(os.TempDir(), "dockhand-github-login.lock")
	}
	return filepath.Join(home, ".dockhand", "github-login.lock")
}()

LoginLock is the file processes renewing dockhand's login take in turn, beside its configuration: ~/.dockhand/github-login.lock. The command line sets it where its configuration is elsewhere.

Functions

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

RateLimitError says a refusal of go-github's own, from GitHub's rate limit, as forge.RateLimitError, with when it lifts. The transport refuses with one already (rateLimited); what reaches here is a refusal it couldn't read from GitHub's headers, as a secondary limit's 403 that names no wait.

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 Requests

func Requests() int64

Requests is how many requests this process has sent GitHub's API.

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 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) 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
	// contains filtered or unexported fields
}

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

func SystemClient

func SystemClient(store credential.Store) *Client

SystemClient is GitHub as the person's login reaches it: GH_TOKEN or GITHUB_TOKEN where set, else the login dockhand keeps in the store. 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) Authenticate

func (c *Client) Authenticate(ctx context.Context) error

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.

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

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 SystemIdentity

func SystemIdentity(credentials SystemCredentials) *IdentityWatcher

SystemIdentity watches the login SystemClient uses.

func (*IdentityWatcher) Current

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

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 SystemCredentials

type SystemCredentials struct {
	Store credential.Store
	Key   credential.Key
	// Lock is the file processes renewing the login take in turn;
	// LoginLock where empty.
	Lock 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