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 ¶
- Constants
- Variables
- func GHConfigToken(host string) string
- func NewestRelease(_ *http.Request, body []byte) time.Time
- func Token(ctx context.Context, o TokenOptions) (string, string)
- type Client
- func (c *Client) Authenticated() bool
- func (c *Client) Dir(ctx context.Context, repo, ref, dir string) ([]string, error)
- func (c *Client) File(ctx context.Context, repo, ref, path string) ([]byte, error)
- func (c *Client) Recent(ctx context.Context, qs []RecentQuery) (map[RecentQuery]Recent, error)
- func (c *Client) ReleaseBodies(ctx context.Context, repo string, tags []string) (map[string]Release, error)
- func (c *Client) Releases(ctx context.Context, repo string) ([]Release, error)
- func (c *Client) RootFiles(ctx context.Context, repo string) ([]File, error)
- func (c *Client) SearchRepos(ctx context.Context, name string) ([]Repo, error)
- func (c *Client) Tags(ctx context.Context, repo string) ([]string, error)
- type File
- type Options
- type Recent
- type RecentQuery
- type Release
- type Repo
- type Runner
- type TokenOptions
Examples ¶
Constants ¶
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 )
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.
const DefaultHost = "github.com"
DefaultHost is the GitHub host whose token relgap looks up.
Variables ¶
var ( ErrAuth = errors.New("github rejected the token") ErrNotFound = errors.New("not found on github") )
Errors returned by Client methods.
Functions ¶
func GHConfigToken ¶
GHConfigToken returns the oauth_token for host in gh's hosts.yml, or "".
func NewestRelease ¶
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 (*Client) Authenticated ¶
Authenticated reports whether the client has a token and uses GraphQL.
func (*Client) Dir ¶ added in v0.4.0
Dir returns the names of the files, not directories, in dir of repo at ref.
func (*Client) File ¶
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 ¶
Releases lists every release of repo ("owner/name"), drafts included, newest first.
func (*Client) RootFiles ¶
RootFiles lists the top directory of repo on its default branch through the contents API.
func (*Client) SearchRepos ¶ added in v0.4.0
SearchRepos returns up to 30 repositories whose name contains name, most stars first.
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 RecentQuery ¶
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 Runner ¶
Runner starts a command; execx.Runner implements it.