github

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 9 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 github wraps the GitHub REST API with the narrow surface Golem needs. All access goes through Client so the sync engine can be tested against an in-memory fake with no network.

Index

Constants

This section is empty.

Variables

View Source
var ErrMergeConflictHead = errors.New("github: pull request head moved before merge")

ErrMergeConflictHead: the head moved after validation (409).

View Source
var ErrMergeForbidden = errors.New("github: merge refused by permissions or branch protection")

ErrMergeForbidden: permissions or branch protection refused it (403). A 405 (not mergeable yet) stays a plain, retryable error.

View Source
var ErrPullRequestUnprocessable = errors.New("github: pull request request was unprocessable")

ErrPullRequestUnprocessable wraps a 422 from the pull-request create endpoint. GitHub answers 422 when the request is well-formed but cannot be applied — most often "A pull request already exists for <owner>:<branch>", which is exactly what a redelivered outbox row provokes (finding I2).

It is a status-code sentinel rather than a message match on purpose. A 422 for some other reason (a base branch that does not exist, say) must not be mistaken for "the pull request is already there": the caller confirms that by looking the pull request up with FindPullRequest, and falls back to the ordinary retry path when there is none.

Functions

This section is empty.

Types

type CheckFailure

type CheckFailure struct {
	Name       string
	Conclusion string
	DetailsURL string
	// Summary is the check's own output summary where it has one. It is the
	// only part of a failure the agent can act on without fetching logs, so
	// it is carried even though it is often empty.
	Summary string
	// NeedsHuman marks a failure that no commit can clear, so attempting a
	// fix is guaranteed to fail and only spends an agent run and a push.
	NeedsHuman bool
}

CheckFailure is one failing check on a pull request's head commit.

type Client

type Client interface {
	ListIssuesSince(ctx context.Context, owner, repo, label string, since time.Time, etag string) (IssuePage, error)
	GetIssue(ctx context.Context, owner, repo string, number int) (Issue, error)
	CreateIssue(ctx context.Context, owner, repo, title, body string, labels []string) (Issue, error)
	SetIssueState(ctx context.Context, owner, repo string, number int, state string) error
	AddLabel(ctx context.Context, owner, repo string, number int, label string) error
	RemoveLabel(ctx context.Context, owner, repo string, number int, label string) error
	CreateComment(ctx context.Context, owner, repo string, number int, body string) error
	CreatePullRequest(ctx context.Context, owner, repo, head, base, title, body string, draft bool) (PullRequest, error)
	// FindPullRequest looks up an existing pull request by its head branch.
	// It is what makes a KindPR redelivery converge instead of parking: a
	// pull request that already exists is reported rather than re-created.
	FindPullRequest(ctx context.Context, owner, repo, head string) (PullRequest, bool, error)
	DefaultBranch(ctx context.Context, owner, repo string) (string, error)
	// GetPullRequest and ListFailedChecks are what the pull-request monitor
	// reads: whether the branch still merges, whether CI is green, and
	// whether the pull request is still open at all.
	GetPullRequest(ctx context.Context, owner, repo string, number int) (PullRequestStatus, error)
	// MergePullRequest merges pinned to expectedHeadSHA (a moved head is
	// refused) and returns the merge commit SHA.
	MergePullRequest(ctx context.Context, owner, repo string, number int, expectedHeadSHA string) (string, error)
	ListFailedChecks(ctx context.Context, owner, repo, ref string) ([]CheckFailure, error)
}

Client is the GitHub surface Golem depends on.

func New

func New(token, apiBase string) (Client, error)

New returns a Client authenticated with token. apiBase is empty for github.com, or a GitHub Enterprise base URL (with trailing slash).

apiBase is assigned directly to the underlying client's BaseURL/UploadURL rather than via WithEnterpriseURLs, which appends "/api/v3/" to the path when the host has no "api." prefix — that rewrite is wrong for test servers and for Enterprise operators who already include "/api/v3/" in apiBase. GitHub Enterprise operators must therefore supply the full base URL including the "/api/v3/" suffix.

type Fake

type Fake struct {
	Issues   map[int]Issue
	Comments map[int][]string
	PRs      []PullRequest
	Default  string // default branch returned by DefaultBranch

	// PRStatuses and FailedChecks drive the pull-request monitor. Both are
	// keyed so a test can set up one unhealthy pull request without having
	// to describe every other one: an absent entry means healthy.
	PRStatuses map[int]PullRequestStatus
	// MergeErr makes MergePullRequest return this error for the number,
	// ahead of every other check.
	MergeErr     map[int]error
	FailedChecks map[string][]CheckFailure
	// DraftByHead records the draft flag each PR was opened with.
	DraftByHead map[string]bool

	// ETag, when non-empty, simulates GitHub's conditional-request caching:
	// if ListIssuesSince is called with an etag argument equal to ETag, it
	// returns IssuePage{ETag: ETag, NotModified: true} with no issues,
	// mirroring a real 304 response. This does not model real HTTP caching
	// semantics (e.g. ETag is never itself changed by adding issues) — it is
	// a deliberately simple lever for tests that need to exercise a
	// caller's NotModified handling.
	ETag string

	// PageETag, when non-empty, is the ETag returned on a normal (non-304)
	// page. It is separate from ETag so a test can distinguish "the ETag
	// GitHub is offering now" from "the ETag that provokes a 304", which is
	// what the partial-failure guard needs: a page that fails partway must
	// not store a fresh ETag, or the next poll answers 304 and the issue
	// that failed is never retried.
	PageETag string

	// FailNext, when non-nil, is returned by the next call to any method and
	// then cleared — for exercising retry paths.
	FailNext error

	// Calls records method names in order, for asserting what was and was not
	// called.
	Calls []string
	// contains filtered or unexported fields
}

Fake is an in-memory Client for tests. Its methods are safe for concurrent use — each takes the internal lock. Reading its exported fields (Comments, PRs, Calls, Issues, ETag, FailNext) directly is safe only when nothing else can be calling a Fake method concurrently, which holds for every synchronous test. A caller that inspects or configures state from a separate goroutine while a worker may be calling into the fake in the background — as the ghsync worker's ingest and drain loops do — must use the accessor methods instead (CommentsFor, CallsSnapshot, PRsSnapshot, IssueByNumber, SetFailNext), or race with the lock every method already takes.

func NewFake

func NewFake() *Fake

NewFake returns an empty Fake with a "main" default branch.

func (*Fake) AddIssue

func (f *Fake) AddIssue(i Issue)

AddIssue seeds an issue into the fake.

func (*Fake) AddLabel

func (f *Fake) AddLabel(_ context.Context, _, _ string, number int, label string) error

func (*Fake) CallsSnapshot

func (f *Fake) CallsSnapshot() []string

CallsSnapshot returns a copy of the method-call log recorded so far. Safe for concurrent use, unlike reading the Calls field directly.

func (*Fake) CommentsFor

func (f *Fake) CommentsFor(number int) []string

CommentsFor returns a copy of the comments recorded for the given issue number. Reading the Comments field directly is safe only when nothing else can be calling CreateComment concurrently (true for every synchronous test); a caller that polls Comments from a separate goroutine while a worker may be delivering outbox rows in the background — as the ghsync worker's drain loop does — must use this method instead, or race with the lock CreateComment already takes.

func (*Fake) CreateComment

func (f *Fake) CreateComment(_ context.Context, _, _ string, number int, body string) error

func (*Fake) CreateIssue

func (f *Fake) CreateIssue(_ context.Context, _, _, title, body string, labels []string) (Issue, error)

func (*Fake) CreatePullRequest

func (f *Fake) CreatePullRequest(_ context.Context, _, _, head, _, _, _ string, draft bool) (PullRequest, error)

CreatePullRequest mirrors GitHub's behaviour for a branch that already has a pull request: it refuses with ErrPullRequestUnprocessable rather than opening a second one. That is the 422 a redelivered outbox row provokes, and modelling it here is what lets the suite see finding I2 at all.

func (*Fake) DefaultBranch

func (f *Fake) DefaultBranch(_ context.Context, _, _ string) (string, error)

func (*Fake) FindPullRequest

func (f *Fake) FindPullRequest(_ context.Context, _, _, head string) (PullRequest, bool, error)

func (*Fake) GetIssue

func (f *Fake) GetIssue(_ context.Context, _, _ string, number int) (Issue, error)

func (*Fake) GetPullRequest

func (f *Fake) GetPullRequest(_ context.Context, _, _ string, number int) (PullRequestStatus, error)

PRStatus is what GetPullRequest returns, keyed by pull request number. A number with no entry answers as an open, clean pull request, so a test that does not care about the monitor gets the healthy case for free.

func (*Fake) IssueByNumber

func (f *Fake) IssueByNumber(number int) (Issue, bool)

IssueByNumber returns a copy of the issue with the given number, and whether it exists. Safe for concurrent use, unlike reading the Issues field directly.

func (*Fake) ListFailedChecks

func (f *Fake) ListFailedChecks(_ context.Context, _, _, ref string) ([]CheckFailure, error)

func (*Fake) ListIssuesSince

func (f *Fake) ListIssuesSince(_ context.Context, _, _, label string, since time.Time, etag string) (IssuePage, error)

func (*Fake) MergePullRequest

func (f *Fake) MergePullRequest(_ context.Context, _, _ string, number int, expectedHeadSHA string) (string, error)

MergePullRequest applies the same contract as the real client: the pinned head must match, and a number with no recorded status answers as an open clean pull request. A test controls refusal by setting MergeErr or a mismatched PRStatuses[number].HeadSHA.

func (*Fake) PRsSnapshot

func (f *Fake) PRsSnapshot() []PullRequest

PRsSnapshot returns a copy of the pull requests created so far. Safe for concurrent use, unlike reading the PRs field directly.

func (*Fake) RemoveLabel

func (f *Fake) RemoveLabel(_ context.Context, _, _ string, number int, label string) error

func (*Fake) SetFailNext

func (f *Fake) SetFailNext(err error)

SetFailNext sets the error returned by the next call to any method (then cleared), mirroring the FailNext field. Safe for concurrent use, unlike writing the FailNext field directly.

func (*Fake) SetIssueState

func (f *Fake) SetIssueState(_ context.Context, _, _ string, number int, state string) error

type Issue

type Issue struct {
	Number    int
	Title     string
	Body      string
	State     string // open | closed
	HTMLURL   string
	UpdatedAt time.Time
	Labels    []string
}

Issue is Golem's projection of a GitHub issue — only the fields the sync engine reads.

func (Issue) HasLabel

func (i Issue) HasLabel(name string) bool

HasLabel reports whether the issue carries the named label.

func (Issue) TicketDescription

func (i Issue) TicketDescription() string

TicketDescription builds the ticket description for this issue: the title, a blank line, then the body — or the title alone when the body is empty.

It lives on Issue, in this package, because it is the SINGLE definition of "what text does this issue become". Both paths that turn an issue into a ticket — internal/cli (--from-issue and issue sync) and internal/orchestrator/ghsync (ingest) — call it, so the two cannot produce different bytes from the same issue. They did before: the CLI combined title and body while ingest stored the body alone, which meant an orchestrator agent was never shown the issue title at all, and an issue whose title says everything and whose body is empty became a ticket with an empty description and an agent with an empty task.

It takes no arguments precisely so a caller cannot pass title and body in the wrong order, or pass one issue's title with another's body.

The composed text is what the approval gate hashes. See ghsync.HashDescription: every byte of untrusted issue text that can reach an agent prompt has to be covered by that hash, and after this method the title is such a byte.

type IssuePage

type IssuePage struct {
	Issues      []Issue
	ETag        string
	NotModified bool
}

IssuePage is one response from ListIssuesSince. NotModified is true when GitHub answered 304 to a conditional request, in which case Issues is empty and the caller should keep its existing state.

type PullRequest

type PullRequest struct {
	Number  int
	HTMLURL string
}

PullRequest is Golem's projection of a created pull request.

type PullRequestStatus

type PullRequestStatus struct {
	Number int
	// State is "open" or "closed". Merged distinguishes the two ways a pull
	// request closes, because they mean opposite things for the ticket: a
	// merge is the work landing, a close is it being abandoned.
	State  string
	Merged bool
	// Mergeable is nil while GitHub is still computing the merge commit.
	// That is a genuinely unknown answer and must not be read as "no
	// conflict" — the field is a pointer so the caller cannot accidentally
	// treat "not yet known" as false.
	Mergeable *bool
	// MergeableState is GitHub's own summary: clean, dirty (conflicts),
	// blocked (a required review or check), unstable (a non-required check
	// failed), behind, or unknown.
	MergeableState string
	HeadSHA        string
	BaseRef        string
	// MergeCommitSHA is set once Merged: the commit the base now contains.
	MergeCommitSHA string
}

PullRequestStatus is everything the monitor needs to decide whether a pull request is healthy, needs work, or is finished.

func (PullRequestStatus) Conflicted

func (s PullRequestStatus) Conflicted() bool

Conflicted reports whether the pull request cannot merge because the branch conflicts with its base — as opposed to any of the other reasons a pull request may be unmergeable, such as an outstanding review.

Both signals must agree. mergeable_state alone is not enough: it reads "unknown" while GitHub computes, and briefly after a push, and acting on that would send a ticket back to revising to resolve a conflict that does not exist.

Jump to

Keyboard shortcuts

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