github

package
v0.0.0-...-34312ec Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 21 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 implements api.Tracker over the GitHub issue thread, which is the only state the factory keeps. It also holds the comment marker codec, the ChatOps parser with its trust rule, and the PR body renderer; all three are ported from the v8 TypeScript runner and keep its wire formats.

Index

Constants

View Source
const (
	LabelReady            = "factory:ready"
	LabelBlocked          = "factory:blocked"
	LabelOverlap          = "factory:overlap"
	LabelTriaging         = "factory:triaging"
	LabelPlanning         = "factory:planning"
	LabelAwaitingApproval = "factory:awaiting-approval"
	LabelBuilding         = "factory:building"
	LabelVerifying        = "factory:verifying"
	LabelInReview         = "factory:in-review"
	LabelNeedsInfo        = "factory:needs-info"
	LabelNeedsHuman       = "factory:needs-human"
	LabelFailed           = "factory:failed"
	LabelMonitor          = "factory:monitor"
	LabelLedger           = "factory:ledger"
)

Label names, ported from src/labels.ts. They are the only place a factory label is spelled.

View Source
const RequestMarker = "request"

RequestMarker names the hidden marker whose payload is a hash of the whole request a ticket was filed from.

View Source
const RoleCacheTTL = 5 * time.Minute

RoleCacheTTL is how long a login's role is remembered, so a waiting issue does not cost an API call per poll.

Variables

View Source
var ErrNoSuchUser = errors.New("github tracker: no such user")

ErrNoSuchUser is what a KindOf lookup returns for an actor GitHub cannot resolve, such as an App without a backing user account.

Functions

func AllowedBot

func AllowedBot(actor, list string) bool

AllowedBot reports whether actor is on the comma-separated allow list, or the list is "*".

func BotLogin

func BotLogin(login string) string

BotLogin returns login lower-cased with the "[bot]" suffix REST uses, which GraphQL leaves off.

func CheckActor

func CheckActor(ctx context.Context, kindOf KindOf, actor, allowedBots string) (bool, error)

CheckActor reports whether actor is a human, or a bot on the allow list. The allow list never applies to a User account. A failed lookup is an error and refuses the actor.

func Epic

func Epic(t *Tracker) plancmd.EpicFunc

Epic returns how `factory plan --epic n` reads epic issue n: its body. A missing issue is a "not found" error that names the epic.

func IssueRef

func IssueRef(n int) string

IssueRef names a GitHub issue so the PR body links to it.

func OwnsLabel

func OwnsLabel(name string) bool

OwnsLabel reports whether name is a lifecycle label SetPhase may add or remove.

func PlanApproval

func PlanApproval(ctx context.Context, tr *Trust, cs []Comment) int64

PlanApproval returns the id of the comment that approves the latest plan in the thread, or 0. Approval is a trusted `/factory approve` comment posted after that plan and not yet handled, and no later revise or cancel has voided it. The task text, an earlier comment, a plain-text "ok" or a UI toggle never count, so act cannot start in the turn that wrote the plan.

func Redact

func Redact(s string) string

Redact drops every line that carries model, token, cost, claude.ai or key detail.

func RenderPRBody

func RenderPRBody(body string, p PRParts) string

RenderPRBody assembles the PR body: What changed, the pr stage's text, Acceptance, the closing line and the pointer to the run-details comment. It carries no footer, and Redact runs over the result so no model name, token count, cost or claude.ai link reaches the PR (#210).

func RenderWhatChanged

func RenderWhatChanged(goal string, files []string, proof *Proof) string

RenderWhatChanged builds the body's opening section from the goal, the diff and the proof (src/engine/pr-body.ts).

func SameLogin

func SameLogin(a, b string) bool

SameLogin reports whether a and b name the same account. Logins are case-insensitive, a "[bot]" suffix is ignored, and an empty login matches nothing.

func Unsigned

func Unsigned(body string) string

Unsigned removes a trailing control-plane signature line.

Types

type Command

type Command struct {
	Kind  CommandKind
	Text  string
	Force bool
	Step  string
}

Command is a parsed comment. Text is revise feedback or an answer; Force and Step belong to retry.

func ParseChatOps

func ParseChatOps(body string) Command

ParseChatOps turns a comment body into a command: a `/factory <verb>` line, or else a plain-text answer. It does no trust check; use Trust.Command for that.

type CommandKind

type CommandKind string

CommandKind names what a comment asks for.

const (
	CmdApprove CommandKind = "approve"
	CmdRevise  CommandKind = "revise"
	CmdRetry   CommandKind = "retry"
	CmdCancel  CommandKind = "cancel"
	CmdAnswer  CommandKind = "answer"
)

The ChatOps verbs, plus a plain-text answer to the latest open question.

type Comment

type Comment struct {
	ID     int64
	Author string
	// Actor is who last created or edited the comment when that is known; empty means the author.
	Actor     string
	Body      string
	CreatedAt time.Time
}

Comment is one issue comment as the codec and trust rules see it.

func FindStatus

func FindStatus(comments []Comment) (Comment, bool)

FindStatus returns the latest comment that carries the status marker.

type Config

type Config struct {
	Owner, Repo string
	// HTTPClient carries the credentials; nil uses http.DefaultClient.
	HTTPClient *http.Client
	// BaseURL overrides the API root (GitHub Enterprise, or a test server). Empty means api.github.com.
	BaseURL string
	// Now is the clock for status times and the role cache; nil uses time.Now.
	Now func() time.Time
	// AllowedBots is a comma-separated list of bot logins whose comments count, or "*". Empty trusts humans only.
	AllowedBots string
}

Config says which repository to track and how to reach GitHub. Auth lives in HTTPClient (an injected transport), never in this package.

type Event

type Event struct {
	Kind            EventKind
	Action          string
	Actor, Author   string
	Label, Assignee string
}

Event is the part of a GitHub event the trigger rules read. Actor is who caused it; Author wrote the comment or review; Label and Assignee are those the event names.

func (Event) Fires

func (e Event) Fires(cfg TriggerConfig) bool

Fires reports whether e may start a run. Deleting or editing someone else's comment or review fires an event with the moderator as actor, so the text must come from the actor; a label or an assignment fires only when it is the configured one.

type EventKind

type EventKind string

EventKind is the GitHub object an Event is about.

const (
	EvComment EventKind = "comment"
	EvReview  EventKind = "review"
	EvIssue   EventKind = "issue"
)

The event kinds the trigger rules distinguish.

type KindOf

type KindOf func(ctx context.Context, login string) (string, error)

KindOf looks up a login's account type ("User", "Bot", "Organization").

type PRParts

type PRParts struct {
	WhatChanged string
	Acceptance  string
	Closes      string
	// RunDetails names the issue holding the run-details comment (see IssueRef).
	RunDetails string
}

PRParts is what the PR body is built from beyond the pr stage's own text. Each piece is already rendered.

type Proof

type Proof struct {
	Status   string
	Tests    []string
	Reverted []string
}

Proof is the verify stage's test-that-bites result.

type Role

type Role string

Role is a login's permission on the repository.

const (
	RoleAdmin    Role = "admin"
	RoleMaintain Role = "maintain"
	RoleWrite    Role = "write"
	RoleTriage   Role = "triage"
	RoleRead     Role = "read"
	RoleNone     Role = "none"
)

Repository roles, as GitHub reports them.

func (Role) Trusted

func (r Role) Trusted() bool

Trusted reports whether the role is write access or higher (src/ports/scm.ts TRUSTED_ROLES).

type Tracker

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

Tracker is the GitHub issue thread as an api.Tracker.

func New

func New(cfg Config) (*Tracker, error)

New returns a Tracker for cfg.Owner/cfg.Repo.

func (*Tracker) Ack

func (t *Tracker) Ack(ctx context.Context, id api.TicketID, ref int64) error

Ack records on the thread that the command in comment ref was handled.

func (*Tracker) Active

func (t *Tracker) Active(ctx context.Context) ([]api.Ticket, error)

Active lists open issues labeled factory:building or factory:verifying, oldest first, in one paged query (the issues API cannot OR labels, so the label test is made here). Ready, in-review and parked issues are not active. Pull requests are skipped.

func (*Tracker) Commands

func (t *Tracker) Commands(ctx context.Context, id api.TicketID) ([]observe.Command, error)

Commands returns the retry, cancel, approve and revise commands trusted humans wrote that the loop has not yet acknowledged, oldest first. A comment from anyone else, or one that only looks like a command inside a marker, is ignored.

func (*Tracker) Comment

func (t *Tracker) Comment(ctx context.Context, id api.TicketID, body string) (err error)

Comment posts body on the issue thread through sanitize.Publication (which drops every `<!--`), unless ctx came from marker.Allow: the caller then built body itself and its markers go out as written.

func (*Tracker) Comments

func (t *Tracker) Comments(ctx context.Context, id api.TicketID) ([]string, error)

Comments returns the bodies of the issue's comments, oldest first. A comment carrying a queue marker is kept only when the tracker's own login wrote it: the merge queue merges the PR a marker names, so a marker anyone else posts (naming a fork PR, say) must never reach it.

func (*Tracker) Create

func (t *Tracker) Create(ctx context.Context, tk api.Ticket) (out api.TicketID, err error)

Create files t as an open issue and returns its number. The body is posted as given, so a planned body v1 (with its Files and Blocked by lines) reaches GitHub byte for byte. With no labels the issue gets factory:ready. A title already filed, found by search or created by this Tracker, returns the first id and posts nothing. A body carrying a request marker is matched on title and marker together, so two requests with the same first line stay two issues.

func (*Tracker) Find

func (t *Tracker) Find(ctx context.Context, title string) (api.TicketID, bool, error)

Find searches the repository's open issues for one with exactly this title. The search API matches loosely, so each hit is compared exactly and pull requests are ignored.

func (*Tracker) Get

func (t *Tracker) Get(ctx context.Context, id api.TicketID) (api.Ticket, error)

Get reads one issue. A pull request number is an error.

func (*Tracker) InPhase

func (t *Tracker) InPhase(ctx context.Context, p api.Phase) ([]api.TicketID, error)

InPhase lists the open issues that show phase p, in the order they entered it: by when the phase label was added, read from the issue timeline. Phases that share a label are told apart only for queued, where the issue must also carry a queue marker comment and its first marker's time is the entry time (done and queued tickets share factory:in-review). Pull requests are skipped.

func (*Tracker) LoadPosition

func (t *Tracker) LoadPosition(ctx context.Context, id api.TicketID) (observe.Position, bool, error)

LoadPosition reads the position the runner last wrote on the issue.

func (*Tracker) Name

func (t *Tracker) Name() string

Name returns "github".

func (*Tracker) OpenPR

func (t *Tracker) OpenPR(ctx context.Context, id api.TicketID, branch, base string, files []string) (num int, err error)

OpenPR opens the draft pull request for a finished branch, its body rendered from the plan, the verdict and the pr stage's text in the thread. It returns 0 without opening when the branch has had a pull request, open or closed.

func (*Tracker) Parked

func (t *Tracker) Parked(ctx context.Context) ([]api.Ticket, error)

Parked lists the open issues that carry the factory:needs-human label, oldest first.

func (*Tracker) PutSpendComment

func (t *Tracker) PutSpendComment(ctx context.Context, id api.TicketID, body string) (err error)

PutSpendComment edits the issue's spend ledger comment in place, or posts it the first time.

func (*Tracker) Ready

func (t *Tracker) Ready(ctx context.Context) ([]api.Ticket, error)

Ready lists the open issues that carry the factory:ready label, oldest first. Pull requests are skipped.

func (*Tracker) RecordStatus

func (t *Tracker) RecordStatus(ctx context.Context, id api.TicketID, p api.Phase) (err error)

RecordStatus writes the phase into the status comment, creating it on first use. Running sets the first dispatch time once; Merged sets the merge time; other phases only update the phase.

func (*Tracker) SavePosition

func (t *Tracker) SavePosition(ctx context.Context, id api.TicketID, p observe.Position) (err error)

SavePosition writes the workflow position into one comment, edited in place, posted the first time.

func (*Tracker) SetPhase

func (t *Tracker) SetPhase(ctx context.Context, id api.TicketID, p api.Phase) (err error)

SetPhase swaps the issue's lifecycle label for the one that shows phase. It removes only labels the factory owns (see OwnsLabel) and leaves type, provenance and human labels alone.

func (*Tracker) SpendComment

func (t *Tracker) SpendComment(ctx context.Context, id api.TicketID) (string, error)

SpendComment returns the body of the spend ledger comment on the issue, or "" when none exists.

func (*Tracker) Thread

func (t *Tracker) Thread(ctx context.Context, id api.TicketID) (store.Thread, error)

Thread reads what the issue thread says about the run: the status marker's first dispatch and merge times and how many `/factory` commands trusted humans wrote. It lets store.Rebuild restore those rows after the store is deleted.

type TriggerConfig

type TriggerConfig struct{ Label, Assignee string }

TriggerConfig names the label and assignee that start a run. An empty one never matches.

type Trust

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

Trust decides whose comments count. One rule for labels and commands: the runner's own login, or write access or higher. An author association alone is not enough, since a read-only collaborator is a COLLABORATOR too.

func NewTrust

func NewTrust(self string, roleOf func(ctx context.Context, login string) (Role, error), now func() time.Time) *Trust

NewTrust builds a Trust for the runner login self. roleOf looks a login up on the repository; a failed lookup trusts nobody.

func (*Trust) Command

func (t *Trust) Command(ctx context.Context, c Comment) (Command, bool)

Command parses c when a trusted author wrote it and it is not one of the runner's own marker comments. Anyone else's words are ignored (ok is false), so a read-only collaborator's text never becomes a command or an answer.

func (*Trust) Trusted

func (t *Trust) Trusted(ctx context.Context, login string) bool

Trusted reports whether login is the runner or has write access or higher, and, once WithActors is set, a human or an allow-listed bot.

func (*Trust) TrustedOnly

func (t *Trust) TrustedOnly(ctx context.Context, comments []Comment) []Comment

TrustedOnly keeps the comments the runner or a writer wrote, whatever their bodies say.

func (*Trust) WithActors

func (t *Trust) WithActors(kindOf KindOf, allowedBots string) *Trust

WithActors makes Trusted also require a human account, or a bot on the allowedBots list.

Jump to

Keyboard shortcuts

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