Documentation
¶
Overview ¶
Package github provides client functionality for interacting with the GitHub API, including user authentication and organization validation.
Index ¶
- type APIClient
- type AppInstallation
- type Client
- func (c *Client) AppInstallationInfo(ctx context.Context) (*AppInstallation, error)
- func (c *Client) AuthenticatedUser(ctx context.Context) (*User, error)
- func (c *Client) FindPRsForCommit(ctx context.Context, owner, repo, commitSHA string) ([]int, error)
- func (c *Client) UserAndOrgs(ctx context.Context) (username string, orgs []string, err error)
- func (c *Client) UserTier(ctx context.Context, username string) (Tier, error)
- func (c *Client) ValidateOrgMembership(ctx context.Context, org string) (username string, orgs []string, err error)
- type MarketplaceAccount
- type MarketplacePlan
- type MockClient
- type Organization
- type Tier
- type TierCache
- type User
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
UserAndOrgs returns the mock user info.
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 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 ¶
NewTierCache creates a new tier cache with the specified TTL. A background goroutine periodically cleans up expired entries.
func (*TierCache) Get ¶
Get retrieves a tier from the cache. Returns (tier, true) if found and not expired, (TierFree, false) otherwise.