repos

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArchivedRepo

type ArchivedRepo struct {
	ID   int64
	Name string
}

ArchivedRepo represents a repository that was newly archived and excluded during sync.

type CodeHostClient

type CodeHostClient interface {
	ListRepositories(ctx context.Context) ([]CodeHostRepository, error)
	// GetRepository fetches a single repository by name (e.g., "owner/repo").
	// Returns nil if the repository doesn't exist.
	GetRepository(ctx context.Context, name string) (*CodeHostRepository, error)
}

CodeHostClient interface for syncing repositories.

type CodeHostConfig

type CodeHostConfig struct {
	Type            string
	URL             string
	Token           string            // Can be literal or env var reference like "$CS_GITHUB_TOKEN"
	ExcludeArchived bool              // When true, archived repos are excluded from sync
	CleanupArchived bool              // When true, auto-cleanup index for archived repos
	Repos           []string          // Specific repos to index (if empty, syncs all accessible repos)
	RepoConfigs     []RepoConfigEntry // Per-repo configuration (branches, exclude, etc.)
}

CodeHostConfig represents a code host configuration from config file.

func (*CodeHostConfig) GetRepoConfig

func (ch *CodeHostConfig) GetRepoConfig(repoName string) *RepoConfigEntry

GetRepoConfig returns the config for a specific repo name, or nil if not found.

func (*CodeHostConfig) ResolveToken

func (ch *CodeHostConfig) ResolveToken() string

ResolveToken resolves the token, expanding environment variable references.

type CodeHostRepository

type CodeHostRepository struct {
	Name          string
	FullName      string
	CloneURL      string
	DefaultBranch string
	Private       bool
	Archived      bool
}

CodeHostRepository represents a repository from a code host.

type Connection

type Connection struct {
	ID              int64
	Name            string
	Type            string // github, gitlab, gitea, bitbucket
	URL             string
	Token           string
	ExcludeArchived bool     // When true, archived repos are excluded from sync
	CleanupArchived bool     // When true, auto-cleanup index for archived repos
	Repos           []string // Specific repos to sync (if empty, syncs all accessible repos)
	CreatedAt       time.Time
	UpdatedAt       time.Time
}

Connection represents a code host connection.

type RepoConfigEntry

type RepoConfigEntry struct {
	Name     string   // Repository name (e.g., "owner/repo")
	Branches []string // Specific branches to index (empty = default branch only)
	Exclude  bool     // If true, exclude this repo from indexing (can be re-included)
	Delete   bool     // If true, permanently delete this repo (won't be re-added on sync)
}

RepoConfigEntry represents per-repo configuration from config file.

type RepoListOptions

type RepoListOptions struct {
	ConnectionID *int64
	Search       string // Search by name (case-insensitive)
	Status       string // Filter by index status
	Limit        int    // Max results (default 50)
	Offset       int    // Pagination offset
}

RepoListOptions contains options for listing repositories with pagination.

type RepoListResult

type RepoListResult struct {
	Repos      []Repository `json:"repos"`
	TotalCount int          `json:"total_count"`
	Limit      int          `json:"limit"`
	Offset     int          `json:"offset"`
	HasMore    bool         `json:"has_more"`
}

RepoListResult represents the result of listing repositories.

type RepoStats

type RepoStats struct {
	Total    int `json:"total"`
	Indexed  int `json:"indexed"`
	Pending  int `json:"pending"`
	Indexing int `json:"indexing"`
	Failed   int `json:"failed"`
	Excluded int `json:"excluded"`
	Deleted  int `json:"deleted"`
	Stale    int `json:"stale,omitempty"`
}

RepoStats contains repository statistics.

type Repository

type Repository struct {
	ID            int64
	ConnectionID  int64
	Name          string
	CloneURL      string
	DefaultBranch string
	Branches      db.StringArray
	LastIndexed   *time.Time
	IndexStatus   string
	Excluded      bool // When true, repo is excluded from sync and indexing
	Deleted       bool // When true, repo is permanently excluded and won't be re-added on sync
	Archived      bool // Reflects archived status from code host
	CreatedAt     time.Time
	UpdatedAt     time.Time
}

Repository represents a code repository.

type Service

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

Service handles repository operations.

func NewService

func NewService(pool db.Pool, encryptor *crypto.TokenEncryptor) *Service

NewService creates a new repository service.

func (*Service) ClaimPendingIndexJob

func (s *Service) ClaimPendingIndexJob(ctx context.Context, workerID string) (*Repository, error)

ClaimPendingIndexJob atomically claims a single pending job for this worker Uses SELECT FOR UPDATE SKIP LOCKED to prevent race conditions.

func (*Service) CleanupStaleIndexing

func (s *Service) CleanupStaleIndexing(
	ctx context.Context,
	olderThan time.Duration,
) (int64, error)

CleanupStaleIndexing resets repositories stuck in 'indexing' or 'cloning' state for too long.

func (*Service) CreateConnection

func (s *Service) CreateConnection(
	ctx context.Context,
	name, connType, url, token string,
	excludeArchived bool,
	cleanupArchived bool,
) (*Connection, error)

CreateConnection creates a new code host connection.

func (*Service) CreateRepository

func (s *Service) CreateRepository(
	ctx context.Context,
	connectionID int64,
	name, cloneURL, defaultBranch string,
	branches []string,
) (*Repository, error)

CreateRepository adds a new repository.

func (*Service) DeleteConnection

func (s *Service) DeleteConnection(ctx context.Context, id int64) error

DeleteConnection removes a connection and all its repositories.

func (*Service) DeleteRepository

func (s *Service) DeleteRepository(ctx context.Context, id int64) error

DeleteRepository marks a repository as deleted (soft delete). Deleted repos are permanently excluded and won't be re-added on sync.

func (*Service) ExcludeRepository

func (s *Service) ExcludeRepository(ctx context.Context, id int64) error

ExcludeRepository marks a repository as excluded (soft delete) Excluded repos are skipped during sync and should be removed from the index.

func (*Service) FindRepositoryByZoektName

func (s *Service) FindRepositoryByZoektName(
	ctx context.Context,
	zoektName string,
) (*Repository, error)

FindRepositoryByZoektName finds a repository by the name returned from Zoekt search. Zoekt may return names like "gitlab.example.com/group/repo" while the database stores just "group/repo". This method tries multiple matching strategies: 1. Exact match on name 2. Match where clone_url contains the zoekt name 3. Match where zoekt name ends with the database name.

func (*Service) GetConnection

func (s *Service) GetConnection(ctx context.Context, id int64) (*Connection, error)

GetConnection retrieves a connection by ID.

func (*Service) GetConnectionByName

func (s *Service) GetConnectionByName(ctx context.Context, name string) (*Connection, error)

GetConnectionByName retrieves a connection by name.

func (*Service) GetConnectionStats

func (s *Service) GetConnectionStats(ctx context.Context, connectionID int64) (*RepoStats, error)

GetConnectionStats returns statistics for a specific connection.

func (*Service) GetPendingIndexJobs

func (s *Service) GetPendingIndexJobs(ctx context.Context, limit int) ([]Repository, error)

GetPendingIndexJobs returns repositories that need indexing Uses FOR UPDATE SKIP LOCKED to prevent multiple workers from picking the same jobs.

func (*Service) GetRepoStats

func (s *Service) GetRepoStats(ctx context.Context, staleThreshold time.Time) (*RepoStats, error)

GetRepoStats returns repository statistics grouped by index status.

func (*Service) GetRepositoriesByStatus

func (s *Service) GetRepositoriesByStatus(
	ctx context.Context,
	status string,
	limit int,
) ([]Repository, error)

GetRepositoriesByStatus returns repositories with a specific status.

func (*Service) GetRepository

func (s *Service) GetRepository(ctx context.Context, id int64) (*Repository, error)

GetRepository retrieves a repository by ID.

func (*Service) GetRepositoryByName

func (s *Service) GetRepositoryByName(ctx context.Context, name string) (*Repository, error)

GetRepositoryByName retrieves a repository by name.

func (*Service) GetStats

func (s *Service) GetStats(ctx context.Context) (*RepoStats, error)

GetStats returns repository statistics.

func (*Service) IncludeRepository

func (s *Service) IncludeRepository(ctx context.Context, id int64) error

IncludeRepository marks a repository as included (un-exclude).

func (*Service) ListConnections

func (s *Service) ListConnections(ctx context.Context) ([]Connection, error)

ListConnections returns all configured connections.

func (*Service) ListRepositories

func (s *Service) ListRepositories(ctx context.Context, connectionID *int64) ([]Repository, error)

ListRepositories returns all repositories, optionally filtered by connection.

func (*Service) ListRepositoriesWithOptions

func (s *Service) ListRepositoriesWithOptions(
	ctx context.Context,
	opts RepoListOptions,
) (*RepoListResult, error)

ListRepositoriesWithOptions returns repositories with filtering and pagination.

func (*Service) ReindexAll

func (s *Service) ReindexAll(ctx context.Context) (int64, error)

ReindexAll marks all indexed repositories for re-indexing.

func (*Service) ReindexConnection

func (s *Service) ReindexConnection(ctx context.Context, connectionID int64) (int64, error)

ReindexConnection marks all repositories in a connection for re-indexing.

func (*Service) RestoreRepository

func (s *Service) RestoreRepository(ctx context.Context, id int64) error

RestoreRepository un-deletes a repository (clears both deleted and excluded flags). This allows a previously deleted repo to be re-added and indexed.

func (*Service) ScheduleIndex

func (s *Service) ScheduleIndex(ctx context.Context, repoID int64) error

ScheduleIndex marks a repository for indexing.

func (*Service) SetRepoPollInterval

func (s *Service) SetRepoPollInterval(
	ctx context.Context,
	repoID int64,
	intervalSeconds int,
) error

SetRepoPollInterval sets a custom poll interval for a repository. Pass 0 to reset to default (NULL).

func (*Service) SyncCodeHostsFromConfig

func (s *Service) SyncCodeHostsFromConfig(
	ctx context.Context,
	codeHosts map[string]CodeHostConfig,
) ([]int64, error)

SyncCodeHostsFromConfig syncs code hosts from config to database Config-defined code hosts take precedence and will overwrite existing connections with the same name Returns a list of connection IDs that were synced (for triggering repo discovery).

func (*Service) SyncRepositories

func (s *Service) SyncRepositories(
	ctx context.Context,
	connectionID int64,
	client CodeHostClient,
	excludeArchived bool,
	cleanupArchived bool,
	allowedRepos []string,
	repoConfigs []RepoConfigEntry,
) ([]ArchivedRepo, error)

SyncRepositories fetches and syncs repositories from a connection Excluded repos are skipped - they won't be re-added or updated If excludeArchived is true, archived repos will be soft-deleted (excluded) If a repo becomes unarchived, it will be re-included on next sync. If allowedRepos is non-empty, only repos matching those patterns will be synced. If repoConfigs is non-empty, per-repo settings (branches, exclude) are applied. If cleanupArchived is true, returns repos that were newly excluded due to archival so the caller can queue cleanup jobs.

func (*Service) TouchRepository

func (s *Service) TouchRepository(ctx context.Context, id int64) error

TouchRepository updates the updated_at timestamp for a repository. This is used as a heartbeat during long-running indexing operations to prevent the stale cleanup from resetting the repository status.

func (*Service) TriggerSyncAllRepos

func (s *Service) TriggerSyncAllRepos(ctx context.Context) (int, error)

TriggerSyncAllRepos marks all indexed or failed repos as pending for re-indexing. Returns the count of repos marked for sync.

func (*Service) TriggerSyncStaleRepos

func (s *Service) TriggerSyncStaleRepos(
	ctx context.Context,
	staleThreshold time.Time,
) (int, error)

TriggerSyncStaleRepos marks stale repos as pending for re-indexing. Returns the count of repos marked for sync.

func (*Service) UpdateBranches

func (s *Service) UpdateBranches(ctx context.Context, repoID int64, branches []string) error

UpdateBranches updates the branches for a repository.

func (*Service) UpdateConnection

func (s *Service) UpdateConnection(
	ctx context.Context,
	id int64,
	name, connType, url, token string,
	excludeArchived bool,
	cleanupArchived bool,
) (*Connection, error)

UpdateConnection updates an existing code host connection.

func (*Service) UpdateIndexStatus

func (s *Service) UpdateIndexStatus(
	ctx context.Context,
	id int64,
	status string,
	indexed bool,
) error

UpdateIndexStatus updates the indexing status of a repository Uses optimistic locking to prevent race conditions.

func (*Service) UpdateIndexStatusIfMatch

func (s *Service) UpdateIndexStatusIfMatch(
	ctx context.Context,
	id int64,
	expectedStatus, newStatus string,
	indexed bool,
) (bool, error)

UpdateIndexStatusIfMatch updates status only if current status matches expected This prevents race conditions when multiple workers process the same repo.

Jump to

Keyboard shortcuts

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