Help us shape the future of Go! Take the 2026 Go Developer Survey

pulls

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package pulls owns the pull-request orchestrator. PRs reuse the S21 `issues` row for title/body/state/timeline; this package owns the PR-specific surface — opening, synchronizing, mergeability detection, merge execution.

Entry points are:

Create     — opens a PR (creates the issue row + the pull_requests row)
Synchronize — refreshes commit + file lists + emits a synchronized event
Mergeability — recomputes mergeable / mergeable_state via merge-tree
Merge      — performs the requested merge strategy in a temp worktree
Edit       — title/body
SetState   — close / reopen
SetReady   — draft → ready

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSameBranch       = errors.New("pulls: base and head must differ")
	ErrBaseNotFound     = errors.New("pulls: base ref not found")
	ErrHeadNotFound     = errors.New("pulls: head ref not found")
	ErrNoCommitsToMerge = errors.New("pulls: head has no commits ahead of base")
	ErrAlreadyMerged    = errors.New("pulls: already merged")
	ErrAlreadyClosed    = errors.New("pulls: already closed")
	ErrMergeBlocked     = errors.New("pulls: merge blocked (mergeable_state != clean)")
	ErrMergeMethodOff   = errors.New("pulls: requested merge method is disabled on this repo")
	ErrConcurrentMerge  = errors.New("pulls: PR is being merged by another request")
	ErrPRNotFound       = errors.New("pulls: PR not found")
)

Errors surfaced to handlers.

Functions

func AllowedMethod

func AllowedMethod(repo reposdb.Repo, method string) bool

AllowedMethod returns true when the repo allows the named merge strategy. Falls open for unknown methods so callers get a clear error from the orchestrator.

func EditPR

func EditPR(ctx context.Context, deps Deps, prID int64, title, body string) error

EditPR updates the PR's title + body. Body markdown is re-rendered via the same pipeline issues.Create uses so HTML is consistent.

func Merge

func Merge(ctx context.Context, deps Deps, p MergeParams) error

Merge performs the requested merge inside a temp worktree:

  1. Lock the pull_requests row (FOR UPDATE) to serialize concurrent attempts. The transaction holds for the whole job to keep the lock until the DB state reflects the merge.
  2. Validate state: not merged, not closed, mergeable_state == clean, method allowed by the repo config.
  3. Resolve identity (author = PR author for squash, merger otherwise; committer = merger).
  4. Run repogit.PerformMerge against the bare repo.
  5. Persist merge_commit_sha + merged_at + flip the issue to closed with state_reason='completed'.
  6. Auto-close linked issues parsed from PR body + commit messages.
  7. Emit a `merged` timeline event.

func Mergeability

func Mergeability(ctx context.Context, deps Deps, gitDir string, prID int64) error

Mergeability runs the merge-tree probe and persists the result. Order of state checks (highest priority first):

dirty   — git merge-tree reports conflicts
behind  — head has no commits ahead of base
blocked — required reviews missing OR an undismissed
          request_changes review exists (S23 gate)
clean   — merge-tree clean and review gate satisfied

`blocked` is set by the S23 review evaluator; when no protection rule applies and no request_changes review exists, the gate is a no-op and we fall through to clean.

func SetReady

func SetReady(ctx context.Context, deps Deps, actorUserID, prID int64) error

SetReady flips draft → false and emits a `ready_for_review` event.

func SetState

func SetState(ctx context.Context, deps Deps, gitDir string, actorUserID, prID int64, newState string) error

SetState delegates to issues.SetState with a couple of PR-specific guardrails:

  • reopening a merged PR is a no-op (a merged PR can't reopen).
  • reopening when base or head no longer resolves returns a typed error so the handler can render "branches moved, cannot reopen".

func Synchronize

func Synchronize(ctx context.Context, deps Deps, gitDir string, prID int64) error

Synchronize re-snapshots the PR's base/head OIDs, refreshes the commits + files lists, and emits a `synchronized` event into the issue timeline. Called from the pr:synchronize worker job after any push to the head ref.

Types

type CreateParams

type CreateParams struct {
	RepoID       int64
	AuthorUserID int64
	Title        string
	Body         string
	BaseRef      string
	HeadRef      string
	Draft        bool
	GitDir       string // resolved from RepoFS by the caller
}

CreateParams describes a new-PR request.

type CreateResult

type CreateResult struct {
	Issue       issuesdb.Issue
	PullRequest pullsdb.PullRequest
}

CreateResult bundles the issue row + the PR row (post-snapshot).

func Create

func Create(ctx context.Context, deps Deps, p CreateParams) (CreateResult, error)

Create opens a PR. Validates that base/head are distinct and resolve in the on-disk repo, snapshots their OIDs, then creates the issues row + pull_requests row in one tx. Mergeability is `unknown` until the worker job ticks.

type Deps

type Deps struct {
	Pool   *pgxpool.Pool
	Logger *slog.Logger
	// Audit is optional; when non-nil, Merge and SetState write audit
	// rows. Mirrors the issues orchestrator's contract so PR-side
	// state changes are equally traceable (S00-S25 audit, M).
	Audit *audit.Recorder
}

Deps wires this package against the rest of the runtime.

type MergeParams

type MergeParams struct {
	PRID         int64
	ActorUserID  int64
	GitDir       string
	Method       string // "merge" | "squash" | "rebase"
	Subject      string // optional override; falls back to "<title> (#<number>)"
	Body         string
	WorktreesDir string // optional, defaults to <gitDir>/.tmp-worktrees
	Now          func() time.Time
}

MergeParams describes a merge request.

Directories

Path Synopsis
Package review owns PR-review orchestration: inline comments, review submission with attached pending comments, dismissal, reviewer requests, thread resolution, and the required-reviews gate consulted by the merge handler.
Package review owns PR-review orchestration: inline comments, review submission with attached pending comments, dismissal, reviewer requests, thread resolution, and the required-reviews gate consulted by the merge handler.

Jump to

Keyboard shortcuts

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