backend

package
v0.0.0-...-ddec407 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package backend provides a GitHub API client for git-janitor.

It fetches repository metadata (visibility, fork status, description, issues/PR counts, etc.) to power GitHub-specific checks and enrich the Facts tab in the TUI.

Authentication uses GITHUB_TOKEN or GH_TOKEN from the environment. When no token is available, all GitHub features are silently disabled.

Index

Constants

This section is empty.

Variables

View Source
var ErrRateLimited = errors.New("github: rate limited, try again later")

ErrRateLimited is returned when the GitHub API rate limit is too low to safely make more requests.

Functions

func CollectSecurityAlerts

func CollectSecurityAlerts(ctx context.Context, c *Client, data *models.PlatformInfo)

CollectSecurityAlerts fetches open security alert counts from all three GitHub security APIs (Dependabot, code scanning, secret scanning).

Each API call is independent — a 403 (insufficient permissions) on one does not prevent the others from being queried. Fields remain -1 when the corresponding API is not accessible.

Total: up to 3 API calls.

func ExtractOwnerRepo

func ExtractOwnerRepo(rawURL string) (owner, repo string, err error)

ExtractOwnerRepo parses a GitHub remote URL into owner and repo.

Supported formats:

Types

type Cache

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

Cache is a simple TTL cache for GitHub PlatformInfo, keyed by "owner/repo".

func NewCache

func NewCache(ttl time.Duration) *Cache

NewCache creates a cache with the given default TTL.

func (*Cache) Get

func (c *Cache) Get(key string) (*models.PlatformInfo, bool)

Get returns cached data if present and not expired.

func (*Cache) Set

func (c *Cache) Set(key string, data *models.PlatformInfo)

Set stores data in the cache with the current TTL.

func (*Cache) SetTTL

func (c *Cache) SetTTL(ttl time.Duration)

SetTTL changes the cache TTL for future entries. Existing entries keep their original expiry.

type Client

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

Client wraps the go-github client with token resolution and rate-limit awareness.

func NewClient

func NewClient() *Client

NewClient creates a Client from environment variables.

It checks GITHUB_TOKEN first, then GH_TOKEN as fallback. If neither is set, Available() returns false and all fetch operations return immediately with a nil PlatformInfo.

func (*Client) AddIssueAssignees

func (c *Client) AddIssueAssignees(ctx context.Context, owner, repo string, number int, logins []string) error

AddIssueAssignees assigns the given logins to the specified issue. The call requires at least triage permission on the repository; GitHub silently drops logins that are not allowed to be assigned, so callers should verify the effect via a follow-up fetch if certainty is needed.

func (*Client) AuthenticatedLogin

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

AuthenticatedLogin returns the GitHub login of the token holder, fetching it on first use and caching the result. Returns "" when the token is not available or the lookup fails.

func (*Client) Available

func (c *Client) Available() bool

Available reports whether a valid token was found.

func (*Client) CloseIssue

func (c *Client) CloseIssue(ctx context.Context, owner, repo string, number int) error

CloseIssue transitions the given issue to the "closed" state. Requires the token holder to have write access to the repository.

func (*Client) DisableActions

func (c *Client) DisableActions(ctx context.Context, owner, repo string) error

DisableActions disables GitHub Actions on the given repository.

func (*Client) EnableBranchProtection

func (c *Client) EnableBranchProtection(ctx context.Context, owner, repo, branch string) error

EnableBranchProtection sets a minimal branch protection rule on the given branch: requires at least one pull request review before merging.

func (*Client) EnableDeleteBranchOnMerge

func (c *Client) EnableDeleteBranchOnMerge(ctx context.Context, owner, repo string) error

EnableDeleteBranchOnMerge enables the "Automatically delete head branches" setting on the given repository.

func (*Client) Fetch

func (c *Client) Fetch(ctx context.Context, owner, repo string, opts FetchOptions) *models.PlatformInfo

Fetch retrieves GitHub repo data, using the cache unless ForceRefresh is true.

func (*Client) GetIssueDetail

func (c *Client) GetIssueDetail(ctx context.Context, owner, repo string, number int) *models.IssueDetail

GetIssueDetail fetches full detail for a single issue.

func (*Client) GetPullRequestDetail

func (c *Client) GetPullRequestDetail(ctx context.Context, owner, repo string, number int) *models.PullRequestDetail

GetPullRequestDetail fetches full detail for a single pull request.

func (*Client) GetWorkflowRunDetail

func (c *Client) GetWorkflowRunDetail(ctx context.Context, owner, repo string, runID int64) *models.WorkflowRunDetail

GetWorkflowRunDetail fetches full detail for a single workflow run.

func (*Client) ListIssues

func (c *Client) ListIssues(ctx context.Context, owner, repo string, page, perPage int) ([]models.Issue, bool, error)

ListIssues fetches a page of open issues sorted by last updated (descending). Returns the issues, whether more pages exist, and any error. The GitHub API includes PRs in the issues endpoint — we filter them out.

func (*Client) ListPullRequests

func (c *Client) ListPullRequests(ctx context.Context, owner, repo string, page, perPage int) ([]models.PullRequest, bool, error)

ListPullRequests fetches a page of open pull requests sorted by last updated (descending). Returns the PRs, whether more pages exist, and any error.

func (*Client) ListWorkflowRuns

func (c *Client) ListWorkflowRuns(ctx context.Context, owner, repo string, page, perPage int) ([]models.WorkflowRun, bool, error)

ListWorkflowRuns fetches a page of recent workflow runs. Returns the runs, whether more pages exist, and any error.

func (*Client) Rate

func (c *Client) Rate() RateInfo

Rate returns the current rate-limit info.

func (*Client) Scopes

func (c *Client) Scopes() string

Scopes returns the token's OAuth scopes as reported by GitHub. Empty string if not yet known (no API call made) or if using a fine-grained token.

func (*Client) SetDescription

func (c *Client) SetDescription(ctx context.Context, owner, repo, description string) error

SetDescription updates the repository description on GitHub.

type FetchOptions

type FetchOptions struct {
	ForceRefresh  bool
	FetchSecurity bool
}

FetchOptions controls what data to fetch.

type RateInfo

type RateInfo struct {
	Remaining int
	Limit     int
	ResetAt   time.Time
}

RateInfo tracks GitHub API rate-limit state.

type Runner

type Runner struct {
	*Client
}

Runner wraps a Client for use as a context-injected runner by the engine.

func NewRunner

func NewRunner() *Runner

NewRunner creates a Runner backed by a new Client.

Jump to

Keyboard shortcuts

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