ghactx

package
v3.77.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package ghactx collects the CI identity of a GitHub Actions run.

In a runner the git checkout is a detached HEAD, so the local repository is not a usable source of branch or repo identity. The environment is, and the webhook payload GitHub writes to GITHUB_EVENT_PATH is richer still: it carries the repository's id, visibility, default branch and licence, and for a pull_request event the PR number and both refs — all without a network call.

Index

Constants

View Source
const Provider = "github-actions"

Provider identifies the CI system in the collected context.

Variables

This section is empty.

Functions

func Collect

func Collect(ctx context.Context, opt Options) *vdb.CliCIContext

Collect builds the CI context, cheapest source first.

Tier 0  environment variables      free
Tier 1  GITHUB_EVENT_PATH          free, on disk, and the richest source
Tier 2  GitHub REST                one call each, only for what is still missing

Returns nil outside a GitHub Actions runner.

Types

type CollectorLookup

type CollectorLookup struct {
	Collector *github.ArtifactCollector
}

CollectorLookup adapts *github.ArtifactCollector to RESTLookup. It lives here rather than in the github package so ghactx owns the shape it needs and the artifact downloader stays unaware of CI context.

func (CollectorLookup) GetRepository

func (l CollectorLookup) GetRepository(ctx context.Context) (*RepoInfo, error)

GetRepository implements RESTLookup.

func (CollectorLookup) GetWorkflowRun

func (l CollectorLookup) GetWorkflowRun(ctx context.Context) (*RunInfo, error)

GetWorkflowRun implements RESTLookup.

func (CollectorLookup) ListPullsForCommit

func (l CollectorLookup) ListPullsForCommit(ctx context.Context, sha string) ([]PullInfo, error)

ListPullsForCommit implements RESTLookup.

type Event

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

Event is the webhook payload GitHub wrote to disk for this run, held as a generic tree because its shape depends on the event type and only a handful of paths are ever read.

func EventFromJSON

func EventFromJSON(b []byte) *Event

EventFromJSON parses a payload from bytes. Used by tests and by --from-dir runs replaying a captured event.

func LoadEvent

func LoadEvent() *Event

LoadEvent reads GITHUB_EVENT_PATH. Returns nil when the variable is unset or the file is unreadable — every accessor tolerates a nil receiver, so callers need no branch for it.

func (*Event) Bool

func (e *Event) Bool(path ...string) bool

Bool reads a boolean at the given path.

func (*Event) Has

func (e *Event) Has(path ...string) bool

Has reports whether a value exists at the given path.

func (*Event) Int

func (e *Event) Int(path ...string) int

Int reads a number at the given path as an int.

func (*Event) Int64

func (e *Event) Int64(path ...string) int64

Int64 reads a number at the given path. JSON numbers decode as float64, which is exact for GitHub's ids up to 2^53 — comfortably beyond current values.

func (*Event) String

func (e *Event) String(path ...string) string

String reads a string at the given path.

type Options

type Options struct {
	// NoAPI suppresses every network lookup. Environment variables and the
	// event payload on disk are still read.
	NoAPI bool
	// Lookup performs the REST fallbacks. Nil disables them, same as NoAPI.
	Lookup RESTLookup
	// Warn receives a note whenever an optional lookup failed, so a degraded
	// context is visible in the workflow log rather than silently thinner.
	Warn func(format string, args ...any)
}

Options controls how much effort collection puts in.

type PullInfo

type PullInfo struct {
	Number  int
	State   string
	BaseRef string
	HeadRef string
}

type RESTLookup

type RESTLookup interface {
	GetWorkflowRun(ctx context.Context) (*RunInfo, error)
	GetRepository(ctx context.Context) (*RepoInfo, error)
	ListPullsForCommit(ctx context.Context, sha string) ([]PullInfo, error)
}

RESTLookup is the subset of the GitHub artifact collector this package uses to fill gaps. Declared as an interface so the collector stays testable and so this package does not depend on the artifact download machinery.

type RepoInfo

type RepoInfo struct {
	ID            int64
	Visibility    string
	DefaultBranch string
	OwnerType     string
	LicenseSpdxID string
}

type RunInfo

type RunInfo struct {
	RunNumber       int
	RunAttempt      int
	Event           string
	HeadBranch      string
	HeadSHA         string
	HTMLURL         string
	Name            string
	Actor           string
	TriggeringActor string
	PullRequest     int
}

RunInfo, RepoInfo and PullInfo mirror the REST shapes without importing them, so a caller can adapt any client.

Jump to

Keyboard shortcuts

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