Documentation
¶
Overview ¶
Package gh talks to GitHub: scale-set API, listener, and REST helpers.
Index ¶
- Constants
- Variables
- func Token(file string) (string, error)
- type Events
- type FileChange
- type Listener
- type PR
- type PRState
- type REST
- func (r *REST) BranchExists(ctx context.Context, owner, repo, branch string) (bool, error)
- func (r *REST) CancelRun(ctx context.Context, owner, repo string, runID int64) error
- func (r *REST) CommitFiles(ctx context.Context, owner, repo, branch, message string, files []FileChange) (sha string, changed bool, err error)
- func (r *REST) CreateBranch(ctx context.Context, owner, repo, branch, base string) (sha string, created bool, err error)
- func (r *REST) CreateRegistrationToken(ctx context.Context, t core.Target) (string, error)
- func (r *REST) DefaultBranch(ctx context.Context, owner, repo string) (string, error)
- func (r *REST) DeleteRunner(ctx context.Context, t core.Target, id int64) error
- func (r *REST) DispatchWorkflow(ctx context.Context, owner, repo, file, ref string, inputs map[string]any) error
- func (r *REST) FindVariable(ctx context.Context, owner, repo, name string) (Variable, bool, error)
- func (r *REST) ForceCancelRun(ctx context.Context, owner, repo string, runID int64) error
- func (r *REST) GetFile(ctx context.Context, owner, repo, p, ref string) ([]byte, string, error)
- func (r *REST) GetPRState(ctx context.Context, owner, repo string, number int) (PRState, error)
- func (r *REST) GetRepo(ctx context.Context, owner, repo string) (Repo, error)
- func (r *REST) GetRun(ctx context.Context, owner, repo string, runID int64) (Run, error)
- func (r *REST) ListOrgRepos(ctx context.Context, org string) ([]Repo, error)
- func (r *REST) ListOrgRunnerGroups(ctx context.Context, org string) ([]RunnerGroup, error)
- func (r *REST) ListRunners(ctx context.Context, t core.Target) ([]Runner, error)
- func (r *REST) ListWorkflowFiles(ctx context.Context, owner, repo, ref string) ([]WorkflowFile, error)
- func (r *REST) OpenPR(ctx context.Context, owner, repo, head, base, title, body string) (PR, error)
- func (r *REST) PRsForCommit(ctx context.Context, owner, repo, sha, branch string) ([]PRState, error)
- func (r *REST) RerunJob(ctx context.Context, owner, repo string, runID int64, jobName string) error
- func (r *REST) TagExists(ctx context.Context, owner, repo, tag string) (bool, error)
- func (r *REST) TokenScopes(ctx context.Context) ([]string, error)
- func (r *REST) UpdateVariable(ctx context.Context, scope core.Target, name, value string) error
- type Repo
- type Run
- type Runner
- type RunnerGroup
- type ScaleSets
- func (s *ScaleSets) Delete(ctx context.Context, key core.ScaleSetKey) error
- func (s *ScaleSets) DeleteByID(ctx context.Context, t core.Target, id int) error
- func (s *ScaleSets) Ensure(ctx context.Context, key core.ScaleSetKey, name string, labels []string, ...) (int, error)
- func (s *ScaleSets) GenerateJIT(ctx context.Context, key core.ScaleSetKey, runnerName string, ...) (string, int, error)
- func (s *ScaleSets) ID(key core.ScaleSetKey) (int, bool)
- func (s *ScaleSets) NewListener(key core.ScaleSetKey, events Events, log *slog.Logger, capacity int) (*Listener, error)
- func (s *ScaleSets) OpenSession(ctx context.Context, key core.ScaleSetKey, owner string) (Session, error)
- func (s *ScaleSets) RemoveRunner(ctx context.Context, key core.ScaleSetKey, runnerID int) error
- type Session
- type Stats
- type Variable
- type WorkflowFile
Constants ¶
const DefaultRunnerGroupID = 1
DefaultRunnerGroupID is the id of the default runner group. Repo-level scale sets always use it.
const ResultCanceled = "canceled"
ResultCanceled is the result GitHub reports for a reassigned job.
const ScopesUnknown = "unknown"
ScopesUnknown is returned by TokenScopes for tokens that do not report OAuth scopes (fine-grained PATs, app tokens).
Variables ¶
var ( // ErrRunnerBusy: deleting a runner that is running a job (HTTP 422). ErrRunnerBusy = errors.New("runner is busy") // ErrNotFound wraps HTTP 404. ErrNotFound = errors.New("not found") // ErrJobNotFound: no job with that name in the run. ErrJobNotFound = errors.New("job not found in run") )
Typed REST errors.
var ( ErrTokenPerms = errors.New("token file is group or world accessible") ErrTokenEmpty = errors.New("token file is empty") )
Token errors.
var ErrUnknownScaleSet = errors.New("scale set id unknown; call Ensure first")
ErrUnknownScaleSet is returned when a key has no known scale-set id yet (Ensure has not run).
Functions ¶
Types ¶
type Events ¶
type Events interface {
Assigned(key core.ScaleSetKey, j core.Job)
// Reassigned: GitHub cancelled an assigned job that had no runner
// (result canceled, empty runner name). The job is assigned again every
// ~5 minutes; it is not a completion.
Reassigned(key core.ScaleSetKey, j core.Job)
Started(key core.ScaleSetKey, j core.Job)
Completed(key core.ScaleSetKey, j core.Job)
Stats(key core.ScaleSetKey, s Stats)
}
Events receives scale-set events. Calls come from a single goroutine, in the order documented on Listener. Redelivery after errors or reconnects means every method must be idempotent.
type FileChange ¶
FileChange is one file written by CommitFiles.
type Listener ¶
type Listener struct {
// Backoff bounds; zero values use defaults.
MinBackoff, MaxBackoff time.Duration
// ConflictBackoff is the minimum wait after a session conflict.
ConflictBackoff time.Duration
// contains filtered or unexported fields
}
Listener runs the scale-set long poll for one scale set.
Order of event delivery within one message (deterministic):
- Reassigned (canceled with no runner), in message order
- Assigned
- Started
- Completed (after Started: a short job's Started and Completed can share a message)
- Stats (initial session statistics arrive as a synthetic first message)
JobAvailable messages are ignored (the service never sends them for scale sets; jobs arrive as JobAssigned).
func (*Listener) Run ¶
Run polls until ctx is done, reconnecting with jittered exponential backoff after any error. A session conflict (another active session for the scale set) is logged and retried, never fatal. It returns ctx.Err() on shutdown.
func (*Listener) SetCapacity ¶
SetCapacity sets maxCapacity sent to the service on each poll (effective on the next poll, up to ~50s later). Safe for concurrent use.
type PRState ¶
type PRState struct {
Number int
Merged bool
Closed bool
ClosedAt time.Time // merge or close time; zero while open
}
PRState is the lifecycle state of a pull request.
type REST ¶
type REST struct {
// contains filtered or unexported fields
}
REST is a thin, typed wrapper over the GitHub REST API.
func NewREST ¶
func NewREST(token string, opts ...github.ClientOptionsFunc) (*REST, error)
NewREST builds a REST client. Extra options (for example github.WithURLs in tests) are applied after the token.
func (*REST) BranchExists ¶
BranchExists reports whether a branch exists.
func (*REST) CommitFiles ¶
func (r *REST) CommitFiles(ctx context.Context, owner, repo, branch, message string, files []FileChange) (sha string, changed bool, err error)
CommitFiles writes files to branch as ONE commit (git data API) and returns the commit SHA. If every file already matches the branch tip, nothing is committed and the tip SHA is returned with changed=false.
func (*REST) CreateBranch ¶
func (r *REST) CreateBranch(ctx context.Context, owner, repo, branch, base string) (sha string, created bool, err error)
CreateBranch creates branch from the tip of base and returns the new tip SHA. If the branch already exists it is left alone and its tip is returned with created=false.
func (*REST) CreateRegistrationToken ¶
CreateRegistrationToken returns a runner registration token for config.sh.
func (*REST) DefaultBranch ¶
DefaultBranch returns a repository's default branch.
func (*REST) DeleteRunner ¶
DeleteRunner deregisters a runner. A busy runner yields ErrRunnerBusy; a runner that is already gone is not an error.
func (*REST) DispatchWorkflow ¶
func (r *REST) DispatchWorkflow(ctx context.Context, owner, repo, file, ref string, inputs map[string]any) error
DispatchWorkflow triggers a workflow_dispatch run (smoke test).
func (*REST) FindVariable ¶
FindVariable looks a variable up the way a workflow does: repo first, then the owner's org. found=false when neither has it.
func (*REST) ForceCancelRun ¶
ForceCancelRun force-cancels a run (use after CancelRun did not take).
func (*REST) GetPRState ¶
GetPRState reports whether a PR is merged or closed.
func (*REST) ListOrgRepos ¶
ListOrgRepos lists every repository of an org (including archived).
func (*REST) ListOrgRunnerGroups ¶
ListOrgRunnerGroups lists an org's runner groups.
func (*REST) ListRunners ¶
ListRunners lists runners registered at a target (repo or org).
func (*REST) ListWorkflowFiles ¶
func (r *REST) ListWorkflowFiles(ctx context.Context, owner, repo, ref string) ([]WorkflowFile, error)
ListWorkflowFiles lists .yml/.yaml files in .github/workflows at ref. A missing directory yields no files.
func (*REST) PRsForCommit ¶
func (r *REST) PRsForCommit(ctx context.Context, owner, repo, sha, branch string) ([]PRState, error)
PRsForCommit lists PRs (open, closed or merged) whose head is sha and whose head branch is branch. A run's own pull_requests list is emptied once its PR merges, so stale-run checks use this instead.
func (*REST) RerunJob ¶
RerunJob re-runs the job named jobName of run runID. The scale-set JobID is a GUID, so the numeric REST id is found by listing the run's jobs.
func (*REST) TokenScopes ¶
TokenScopes returns the OAuth scopes of the token (X-OAuth-Scopes). Tokens that do not report scopes (fine-grained PATs) yield []string{ScopesUnknown}.
type Repo ¶
type Repo struct {
Owner string
Name string
DefaultBranch string
Archived bool
Private bool
Fork bool
}
Repo is the subset of repository metadata needed for scanning.
type Run ¶
type Run struct {
ID int64
Event string
HeadBranch string
HeadSHA string
PRs []int
Status string
Conclusion string
CreatedAt time.Time
}
Run is the subset of a workflow run the manager needs.
type Runner ¶
type Runner struct {
ID int64
Name string
OS string
Status string // online | offline
Busy bool
Labels []string
}
Runner is a registered self-hosted runner.
type RunnerGroup ¶
RunnerGroup is an org runner group.
type ScaleSets ¶
type ScaleSets struct {
// contains filtered or unexported fields
}
ScaleSets manages the scale sets an instance owns. It keeps one scaleset client per target and remembers the id of every scale set it ensured.
func NewScaleSets ¶
NewScaleSets returns a ScaleSets authenticating with a token (PAT or gh OAuth token).
func (*ScaleSets) DeleteByID ¶
DeleteByID removes a scale set by id (reconciler use; id from a state DB).
func (*ScaleSets) Ensure ¶
func (s *ScaleSets) Ensure(ctx context.Context, key core.ScaleSetKey, name string, labels []string, runnerGroup string) (int, error)
Ensure gets or creates the scale set `name` for key, aligns its labels with labels (name is always included) and returns its id. Created scale sets set DisableUpdate so GitHub never self-updates our runners.
func (*ScaleSets) GenerateJIT ¶
func (s *ScaleSets) GenerateJIT(ctx context.Context, key core.ScaleSetKey, runnerName string, workFolder ...string) (string, int, error)
GenerateJIT registers an ephemeral runner named runnerName and returns the encoded JIT config and the runner id. workFolder is optional (Windows VMs pass an absolute per-job folder); empty keeps the runner default `_work`.
func (*ScaleSets) ID ¶
func (s *ScaleSets) ID(key core.ScaleSetKey) (int, bool)
ID returns the scale-set id remembered for key.
func (*ScaleSets) NewListener ¶
func (s *ScaleSets) NewListener(key core.ScaleSetKey, events Events, log *slog.Logger, capacity int) (*Listener, error)
NewListener builds a Listener for key. Ensure must have run for key.
func (*ScaleSets) OpenSession ¶
func (s *ScaleSets) OpenSession(ctx context.Context, key core.ScaleSetKey, owner string) (Session, error)
OpenSession opens a message session for key's scale set.
func (*ScaleSets) RemoveRunner ¶
RemoveRunner deregisters a runner by id. A missing runner is not an error.
type Stats ¶
type Stats struct {
Assigned int // jobs assigned (running + assigned-unstarted)
Running int
Registered int // registered runners
Busy int
Idle int
}
Stats is the scale-set service's view of a scale set.
type WorkflowFile ¶
WorkflowFile is a workflow file on a ref.