graph

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractOrg

func ExtractOrg(lang Language, name string) string

ExtractOrg extracts the org from a module name. For Go modules: "github.com/grokify/mogo" -> "github.com/grokify" For npm: "@agentplexus/core" -> "@agentplexus"

func NewModuleID

func NewModuleID(lang Language, name string) string

NewModuleID creates a module ID from language and name.

func StreamToCache

func StreamToCache(cache *Cache, key string, r io.Reader) ([]byte, error)

StreamToCache streams reader content to cache.

func WithCache

func WithCache[T any](cache *Cache, key string, fetch func() (T, error)) (T, error)

WithCache adds caching to a reader function.

Types

type Builder

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

Builder constructs a dependency graph from GitHub repositories.

func NewBuilder

func NewBuilder(token string) *Builder

NewBuilder creates a new graph builder with GitHub authentication.

func NewBuilderWithConfig

func NewBuilderWithConfig(cfg BuilderConfig) *Builder

NewBuilderWithConfig creates a new graph builder with configuration.

func (*Builder) Build

func (b *Builder) Build(ctx context.Context, portfolio Portfolio) (*DependencyGraph, error)

Build constructs a dependency graph from the portfolio configuration.

type BuilderConfig

type BuilderConfig struct {
	// Token is the GitHub personal access token.
	Token string

	// MaxRetries is the maximum number of retry attempts for API calls.
	// Default is 3.
	MaxRetries int

	// InitialBackoff is the initial backoff duration for retries.
	// Default is 1 second.
	InitialBackoff time.Duration

	// Cache is an optional cache for API responses.
	Cache *Cache
}

BuilderConfig configures the graph builder.

type Cache

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

Cache provides caching for graph-related data. It uses a simple file-based cache with TTL support.

func NewCache

func NewCache(cfg CacheConfig) (*Cache, error)

NewCache creates a new cache with the given configuration.

func (*Cache) Clear

func (c *Cache) Clear(ctx context.Context) error

Clear removes all entries from the cache.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, key string) error

Delete removes a value from the cache.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string) ([]byte, bool)

Get retrieves a cached value by key.

func (*Cache) Prune

func (c *Cache) Prune(ctx context.Context) (int, error)

Prune removes expired entries from the cache.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, data []byte) error

Set stores a value in the cache.

func (*Cache) Stats

func (c *Cache) Stats(ctx context.Context) CacheStats

Stats returns cache statistics.

type CacheConfig

type CacheConfig struct {
	// Dir is the directory for file-based cache. If empty, uses temp dir.
	Dir string

	// TTL is the time-to-live for cached entries. Default is 1 hour.
	TTL time.Duration

	// MemoryOnly disables file-based caching.
	MemoryOnly bool
}

CacheConfig configures the cache behavior.

type CacheStats

type CacheStats struct {
	MemoryEntries int   `json:"memoryEntries"`
	FileEntries   int   `json:"fileEntries"`
	TotalSizeKB   int64 `json:"totalSizeKB"`
}

CacheStats provides statistics about cache usage.

type CachedBuilder

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

CachedBuilder wraps Builder with caching support.

func NewCachedBuilder

func NewCachedBuilder(token string, cache *Cache) *CachedBuilder

NewCachedBuilder creates a new builder with caching.

func (*CachedBuilder) Build

func (cb *CachedBuilder) Build(ctx context.Context, portfolio Portfolio) (*DependencyGraph, error)

Build constructs a dependency graph with caching.

func (*CachedBuilder) InvalidateGraph

func (cb *CachedBuilder) InvalidateGraph(ctx context.Context, portfolio Portfolio) error

InvalidateGraph removes the cached graph for a portfolio.

type CallbackProgress

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

CallbackProgress adapts a callback function to the Progress interface.

func NewCallbackProgress

func NewCallbackProgress(callback ProgressCallback) *CallbackProgress

NewCallbackProgress creates a progress reporter that calls a callback.

func (*CallbackProgress) Complete

func (cp *CallbackProgress) Complete()

Complete finishes progress tracking.

func (*CallbackProgress) Error

func (cp *CallbackProgress) Error(repo string, err error)

Error reports an error.

func (*CallbackProgress) FoundModule

func (cp *CallbackProgress) FoundModule(module string)

FoundModule reports a found module.

func (*CallbackProgress) ProcessRepo

func (cp *CallbackProgress) ProcessRepo(repo string)

ProcessRepo reports progress on a repository.

func (*CallbackProgress) Start

func (cp *CallbackProgress) Start(totalOrgs int)

Start begins tracking progress.

func (*CallbackProgress) StartOrg

func (cp *CallbackProgress) StartOrg(org string, repoCount int)

StartOrg begins processing an organization.

type Cycle

type Cycle struct {
	Modules []string `json:"modules"`
}

Cycle represents a dependency cycle (should not happen in Go).

type DOTConfig

type DOTConfig struct {
	// Title is the graph title.
	Title string

	// RankDir is the direction of graph layout: "TB" (top-bottom), "LR" (left-right).
	RankDir string

	// ShowExternal includes external (non-managed) dependencies.
	ShowExternal bool

	// ShowVersions includes version labels on edges.
	ShowVersions bool

	// ClusterByOrg groups nodes by organization.
	ClusterByOrg bool

	// ColorManaged is the color for managed modules.
	ColorManaged string

	// ColorExternal is the color for external modules.
	ColorExternal string
}

DOTConfig configures DOT output generation.

func DefaultDOTConfig

func DefaultDOTConfig() DOTConfig

DefaultDOTConfig returns default DOT configuration.

type DependencyGraph

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

DependencyGraph is the default implementation of Graph.

func BuildFromSnapshot

func BuildFromSnapshot(snapshot *GraphSnapshot) *DependencyGraph

BuildFromSnapshot reconstructs a graph from a snapshot.

func NewGraph

func NewGraph() *DependencyGraph

NewGraph creates a new empty dependency graph.

func (*DependencyGraph) AddModule

func (g *DependencyGraph) AddModule(module Module)

AddModule adds a module to the graph.

func (*DependencyGraph) AllModules

func (g *DependencyGraph) AllModules() []Module

AllModules returns all modules in the graph.

func (*DependencyGraph) Build

func (g *DependencyGraph) Build(ctx context.Context, portfolio Portfolio) error

Build constructs the graph from a portfolio configuration. Note: The actual fetching from GitHub is done by the GraphBuilder.

func (*DependencyGraph) Dependencies

func (g *DependencyGraph) Dependencies(moduleID string) []Module

Dependencies returns all modules that the given module depends on.

func (*DependencyGraph) Dependents

func (g *DependencyGraph) Dependents(moduleID string) []Module

Dependents returns all modules that depend on the given module.

func (*DependencyGraph) FilterByLanguage

func (g *DependencyGraph) FilterByLanguage(lang Language) Graph

FilterByLanguage returns a new graph containing only modules of the specified language.

func (*DependencyGraph) FilterByOrg

func (g *DependencyGraph) FilterByOrg(org string) Graph

FilterByOrg returns a new graph containing only modules from the specified org.

func (*DependencyGraph) GetModule

func (g *DependencyGraph) GetModule(id string) (*Module, bool)

GetModule returns a module by ID.

func (*DependencyGraph) ManagedModules

func (g *DependencyGraph) ManagedModules() []Module

ManagedModules returns only managed modules (those in the portfolio).

func (*DependencyGraph) Snapshot

func (g *DependencyGraph) Snapshot() *GraphSnapshot

Snapshot creates a point-in-time snapshot of the graph.

func (*DependencyGraph) StaleModules

func (g *DependencyGraph) StaleModules(dependency string, minVersion string) []StaleModule

StaleModules finds managed modules using outdated versions of a dependency.

func (*DependencyGraph) Stats

func (g *DependencyGraph) Stats() GraphStats

Stats returns statistics about the graph.

func (*DependencyGraph) ToDOT

func (g *DependencyGraph) ToDOT(cfg DOTConfig) string

ToDOT returns the graph as a DOT string.

func (*DependencyGraph) ToMermaid

func (g *DependencyGraph) ToMermaid(cfg MermaidConfig) string

ToMermaid returns the graph as a Mermaid string.

func (*DependencyGraph) UpgradeOrder

func (g *DependencyGraph) UpgradeOrder() (*UpgradeOrder, error)

UpgradeOrder returns modules in topological order for upgrades. Uses Kahn's algorithm for topological sorting. Only includes managed modules.

func (*DependencyGraph) Validate

func (g *DependencyGraph) Validate() []ValidationIssue

Validate checks the graph for issues.

func (*DependencyGraph) WriteDOT

func (g *DependencyGraph) WriteDOT(w io.Writer, cfg DOTConfig) error

WriteDOT writes the graph in DOT format for Graphviz.

func (*DependencyGraph) WriteMermaid

func (g *DependencyGraph) WriteMermaid(w io.Writer, cfg MermaidConfig) error

WriteMermaid writes the graph in Mermaid format.

type GoModCache

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

GoModCache provides caching specifically for go.mod files.

func NewGoModCache

func NewGoModCache(cache *Cache) *GoModCache

NewGoModCache creates a cache for go.mod files.

func (*GoModCache) Get

func (gmc *GoModCache) Get(ctx context.Context, owner, repo, ref string) ([]byte, bool)

Get retrieves a cached go.mod file.

func (*GoModCache) Set

func (gmc *GoModCache) Set(ctx context.Context, owner, repo, ref string, content []byte) error

Set stores a go.mod file in the cache.

type GoModInfo

type GoModInfo struct {
	Module  string          `json:"module"`
	Go      string          `json:"go"`
	Require []ModuleVersion `json:"require,omitempty"`
	Replace []ModuleReplace `json:"replace,omitempty"`
	Exclude []ModuleVersion `json:"exclude,omitempty"`
}

GoModInfo contains parsed go.mod information.

func ParseGoMod

func ParseGoMod(content []byte) (*GoModInfo, error)

ParseGoMod parses a go.mod file content and returns structured information.

func (*GoModInfo) AllDependencies

func (g *GoModInfo) AllDependencies() []ModuleVersion

AllDependencies returns all dependencies including indirect ones.

func (*GoModInfo) DirectDependencies

func (g *GoModInfo) DirectDependencies() []ModuleVersion

DirectDependencies returns only the direct (non-indirect) dependencies.

func (*GoModInfo) GetReplacement

func (g *GoModInfo) GetReplacement(path string) (ModuleVersion, bool)

GetReplacement returns the replacement for a module path, if any.

func (*GoModInfo) HasLocalReplaces

func (g *GoModInfo) HasLocalReplaces() bool

HasLocalReplaces checks if the module has any local replace directives.

func (*GoModInfo) IsLocalReplace

func (g *GoModInfo) IsLocalReplace(r ModuleReplace) bool

IsLocalReplace checks if a replace directive points to a local path.

func (*GoModInfo) IsReplaced

func (g *GoModInfo) IsReplaced(path string) bool

IsReplaced checks if a module path is replaced.

type Graph

type Graph interface {
	// Build constructs the graph from the portfolio configuration.
	Build(ctx context.Context, portfolio Portfolio) error

	// AddModule adds a module to the graph.
	AddModule(module Module)

	// GetModule returns a module by ID.
	GetModule(id string) (*Module, bool)

	// Dependents returns all modules that depend on the given module.
	Dependents(moduleID string) []Module

	// Dependencies returns all modules that the given module depends on.
	Dependencies(moduleID string) []Module

	// UpgradeOrder returns modules in topological order for upgrades.
	// Only includes managed modules.
	UpgradeOrder() (*UpgradeOrder, error)

	// StaleModules finds managed modules using outdated versions of a dependency.
	StaleModules(dependency string, minVersion string) []StaleModule

	// FilterByOrg returns a new graph containing only modules from the specified org.
	FilterByOrg(org string) Graph

	// FilterByLanguage returns a new graph containing only modules of the specified language.
	FilterByLanguage(lang Language) Graph

	// AllModules returns all modules in the graph.
	AllModules() []Module

	// ManagedModules returns only managed modules (those in the portfolio).
	ManagedModules() []Module

	// Snapshot creates a point-in-time snapshot of the graph.
	Snapshot() *GraphSnapshot
}

Graph is the interface for dependency graph operations.

type GraphSnapshot

type GraphSnapshot struct {
	Portfolio Portfolio         `json:"portfolio"`
	Timestamp string            `json:"timestamp"`
	Modules   map[string]Module `json:"modules"` // keyed by module ID
}

GraphSnapshot represents a point-in-time snapshot of the dependency graph.

type GraphStats

type GraphStats struct {
	TotalModules    int              `json:"totalModules"`
	ManagedModules  int              `json:"managedModules"`
	ExternalModules int              `json:"externalModules"`
	TotalEdges      int              `json:"totalEdges"`
	ByLanguage      map[Language]int `json:"byLanguage"`
	ByOrg           map[string]int   `json:"byOrg"`
}

GraphStats contains statistics about the dependency graph.

type Language

type Language string

Language represents the programming language of a module.

const (
	LanguageGo         Language = "go"
	LanguageTypeScript Language = "typescript"
	LanguageSwift      Language = "swift"
	LanguagePython     Language = "python"
	LanguageRust       Language = "rust"
)

func ParseModuleID

func ParseModuleID(id string) (Language, string)

ParseModuleID parses a module ID into language and name.

func (Language) ManifestFile

func (l Language) ManifestFile() string

ManifestFile returns the manifest file name for this language.

type MermaidConfig

type MermaidConfig struct {
	// Direction is "TB", "BT", "LR", or "RL".
	Direction string

	// ShowExternal includes external dependencies.
	ShowExternal bool
}

MermaidConfig configures Mermaid diagram output.

func DefaultMermaidConfig

func DefaultMermaidConfig() MermaidConfig

DefaultMermaidConfig returns default Mermaid configuration.

type Module

type Module struct {
	// ID is a universal identifier: "go:github.com/grokify/mogo" or "npm:@agentplexus/core"
	ID string `json:"id"`

	// Language of this module
	Language Language `json:"language"`

	// Name is the module name (e.g., "github.com/grokify/mogo")
	Name string `json:"name"`

	// Org is the GitHub org (e.g., "github.com/grokify")
	Org string `json:"org"`

	// Version is the current version tag
	Version string `json:"version"`

	// Repo is the GitHub repository info (nil for external modules)
	Repo *model.Repo `json:"repo,omitempty"`

	// IsManaged is true if this module is in the portfolio
	IsManaged bool `json:"isManaged"`

	// Dependencies are modules this module depends on
	Dependencies []ModuleRef `json:"dependencies,omitempty"`

	// Dependents are modules that depend on this module
	Dependents []ModuleRef `json:"dependents,omitempty"`
}

Module represents a dependency module in the graph.

type ModuleRef

type ModuleRef struct {
	ID        string `json:"id"`
	Version   string `json:"version"`
	IsManaged bool   `json:"isManaged"`
}

ModuleRef is a lightweight reference to a module with version.

type ModuleReplace

type ModuleReplace struct {
	Old ModuleVersion `json:"old"`
	New ModuleVersion `json:"new"`
}

ModuleReplace represents a replace directive in go.mod.

type ModuleVersion

type ModuleVersion struct {
	Path     string `json:"path"`
	Version  string `json:"version"`
	Indirect bool   `json:"indirect,omitempty"`
}

ModuleVersion represents a module with its version.

type Portfolio

type Portfolio struct {
	Name      string   `json:"name" yaml:"name"`
	Orgs      []string `json:"orgs" yaml:"orgs"`            // ["github.com/grokify", "github.com/agentplexus"]
	GraphRepo string   `json:"graphRepo" yaml:"graph_repo"` // Where to persist the graph
	Languages []string `json:"languages" yaml:"languages"`  // ["go", "typescript"]
}

Portfolio represents a collection of GitHub orgs managed together.

type Progress

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

Progress tracks and reports build progress.

func NewProgress

func NewProgress(cfg ProgressConfig) *Progress

NewProgress creates a new progress reporter.

func (*Progress) Complete

func (p *Progress) Complete()

Complete finishes progress tracking and prints summary.

func (*Progress) Error

func (p *Progress) Error(repo string, err error)

Error reports an error during processing.

func (*Progress) FoundModule

func (p *Progress) FoundModule(module string)

FoundModule reports that a Go module was found.

func (*Progress) ProcessRepo

func (p *Progress) ProcessRepo(repo string)

ProcessRepo reports progress on processing a repository.

func (*Progress) Start

func (p *Progress) Start(totalOrgs int)

Start begins tracking progress for a build operation.

func (*Progress) StartOrg

func (p *Progress) StartOrg(org string, repoCount int)

StartOrg begins processing an organization.

type ProgressCallback

type ProgressCallback func(event ProgressEvent)

ProgressCallback is a function called during graph building for progress updates.

type ProgressConfig

type ProgressConfig struct {
	// Writer is where progress is written. Default is os.Stderr.
	Writer io.Writer

	// Enabled controls whether progress is reported.
	Enabled bool

	// MinInterval is the minimum time between progress updates.
	// Default is 100ms.
	MinInterval time.Duration
}

ProgressConfig configures progress reporting.

type ProgressEvent

type ProgressEvent struct {
	Type    ProgressEventType `json:"type"`
	Org     string            `json:"org,omitempty"`
	Repo    string            `json:"repo,omitempty"`
	Module  string            `json:"module,omitempty"`
	Current int               `json:"current,omitempty"`
	Total   int               `json:"total,omitempty"`
	Error   error             `json:"error,omitempty"`
}

ProgressEvent represents a progress update during graph building.

type ProgressEventType

type ProgressEventType string

ProgressEventType indicates the type of progress event.

const (
	ProgressEventStart    ProgressEventType = "start"
	ProgressEventOrg      ProgressEventType = "org"
	ProgressEventRepo     ProgressEventType = "repo"
	ProgressEventModule   ProgressEventType = "module"
	ProgressEventError    ProgressEventType = "error"
	ProgressEventComplete ProgressEventType = "complete"
)

type RepoListCache

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

RepoListCache provides caching for repository listings.

func NewRepoListCache

func NewRepoListCache(cache *Cache) *RepoListCache

NewRepoListCache creates a cache for repo listings.

func (*RepoListCache) Get

func (rlc *RepoListCache) Get(ctx context.Context, owner string) ([]string, bool)

Get retrieves a cached repo list.

func (*RepoListCache) Set

func (rlc *RepoListCache) Set(ctx context.Context, owner string, repos []string) error

Set stores a repo list in the cache.

type StaleModule

type StaleModule struct {
	Module     Module `json:"module"`
	Dependency string `json:"dependency"`
	Current    string `json:"current"`
	Latest     string `json:"latest"`
}

StaleModule represents a module using an outdated dependency.

type UpgradeOrder

type UpgradeOrder struct {
	Modules []Module `json:"modules"`
	Cycles  []Cycle  `json:"cycles,omitempty"`
}

UpgradeOrder represents the recommended order for upgrading modules.

type ValidationIssue

type ValidationIssue struct {
	Type    string `json:"type"`
	Module  string `json:"module"`
	Message string `json:"message"`
}

ValidationIssue represents a problem found during graph validation.

Jump to

Keyboard shortcuts

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