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 ¶
- Variables
- type CheckFailure
- type Client
- type Fake
- func (f *Fake) AddIssue(i Issue)
- func (f *Fake) AddLabel(_ context.Context, _, _ string, number int, label string) error
- func (f *Fake) CallsSnapshot() []string
- func (f *Fake) CommentsFor(number int) []string
- func (f *Fake) CreateComment(_ context.Context, _, _ string, number int, body string) error
- func (f *Fake) CreateIssue(_ context.Context, _, _, title, body string, labels []string) (Issue, error)
- func (f *Fake) CreatePullRequest(_ context.Context, _, _, head, _, _, _ string, draft bool) (PullRequest, error)
- func (f *Fake) DefaultBranch(_ context.Context, _, _ string) (string, error)
- func (f *Fake) FindPullRequest(_ context.Context, _, _, head string) (PullRequest, bool, error)
- func (f *Fake) GetIssue(_ context.Context, _, _ string, number int) (Issue, error)
- func (f *Fake) GetPullRequest(_ context.Context, _, _ string, number int) (PullRequestStatus, error)
- func (f *Fake) IssueByNumber(number int) (Issue, bool)
- func (f *Fake) ListFailedChecks(_ context.Context, _, _, ref string) ([]CheckFailure, error)
- func (f *Fake) ListIssuesSince(_ context.Context, _, _, label string, since time.Time, etag string) (IssuePage, error)
- func (f *Fake) MergePullRequest(_ context.Context, _, _ string, number int, expectedHeadSHA string) (string, error)
- func (f *Fake) PRsSnapshot() []PullRequest
- func (f *Fake) RemoveLabel(_ context.Context, _, _ string, number int, label string) error
- func (f *Fake) SetFailNext(err error)
- func (f *Fake) SetIssueState(_ context.Context, _, _ string, number int, state string) error
- type Issue
- type IssuePage
- type PullRequest
- type PullRequestStatus
Constants ¶
This section is empty.
Variables ¶
var ErrMergeConflictHead = errors.New("github: pull request head moved before merge")
ErrMergeConflictHead: the head moved after validation (409).
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.
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 ¶
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 (*Fake) CallsSnapshot ¶
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 ¶
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 (*Fake) CreateIssue ¶
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 (*Fake) FindPullRequest ¶
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 ¶
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 (*Fake) ListIssuesSince ¶
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 (*Fake) SetFailNext ¶
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.
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) TicketDescription ¶
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 ¶
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 ¶
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.