issuex

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package issuex is the spec CLI's forge client: it reads a GitHub or GitLab issue, pull request or merge request with its comments, which is one of the inputs the CLI accepts, and posts a comment back for --comment.

The forge is decided from the URL's host, or, without a URL, from the working tree's origin remote. A host that names neither forge is refused before any request is sent, so a token never leaves for a host it does not belong to. Tokens come from GITHUB_TOKEN or GH_TOKEN, and GITLAB_TOKEN; GITHUB_API_URL and GITLAB_API_URL name a self-hosted instance.

Index

Constants

View Source
const MaxResponseBodyBytes = 8 << 20

MaxResponseBodyBytes is the maximum number of bytes read from an HTTP response body by the shared transport safety utilities (8 MB = 8388608 bytes).

View Source
const MaxResponseBytes = MaxResponseBodyBytes

MaxResponseBytes is an alias for MaxResponseBodyBytes.

Variables

View Source
var (
	// ErrNoToken indicates that an operation requiring authentication was attempted without credentials.
	ErrNoToken = errors.New("no authentication token configured")

	// ErrNotFound indicates that the requested repository, issue, or pull request was not found.
	ErrNotFound = errors.New("resource not found")

	// ErrConflict indicates a state conflict, such as a merge conflict or duplicate label.
	ErrConflict = errors.New("conflict")

	// ErrRateLimited indicates that the forge rate limit has been exceeded and retries were exhausted or backoff was too long.
	ErrRateLimited = errors.New("rate limited")

	// ErrUnsupportedForge indicates that the requested or detected forge type is not supported.
	ErrUnsupportedForge = errors.New("unsupported forge")

	// ErrAmbiguousForge indicates that the forge type could not be determined from configuration or environment.
	ErrAmbiguousForge = errors.New("ambiguous forge")
)

Sentinel errors defined by the issuex package.

Functions

func HandleResponse

func HandleResponse(resp *http.Response, authenticated bool) error

HandleResponse inspects an HTTP response and client authentication state. If the status indicates an error (status >= 400), it returns the error classifyError describes; for successful responses (status < 400), it returns nil.

func HandleResponseStatus

func HandleResponseStatus(statusCode int, authenticated bool) error

HandleResponseStatus evaluates an HTTP status code and client authentication state.

func IsConflict

func IsConflict(err error) bool

IsConflict returns true if the error wraps ErrConflict or is an HTTPError with status 409, and false otherwise.

func IsNoToken

func IsNoToken(err error) bool

IsNoToken returns true if the error wraps ErrNoToken, and false otherwise.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if the error wraps ErrNotFound or is an HTTPError with status 404, and false otherwise.

func IsRateLimited

func IsRateLimited(err error) bool

IsRateLimited returns true if the error wraps ErrRateLimited or is an HTTPError with status 429 or a 403 rate limit (forbiddenByRateLimit), and false otherwise.

func LimitResponseBody

func LimitResponseBody(r io.Reader) io.Reader

LimitResponseBody wraps an io.Reader in an io.LimitReader bounded to MaxResponseBodyBytes (8 MB).

func ReadResponse

func ReadResponse(resp *http.Response) ([]byte, error)

ReadResponse reads resp.Body bounded to MaxResponseBodyBytes (8 MB) and closes resp.Body.

func ReadResponseBody

func ReadResponseBody(r io.Reader) ([]byte, error)

ReadResponseBody reads from r bounded to MaxResponseBodyBytes (8 MB).

func RegisterAdapter

func RegisterAdapter(forge ForgeType, factory func(Options) (Client, error))

RegisterAdapter registers a client factory function for a specific ForgeType. This allows provider packages (such as issuex_github or issuex_gitlab) to register their implementations.

Types

type Client

type Client interface {
	// Authenticated reports whether the client holds a token and so can write.
	Authenticated() bool
	// Close releases the client's resources.
	Close() error

	// ReadIssue reads an issue, pull request or merge request and its comments.
	ReadIssue(ctx context.Context, ref IssueRef) (IssueThread, error)
	// AddComment posts a comment on the thread and returns its web URL.
	AddComment(ctx context.Context, ref IssueRef, body string) (string, error)
	// ListComments reads the thread's comments, oldest first.
	ListComments(ctx context.Context, ref IssueRef) (CommentList, error)
}

Client is the forge-neutral client the spec CLI reads its input through and posts the finished PRD back with. It covers GitHub and GitLab issues, pull requests and merge requests.

func NewGitHub

func NewGitHub(o Options) (Client, error)

NewGitHub constructs a GitHub client adapter directly from Options.

func NewGitLab

func NewGitLab(o Options) (Client, error)

NewGitLab constructs a GitLab client adapter directly from Options.

func NewNoOp

func NewNoOp() Client

NewNoOp creates a new NoOpClient satisfying the Client interface.

func NewWithOptions

func NewWithOptions(o Options) (Client, error)

NewWithOptions instantiates a Client based on the provided Options.

type Comment

type Comment struct {
	ID        int64     `json:"id,omitempty"`
	Body      string    `json:"body"`
	User      User      `json:"user"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	URL       string    `json:"url,omitempty"`
	HTMLURL   string    `json:"html_url,omitempty"`
}

Comment represents an issue comment.

type CommentList

type CommentList struct {
	Comments  []Comment `json:"comments"`
	Truncated bool      `json:"truncated"`
}

CommentList contains comments returned from an issue.

type ForgeType

type ForgeType string

ForgeType identifies a supported Git forge platform.

const (
	// ForgeTypeUnknown represents an unknown or unresolved forge type.
	ForgeTypeUnknown ForgeType = ""
	// ForgeTypeGitHub represents the GitHub platform.
	ForgeTypeGitHub ForgeType = "github"
	// ForgeTypeGitLab represents the GitLab platform.
	ForgeTypeGitLab ForgeType = "gitlab"
)

func DetectForge

func DetectForge(o Options) (ForgeType, error)

DetectForge resolves the ForgeType from explicit configuration, environment variables, or the local git origin remote.

type HTTPError

type HTTPError struct {
	Method             string
	Path               string
	Status             int
	Message            string
	RetryAfterHeader   string
	RateLimitReset     string
	RateLimitRemaining string
}

HTTPError represents an HTTP error response returned by a forge API.

func HTTPErrorFromResponse

func HTTPErrorFromResponse(resp *http.Response) *HTTPError

HTTPErrorFromResponse constructs an HTTPError from an http.Response.

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error formats the HTTPError as "<Method> <Path>: <Status> <Message>".

func (*HTTPError) RetryAfter

func (e *HTTPError) RetryAfter() (time.Duration, bool)

RetryAfter calculates the backoff duration when status is 429 or status is 403 with RateLimitRemaining "0". It inspects numeric seconds in Retry-After header or epoch timestamp in X-RateLimit-Reset header, adds a 1-second buffer, and returns the duration with true. If not rate limited or headers are missing, it returns (0, false).

type Issue

type Issue struct {
	Number    int        `json:"number"`
	Title     string     `json:"title"`
	Body      string     `json:"body"`
	State     string     `json:"state"`
	URL       string     `json:"url,omitempty"`
	HTMLURL   string     `json:"html_url,omitempty"`
	Author    User       `json:"author"`
	Labels    []string   `json:"labels,omitempty"`
	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	ClosedAt  *time.Time `json:"closed_at,omitempty"`
	IsPR      bool       `json:"is_pr"`
}

Issue represents an issue or pull request metadata.

type IssueRef

type IssueRef struct {
	Repo          Repo `json:"repo"`
	Number        int  `json:"number"`
	IsPullRequest bool `json:"is_pull_request"`
}

IssueRef identifies an issue or pull request in a repository by number.

func ParseIssueURL

func ParseIssueURL(s string) (IssueRef, bool)

ParseIssueURL parses GitHub web issue/PR URLs and GitLab web issue/MR URLs into an IssueRef. Returns (IssueRef{}, false) if the input is malformed, non-numeric, or a non-issue path.

func (IssueRef) String

func (r IssueRef) String() string

String returns "owner/repo#number".

func (IssueRef) URL

func (r IssueRef) URL() string

URL returns the canonical web URL for the issue or pull request.

type IssueThread

type IssueThread struct {
	Issue       Issue     `json:"issue"`
	Comments    []Comment `json:"comments"`
	Truncated   bool      `json:"truncated"`
	CommentsErr error     `json:"comments_err,omitempty"`
}

IssueThread represents an issue and its comments.

type NoOpClient

type NoOpClient struct{}

NoOpClient is an offline, forge-disabled client. It reads nothing and writes nothing: a workspace with no forge gets it.

func (*NoOpClient) AddComment

func (c *NoOpClient) AddComment(ctx context.Context, ref IssueRef, body string) (string, error)

AddComment performs no remote operation and returns an empty URL and nil.

func (*NoOpClient) Authenticated

func (c *NoOpClient) Authenticated() bool

Authenticated returns false for NoOpClient.

func (*NoOpClient) Close

func (c *NoOpClient) Close() error

Close returns nil for NoOpClient.

func (*NoOpClient) ListComments

func (c *NoOpClient) ListComments(ctx context.Context, ref IssueRef) (CommentList, error)

ListComments returns a zero-value CommentList and nil.

func (*NoOpClient) ReadIssue

func (c *NoOpClient) ReadIssue(ctx context.Context, ref IssueRef) (IssueThread, error)

ReadIssue returns a zero-value IssueThread and nil.

type Options

type Options struct {
	BaseURL    string
	Token      string
	HTTPClient *http.Client
	UserAgent  string
	NoOp       bool
	Repo       Repo
	RemoteURL  string
	// Dir is the working tree whose origin remote decides the forge when
	// nothing else does. Empty means the process's working directory.
	Dir string
	// contains filtered or unexported fields
}

Options configures client creation for Git forge operations.

type Repo

type Repo struct {
	Owner string `json:"owner"`
	Name  string `json:"name"`
	Host  string `json:"host,omitempty"`
}

Repo names a Git repository across GitHub or GitLab. For GitLab, Owner may contain nested group segments (e.g. "group/subgroup").

func DetectRepo

func DetectRepo(dir string) (Repo, bool)

DetectRepo executes "git -C <dir> remote get-url origin" and parses the output with ParseRemote. If the directory is not a git repository or origin remote is not configured, it returns (Repo{}, false) without error.

func ParseRemote

func ParseRemote(remote string) (Repo, bool)

ParseRemote parses git remote URLs across HTTPS, SSH, and SCP-style syntax matching a supported forge host into a Repo. It supports multi-segment owner paths (e.g. for GitLab) and strips the .git suffix. Returns (Repo{}, false) for invalid remote URLs, missing hosts, or unsupported formats.

func ParseRepo

func ParseRepo(s string) (Repo, bool)

ParseRepo parses a slash-delimited repository path (e.g. "owner/repo" or "group/subgroup/project") into a Repo. The last segment is assigned to Name, and all preceding segments joined by slash are assigned to Owner. Returns (Repo{}, false) if the input is empty or lacks slash-delimited segments.

func (Repo) String

func (r Repo) String() string

String returns "Owner/Name".

func (Repo) Valid

func (r Repo) Valid() bool

Valid reports whether both Owner and Name are non-empty.

type User

type User struct {
	Login string `json:"login"`
}

User represents an account on the forge.

Jump to

Keyboard shortcuts

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