github

package
v2.0.0-alpha.202610070... Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package github gives vet a GitHub token and a GitHub API client. A GitHub target is a clone, and the token provider replaces "vet connect" (decisions D8).

Index

Constants

This section is empty.

Variables

View Source
var ErrNoToken = errors.New("github: no token")

ErrNoToken means that no provider has a token. vet then calls GitHub anonymously, with the lower rate limit.

Functions

func IsCommitSHA

func IsCommitSHA(s string) bool

IsCommitSHA reports a full commit SHA of 40 hex characters.

func ListAll

func ListAll[T any](list func(page int) ([]T, *gh.Response, error)) ([]T, error)

ListAll calls list for each page of a GitHub API list, from the first page, and returns the items of all pages.

func NewClient

func NewClient(ctx context.Context, tp TokenProvider, baseURL string, hc *http.Client) (*gh.Client, error)

NewClient returns a GitHub API client with the provider's token. With no token the client is anonymous. A baseURL other than the public API points the client at another server, for example a test stub.

func OriginRepo

func OriginRepo(dir string) string

OriginRepo returns owner/repo of the github.com origin remote of the git repository at dir or above it, or "".

func Reason

func Reason(err error) (reason string, token bool)

Reason returns a short reason for an error of a GitHub API call, as "GitHub API answered 403", and whether a token can fix it. An error that is not of the API keeps its text.

func ReleaseTag

func ReleaseTag(version string) (string, bool)

ReleaseTag returns the release tag of a vet version, or false for a development build, a pseudo-version or a version with build metadata.

func RemoteRepo

func RemoteRepo(u string) string

RemoteRepo returns owner/repo of a github.com remote URL, or "".

func SemverOf

func SemverOf(v string) string

SemverOf returns the version in the form of golang.org/x/mod/semver, with the leading v, or "" for a version that is not valid. A release build sets the version with no v.

func Source

func Source(ctx context.Context, tp TokenProvider) (string, error)

Source names where a provider finds its token: GITHUB_TOKEN, GH_TOKEN or gh auth token. It returns ErrNoToken when no provider has a token.

func WorktreeRoot

func WorktreeRoot(dir string) (string, bool)

WorktreeRoot returns the root of the git working tree at dir or above it, or false when dir is in no git repository.

Types

type Asset

type Asset struct {
	Name      string    `json:"name"`
	UpdatedAt time.Time `json:"updated_at"`
}

Asset is a file of a release.

type ChainProvider

type ChainProvider []TokenProvider

ChainProvider returns the token of the first provider that has one.

func (ChainProvider) Token

func (c ChainProvider) Token(ctx context.Context) (string, error)

Token tries each provider in order.

type Choice

type Choice struct {
	// Major is the major version, such as v2, or "" for each major.
	Major string
	// Prerelease takes a pre-release too.
	Prerelease bool
	// Immutable takes only a release that GitHub marks immutable.
	Immutable bool
	// Cooldown skips a release that is younger. The age counts from the
	// latest of the publish time and the asset update times. A release
	// with a time after Now does not count.
	Cooldown time.Duration
	// Minimum skips a release below this version, or "".
	Minimum string
	Now     time.Time
}

Choice is the rule that picks a release.

func (Choice) Newest

func (c Choice) Newest(releases []Release) (Release, bool)

Newest returns the newest release that the choice takes, or false.

type EnvProvider

type EnvProvider struct {
	LookupEnv func(string) (string, bool)
}

EnvProvider reads GITHUB_TOKEN, then GH_TOKEN.

func (EnvProvider) Token

func (p EnvProvider) Token(context.Context) (string, error)

Token returns the token of the first variable that is set.

type GHProvider

type GHProvider struct {
	// Command replaces the gh binary, for tests.
	Command string
	Timeout time.Duration
}

GHProvider runs "gh auth token".

func (GHProvider) Token

func (p GHProvider) Token(ctx context.Context) (string, error)

Token runs gh. A missing gh or a gh with no login is ErrNoToken.

type Release

type Release struct {
	Tag         string    `json:"tag_name"`
	Draft       bool      `json:"draft"`
	Prerelease  bool      `json:"prerelease"`
	Immutable   bool      `json:"immutable"`
	PublishedAt time.Time `json:"published_at"`
	Assets      []Asset   `json:"assets"`
}

Release is a release of a repository, with the fields that a release choice reads. go-github has no immutable field, so vet decodes the API answer itself.

func ListReleases

func ListReleases(ctx context.Context, client *gh.Client, owner, repo string) ([]Release, error)

ListReleases returns each release of owner/repo. It never reads the latest release of the repository, which can belong to another major version.

type TokenProvider

type TokenProvider interface {
	Token(ctx context.Context) (string, error)
}

TokenProvider returns a GitHub token.

func DefaultProvider

func DefaultProvider() TokenProvider

DefaultProvider reads GITHUB_TOKEN, then GH_TOKEN, then "gh auth token".

Jump to

Keyboard shortcuts

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