github

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 19 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 lists a repository's releases and tags and fetches release bodies. With a token it uses batched GraphQL queries; without one it falls back to the REST API.

Index

Examples

Constants

View Source
const (
	// RecentWindow is how many of the newest releases or tags [Client.Recent] reads per repo.
	RecentWindow = 30
	// RecentBatchMin is the fewest repositories a GraphQL query of [Client.Recent] asks about
	// while more remain, so a small project still sends one query.
	RecentBatchMin = 10
	// RecentBatchMax is the most repositories one GraphQL query of [Client.Recent] asks about.
	RecentBatchMax = 25
)
View Source
const (
	SourceGHToken     = "GH_TOKEN"
	SourceGitHubToken = "GITHUB_TOKEN" //nolint:gosec // an environment variable name, not a credential
	SourceGHConfig    = "gh config"
	SourceGHKeyring   = "gh auth token"
)

Token sources reported by Token.

View Source
const DefaultHost = "github.com"

DefaultHost is the GitHub host whose token relgap looks up.

Variables

View Source
var (
	ErrAuth     = errors.New("github rejected the token")
	ErrNotFound = errors.New("not found on github")
)

Errors returned by Client methods.

Functions

func GHConfigToken

func GHConfigToken(host string) string

GHConfigToken returns the oauth_token for host in gh's hosts.yml, or "".

func NewestRelease

func NewestRelease(_ *http.Request, body []byte) time.Time

NewestRelease returns the newest release publish date in a REST or GraphQL response body, or the zero time when the body has none. It is an httpx.AgeFunc.

func Token

func Token(ctx context.Context, o TokenOptions) (string, string)

Token finds a GitHub token the way gh does: GH_TOKEN, GITHUB_TOKEN, gh's config file, then gh's keyring through `gh auth token`. It returns "" when there is none, and the source otherwise.

Example
package main

import (
	"context"
	"fmt"

	"github.com/sassdavid/relgap/internal/github"
)

func main() {
	token, source := github.Token(context.Background(), github.TokenOptions{
		Lookup: func(k string) (string, bool) {
			return map[string]string{"GITHUB_TOKEN": "t0ken"}[k], k == "GITHUB_TOKEN"
		},
	})
	fmt.Println(len(token), source)
}
Output:
5 GITHUB_TOKEN

Types

type Client

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

Client reads releases and tags of public or token-readable repositories.

func New

func New(hc *http.Client, o Options) (*Client, error)

New returns a client that sends requests through hc and authenticates with o.Token.

func (*Client) Authenticated

func (c *Client) Authenticated() bool

Authenticated reports whether the client has a token and uses GraphQL.

func (*Client) Dir added in v0.4.0

func (c *Client) Dir(ctx context.Context, repo, ref, dir string) ([]string, error)

Dir returns the names of the files, not directories, in dir of repo at ref.

func (*Client) File

func (c *Client) File(ctx context.Context, repo, ref, path string) ([]byte, error)

File returns the content of path in repo at ref (empty means the default branch) through the contents API with the raw media type, which works for files over 1 MB.

func (*Client) Recent

func (c *Client) Recent(ctx context.Context, qs []RecentQuery) (map[RecentQuery]Recent, error)

Recent returns the newest releases or tags of every repository, keyed by query, so a repository asked for both releases and tags gets both. With a token it sends GraphQL queries of RecentBatchMin to RecentBatchMax repositories, as many at once as the client's concurrency allows; without one it makes REST calls per repo. The error is set only when nothing could be asked; per-repo failures are in Recent.Err.

func (*Client) ReleaseBodies

func (c *Client) ReleaseBodies(ctx context.Context, repo string, tags []string) (map[string]Release, error)

ReleaseBodies returns the releases of repo for tags, keyed by tag. Tags without a release are left out.

func (*Client) Releases

func (c *Client) Releases(ctx context.Context, repo string) ([]Release, error)

Releases lists every release of repo ("owner/name"), drafts included, newest first.

func (*Client) RootFiles

func (c *Client) RootFiles(ctx context.Context, repo string) ([]File, error)

RootFiles lists the top directory of repo on its default branch through the contents API.

func (*Client) SearchRepos added in v0.4.0

func (c *Client) SearchRepos(ctx context.Context, name string) ([]Repo, error)

SearchRepos returns up to 30 repositories whose name contains name, most stars first.

func (*Client) Tags

func (c *Client) Tags(ctx context.Context, repo string) ([]string, error)

Tags lists every tag name of repo.

type File

type File struct {
	Name string `json:"name"`
	Size int    `json:"size"` // -1 when the host doesn't say
	Dir  bool   `json:"dir"`
}

File is an entry of a repository directory listing.

type Options

type Options struct {
	Token       string // empty means unauthenticated REST calls
	Logger      *slog.Logger
	BatchSize   int // release bodies per GraphQL query; default 10
	Concurrency int // body queries in flight; default 8
}

Options configures New.

type Recent

type Recent struct {
	Releases []Release // newest first, at most [RecentWindow]
	Err      error
}

Recent is the answer for one repository. Tag-only answers have only Release.Tag set.

type RecentQuery

type RecentQuery struct {
	Repo string // owner/name
	Tags bool
}

RecentQuery asks for the newest releases of Repo, or its newest tags when Tags is set.

type Release

type Release struct {
	Tag         string
	Draft       bool
	Prerelease  bool
	PublishedAt time.Time
	URL         string
	Body        string
}

Release is a GitHub release. Body is empty in Client.Releases results when the client uses GraphQL; Client.ReleaseBodies fills it.

type Repo added in v0.4.0

type Repo struct {
	FullName string
	Stars    int
}

Repo is one repository of a search result.

type Runner

type Runner interface {
	Run(ctx context.Context, c execx.Cmd) (*execx.Result, error)
}

Runner starts a command; execx.Runner implements it.

type TokenOptions

type TokenOptions struct {
	Host        string                      // default [DefaultHost]
	Lookup      func(string) (string, bool) // environment
	ConfigToken func(host string) string    // nil skips gh's config file
	Runner      Runner                      // runs `gh auth token`; nil skips it
}

TokenOptions says where Token looks.

Jump to

Keyboard shortcuts

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