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
- Variables
- func HandleResponse(resp *http.Response, authenticated bool) error
- func HandleResponseStatus(statusCode int, authenticated bool) error
- func IsConflict(err error) bool
- func IsNoToken(err error) bool
- func IsNotFound(err error) bool
- func IsRateLimited(err error) bool
- func LimitResponseBody(r io.Reader) io.Reader
- func ReadResponse(resp *http.Response) ([]byte, error)
- func ReadResponseBody(r io.Reader) ([]byte, error)
- func RegisterAdapter(forge ForgeType, factory func(Options) (Client, error))
- type Client
- type Comment
- type CommentList
- type ForgeType
- type HTTPError
- type Issue
- type IssueRef
- type IssueThread
- type NoOpClient
- func (c *NoOpClient) AddComment(ctx context.Context, ref IssueRef, body string) (string, error)
- func (c *NoOpClient) Authenticated() bool
- func (c *NoOpClient) Close() error
- func (c *NoOpClient) ListComments(ctx context.Context, ref IssueRef) (CommentList, error)
- func (c *NoOpClient) ReadIssue(ctx context.Context, ref IssueRef) (IssueThread, error)
- type Options
- type Repo
- type User
Constants ¶
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).
const MaxResponseBytes = MaxResponseBodyBytes
MaxResponseBytes is an alias for MaxResponseBodyBytes.
Variables ¶
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 ¶
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 ¶
HandleResponseStatus evaluates an HTTP status code and client authentication state.
func IsConflict ¶
IsConflict returns true if the error wraps ErrConflict or is an HTTPError with status 409, and false otherwise.
func IsNotFound ¶
IsNotFound returns true if the error wraps ErrNotFound or is an HTTPError with status 404, and false otherwise.
func IsRateLimited ¶
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 ¶
LimitResponseBody wraps an io.Reader in an io.LimitReader bounded to MaxResponseBodyBytes (8 MB).
func ReadResponse ¶
ReadResponse reads resp.Body bounded to MaxResponseBodyBytes (8 MB) and closes resp.Body.
func ReadResponseBody ¶
ReadResponseBody reads from r bounded to MaxResponseBodyBytes (8 MB).
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 NewNoOp ¶
func NewNoOp() Client
NewNoOp creates a new NoOpClient satisfying the Client interface.
func NewWithOptions ¶
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 ¶
CommentList contains comments returned from an issue.
type ForgeType ¶
type ForgeType string
ForgeType identifies a supported Git forge platform.
func DetectForge ¶
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 ¶
HTTPErrorFromResponse constructs an HTTPError from an http.Response.
func (*HTTPError) RetryAfter ¶
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 ¶
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.
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 ¶
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) 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 ¶
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 ¶
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 ¶
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.