githubtest

package
v0.0.0-...-0c72368 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 23 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 fake GitHub REST server for tests: App and token authentication, organisation and user account listings with pagination, single repositories, rate-limit headers, ETags and 304s, fault injection and a request ledger.

Index

Constants

View Source
const Limit = 5000

Limit is the core rate limit of every credential.

Variables

View Source
var (
	AllPermissions = map[string]string{"contents": "read", "metadata": "read", "issues": "read", "pull_requests": "read",
		"checks": "read", "statuses": "read"}
	AllEvents = []string{"push", "release", "repository", "issues", "pull_request", "pull_request_review"} //nolint:gochecknoglobals // test data
)

AllPermissions and AllEvents are what the fake's installations grant unless SetGrants says otherwise: every permission and event the portal asks for.

Functions

This section is empty.

Types

type Commit

type Commit struct {
	SHA, Message, AuthorName, AuthorLogin string
	Date                                  time.Time
	Parents                               []string // nil: the next commit of the list (a linear history), none for the last
	// Additions, Deletions and Files are its diff stats, answered by POST /graphql; all zero: GitHub answers null for it.
	Additions, Deletions, Files int
}

Commit is a commit on the fake default branch.

type Delivery

type Delivery struct {
	GUID         string
	Event        string
	Action       string
	RepositoryID int64
	At           time.Time
	Status       int // the portal's answer; 0: none
}

Delivery is a line of the App's webhook delivery log (SetDeliveries).

type Issue

type Issue struct {
	Number           int
	Title, Author    string
	Bot              bool // the author's account type is Bot
	Created, Updated time.Time
	Labels           []string
	Assignees        []string
	Comments         int
	Milestone, Type  string
}

Issue is an open issue of the fake.

type Pull

type Pull struct {
	Number                      int
	Title, Author               string
	Bot, Draft, Fork            bool // Bot: the author's account type is Bot
	Created, Updated            time.Time
	Base, Head, HeadSHA         string
	Labels                      []string
	Requested                   int      // people asked to review, named reviewer0, reviewer1, ... after Reviewers
	Reviewers                   []string // people asked to review, by login
	Review, Mergeable, Checks   string   // GraphQL's reviewDecision, mergeable and the head's statusCheckRollup state
	Approved, Changes           bool     // a reviewer's latest opinion
	Additions, Deletions, Files int
}

Pull is an open pull request of the fake: what the REST listing gives and what POST /graphql answers for it.

type Release

type Release struct {
	Tag, Name, Body, Author string
	Published               time.Time
	Pre, Draft              bool
}

Release is a release as GitHub lists it (SetReleases).

type Server

type Server struct {
	*httptest.Server
	AppID int64
	// contains filtered or unexported fields
}

Server is a fake GitHub. Its methods are safe for concurrent use.

func New

func New(t testing.TB) *Server

New starts a fake GitHub with a fresh App key; it is closed when the test ends.

func (*Server) AddRepo

func (s *Server) AddRepo(org string, r gh.Repository) int64

AddRepo adds a repository to org and returns its id. Fields left empty get defaults.

func (*Server) AddToken

func (s *Server) AddToken(tok string)

AddToken accepts tok as a personal access token.

func (*Server) AddUser

func (s *Server) AddUser(login string)

AddUser makes login a user account (owners are organisations otherwise): GET /orgs/{login}/repos answers 404, its repositories list at /users/{login}/repos (public ones), /user/repos (for its own token) and /installation/repositories.

func (*Server) AddUserToken

func (s *Server) AddUserToken(tok, login string)

AddUserToken accepts tok as a personal access token of the user account login.

func (*Server) Asked

func (s *Server) Asked(tok string) []string

Asked is every request made with the personal token tok (one with a login or a reach), as "METHOD /path status".

func (*Server) BaseURL

func (s *Server) BaseURL() string

BaseURL is the REST base with a trailing slash.

func (*Server) Count

func (s *Server) Count(prefix string) int

Count returns how many requests started with prefix ("GET /orgs/acme/repos").

func (*Server) Exhaust

func (s *Server) Exhaust(credential string)

Exhaust uses up the rate limit of credential (a token, or an installation's account login).

func (*Server) Expires

func (s *Server) Expires(tok string, end time.Time)

Expires makes the personal token tok end at end: every answer to it carries GitHub's GitHub-Authentication-Token-Expiration header, and after end it is refused (401).

func (*Server) Fail

func (s *Server) Fail(pattern string, status, times int, retryAfter time.Duration)

Fail makes the next times requests whose "METHOD /path" starts with pattern answer status. A 429 or 403 with retryAfter sends Retry-After (a secondary rate limit).

func (*Server) Heal

func (s *Server) Heal()

Heal drops every injected fault.

func (*Server) ID

func (s *Server) ID(owner, name string) int64

lookup returns the repository at owner/name. Callers hold s.mu. ID is the id of the repository owner/name, 0 when there is none.

func (*Server) Install

func (s *Server) Install(login string) int64

Install installs the App on login and returns the installation id.

func (*Server) Ledger

func (s *Server) Ledger() []string

Ledger returns "METHOD /path?query status" for every request so far.

func (*Server) PrivateKeyPEM

func (s *Server) PrivateKeyPEM() []byte

PrivateKeyPEM is the App private key.

func (*Server) Reach

func (s *Server) Reach(tok string, ids ...int64)

Reach limits what the personal token tok reads to the repositories ids, as a person's token reads only what GitHub lets that person read: listings leave the others out and every /repos/{owner}/{name} call of another answers 404. Without it a token reads every one.

func (*Server) Remove

func (s *Server) Remove(id int64)

Remove deletes the repository with id.

func (*Server) Select

func (s *Server) Select(login string, ids ...int64)

Select gives the App's installation on login only the repositories ids ("Only select repositories"; none at all with no ids): /installation/repositories lists only them and the installation listing says "selected". Without it, an installation has all.

func (*Server) SetBranch

func (s *Server) SetBranch(id int64, name string, cs ...Commit)

SetBranch sets a branch of the repository with id to its own commits, newest first; the last one names its parents in the default branch (or another branch). No commits deletes the branch.

func (*Server) SetCommits

func (s *Server) SetCommits(id int64, cs ...Commit)

SetCommits replaces the default-branch commits of the repository with id (newest first). No commits means an empty repository.

func (*Server) SetDeliveries

func (s *Server) SetDeliveries(d ...Delivery)

SetDeliveries sets the App's webhook delivery log, newest first.

func (*Server) SetFile

func (s *Server) SetFile(id int64, path, content string)

SetFile writes a file on the default branch of the repository with id; an empty content deletes it.

func (*Server) SetGrants

func (s *Server) SetGrants(login string, permissions map[string]string, events []string)

SetGrants sets what the App's installation on login may read and which events it is sent.

func (*Server) SetIssues

func (s *Server) SetIssues(id int64, is ...Issue)

SetIssues replaces the open issues of the repository id.

func (*Server) SetPulls

func (s *Server) SetPulls(id int64, ps ...Pull)

SetPulls replaces the open pull requests of the repository id.

func (*Server) SetRelease

func (s *Server) SetRelease(id int64, tag, name string, published time.Time)

SetRelease sets the one release of the repository with id; an empty tag removes it.

func (*Server) SetReleases

func (s *Server) SetReleases(id int64, rels ...Release)

SetReleases sets the releases of the repository with id, replacing its earlier ones; GitHub lists them newest first.

func (*Server) SetTag

func (s *Server) SetTag(id int64, name, sha string)

SetTag points a tag of the repository with id at a commit; an empty SHA deletes it.

func (*Server) SetTreeLimit

func (s *Server) SetTreeLimit(n int)

SetTreeLimit makes a recursive tree of more than n entries come truncated, its first n entries only, as GitHub truncates one past 100,000 entries or 7 MB; 0 lifts the limit.

func (*Server) Uninstall

func (s *Server) Uninstall(login string)

Uninstall removes the App's installation on login: it is no longer listed and its tokens read nothing.

func (*Server) Update

func (s *Server) Update(id int64, fn func(*gh.Repository))

Update changes the repository with id in place, for example to rename it or touch pushed_at.

Jump to

Keyboard shortcuts

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