tenant

package
v0.26.16 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusActive    = "active"
	StatusSuspended = "suspended"
)

Variables

View Source
var ErrSessionInvalid = errors.New("session expired or not found")
View Source
var ErrTokenInvalid = errors.New("invalid or revoked API token")

Functions

func AcceptToS

func AcceptToS(ctx context.Context, db *sql.DB, tenantID string) error

func BillingPeriodStart

func BillingPeriodStart() time.Time

BillingPeriodStart returns the start of the current monthly billing period (first day of the current month, midnight UTC).

func CreateAPIToken

func CreateAPIToken(ctx context.Context, db *sql.DB, tenantID, name string) (string, error)

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

func DeleteTenant(ctx context.Context, db *sql.DB, tenantID string) error

DeleteTenant removes a tenant by ID. Related rows (installations, tokens, usage records) are removed automatically via ON DELETE CASCADE.

func DestroySession

func DestroySession(ctx context.Context, db *sql.DB, rawToken string) error

DestroySession removes a session by its raw token.

func GetLastSignIn

func GetLastSignIn(ctx context.Context, db *sql.DB, tenantID string) *time.Time

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

func GetUsageCount(ctx context.Context, db *sql.DB, tenantID string, since time.Time) (int, error)

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 HashToken

func HashToken(raw string) string

HashToken returns the hex-encoded SHA-256 hash of a raw token.

func NextBillingPeriodStart

func NextBillingPeriodStart() time.Time

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

func RevokeAPIToken(ctx context.Context, db *sql.DB, tenantID, tokenID string) error

RevokeAPIToken deletes a token owned by the given tenant.

func SaveInstallation

func SaveInstallation(ctx context.Context, db *sql.DB, tenantID string, installationID, appID int64, targetType, targetLogin string) error

SaveInstallation upserts a GitHub App installation for a tenant.

func SuspendInstallation

func SuspendInstallation(ctx context.Context, db *sql.DB, installationID int64) error

SuspendInstallation marks an installation as suspended.

Types

type APITokenInfo

type APITokenInfo struct {
	ID        string
	Name      string
	LastUsed  *time.Time
	CreatedAt time.Time
}

func ListAPITokens

func ListAPITokens(ctx context.Context, db *sql.DB, tenantID string) ([]APITokenInfo, error)

ListAPITokens returns all tokens for a tenant, ordered by creation time descending.

type ActiveInstallation

type ActiveInstallation struct {
	InstallationID int64
	TargetLogin    string
}

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

func ListInstallations(ctx context.Context, db *sql.DB, tenantID string) ([]Installation, error)

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 GetTenantByGitHubID(ctx context.Context, db *sql.DB, githubID int64) (*Tenant, error)

func GetTenantByID

func GetTenantByID(ctx context.Context, db *sql.DB, id string) (*Tenant, error)

func GetTenantByUsername

func GetTenantByUsername(ctx context.Context, db *sql.DB, username string) (*Tenant, error)

GetTenantByUsername returns the tenant with the given GitHub username.

func ListTenants

func ListTenants(ctx context.Context, db *sql.DB) ([]*Tenant, error)

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

func UpdateTenantStatus(ctx context.Context, db *sql.DB, tenantID, status string) (*Tenant, error)

UpdateTenantStatus updates a tenant's status (active or suspended).

func UpsertTenant

func UpsertTenant(ctx context.Context, db *sql.DB, githubID int64, username, email, avatarURL, name, company, location, bio string) (*Tenant, error)

func ValidateAPIToken

func ValidateAPIToken(ctx context.Context, db *sql.DB, rawToken string) (*Tenant, error)

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.

func ValidateSession

func ValidateSession(ctx context.Context, db *sql.DB, rawToken string) (*Tenant, error)

ValidateSession checks the session token and returns the associated tenant.

type TenantWithoutInstall

type TenantWithoutInstall struct {
	Username string
	Plan     string
}

TenantWithoutInstall is an active tenant with no GitHub App installation.

func ListTenantsWithoutInstall

func ListTenantsWithoutInstall(ctx context.Context, db *sql.DB) ([]TenantWithoutInstall, error)

ListTenantsWithoutInstall returns active tenants that have no GitHub App installation.

Jump to

Keyboard shortcuts

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