pr

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 14 Imported by: 1 module, 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 pr provides hew's pull-request workflow in-process: resolve the issue a branch is for, compose the body the conventions prescribe, and create or amend the pull request through an isolated transport. Its API is experimental until hew 1.0.

A Client is created with Open, which resolves the worktree, backend, repository and local branch state from the caller's inputs — never the process's working directory or environment — and carries typed requests, results, warnings and errors. Nothing here prints, exits, or imports the CLI.

Index

Constants

View Source
const (
	// FieldBase is a base branch that could not be resolved or is the head
	// branch itself.
	FieldBase = "base"
	// FieldHead is a head branch that cannot carry a pull request.
	FieldHead = "head"
	// FieldBody is conflicting body sources.
	FieldBody = "body"
	// FieldBodyEdit is an existing body the section reader cannot reproduce
	// losslessly.
	FieldBodyEdit = "body-edit"
)

Request fields named by an *tracker.InvalidInputError this package returns. They are library terms, not flag spellings: the CLI adapter maps them onto its flags and sentences.

Variables

This section is empty.

Functions

This section is empty.

Types

type AmbiguityKind

type AmbiguityKind string

AmbiguityKind names why an amendment could not select one pull request.

const (
	// AmbiguousMatches is a head name that matches several open pull
	// requests — more than one base branch, or a fork's head.
	AmbiguousMatches AmbiguityKind = "several-matches"
	// AmbiguousHead is one match whose head repository is not the repository
	// being amended.
	AmbiguousHead AmbiguityKind = "foreign-head"
)

type AmbiguousPullRequestError

type AmbiguousPullRequestError struct {
	Kind           AmbiguityKind
	Head           string
	OpenCount      int
	Number         int
	HeadRepository string
	Repo           string
}

AmbiguousPullRequestError reports an amendment that cannot safely select the pull request to revise. Mutation needs proof that the match is the only one and that its head belongs to the repository, or an edit could land on somebody else's pull request.

func (*AmbiguousPullRequestError) Error

func (e *AmbiguousPullRequestError) Error() string

type AmendRequest

type AmendRequest struct {
	// For declares the issues the pull request closes, primary first. Empty
	// infers exactly one from the branch and the acting identity's claim.
	For []int
	// Title replaces the pull-request title when set; nil preserves it.
	Title *string
	// Edit changes the body. The zero value edits nothing, which is how a
	// title-only amendment preserves even a free-form body.
	Edit BodyEdit
}

AmendRequest configures Amend.

type AmendResult

type AmendResult = CreateResult

AmendResult is the pull request an amendment revised.

type Body

type Body struct {
	// Sections compose the What / Why / Testing / Risk template.
	Sections Sections
	// Text is a body the caller already has — a file it read, or an existing
	// body being revalidated. It is mutually exclusive with Sections.
	Text string
	// Trailers are the closing references the body must carry. A body that
	// already closes a different set is refused rather than repaired.
	Trailers tracker.PRTrailers
}

Body is a pull-request body to compose: structured sections or text the caller already has, at most one source, plus the closing references the pull request carries. Supplied text is used verbatim apart from missing trailers; composition never rewrites the author's words.

type BodyCheck

type BodyCheck = tracker.BodyCheck

BodyCheck is the advisory result of checking body text the caller already has.

func CheckBody

func CheckBody(text string) (BodyCheck, error)

CheckBody reports advisory findings for body text the caller already has. It never rewrites the text and never fails on a finding.

type BodyEdit

type BodyEdit struct {
	What    *string
	Why     *string
	Testing *string
	Risk    *string
	Text    *string
}

BodyEdit is an amendment's body change. Every field is optional: a nil section pointer preserves the existing value, a pointer to the empty string removes that section, and Text replaces the whole body. Text and the section pointers are mutually exclusive.

func (BodyEdit) IsZero

func (e BodyEdit) IsZero() bool

IsZero reports whether the edit changes no part of the body.

type Client

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

Client is an open pull-request workflow for one checkout and repository. It is safe for concurrent use.

func Open

func Open(ctx context.Context, cfg Config) (*Client, error)

Open opens the tracker a pull request is composed against and the GitHub client it is created through. The backend is resolved once and fixed for the life of the Client; a local tracker has no pull requests, so Open refuses it rather than deferring the failure to the first operation.

func Wrap

func Wrap(store gh.IssueStore, prs gh.PullRequests, repo gh.Repo, state git.State) *Client

Wrap makes a Client from an already-open store and pull-request client: the seam hew's own CLI uses, where it has opened the store once and already knows the branch state. External callers use Open, which resolves all of that.

func (*Client) Amend

func (c *Client) Amend(ctx context.Context, req AmendRequest) (AmendResult, error)

Amend revises the open pull request on the branch: the title when one is given, the named body sections, or the whole body. Omitted sections keep their existing values, and the closing references are re-checked against the declared issue set so an edit cannot quietly change what the merge closes.

Amendment requires exactly one open pull request whose head repository is this repository; the API's head-name match alone also covers fork heads and several base branches, and editing one of those would be somebody else's pull request.

func (*Client) Create

func (c *Client) Create(ctx context.Context, req CreateRequest) (CreateResult, error)

Create opens the draft pull request the workflow prescribes: the linked issue inferred from the claim and branch, exactly one Fixes reference per declared issue, and the repository's default branch as the base.

type ComposedBody

type ComposedBody = tracker.ComposedBody

ComposedBody is the body to publish and the advisory findings about it. Findings never fail the call and never change the text.

func ComposeBody

func ComposeBody(b Body) (ComposedBody, error)

ComposeBody renders a pull-request body, enforcing the closing-reference rule first: the body closes exactly the declared set, and the missing trailers are appended. A conflicting source or a mismatched closing set is a *tracker.InvalidInputError; unmarked code is an advisory finding on the result.

type Config

type Config struct {
	// Worktree is the directory the caller works from. Open reads its current
	// branch and upstream from here and never reads the process's working
	// directory.
	Worktree string
	// Backend optionally pins the backend. Empty means use the backend Detect
	// finds; a local tracker has no pull requests and Open refuses it with
	// NoPullRequestsError.
	Backend tracker.Backend
	// Repo is the GitHub repository as owner/name. Empty means detect it from
	// a github.com origin remote.
	Repo string
	// Actor is the identity the workflow reasons about. On GitHub the acting
	// identity is the authenticated login, so Open reads it there and this is
	// ignored.
	Actor string
	// Notice receives lock-wait and migration notices from the tracker read.
	// Nil discards them; the workflow never writes warnings itself — they ride
	// on the result.
	Notice io.Writer
	// Transport carries every GitHub API request, reads and writes alike. Nil
	// means the default HTTP client; a consumer's tests supply an httptest
	// server's transport to answer GitHub without the network or a `gh`
	// process.
	Transport http.RoundTripper
}

Config configures Open. Its inputs mirror tracker.Config, because the pull request is opened against the tracker the same caller reads.

type CreateRequest

type CreateRequest struct {
	// For declares the issues the pull request closes, primary first. Empty
	// infers exactly one from the branch and the acting identity's claim.
	// Several issues are only ever declared, never inferred.
	For []int
	// Title overrides the derived default. Empty derives
	// `<prefix>: <issue title>` from the primary issue's type.
	Title string
	// Body supplies the body: structured sections, or text the caller already
	// has. The closing references are resolved from For; any set on Body
	// itself is replaced.
	Body Body
	// Ready opens the pull request for review instead of as a draft.
	Ready bool
	// Base overrides the target branch. Empty uses the repository's default.
	Base string
}

CreateRequest configures Create. Every body input is optional: the point of the workflow is that tracker state fills the gaps.

type CreateResult

type CreateResult struct {
	Number      int               `json:"number"`
	URL         string            `json:"url"`
	Draft       bool              `json:"draft"`
	Fixes       int               `json:"fixes"`
	FixedIssues []int             `json:"fixedIssues"`
	Head        string            `json:"head"`
	Base        string            `json:"base"`
	Title       string            `json:"title"`
	Warnings    []Warning         `json:"warnings"`
	Findings    []tracker.Finding `json:"findings"`
}

CreateResult is the pull request a create produced, with the advisory warnings and findings that did not fail it.

func (CreateResult) MarshalJSON

func (r CreateResult) MarshalJSON() ([]byte, error)

MarshalJSON renders the fixed issues, warnings and findings as arrays even when empty.

type ExistingPullRequestError

type ExistingPullRequestError struct {
	Issue  int
	Head   string
	Number int
}

ExistingPullRequestError reports that the branch already has an open pull request, which the API would refuse as a second one. Issue is the linked issue and Number the existing pull request.

func (*ExistingPullRequestError) Error

func (e *ExistingPullRequestError) Error() string

type InferenceFailure

type InferenceFailure string

InferenceFailure names why issue inference could not settle on one issue.

const (
	// InferenceConflict is one claim contradicted by a branch naming a
	// different open issue: two signals disagree, and one of them is wrong.
	InferenceConflict InferenceFailure = "branch-claim-conflict"
	// InferenceAmbiguous is several issues claimed by the acting identity
	// with no branch number to break the tie.
	InferenceAmbiguous InferenceFailure = "several-claims"
	// InferenceMissing is no claim and no branch number naming an open
	// issue — the branch carries no usable signal at all.
	InferenceMissing InferenceFailure = "no-signal"
)

type IssueInferenceError

type IssueInferenceError struct {
	Failure     InferenceFailure
	Branch      string
	Actor       string
	Claimed     []int
	BranchIssue int
}

IssueInferenceError reports that the issue a pull request closes could not be resolved. Guessing is not offered: a wrong closing reference closes the wrong issue on merge. Claimed carries the acting identity's open non-epic claims, and BranchIssue the number the branch names (0 when it names none).

func (*IssueInferenceError) Error

func (e *IssueInferenceError) Error() string

type NoOpenPullRequestError

type NoOpenPullRequestError struct {
	Issue int
	Head  string
}

NoOpenPullRequestError reports an amendment on a branch with no open pull request to revise.

func (*NoOpenPullRequestError) Error

func (e *NoOpenPullRequestError) Error() string

type NoPullRequestsError

type NoPullRequestsError struct{}

NoPullRequestsError reports a workflow that needs pull requests on a backend that has none: the local tracker implements issue storage only.

func (*NoPullRequestsError) Error

func (e *NoPullRequestsError) Error() string

type NoUpstreamError

type NoUpstreamError struct {
	Branch string
}

NoUpstreamError reports a branch GitHub has never seen. A pull request can only be opened for a ref the remote already has, so the branch has to be pushed first; Branch is the local name, which is what the push advice needs.

func (*NoUpstreamError) Error

func (e *NoUpstreamError) Error() string

type Sections

type Sections struct {
	What    string
	Why     string
	Testing string
	Risk    string
}

Sections are the pull-request body's structured fields, mirroring the issue template: What replaces the issue's Fix/Approach, Why its Problem/Goal, Testing is the evidence an issue never carries, and Risk is the merge claim a reviewer weighs.

func (Sections) IsZero

func (s Sections) IsZero() bool

IsZero reports whether no section was provided.

type Warning

type Warning struct {
	Kind WarningKind `json:"kind"`
	// Issue is the subject of WarnUnclaimed and WarnPrimaryTitle.
	Issue int `json:"issue"`
	// Issues is the declared set, primary first, for WarnNoSectionDefaults.
	Issues []int `json:"issues"`
	// Branch is the head a WarnBranchPrefix is about.
	Branch string `json:"branch"`
	// Assignees and Actor describe the claim WarnUnclaimed found.
	Assignees []string `json:"assignees"`
	Actor     string   `json:"actor"`
	// Groups partitions the declared set for WarnUnrelatedSet.
	Groups [][]int `json:"groups"`
}

Warning is one advisory notice. The fields a kind uses are documented on that kind; the rest stay zero.

func (Warning) MarshalJSON

func (w Warning) MarshalJSON() ([]byte, error)

MarshalJSON renders every set a warning can carry as an array even when the warning's kind does not use it.

type WarningKind

type WarningKind string

WarningKind identifies one advisory notice about a pull-request operation, so a caller can filter by cause and render its own sentence. A warning never fails the call: the work is already committed by the time one fires.

const (
	// WarnBranchPrefix reports a head branch outside the prescribed prefixes.
	WarnBranchPrefix WarningKind = "branch-prefix"
	// WarnUnclaimed reports a linked issue the acting identity does not hold.
	// Assignees is empty for an unclaimed issue and names the other claimant
	// otherwise.
	WarnUnclaimed WarningKind = "unclaimed"
	// WarnUnrelatedSet reports declared issues with no shared parent, area
	// label or dependency. Whether a combined pull request stays reviewable
	// is a judgement the library cannot make, so it advises instead.
	WarnUnrelatedSet WarningKind = "unrelated-set"
	// WarnNoSectionDefaults reports a declared set whose What and Why have no
	// default: one issue's Fix does not describe the others.
	WarnNoSectionDefaults WarningKind = "no-section-defaults"
	// WarnPrimaryTitle reports a derived title that comes from the primary
	// issue alone while the pull request closes several.
	WarnPrimaryTitle WarningKind = "primary-title"
	// WarnMissingTesting reports a body with no Testing section.
	WarnMissingTesting WarningKind = "missing-testing"
	// WarnMissingRisk reports a review-ready body with no Risk section.
	WarnMissingRisk WarningKind = "missing-risk"
)

Jump to

Keyboard shortcuts

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