github

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 16 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 talks to the GitHub REST API and resolves how an organization was named on the command line.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultAPIBase added in v1.1.0

func DefaultAPIBase(host string) string

DefaultAPIBase returns the REST API base URL for a host.

func DefaultProtocol

func DefaultProtocol(host string) string

DefaultProtocol returns "ssh" when the gh CLI is configured for SSH on host, otherwise "https".

func FindToken

func FindToken(host string) (token, source string)

FindToken locates a token for host: environment variables first (matching the gh CLI's own precedence), then gh's stored credentials. The returned source is a short label for display, e.g. "$GITHUB_TOKEN" or "gh auth".

Tokens are strictly scoped to the host they belong to: GH_TOKEN and GITHUB_TOKEN authenticate github.com only, GH_ENTERPRISE_TOKEN and GITHUB_ENTERPRISE_TOKEN authenticate everything else. A github.com token is never sent to another host — a typo'd or hostile host name must not receive it. gh's stored credentials are already looked up per host.

func NormalizeHost added in v1.2.0

func NormalizeHost(host string) string

NormalizeHost canonicalizes a host name so that spellings which resolve to the same GitHub host take the same code path. Every host decision keys off this string — which token applies (FindToken) and which API base is used (DefaultAPIBase) — so an un-normalized "www.github.com" would be treated as a GitHub Enterprise host: the wrong API base, and a github.com token sent to a host the docs promise it never reaches.

Types

type APIError

type APIError struct {
	Status      int
	Message     string
	RateLimited bool
}

APIError is a non-2xx response from the API.

func (*APIError) Error

func (e *APIError) Error() string

type Client

type Client struct {
	Host    string
	APIBase string
	Token   string
	HTTP    *http.Client
	// contains filtered or unexported fields
}

Client is a minimal GitHub REST client.

func NewClient

func NewClient(host, token string) *Client

NewClient returns a client for host. The API base URL can be overridden with GITOPS_GITHUB_API (used by tests and trusted API proxies). The token is sent as a Bearer header to whatever the base names, so the override must be https — or http on localhost only — and is otherwise ignored.

func (*Client) CurrentUser

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

CurrentUser returns the login of the authenticated user, or "" without a token.

func (*Client) ListRepos

func (c *Client) ListRepos(ctx context.Context, owner Owner, progress func(page, total int)) ([]Repo, error)

ListRepos returns every repository of owner visible to the caller, sorted by name. progress, if non-nil, is called after each page.

func (*Client) LookupOwner

func (c *Client) LookupOwner(ctx context.Context, login string) (Owner, error)

LookupOwner resolves a login to its canonical spelling and account type.

func (*Client) LookupRepo added in v1.2.0

func (c *Client) LookupRepo(ctx context.Context, owner, name string) (Repo, error)

LookupRepo fetches one repository by name. It exists to answer a question the org listing cannot: whether a repository the listing omitted is really gone. A repository that was renamed or transferred still resolves — the API redirects to its current location — so the returned FullName distinguishes a rename from a deletion, and a 404 is the only thing that means "gone".

The returned Repo is display-only: FullName may name a different owner after a transfer, so it is not passed through sanitizeRepo (which rebuilds URLs from an assumed owner) and its clone URLs are cleared rather than trusted.

func (*Client) Overridden added in v1.1.0

func (c *Client) Overridden() bool

Overridden reports whether the API base came from GITOPS_GITHUB_API. When it is set, the token is sent to that endpoint and its repository URLs are taken at face value, so callers should make the override visible.

type Owner

type Owner struct {
	Login string `json:"login"`
	Type  string `json:"type"` // "Organization" or "User"
}

Owner is the resolved account as reported by the API.

func (Owner) IsOrg

func (o Owner) IsOrg() bool

IsOrg reports whether the account is an organization.

type OwnerRef

type OwnerRef struct {
	Host  string
	Owner string
}

OwnerRef is an organization or user account on a GitHub host.

func ParseOwner

func ParseOwner(input string) (OwnerRef, error)

ParseOwner accepts the many ways people write an org: a bare login ("acme"), "github.com/acme", any https://github.com/acme[/...] URL (including /orgs/acme), an SSH remote ("git@github.com:acme/repo.git"), or a GitHub Enterprise host ("https://ghe.example.com/acme").

func (OwnerRef) URL

func (o OwnerRef) URL() string

URL returns the browsable URL of the owner, e.g. https://github.com/acme.

type Repo

type Repo struct {
	Name          string `json:"name"`
	FullName      string `json:"full_name"`
	Private       bool   `json:"private"`
	Archived      bool   `json:"archived"`
	Fork          bool   `json:"fork"`
	DefaultBranch string `json:"default_branch"`
	CloneURL      string `json:"clone_url"`
	SSHURL        string `json:"ssh_url"`
}

Repo is the subset of the repository object gitops uses.

func (Repo) RemoteURL

func (r Repo) RemoteURL(host, protocol string) string

RemoteURL picks the URL to clone from for the requested protocol ("ssh" or "https").

Directories

Path Synopsis
Package githubtest provides a fake GitHub API server for tests.
Package githubtest provides a fake GitHub API server for tests.

Jump to

Keyboard shortcuts

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