forge

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package forge defines the seam between the review UI and a code-hosting service. The TUI depends only on the Forge interface and the value types here; the concrete adapters — ghcli shelling out to `gh`, glabcli to `glab` — live in subpackages and are wired in from cmd, keeping the UI ignorant of which host (or API style) it is talking to.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FetchAttachmentURL added in v0.0.6

func FetchAttachmentURL(ctx context.Context, url string) ([]byte, error)

FetchAttachmentURL is the adapters' shared plain-HTTP path for attachment URLs that need no API authentication (or as a fallback when the CLI cannot serve them). Size-capped and context-bound.

Types

type AttachmentUploader added in v0.0.16

type AttachmentUploader interface {
	UploadAttachment(ctx context.Context, ref PullRequestRef, path string) (string, error)
}

ReviewComment is a single line comment expressed in host API terms. AttachmentUploader is implemented by forges whose API can receive file uploads for use in comment Markdown (GitLab's project uploads endpoint). GitHub's REST API has no such surface — uploads exist only in the web UI — so the capability is an optional interface rather than a Forge method half the implementations would stub with an error. The returned reference is what comment Markdown should embed (GitLab: a project-relative /uploads path the server resolves inside MR notes).

type Comment

type Comment struct {
	ID        int64
	Author    string
	Body      string
	CreatedAt time.Time
	URL       string
}

Comment is a single review comment as returned by the host.

type Forge

type Forge interface {
	PullRequest(ctx context.Context, ref PullRequestRef) (*PullRequest, error)
	Diff(ctx context.Context, ref PullRequestRef) ([]byte, error)
	Threads(ctx context.Context, ref PullRequestRef) ([]Thread, error)
	CreateReview(ctx context.Context, ref PullRequestRef, event ReviewEvent, summary string, comments []ReviewComment) (*SubmittedReview, error)
	// AddGeneralComment posts a conversation-level comment — one that
	// anchors to the request itself, not to a line (a GitHub issue comment,
	// a GitLab MR note).
	AddGeneralComment(ctx context.Context, ref PullRequestRef, body string) (*Comment, error)
	Reply(ctx context.Context, ref PullRequestRef, commentID int64, body string) (*Comment, error)
	// FileContent returns the raw content of path at rev (a commit id,
	// typically the PR head) — the full-file context view's data source.
	FileContent(ctx context.Context, ref PullRequestRef, path, rev string) ([]byte, error)
	// GeneralComments lists the PR's conversation-level comments (GitHub
	// issue comments, GitLab non-system notes), oldest first — review
	// discussion that anchors to the request itself rather than a line.
	GeneralComments(ctx context.Context, ref PullRequestRef) ([]Comment, error)
	// Attachment fetches an image/file referenced from a comment body (an
	// absolute attachment URL, or a host-relative /uploads path), using the
	// adapter's authentication where the host requires it.
	Attachment(ctx context.Context, ref PullRequestRef, url string) ([]byte, error)
}

Forge is the host-agnostic contract the UI submits reviews through. The UI must not know whether the implementation uses gh, REST, GraphQL, github.com, or an enterprise host.

type Kind

type Kind uint8

Kind identifies which family of forge a host belongs to, so the CLI can pick the matching adapter.

const (
	KindGitHub Kind = iota
	KindGitLab
)

func KindForHost

func KindForHost(host string) Kind

KindForHost guesses the forge kind from a host name. gitlab.com and any host containing "gitlab" (the common self-hosted convention) map to GitLab; everything else — including GitHub Enterprise hosts and an empty host — defaults to GitHub. Hosts that do not follow the convention can be supported later via configuration.

func (Kind) String

func (k Kind) String() string

String returns the short forge name, matching the discovery engine names ("gh", "glab") so the UI badge and the --list engine filter agree.

type ListedRequest

type ListedRequest struct {
	Ref       PullRequestRef
	Title     string
	Author    string
	UpdatedAt time.Time
	URL       string
}

ListedRequest is one pull/merge request returned by a discovery listing.

type Lister

type Lister interface {
	List(ctx context.Context, filter string) ([]ListedRequest, error)
}

Lister discovers open pull/merge requests matching a filter. The filter string is engine-specific: a GitHub search query for the gh engine ("is:open review-requested:@me author:x"), a REST query string for the glab engine ("state=opened&reviewer_username=@me"). An empty filter applies the engine's default.

type PullRequest

type PullRequest struct {
	Ref     PullRequestRef
	Title   string
	Body    string // description, as Markdown
	Author  string
	URL     string // browsable page; empty when the adapter did not report one
	HeadOID string
	// BaseOID is the base commit the diff is against, when the host reports
	// it — the revision old-side file content lives at.
	BaseOID string
	BaseRef string
	HeadRef string
}

PullRequest is the metadata the UI needs about a PR.

type PullRequestRef

type PullRequestRef struct {
	Host   string // e.g. "github.com"
	Owner  string
	Repo   string
	Number int
}

PullRequestRef identifies a pull request (or GitLab merge request) on a host.

func ParseRef

func ParseRef(s string) (ref PullRequestRef, ok bool)

ParseRef recognises a pull/merge-request reference in any of these shapes:

https://github.com/owner/repo/pull/418
https://gitlab.com/group/subgroup/repo/-/merge_requests/418
owner/repo#418          (GitHub-style short form)
group/repo!418          (GitLab-style short form, nested groups allowed)
418  (or #418, !418)    — owner/repo (and host) must be supplied separately,
                          e.g. inferred from the origin remote, so Host,
                          Owner, and Repo are left empty.

It returns ok=false when s is not a reference at all (e.g. a file path).

func (PullRequestRef) String

func (r PullRequestRef) String() string

String renders the canonical short form "host/owner/repo#N" — with GitLab's "!" separator when the host looks like a GitLab instance — as used in error messages and the discovery picker. An empty host prints as github.com, matching ParseRef's default.

func (PullRequestRef) WebURL

func (r PullRequestRef) WebURL() string

WebURL returns the browsable page for the pull request on its host, used as a fallback when the adapter did not report one.

type ReviewComment

type ReviewComment struct {
	Path      string
	Body      string
	Line      int
	Side      string // "LEFT" or "RIGHT"
	StartLine int    // 0 when single-line
	StartSide string
}

type ReviewEvent

type ReviewEvent string

ReviewEvent is the action taken when submitting a review.

const (
	EventComment        ReviewEvent = "COMMENT"
	EventApprove        ReviewEvent = "APPROVE"
	EventRequestChanges ReviewEvent = "REQUEST_CHANGES"
)

type SubmittedReview

type SubmittedReview struct {
	ID  int64
	URL string
}

SubmittedReview is the host's acknowledgement of a created review.

type Thread

type Thread struct {
	Root     Comment
	Replies  []Comment
	Resolved bool
	Outdated bool
	Location *diff.Location
}

Thread groups a root review comment with its replies and host-side state.

Directories

Path Synopsis
Package ghcli implements the forge.Forge interface by shelling out to the GitHub CLI (`gh`).
Package ghcli implements the forge.Forge interface by shelling out to the GitHub CLI (`gh`).
Package glabcli implements the forge.Forge interface by shelling out to the GitLab CLI (`glab`), whose `api` subcommand is a deliberate port of `gh api` (same --method/--input/--hostname/--paginate flags).
Package glabcli implements the forge.Forge interface by shelling out to the GitLab CLI (`glab`), whose `api` subcommand is a deliberate port of `gh api` (same --method/--input/--hostname/--paginate flags).

Jump to

Keyboard shortcuts

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