tenant

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRepoLimitExceeded = errors.New("repo limit exceeded for plan")

ErrRepoLimitExceeded is returned when adding repos would exceed the tenant's plan limit.

View Source
var ErrSessionInvalid = errors.New("session expired or not found")

ErrSessionInvalid is returned when a session is expired or not found.

Functions

func AcceptToS

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

AcceptToS records that a tenant has accepted the Terms of Service.

func AddTenantRepos

func AddTenantRepos(ctx context.Context, db *sql.DB, tenantID string, repos []OrgRepo) error

AddTenantRepos adds repos to a tenant's tracking list, enforcing the plan limit. The count check and inserts are in the same transaction to prevent races.

func CheckRepoAccess added in v0.3.1

func CheckRepoAccess(ctx context.Context, cfg *GitHubAppConfig, installationID int64, org, repo string) (bool, error)

CheckRepoAccess verifies that a GitHub App installation can access a specific repo. Returns true if accessible, false if not (404/403), or error on unexpected failure.

func CleanExpiredSessions

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

CleanExpiredSessions removes all expired sessions.

func CreateAppJWT

func CreateAppJWT(cfg *GitHubAppConfig) (string, error)

CreateAppJWT creates a short-lived JWT signed with the GitHub 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 DeactivateTenantRepo

func DeactivateTenantRepo(ctx context.Context, db *sql.DB, tenantID, org, repo string) error

DeactivateTenantRepo marks a repo as inactive.

func DestroySession

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

DestroySession removes a session by its raw token.

func GetWeeklyEventCount

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

GetWeeklyEventCount returns the total events this week for a tenant's repos.

func HashToken

func HashToken(raw string) string

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

func SaveInstallation

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

SaveInstallation stores or updates 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.

func UpdatePlan

func UpdatePlan(ctx context.Context, db *sql.DB, tenantID, plan string, maxRepos, maxEventsPerWeek int) error

UpdatePlan sets a tenant's plan, repo limit, and event limit.

Types

type ActiveInstallation

type ActiveInstallation struct {
	ID    int64
	Login string
}

ActiveInstallation holds the GitHub installation ID and target org login.

func GetActiveInstallations

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

GetActiveInstallations returns non-suspended installations for a tenant.

func GetInstallationForOrg added in v0.3.1

func GetInstallationForOrg(ctx context.Context, db *sql.DB, tenantID, org string) (*ActiveInstallation, error)

GetInstallationForOrg returns the active installation for a tenant's org, if any.

type ActiveRepo

type ActiveRepo struct {
	Org  string
	Repo string
}

ActiveRepo is a repo eligible for import.

func GetActiveReposForInstall

func GetActiveReposForInstall(ctx context.Context, db *sql.DB, tenantID string, installationID int64) ([]ActiveRepo, error)

GetActiveReposForInstall returns active repos for a tenant's installation.

type ActiveTenant

type ActiveTenant struct {
	ID       string
	Username string
}

ActiveTenant represents a tenant with at least one active repo.

func GetActiveTenants

func GetActiveTenants(ctx context.Context, db *sql.DB) ([]ActiveTenant, error)

GetActiveTenants returns all tenants that have accepted ToS and have active repos.

type GitHubAppConfig

type GitHubAppConfig struct {
	AppID      int64
	PrivateKey *rsa.PrivateKey
	BaseURL    string // override for testing, default: https://api.github.com
}

GitHubAppConfig holds the GitHub App credentials for minting installation tokens.

func LoadGitHubAppConfig added in v0.3.1

func LoadGitHubAppConfig() (*GitHubAppConfig, error)

LoadGitHubAppConfig reads GitHub App credentials from environment variables.

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 for 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 GitHub App installation.

func MintInstallationToken

func MintInstallationToken(ctx context.Context, cfg *GitHubAppConfig, installationID int64) (*InstallationToken, error)

MintInstallationToken exchanges an App JWT for an installation-scoped access token. The returned token is valid for 1 hour and should never be persisted.

type OrgRepo

type OrgRepo struct {
	Org  string
	Repo string
}

OrgRepo is a simple org/repo pair.

type OverviewResponse

type OverviewResponse struct {
	Usage UsageSummary   `json:"usage"`
	Repos []RepoOverview `json:"repos"`
}

OverviewResponse combines repo overview and usage summary.

func GetOverview

func GetOverview(ctx context.Context, db *sql.DB, tenantID string, months int) (*OverviewResponse, error)

GetOverview returns the full overview response with repo data and usage summary.

type RepoOverview

type RepoOverview struct {
	Org          string  `json:"org"`
	Repo         string  `json:"repo"`
	Stars        int     `json:"stars"`
	Forks        int     `json:"forks"`
	OpenIssues   int     `json:"open_issues"`
	Events       int     `json:"events"`
	WeeklyEvents int     `json:"weekly_events"`
	WeeklyPct    float64 `json:"weekly_pct"`
	Contributors int     `json:"contributors"`
	Scored       int     `json:"scored"`
	Language     string  `json:"language"`
	License      string  `json:"license"`
	LastImport   string  `json:"last_import"`
	LimitReached bool    `json:"limit_reached"`
}

RepoOverview holds repo metadata and activity stats for the dashboard table.

type Tenant

type Tenant struct {
	ID               string
	GitHubID         int64
	Username         string
	Email            string
	AvatarURL        string
	MaxRepos         int
	MaxEventsPerWeek int
	Plan             string
	ToSAcceptedAt    *time.Time
	CreatedAt        time.Time
	UpdatedAt        time.Time
}

Tenant represents a registered SaaS tenant.

func GetTenantByGitHubID

func GetTenantByGitHubID(ctx context.Context, db *sql.DB, githubID int64) (*Tenant, error)

GetTenantByGitHubID returns a tenant by their GitHub user ID.

func GetTenantByID

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

GetTenantByID returns a tenant by their internal ID.

func UpsertTenant

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

UpsertTenant creates or updates a tenant by GitHub ID.

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 TenantRepo

type TenantRepo struct {
	ID           string  `json:"id"`
	TenantID     string  `json:"tenant_id"`
	Org          string  `json:"org"`
	Repo         string  `json:"repo"`
	Active       bool    `json:"active"`
	LastImportAt *string `json:"last_import_at"`
}

TenantRepo represents a repo tracked by a tenant.

func ListTenantRepos

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

ListTenantRepos returns all active repos for a tenant.

type UsageSummary

type UsageSummary struct {
	MaxRepos         int     `json:"max_repos"`
	ActiveRepos      int     `json:"active_repos"`
	MaxEventsPerWeek int     `json:"max_events_per_week"`
	WeeklyEvents     int     `json:"weekly_events"`
	WeeklyPct        float64 `json:"weekly_pct"`
	LimitReached     bool    `json:"limit_reached"`
}

UsageSummary holds tenant-level usage stats for the dashboard.

Jump to

Keyboard shortcuts

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