github

package
v0.0.0-...-3985b18 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package github provides client functionality for interacting with the GitHub API, including user authentication and organization validation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIClient

type APIClient interface {
	// UserAndOrgs returns the authenticated user's username and organizations.
	UserAndOrgs(ctx context.Context) (username string, orgs []string, err error)

	// ValidateOrgMembership validates that the authenticated user is a member of the specified organization.
	ValidateOrgMembership(ctx context.Context, org string) (username string, orgs []string, err error)

	// UserTier fetches the user's GitHub Marketplace subscription tier.
	UserTier(ctx context.Context, username string) (Tier, error)
}

APIClient defines the interface for GitHub API operations. This allows for mocking in tests while using the real client in production.

type AppInstallation

type AppInstallation struct {
	Account struct {
		Login string `json:"login"`
		Type  string `json:"type"` // "Organization" or "User"
	} `json:"account"`
	ID    int64 `json:"id"`
	AppID int64 `json:"app_id"`
}

AppInstallation represents a GitHub App installation.

type Client

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

Client provides GitHub API functionality.

func NewClient

func NewClient(token string, logger *slog.Logger) *Client

NewClient creates a new GitHub API client with the provided token. If logger is nil, a default discarding logger is used.

func (*Client) AppInstallationInfo

func (c *Client) AppInstallationInfo(ctx context.Context) (*AppInstallation, error)

AppInstallationInfo retrieves information about the GitHub App installation. For installation tokens, we need to use /installation/repositories to get context.

func (*Client) AuthenticatedUser

func (c *Client) AuthenticatedUser(ctx context.Context) (*User, error)

AuthenticatedUser returns the currently authenticated user's info.

func (*Client) FindPRsForCommit

func (c *Client) FindPRsForCommit(ctx context.Context, owner, repo, commitSHA string) ([]int, error)

FindPRsForCommit finds all pull requests associated with a specific commit SHA. This is useful for resolving check_run/check_suite events when GitHub's pull_requests array is empty. Returns a list of PR numbers that contain this commit.

IMPORTANT: Due to race conditions in GitHub's indexing, this may initially return an empty array even for commits that ARE on PR branches. We implement retry logic to handle this: - First empty result: retry immediately after 500ms. - Second empty result: return empty (caller should use TTL cache).

func (*Client) UserAndOrgs

func (c *Client) UserAndOrgs(ctx context.Context) (username string, orgs []string, err error)

UserAndOrgs retrieves the authenticated user's username and list of organizations. For GitHub App tokens, it returns the app's installation org. Returns username, list of organization names, and error.

func (*Client) UserTier

func (c *Client) UserTier(ctx context.Context, username string) (Tier, error)

UserTier fetches the user's GitHub Marketplace subscription tier. Returns TierFree if no subscription exists or on API errors (graceful degradation).

func (*Client) ValidateOrgMembership

func (c *Client) ValidateOrgMembership(ctx context.Context, org string) (username string, orgs []string, err error)

ValidateOrgMembership checks if the authenticated user has access to the specified organization. Returns the authenticated user's username, list of all their organizations, and nil error if successful.

type MarketplaceAccount

type MarketplaceAccount struct {
	Plan MarketplacePlan `json:"plan"`
}

MarketplaceAccount represents a GitHub Marketplace account subscription.

type MarketplacePlan

type MarketplacePlan struct {
	Name string `json:"name"`
}

MarketplacePlan represents a GitHub Marketplace subscription plan.

type MockClient

type MockClient struct {
	Orgs             []string
	Err              error
	Username         string
	LastValidatedOrg string
	Tier             Tier // Mock tier to return

	UserAndOrgsCalls           int
	ValidateOrgMembershipCalls int
	UserTierCalls              int
	// contains filtered or unexported fields
}

MockClient is a mock GitHub API client for testing. Thread-safe for concurrent access.

func (*MockClient) UserAndOrgs

func (m *MockClient) UserAndOrgs(_ context.Context) (username string, orgs []string, err error)

UserAndOrgs returns the mock user info.

func (*MockClient) UserTier

func (m *MockClient) UserTier(_ context.Context, _ string) (Tier, error)

UserTier returns the mock tier.

func (*MockClient) ValidateOrgMembership

func (m *MockClient) ValidateOrgMembership(_ context.Context, org string) (username string, orgs []string, err error)

ValidateOrgMembership validates organization membership using the mock data.

type Organization

type Organization struct {
	Login string `json:"login"`
}

Organization struct to match GitHub API response.

type Tier

type Tier string

Tier represents a GitHub Marketplace pricing tier.

const (
	// TierFree is the default tier for users without a paid subscription.
	TierFree Tier = "free"
	// TierPro is the Pro tier for individual users.
	TierPro Tier = "pro"
	// TierFlock is the Flock tier for teams/organizations.
	TierFlock Tier = "flock"
)

type TierCache

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

TierCache caches tier lookups to reduce API calls to GitHub Marketplace. Implements thread-safe in-memory caching with TTL expiration.

func NewTierCache

func NewTierCache(ttl time.Duration) *TierCache

NewTierCache creates a new tier cache with the specified TTL. A background goroutine periodically cleans up expired entries.

func (*TierCache) Get

func (tc *TierCache) Get(username string) (Tier, bool)

Get retrieves a tier from the cache. Returns (tier, true) if found and not expired, (TierFree, false) otherwise.

func (*TierCache) Set

func (tc *TierCache) Set(username string, tier Tier)

Set stores a tier in the cache with TTL expiration.

func (*TierCache) Stop

func (tc *TierCache) Stop()

Stop stops the cleanup goroutine and waits for it to finish. Safe to call multiple times (subsequent calls are no-ops).

type User

type User struct {
	Login string `json:"login"`
}

User represents the authenticated GitHub user.

Jump to

Keyboard shortcuts

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