forge

package
v0.8.0 Latest Latest
Warning

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

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

Documentation

Overview

Package forge provides a pluggable code forge interface for issue, pull request, and code review management. Each forge provider (GitHub, Gitea, GitLab) implements the ForgeProvider interface and is registered by name with the Manager. The agent accesses forge operations through tool handlers defined in Tools.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccountConfig

type AccountConfig struct {
	// Name is a short identifier (e.g., "github-primary").
	Name string `yaml:"name"`

	// Provider selects the forge backend: "github" or "gitea".
	Provider string `yaml:"provider"`

	// Token is the API authentication token.
	Token string `yaml:"token"`

	// Owner is the default repository owner for unqualified repo references.
	Owner string `yaml:"owner"`

	// Username is the forge username for commit attribution.
	Username string `yaml:"username"`

	// URL is the API base URL. Required for gitea. Optional for GitHub
	// (defaults to https://api.github.com).
	URL string `yaml:"url"`
}

AccountConfig describes a single forge account.

type ChangedFile

type ChangedFile struct {
	// Filename is the path of the changed file.
	Filename string
	// Status describes the change type: "added", "modified", "removed",
	// or "renamed".
	Status string
	// Additions is the number of lines added in this file.
	Additions int
	// Deletions is the number of lines removed in this file.
	Deletions int
	// Patch is the unified diff patch for this file, if available.
	Patch string
}

ChangedFile represents a file changed in a pull request.

type CheckRun

type CheckRun struct {
	// Name is the check run name (e.g., "CI / lint").
	Name string
	// Status is the check lifecycle: "queued", "in_progress", or
	// "completed".
	Status string
	// Conclusion is the outcome when completed: "success", "failure",
	// "neutral", "cancelled", "skipped", "timed_out", or
	// "action_required".
	Conclusion string
	// StartedAt is when the check run started, if available.
	StartedAt *time.Time
	// CompletedAt is when the check run finished, if available.
	CompletedAt *time.Time
	// DetailsURL links to the full check run output.
	DetailsURL string
}

CheckRun represents a CI check run on a pull request.

type Comment

type Comment struct {
	// ID is the forge-assigned comment identifier.
	ID int64
	// Body is the comment text in markdown.
	Body string
	// Author is the username who posted the comment.
	Author string
	// URL is the web URL for the comment.
	URL string
	// CreatedAt is when the comment was posted.
	CreatedAt time.Time
}

Comment represents a comment on an issue or pull request.

type Commit

type Commit struct {
	// SHA is the abbreviated commit hash (typically 7 characters).
	SHA string
	// Message is the full commit message.
	Message string
	// Author is the commit author name.
	Author string
	// Date is when the commit was authored.
	Date time.Time
}

Commit represents a single commit in a pull request.

type Config

type Config struct {
	Accounts []AccountConfig `yaml:"accounts"`
}

Config holds all forge account configurations.

func (*Config) ApplyDefaults

func (c *Config) ApplyDefaults()

ApplyDefaults fills in missing optional fields with sensible values.

func (Config) Configured

func (c Config) Configured() bool

Configured reports whether at least one forge account is configured with a provider and token.

func (Config) Validate

func (c Config) Validate() error

Validate checks that the configuration is internally consistent.

type ContextProvider added in v0.8.0

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

ContextProvider injects forge account configuration into the system prompt so the model knows which accounts are available without guessing. It implements the agent.ContextProvider interface via structural typing.

func NewContextProvider added in v0.8.0

func NewContextProvider(forgeCtx string) *ContextProvider

NewContextProvider creates a forge context provider from a pre-built context string (typically from Manager.Context).

func (*ContextProvider) GetContext added in v0.8.0

func (p *ContextProvider) GetContext(_ context.Context, _ string) (string, error)

GetContext returns the forge account context block. The userMessage parameter is unused because forge context is static per session.

type ForgeProvider

type ForgeProvider interface {
	// Name returns the provider identifier (e.g., "github", "gitea").
	Name() string

	// CreateIssue creates a new issue and returns it with the
	// server-assigned number and URL.
	CreateIssue(ctx context.Context, repo string, issue *Issue) (*Issue, error)

	// UpdateIssue applies a partial update to an existing issue.
	// Only non-nil fields in the update are changed.
	UpdateIssue(ctx context.Context, repo string, number int, update *IssueUpdate) (*Issue, error)

	// GetIssue retrieves a single issue by number.
	GetIssue(ctx context.Context, repo string, number int) (*Issue, error)

	// ListIssues returns issues matching the given filters.
	ListIssues(ctx context.Context, repo string, opts *ListOptions) ([]*Issue, error)

	// AddComment posts a comment on an issue or pull request.
	AddComment(ctx context.Context, repo string, number int, body string) (*Comment, error)

	// ListPRs returns pull requests matching the given filters.
	ListPRs(ctx context.Context, repo string, opts *ListOptions) ([]*PullRequest, error)

	// GetPR retrieves a single pull request by number.
	GetPR(ctx context.Context, repo string, number int) (*PullRequest, error)

	// GetPRFiles returns the files changed in a pull request.
	GetPRFiles(ctx context.Context, repo string, number int) ([]*ChangedFile, error)

	// GetPRDiff returns the unified diff for a pull request.
	GetPRDiff(ctx context.Context, repo string, number int) (string, error)

	// ListPRCommits returns commits in a pull request.
	ListPRCommits(ctx context.Context, repo string, number int) ([]*Commit, error)

	// ListPRReviews returns reviews for a pull request, including
	// inline comments nested under each review.
	ListPRReviews(ctx context.Context, repo string, number int) ([]*Review, error)

	// SubmitReview creates a new review on a pull request.
	SubmitReview(ctx context.Context, repo string, number int, review *ReviewSubmission) (*Review, error)

	// AddReviewComment posts an inline comment on a pull request diff.
	AddReviewComment(ctx context.Context, repo string, number int, comment *ReviewComment) (*ReviewComment, error)

	// ListChecks returns CI check runs for a pull request.
	ListChecks(ctx context.Context, repo string, number int) ([]*CheckRun, error)

	// MergePR merges a pull request using the specified method.
	MergePR(ctx context.Context, repo string, number int, opts *MergeOptions) (*MergeResult, error)

	// AddReaction adds an emoji reaction to an issue/PR or a specific
	// comment. When commentID is 0, the reaction targets the
	// issue/PR itself.
	AddReaction(ctx context.Context, repo string, number int, commentID int64, emoji string) error

	// RequestReview requests reviews from the specified users.
	RequestReview(ctx context.Context, repo string, number int, reviewers []string) error

	// Search performs a forge-native search of the given kind.
	Search(ctx context.Context, query string, kind SearchKind, limit int) ([]SearchResult, error)
}

ForgeProvider is the interface that forge backends implement. All repository parameters use the "owner/repo" format — bare repo name resolution happens in the tool layer, not the provider.

type GitHub

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

GitHub implements ForgeProvider for GitHub.com and GitHub Enterprise using the google/go-github SDK.

func NewGitHub

func NewGitHub(httpClient *http.Client, token, baseURL string, logger *slog.Logger) (*GitHub, error)

NewGitHub creates a GitHub forge provider. The httpClient should be constructed via httpkit.NewClient. If baseURL is non-empty and not the default GitHub API URL, Enterprise URLs are configured.

func (*GitHub) AddComment

func (g *GitHub) AddComment(ctx context.Context, repo string, number int, body string) (*Comment, error)

AddComment posts a comment on an issue or pull request.

func (*GitHub) AddReaction

func (g *GitHub) AddReaction(ctx context.Context, repo string, number int, commentID int64, emoji string) error

AddReaction adds an emoji reaction to an issue/PR or a specific comment.

func (*GitHub) AddReviewComment

func (g *GitHub) AddReviewComment(ctx context.Context, repo string, number int, comment *ReviewComment) (*ReviewComment, error)

AddReviewComment posts an inline comment on a pull request diff.

func (*GitHub) CreateIssue

func (g *GitHub) CreateIssue(ctx context.Context, repo string, issue *Issue) (*Issue, error)

CreateIssue creates a new issue on the repository.

func (*GitHub) GetIssue

func (g *GitHub) GetIssue(ctx context.Context, repo string, number int) (*Issue, error)

GetIssue retrieves a single issue by number.

func (*GitHub) GetPR

func (g *GitHub) GetPR(ctx context.Context, repo string, number int) (*PullRequest, error)

GetPR retrieves a single pull request by number.

func (*GitHub) GetPRDiff

func (g *GitHub) GetPRDiff(ctx context.Context, repo string, number int) (string, error)

GetPRDiff returns the unified diff for a pull request.

func (*GitHub) GetPRFiles

func (g *GitHub) GetPRFiles(ctx context.Context, repo string, number int) ([]*ChangedFile, error)

GetPRFiles returns the files changed in a pull request.

func (*GitHub) ListChecks

func (g *GitHub) ListChecks(ctx context.Context, repo string, number int) ([]*CheckRun, error)

ListChecks returns CI check runs for a pull request.

func (*GitHub) ListIssues

func (g *GitHub) ListIssues(ctx context.Context, repo string, opts *ListOptions) ([]*Issue, error)

ListIssues returns issues matching the given filters.

func (*GitHub) ListPRCommits

func (g *GitHub) ListPRCommits(ctx context.Context, repo string, number int) ([]*Commit, error)

ListPRCommits returns commits in a pull request.

func (*GitHub) ListPRReviews

func (g *GitHub) ListPRReviews(ctx context.Context, repo string, number int) ([]*Review, error)

ListPRReviews returns reviews for a pull request, including inline comments nested under each review.

func (*GitHub) ListPRs

func (g *GitHub) ListPRs(ctx context.Context, repo string, opts *ListOptions) ([]*PullRequest, error)

ListPRs returns pull requests matching the given filters.

func (*GitHub) MergePR

func (g *GitHub) MergePR(ctx context.Context, repo string, number int, opts *MergeOptions) (*MergeResult, error)

MergePR merges a pull request using the specified method.

func (*GitHub) Name

func (g *GitHub) Name() string

Name returns "github".

func (*GitHub) RequestReview

func (g *GitHub) RequestReview(ctx context.Context, repo string, number int, reviewers []string) error

RequestReview requests reviews from the specified users.

func (*GitHub) Search

func (g *GitHub) Search(ctx context.Context, query string, kind SearchKind, limit int) ([]SearchResult, error)

Search performs a forge-native search of the given kind.

func (*GitHub) SubmitReview

func (g *GitHub) SubmitReview(ctx context.Context, repo string, number int, review *ReviewSubmission) (*Review, error)

SubmitReview creates a new review on a pull request.

func (*GitHub) UpdateIssue

func (g *GitHub) UpdateIssue(ctx context.Context, repo string, number int, update *IssueUpdate) (*Issue, error)

UpdateIssue applies a partial update to an existing issue.

type Issue

type Issue struct {
	// Number is the issue number (e.g., #42).
	Number int
	// Title is the issue title.
	Title string
	// Body is the full issue body in markdown.
	Body string
	// State is the issue lifecycle state: "open" or "closed".
	State string
	// Labels lists the label names applied to this issue.
	Labels []string
	// Assignees lists the usernames assigned to this issue.
	Assignees []string
	// Author is the username who created the issue.
	Author string
	// URL is the web URL for the issue.
	URL string
	// CreatedAt is when the issue was created.
	CreatedAt time.Time
	// UpdatedAt is when the issue was last modified.
	UpdatedAt time.Time
	// CommentCount is the number of comments on the issue.
	CommentCount int
}

Issue represents a forge issue or pull request issue entry.

type IssueUpdate

type IssueUpdate struct {
	// Title replaces the issue title when non-nil.
	Title *string
	// Body REPLACES the entire issue body when non-nil — it does not
	// append. Leave nil to keep the existing body unchanged.
	Body *string
	// State changes the issue state when non-nil: "open" or "closed".
	State *string
	// Labels REPLACES all labels when non-nil. Pass an empty slice to
	// remove all labels.
	Labels *[]string
	// Assignees REPLACES all assignees when non-nil.
	Assignees *[]string
}

IssueUpdate holds fields for updating an issue. Pointer fields distinguish "not provided" (nil) from "set to empty value".

type ListOptions

type ListOptions struct {
	// State filters by lifecycle state: "open", "closed", or "all".
	State string
	// Labels is a comma-separated label filter for issues.
	Labels string
	// Assignee filters issues by assignee username.
	Assignee string
	// Base filters PRs by target branch.
	Base string
	// Head filters PRs by source branch.
	Head string
	// Sort controls result ordering: "created", "updated", or
	// "comments".
	Sort string
	// Direction controls sort direction: "asc" or "desc".
	Direction string
	// Limit caps the number of results per page (default 30, max 100).
	Limit int
	// Page selects the result page (1-indexed, default 1).
	Page int
}

ListOptions controls pagination and filtering for list operations.

type Manager

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

Manager holds configured forge providers and routes operations to the appropriate account. The first account is the primary (default).

func NewManager

func NewManager(cfg Config, logger *slog.Logger) (*Manager, error)

NewManager creates a forge manager from the given configuration. Each account is instantiated with its provider-specific implementation.

func (*Manager) Account

func (m *Manager) Account(name string) (ForgeProvider, error)

Account returns the forge provider for the named account. If name is empty, the primary (first configured) account is used.

func (*Manager) AccountConfig

func (m *Manager) AccountConfig(name string) (AccountConfig, error)

AccountConfig returns the configuration for the named account.

func (*Manager) Context added in v0.8.0

func (m *Manager) Context() string

Context returns a markdown block describing the configured forge accounts for injection into a system prompt. The output is structured JSON wrapped in a fenced code block so the model can immediately identify available accounts, their types, and default owners without guessing. Returns an empty string when no accounts are configured. Tokens are never included.

func (*Manager) ResolveRepo

func (m *Manager) ResolveRepo(accountName, repo string) (string, error)

ResolveRepo converts a repo parameter into "owner/repo" format. If repo already contains a slash it is returned as-is. Otherwise the account's default owner is prepended.

type MergeOptions

type MergeOptions struct {
	// Method is the merge strategy: "squash" (default), "merge", or
	// "rebase".
	Method string
	// CommitTitle overrides the merge commit title.
	CommitTitle string
	// CommitMessage overrides the merge commit body. Supports
	// temp:LABEL references.
	CommitMessage string
}

MergeOptions controls how a pull request is merged.

type MergeResult

type MergeResult struct {
	// SHA is the merge commit hash.
	SHA string
	// Message is the merge result message from the forge.
	Message string
}

MergeResult reports the outcome of a pull request merge.

type PullRequest

type PullRequest struct {
	// Number is the PR number (e.g., #99).
	Number int
	// Title is the PR title.
	Title string
	// Body is the full PR description in markdown.
	Body string
	// State is the PR lifecycle state: "open", "closed", or "merged".
	State string
	// Author is the username who opened the PR.
	Author string
	// Head is the source branch name.
	Head string
	// Base is the target branch name.
	Base string
	// Mergeable indicates whether the PR can be merged cleanly. Nil
	// means the mergeability check has not completed yet.
	Mergeable *bool
	// ReviewState is the aggregate review status (e.g., "approved").
	ReviewState string
	// Additions is the total lines added across all files.
	Additions int
	// Deletions is the total lines removed across all files.
	Deletions int
	// ChangedFiles is the number of files modified.
	ChangedFiles int
	// URL is the web URL for the PR.
	URL string
	// CreatedAt is when the PR was opened.
	CreatedAt time.Time
	// UpdatedAt is when the PR was last modified.
	UpdatedAt time.Time
}

PullRequest represents a forge pull request.

type Review

type Review struct {
	// ID is the forge-assigned review identifier.
	ID int64
	// Author is the username who submitted the review.
	Author string
	// State is the review verdict: "APPROVED", "CHANGES_REQUESTED",
	// "COMMENTED", or "DISMISSED".
	State string
	// Body is the review summary text.
	Body string
	// SubmittedAt is when the review was submitted.
	SubmittedAt time.Time
	// InlineComments are line-level comments attached to this review.
	InlineComments []*ReviewComment
}

Review represents a pull request review with optional inline comments.

type ReviewComment

type ReviewComment struct {
	// ID is the forge-assigned comment identifier (zero for new comments).
	ID int64
	// Body is the comment text.
	Body string
	// Path is the file path in the diff.
	Path string
	// Line is the line number in the diff.
	Line int
	// Side selects the diff side: "LEFT" or "RIGHT" (default "RIGHT").
	Side string
}

ReviewComment represents an inline comment on a pull request diff.

type ReviewSubmission

type ReviewSubmission struct {
	// Event is the review action: "APPROVE", "COMMENT", or
	// "REQUEST_CHANGES".
	Event string
	// Body is the review summary text.
	Body string
}

ReviewSubmission holds the parameters for submitting a new review.

type SearchKind

type SearchKind string

SearchKind identifies the type of forge search to perform.

const (
	// SearchIssues searches issues and pull requests.
	SearchIssues SearchKind = "issues"
	// SearchCode searches file contents.
	SearchCode SearchKind = "code"
	// SearchCommits searches commit messages and metadata.
	SearchCommits SearchKind = "commits"
)

type SearchResult

type SearchResult struct {
	// Number is the issue or PR number (zero for non-issue results).
	Number int
	// Title is the result title or abbreviated SHA for commits.
	Title string
	// URL is the web URL for the result.
	URL string
	// Body is a snippet for code results or description for issues.
	Body string
}

SearchResult represents a single search result from a forge.

type Tools

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

Tools holds forge tool dependencies. Each Handle* method takes the raw argument map from the tool registry and returns formatted text for the LLM. Prefix resolution (temp:LABEL, kb:file.md, etc.) is handled universally by the tool registry's ContentResolver before handlers run — individual handlers receive already-resolved content.

func NewTools

func NewTools(mgr *Manager, logger *slog.Logger) *Tools

NewTools creates forge tools backed by the given manager.

func (*Tools) HandleIssueComment

func (t *Tools) HandleIssueComment(ctx context.Context, args map[string]any) (string, error)

HandleIssueComment posts a comment on an issue or pull request.

func (*Tools) HandleIssueCreate

func (t *Tools) HandleIssueCreate(ctx context.Context, args map[string]any) (string, error)

HandleIssueCreate creates a new issue on a forge repository.

func (*Tools) HandleIssueGet

func (t *Tools) HandleIssueGet(ctx context.Context, args map[string]any) (string, error)

HandleIssueGet retrieves a single issue by number.

func (*Tools) HandleIssueList

func (t *Tools) HandleIssueList(ctx context.Context, args map[string]any) (string, error)

HandleIssueList lists issues matching the given filters.

func (*Tools) HandleIssueUpdate

func (t *Tools) HandleIssueUpdate(ctx context.Context, args map[string]any) (string, error)

HandleIssueUpdate updates an existing issue. Body REPLACES the entire issue body when provided — it does not append.

func (*Tools) HandlePRChecks

func (t *Tools) HandlePRChecks(ctx context.Context, args map[string]any) (string, error)

HandlePRChecks returns CI check runs for a pull request.

func (*Tools) HandlePRCommits

func (t *Tools) HandlePRCommits(ctx context.Context, args map[string]any) (string, error)

HandlePRCommits returns commits in a pull request.

func (*Tools) HandlePRDiff

func (t *Tools) HandlePRDiff(ctx context.Context, args map[string]any) (string, error)

HandlePRDiff returns the unified diff for a pull request, truncated at max_lines (default 2000).

func (*Tools) HandlePRFiles

func (t *Tools) HandlePRFiles(ctx context.Context, args map[string]any) (string, error)

HandlePRFiles returns the files changed in a pull request.

func (*Tools) HandlePRGet

func (t *Tools) HandlePRGet(ctx context.Context, args map[string]any) (string, error)

HandlePRGet retrieves a single pull request by number.

func (*Tools) HandlePRList

func (t *Tools) HandlePRList(ctx context.Context, args map[string]any) (string, error)

HandlePRList lists pull requests matching the given filters.

func (*Tools) HandlePRMerge

func (t *Tools) HandlePRMerge(ctx context.Context, args map[string]any) (string, error)

HandlePRMerge merges a pull request.

func (*Tools) HandlePRReview

func (t *Tools) HandlePRReview(ctx context.Context, args map[string]any) (string, error)

HandlePRReview submits a review on a pull request.

func (*Tools) HandlePRReviewComment

func (t *Tools) HandlePRReviewComment(ctx context.Context, args map[string]any) (string, error)

HandlePRReviewComment posts an inline comment on a pull request diff.

func (*Tools) HandlePRReviews

func (t *Tools) HandlePRReviews(ctx context.Context, args map[string]any) (string, error)

HandlePRReviews returns reviews for a pull request with inline comments.

func (*Tools) HandleReact

func (t *Tools) HandleReact(ctx context.Context, args map[string]any) (string, error)

HandleReact adds an emoji reaction to an issue, PR, or comment.

func (*Tools) HandleRequestReview

func (t *Tools) HandleRequestReview(ctx context.Context, args map[string]any) (string, error)

HandleRequestReview requests reviews from specified users.

func (*Tools) HandleSearch

func (t *Tools) HandleSearch(ctx context.Context, args map[string]any) (string, error)

HandleSearch performs a forge-native search.

Jump to

Keyboard shortcuts

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