githubtest

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 22 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 githubtest is a GitHub API stand-in for tests: in-memory organizations with their installed apps, Actions secrets and rulesets, and one or more repositories with rulesets, refs, tags, check runs, environments and issues, served over httptest so the real client speaks to it unchanged.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppJWT

func AppJWT(t *testing.T, appID string, key *rsa.PrivateKey) string

AppJWT mints the JWT a GitHub App authenticates as, issued by appID and signed with the key, for a test that calls the app's own endpoints on the stand-in.

Types

type App

type App struct {
	Key           *rsa.PublicKey
	Record        github.App
	Installations []github.Installation
}

App is a GitHub App the stand-in answers for: its key verifies the app's JWT, and the record and the installations are what GET /app and GET /app/installations give it.

type Deployment

type Deployment struct {
	ID       int64
	Request  github.DeploymentRequest
	Statuses []github.DeploymentStatus
}

Deployment is one deployment made through the API and the statuses added to it.

type Dispatch

type Dispatch struct {
	File   string
	Ref    string
	Inputs map[string]string
}

Dispatch is one workflow_dispatch event: the workflow file, the ref and the inputs.

type EnvSecret

type EnvSecret struct {
	// Value is the plaintext: what the stand-in found on opening the sealed value a put
	// carried, with the key pair an organization secret's is opened with (OrgSecret).
	Value []byte
	// UpdatedAt is when the value was last put; every put sets it.
	UpdatedAt time.Time
}

EnvSecret is one Actions secret of an environment.

type Environment

type Environment struct {
	// Custom says the environment takes deployments from the branches its own patterns
	// name alone (the API's custom_branch_policies), as a put through the API makes it.
	// One without it is an environment any branch may deploy to: the API describes it
	// with no branch policy and refuses it a pattern (404), as GitHub does.
	Custom bool
	// Branches are the name patterns of the branches that may deploy, in the order they
	// were added; Tags the tags' patterns, which a test seeds (the API here adds none)
	// and the listing carries after the branches', each marked as a tag's.
	Branches []string
	Tags     []string
	// Secrets are the environment's Actions secrets, by name: the ones a test seeds and
	// the ones put through the API.
	Secrets map[string]*EnvSecret
}

Environment is one deployment environment of a repository.

type OrgSecret

type OrgSecret struct {
	// Value is the plaintext: what the stand-in found on opening the sealed value a put
	// carried, so a test can assert what was sealed. GitHub never gives it back, and
	// neither does any answer of the stand-in.
	Value []byte
	// Visibility is the secret's visibility as the put gave it (all, private, selected).
	Visibility string
	// RepositoryIDs are the ids of the repositories selected for the secret, as the
	// last put or the last setting of its selection carried them: an empty list, not
	// nil, for a JSON array of none.
	RepositoryIDs []int64
	// UpdatedAt is when the value was last put; every put sets it.
	UpdatedAt time.Time
}

OrgSecret is one Actions secret of an organization.

type Repo

type Repo struct {
	// ID is the repository's id, which an organization secret's selection names it by. A
	// repository added (AddRepo) or made through the API with none gets the next of the
	// stand-in's ids, and one a test put in Repos directly gets its own the first time the
	// API serves it, so the API never answers 0.
	ID int64
	// Private says the repository is private: as a test set it, as its creation through
	// the API asked, or as a PATCH with private last set it.
	Private bool
	// Refs by full name (refs/heads/master, refs/tags/v0.1.0) to the object they point at;
	// the branches listing is the refs/heads entries, none for an empty repository.
	Refs map[string]github.Object
	// TagObjects by SHA: the commit each annotated tag object names.
	TagObjects map[string]string
	// Ancestry lists, per commit, the commits reachable from it (itself included), so a
	// comparison can be answered: base...head is identical when equal, ahead when base
	// is reachable from head, behind when the reverse, diverged otherwise.
	Ancestry map[string][]string
	// MergeBase by "a b" (either order) for a diverged pair.
	MergeBase map[string]string
	// Trees by commit SHA, and Files by "<tree>:<path>" to the blob's content; Blobs
	// by SHA. A commit made through the API gets a tree that copies its base tree's
	// files with the entries changed.
	Trees map[string]string
	Files map[string]string
	Blobs map[string]string
	// Comments by issue or pull request number, oldest first; a comment posted through
	// the API is appended.
	Comments map[int][]github.Comment
	// Releases by tag.
	Releases map[string]github.Release
	// Changed lists, per head commit, the files a comparison against it names.
	Changed map[string][]string
	// Archived says the repository was archived through the API (PATCH with archived).
	Archived bool
	// PullStates by pull request number: open or closed.
	PullStates map[int]string
	// PullRequests as the API lists them, oldest first; one opened through the API is
	// appended, numbered after the highest number the repository knows.
	PullRequests []github.PullRequest
	// Deployments made through the API, oldest first, with their statuses.
	Deployments []*Deployment
	// Dispatches is the workflow_dispatch events started, oldest first.
	Dispatches []Dispatch
	// Workflows are the paths of the workflows GitHub lists for the repository
	// (.github/workflows/ci.yml), in listing order; a dispatch of a file the listing lacks
	// is answered 404 as GitHub answers it. Nil models no listing, and every file dispatches.
	Workflows []string
	// Rulesets are the repository's rulesets, made or replaced through the API, with
	// their ids; Settings its merge settings, as read and as set through the API.
	Rulesets []*github.Ruleset
	Settings github.RepositorySettings
	// Environments are the repository's deployment environments, by name; one put
	// through the API is added.
	Environments map[string]*Environment
	// Issues are the repository's issues, oldest first; one opened through the API is
	// appended, numbered after the highest issue or pull request number the repository
	// knows, and its body kept in IssueBodies by that number. The listing of issues
	// carries the pull requests of PullRequests too, each with a pull_request key, as
	// GitHub's does.
	Issues      []*github.Issue
	IssueBodies map[int]string
	// CheckRuns are the check runs by the ref they are asked for with: a commit SHA, or
	// a branch or tag name. A name with no entry of its own is resolved through Refs to
	// its commit, and answered with that commit's runs.
	CheckRuns map[string][]github.CheckRun
}

Repo is one repository's state.

type Server

type Server struct {
	*httptest.Server

	Repos map[string]*Repo
	// AppID and AppKey are the GitHub App whose JWT the installation endpoints accept;
	// AppInstallation is its installation's id. The installation token it mints is the
	// token every other endpoint accepts.
	AppID           string
	AppKey          *rsa.PublicKey
	AppInstallation int64
	// Apps are the apps the stand-in knows beside that one, by App ID: a JWT issued by
	// one is verified with its key, and GET /app and GET /app/installations answer with
	// its record and installations. Conversions are the apps a manifest's code makes
	// (POST /app-manifests/<code>/conversions), by code; a code is good once.
	Apps        map[string]*App
	Conversions map[string]*github.App

	// Calls records every method and path served, in order; Messages the commit
	// messages of commits made through the API.
	Calls    []string
	Messages []string
	// Bodies are the bodies of the pull requests opened through the API, oldest first.
	Bodies []string
	// Created are the repositories made through the API (owner/name), oldest first.
	Created []string
	// Login is who the token belongs to, as /user answers; octocat by default.
	// Memberships are the token's user's roles in organizations, by organization (admin
	// for an owner, member); an organization not listed answers not found.
	Login       string
	Memberships map[string]string
	// DefaultBranch is the organization's setting for a new repository's initial branch
	// (main by default), which GitHub uses whatever the creation asks.
	DefaultBranch string
	// The organizations' Actions settings, by organization: Variables by name, the
	// runner groups and the hosted runners made through the API. PublicApps are the apps
	// read by slug (/apps/<slug>); OrgInstallations the apps installed on an
	// organization, as its listing gives them.
	Variables        map[string]map[string]github.Variable
	RunnerGroups     map[string][]*github.RunnerGroup
	HostedRunners    map[string][]*github.HostedRunner
	PublicApps       map[string]github.App
	OrgInstallations map[string][]github.OrgInstallation
	// OrgSecrets are the organizations' Actions secrets, by organization then name: the
	// ones a test seeds and the ones put through the API. OrgRulesets are the
	// organizations' own rulesets, by organization, made, replaced and removed through
	// the API, with their ids.
	OrgSecrets  map[string]map[string]*OrgSecret
	OrgRulesets map[string][]*github.Ruleset
	// An organization in OrgSecretsRefused answers every request about its Actions
	// secrets 403, Resource not accessible by integration, whether the secret named
	// stands or not: GitHub's answer to an app that does not hold the organization's
	// Secrets permission.
	OrgSecretsRefused map[string]bool
	// Failing answers a request made with the token, by its method and path (GET
	// /repos/o/r/issues/7/comments), with the status named, before the token's routes
	// look at it: a call GitHub fails, for a test of what a run does when one does. The
	// routes an app's JWT opens (/app/...) and a manifest's conversion are served before
	// it and never fail by it. Set under Update once the server serves.
	Failing map[string]int
	// ImmutableReleases are the organizations' policies on immutable releases, by
	// organization: all, none or selected, none for an organization that never set one.
	// An organization in ImmutableReleasesHidden answers not found to the read and the
	// set alike, as GitHub answers a token that may not administer the organization.
	ImmutableReleases       map[string]string
	ImmutableReleasesHidden map[string]bool
	// contains filtered or unexported fields
}

Server is the stand-in.

func New

func New(t *testing.T) *Server

New starts the stand-in; it stops when the test ends.

func (*Server) AddRepo

func (s *Server) AddRepo(owner, name string, repo *Repo) *Repo

AddRepo registers a repository with its state.

func (*Server) Client

func (s *Server) Client() *github.Client

Client is the real client pointed at the stand-in.

func (*Server) Update

func (s *Server) Update(fn func())

Update runs fn with the stand-in's lock held, for a test that changes the stand-in while the code under test is calling it: a browser flow's goroutine registering the app GitHub would have made, as the serving goroutines read the same maps.

Jump to

Keyboard shortcuts

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