Documentation
¶
Overview ¶
Package github provides functionality to interact with the GitHub API.
Index ¶
- func Token(ctx context.Context, authMode AuthMode) string
- type AuthMode
- type FetchOptions
- type Repo
- func FetchRepos(ctx context.Context, owner string, limit int) ([]Repo, error)
- func FetchReposWithClient(ctx context.Context, client *gh.Client, owner string, limit int) ([]Repo, error)
- func FetchReposWithClientOptions(ctx context.Context, client *gh.Client, owner string, opts FetchOptions) ([]Repo, error)
- func FetchReposWithOptions(ctx context.Context, owner string, opts FetchOptions) ([]Repo, error)
- type RetryBudgetError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AuthMode ¶ added in v0.0.3
type AuthMode string
AuthMode controls how GitHub API credentials are resolved.
const ( // AuthModeAuto resolves credentials from the environment first, then the gh CLI. AuthModeAuto AuthMode = "auto" // AuthModeToken resolves credentials only from environment variables. AuthModeToken AuthMode = "token" // AuthModeGH resolves credentials only via the gh CLI (`gh auth token`). AuthModeGH AuthMode = "gh" )
type FetchOptions ¶ added in v0.0.3
type FetchOptions struct {
// Limit caps the number of repositories returned; 0 means no limit.
Limit int
// Visibility filters repositories by visibility ("all", "public", or "private").
Visibility string
// IncludeForks includes forked repositories when true.
IncludeForks bool
// IncludeArchived includes archived repositories when true.
IncludeArchived bool
// IncludeLanguages, when non-empty, keeps only repositories matching these languages.
IncludeLanguages []string
// ExcludeLanguages removes repositories matching these languages.
ExcludeLanguages []string
// AuthMode selects how the GitHub token is resolved.
AuthMode AuthMode
// Type filters repositories by specific category (e.g. "sources", "forks", "archived", "mirrors", etc.).
Type string
// Sort specifies how the returned repositories list should be ordered.
Sort string
// RetryMax is the maximum number of retry attempts for transient failures.
RetryMax int
// RetryMinBackoff is the minimum delay between retry attempts.
RetryMinBackoff time.Duration
// RetryMaxBackoff is the maximum delay between retry attempts.
RetryMaxBackoff time.Duration
// RequestTimeout bounds a single HTTP request to the GitHub API,
// including its redirects and body read. It does not bound the
// operation as a whole: listing a large organisation is many requests.
RequestTimeout time.Duration
// TotalTimeout bounds the complete operation — every page, every retry
// and every backoff between them. It must be at least RequestTimeout,
// and wants to be far larger: an organisation with 5,000 repositories
// is 50 pages at 100 per page, and a rate-limited run spends most of
// its budget waiting rather than transferring.
TotalTimeout time.Duration
// Timeout is the pre-v0.0.29 single knob, which was applied as both of
// the above at once. Kept so an embedder compiled against the old
// field keeps working: when it is set and the two fields above are
// not, it supplies both.
//
// Deprecated: set RequestTimeout and TotalTimeout instead.
Timeout time.Duration
}
FetchOptions configures repository fetch behavior.
type Repo ¶
type Repo struct {
// ID is GitHub's immutable repository identifier.
ID int64
// Owner is the repository owner's login.
Owner string
// FullName is the canonical owner/name identity.
FullName string
// Name is the repository name (without the owner prefix).
Name string
// Language is the primary programming language, or "Other" when unknown.
Language string
// Visibility is the normalized visibility, either "Public" or "Private".
Visibility string
// DefaultBranch is the repository's default branch name.
DefaultBranch string
// CloneURL is the HTTPS clone URL for the repository.
CloneURL string
// SSHURL is the SSH clone URL for the repository.
SSHURL string
// Fork reports whether the repository is a fork.
Fork bool
// Archived reports whether the repository is archived.
Archived bool
// PushedAt is the timestamp of the last push to any branch. The engine
// compares this against the cached value in <repo>/.corral-state.json to
// skip a `git pull` when nothing has changed upstream.
PushedAt time.Time
// Stars reports the stargazers count for the repository.
Stars int
// IsTemplate reports whether the repository is a template.
IsTemplate bool
// IsMirror reports whether the repository is a mirror.
IsMirror bool
// CanBeSponsored reports whether the repository has sponsorships enabled.
CanBeSponsored bool
}
Repo represents a simplified repository structure returned by the GitHub API.
func FetchRepos ¶
FetchRepos retrieves repositories for a given owner up to the specified limit.
Example ¶
ExampleFetchRepos demonstrates the simplest fetch: every repository for an owner, resolving credentials automatically from the environment or gh CLI.
package main
import (
"context"
"fmt"
"log"
"github.com/sebastienrousseau/corral/internal/github"
)
func main() {
ctx := context.Background()
repos, err := github.FetchRepos(ctx, "sebastienrousseau", 1000)
if err != nil {
log.Fatal(err)
}
fmt.Printf("fetched %d repositories\n", len(repos))
}
Output:
func FetchReposWithClient ¶
func FetchReposWithClient(ctx context.Context, client *gh.Client, owner string, limit int) ([]Repo, error)
FetchReposWithClient allows injecting a GitHub client for retrieving repositories. This is primarily exposed for testing purposes.
func FetchReposWithClientOptions ¶ added in v0.0.3
func FetchReposWithClientOptions(ctx context.Context, client *gh.Client, owner string, opts FetchOptions) ([]Repo, error)
FetchReposWithClientOptions allows injecting a GitHub client and advanced filtering.
func FetchReposWithOptions ¶ added in v0.0.3
FetchReposWithOptions retrieves repositories with explicit fetch options.
Example ¶
ExampleFetchReposWithOptions demonstrates fetching only private, non-fork, non-archived Go repositories using GitHub CLI credentials, with bounded exponential-backoff retries on transient API failures.
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/sebastienrousseau/corral/internal/github"
)
func main() {
ctx := context.Background()
repos, err := github.FetchReposWithOptions(ctx, "sebastienrousseau", github.FetchOptions{
Limit: 100,
Visibility: "private",
IncludeForks: false,
IncludeArchived: false,
IncludeLanguages: []string{"go"},
ExcludeLanguages: []string{"makefile"},
AuthMode: github.AuthModeGH,
RetryMax: 4,
RetryMinBackoff: 500 * time.Millisecond,
RetryMaxBackoff: 8 * time.Second,
})
if err != nil {
log.Fatal(err)
}
for _, r := range repos {
fmt.Printf("%s (%s, %s)\n", r.Name, r.Visibility, r.Language)
}
}
Output:
type RetryBudgetError ¶ added in v0.0.28
type RetryBudgetError struct {
// Wait is how long the server asked us to wait.
Wait time.Duration
// Remaining is how much of --api-total-timeout was left.
Remaining time.Duration
// Status is the HTTP status that triggered the retry, or 0 for a
// transport error.
Status int
}
RetryBudgetError reports that a retry was required but could not be waited out inside the operation's remaining time. It carries the numbers so the message can tell the user what to raise, rather than leaving them with a deadline error and no cause.
func (*RetryBudgetError) Error ¶ added in v0.0.28
func (e *RetryBudgetError) Error() string
Error implements the error interface.