Documentation
¶
Index ¶
- Constants
- Variables
- func AcceptToS(ctx context.Context, db *sql.DB, tenantID string) error
- func BillingPeriodStart() time.Time
- func CreateAPIToken(ctx context.Context, db *sql.DB, tenantID, name string) (string, error)
- func CreateAppJWT(cfg *GitHubAppConfig) (string, error)
- func CreateSession(ctx context.Context, db *sql.DB, tenantID string, ttl time.Duration) (string, error)
- func DeleteTenant(ctx context.Context, db *sql.DB, tenantID string) error
- func DestroySession(ctx context.Context, db *sql.DB, rawToken string) error
- func GetLastSignIn(ctx context.Context, db *sql.DB, tenantID string) *time.Time
- func GetLastSignIns(ctx context.Context, db *sql.DB, tenantIDs []string) (map[string]*time.Time, error)
- func GetUsageCount(ctx context.Context, db *sql.DB, tenantID string, since time.Time) (int, error)
- func HasActiveInstallations(ctx context.Context, db *sql.DB, tenantIDs []string) (map[string]bool, error)
- func HashToken(raw string) string
- func NextBillingPeriodStart() time.Time
- func RecordUsage(ctx context.Context, db *sql.DB, tenantID, username, provider, source string, ...) error
- func RevokeAPIToken(ctx context.Context, db *sql.DB, tenantID, tokenID string) error
- func SaveInstallation(ctx context.Context, db *sql.DB, tenantID string, installationID, appID int64, ...) error
- func SuspendInstallation(ctx context.Context, db *sql.DB, installationID int64) error
- type APITokenInfo
- type ActiveInstallation
- type GitHubAppConfig
- type Installation
- type InstallationToken
- type RecentScored
- type Tenant
- func GetTenantByGitHubID(ctx context.Context, db *sql.DB, githubID int64) (*Tenant, error)
- func GetTenantByID(ctx context.Context, db *sql.DB, id string) (*Tenant, error)
- func GetTenantByUsername(ctx context.Context, db *sql.DB, username string) (*Tenant, error)
- func ListTenants(ctx context.Context, db *sql.DB) ([]*Tenant, error)
- func SearchTenants(ctx context.Context, db *sql.DB, query string, limit, offset int) ([]*Tenant, int, error)
- func UpdateTenantPlan(ctx context.Context, db *sql.DB, tenantID, planName string, ...) (*Tenant, error)
- func UpdateTenantStatus(ctx context.Context, db *sql.DB, tenantID, status string) (*Tenant, error)
- func UpsertTenant(ctx context.Context, db *sql.DB, githubID int64, ...) (*Tenant, error)
- func ValidateAPIToken(ctx context.Context, db *sql.DB, rawToken string) (*Tenant, error)
- func ValidateSession(ctx context.Context, db *sql.DB, rawToken string) (*Tenant, error)
- type TenantWithoutInstall
Constants ¶
const ( StatusActive = "active" StatusSuspended = "suspended" )
Variables ¶
var ErrSessionInvalid = errors.New("session expired or not found")
var ErrTokenInvalid = errors.New("invalid or revoked API token")
Functions ¶
func BillingPeriodStart ¶
BillingPeriodStart returns the start of the current monthly billing period (first day of the current month, midnight UTC).
func CreateAPIToken ¶
CreateAPIToken generates a prefixed API token, stores its hash, and returns the raw token (shown once to the user, never stored).
func CreateAppJWT ¶
func CreateAppJWT(cfg *GitHubAppConfig) (string, error)
CreateAppJWT creates a short-lived JWT signed with the App's private key.
func CreateSession ¶
func CreateSession(ctx context.Context, db *sql.DB, tenantID string, ttl time.Duration) (string, error)
CreateSession generates a random 256-bit token, stores its SHA-256 hash in the database, and returns the raw token for the cookie.
func DeleteTenant ¶
DeleteTenant removes a tenant by ID. Related rows (installations, tokens, usage records) are removed automatically via ON DELETE CASCADE.
func DestroySession ¶
DestroySession removes a session by its raw token.
func GetLastSignIn ¶
GetLastSignIn returns the most recent session creation time for a tenant, or nil.
func GetLastSignIns ¶
func GetLastSignIns(ctx context.Context, db *sql.DB, tenantIDs []string) (map[string]*time.Time, error)
GetLastSignIns returns the most recent session creation time for each of the given tenant IDs. Tenants with no sessions are omitted from the result.
func GetUsageCount ¶
GetUsageCount returns the number of unique contributors scored by a tenant since the given time.
func HasActiveInstallations ¶
func HasActiveInstallations(ctx context.Context, db *sql.DB, tenantIDs []string) (map[string]bool, error)
HasActiveInstallations returns a map of tenant IDs that have at least one non-suspended installation. Tenants with no installation are omitted.
func NextBillingPeriodStart ¶
NextBillingPeriodStart returns the start of the next monthly billing period.
func RecordUsage ¶
func RecordUsage(ctx context.Context, db *sql.DB, tenantID, username, provider, source string, deep bool, repo string) error
RecordUsage logs a scoring event for quota tracking. source indicates how the score was triggered ("ui" or "api"). repo is the optional owner/repo context (empty string for global scoring).
func RevokeAPIToken ¶
RevokeAPIToken deletes a token owned by the given tenant.
Types ¶
type APITokenInfo ¶
func ListAPITokens ¶
ListAPITokens returns all tokens for a tenant, ordered by creation time descending.
type ActiveInstallation ¶
ActiveInstallation is a non-suspended installation summary.
func GetActiveInstallations ¶
func GetActiveInstallations(ctx context.Context, db *sql.DB, tenantID string) ([]ActiveInstallation, error)
GetActiveInstallations returns non-suspended installations for a tenant.
func GetAllActiveInstallations ¶
func GetAllActiveInstallations(ctx context.Context, db *sql.DB, appID int64) ([]ActiveInstallation, error)
GetAllActiveInstallations returns non-suspended installations across all tenants for the given app ID. Used at startup to build the GitHub API token pool.
type GitHubAppConfig ¶
type GitHubAppConfig struct {
AppID int64
PrivateKey *rsa.PrivateKey
BaseURL string // default: https://api.github.com
}
GitHubAppConfig holds the credentials needed to authenticate as a GitHub App.
func LoadGitHubAppConfig ¶
func LoadGitHubAppConfig() (*GitHubAppConfig, error)
LoadGitHubAppConfig reads GITHUB_APP_ID and GITHUB_APP_KEY_PATH from env.
type Installation ¶
type Installation struct {
ID string
TenantID string
InstallationID int64
TargetType string
TargetLogin string
SuspendedAt *time.Time
CreatedAt time.Time
}
Installation represents a GitHub App installation linked to a tenant.
func ListInstallations ¶
ListInstallations returns all installations for a tenant.
type InstallationToken ¶
type InstallationToken struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
}
InstallationToken is a short-lived token scoped to a single installation.
func MintInstallationToken ¶
func MintInstallationToken(ctx context.Context, cfg *GitHubAppConfig, installationID int64) (*InstallationToken, error)
MintInstallationToken exchanges an App JWT for a short-lived installation token.
type RecentScored ¶
type RecentScored struct {
Username string
Provider string
Source string
Deep bool
Repo string
ScoredAt time.Time
}
RecentScored represents a recently scored contributor.
func GetRecentScored ¶
func GetRecentScored(ctx context.Context, db *sql.DB, tenantID string, limit int) ([]RecentScored, error)
GetRecentScored returns the most recently scored contributors for a tenant.
func (RecentScored) Scope ¶
func (r RecentScored) Scope() string
Scope returns a human-readable label for the scoring scope.
type Tenant ¶
type Tenant struct {
ID string
GitHubID int64
Username string
Email string
AvatarURL string
Name string
Company string
Location string
Bio string
Plan string
Status string
MaxContributors int
ToSAcceptedAt *time.Time
CreatedAt time.Time
UpdatedAt time.Time
}
func GetTenantByGitHubID ¶
func GetTenantByID ¶
func GetTenantByUsername ¶
GetTenantByUsername returns the tenant with the given GitHub username.
func ListTenants ¶
ListTenants returns all tenants ordered by creation date.
func SearchTenants ¶
func SearchTenants(ctx context.Context, db *sql.DB, query string, limit, offset int) ([]*Tenant, int, error)
SearchTenants returns a page of tenants matching the query, ordered by most recent sign-in descending, along with the total count of matching tenants.
func UpdateTenantPlan ¶
func UpdateTenantPlan(ctx context.Context, db *sql.DB, tenantID, planName string, maxContributors int) (*Tenant, error)
UpdateTenantPlan updates a tenant's plan and max_contributors.
func UpdateTenantStatus ¶
UpdateTenantStatus updates a tenant's status (active or suspended).
func UpsertTenant ¶
func ValidateAPIToken ¶
ValidateAPIToken hashes the raw token, looks it up, and returns the owning tenant. The last_used_at update is folded into the same CTE query so validation is a single round-trip — previously the update shipped as a fire-and-forget goroutine that could outlive the request and race the connection pool on shutdown.
type TenantWithoutInstall ¶
TenantWithoutInstall is an active tenant with no GitHub App installation.
func ListTenantsWithoutInstall ¶
ListTenantsWithoutInstall returns active tenants that have no GitHub App installation.