Documentation
¶
Overview ¶
Package githubtest is a fake GitHub for tests: an httptest server with the parts of the REST API and the browser flows vx uses, plus helpers to create users, organizations and repositories, set permissions, install the App and fire signed webhook deliveries.
The fake serves the browser side at URL (like https://github.com) and the API at APIURL = URL + "/api/v3" (like GitHub Enterprise Server), so tests exercise configurable base URLs.
gh := githubtest.New(t)
creds := gh.NewApp() // or the manifest flow
api := gh.Client(t) // *github.Client against the fake
alice := gh.AddUser("alice")
gh.AddOrg("acme")
gh.AddRepo("acme/shop", false)
gh.SetPermission("acme/shop", "alice", "write")
inst := gh.Install("acme") // all repositories of acme
gh.Deliver(t, server.URL+"/hooks/github", "installation", gh.InstallationEvent("created", inst))
Index ¶
- func DeliveryIDAt(t time.Time) string
- func NewDeliveryID() string
- func Sign(secret string, body []byte) string
- type Account
- type CheckRun
- type Delivery
- type Installation
- type PR
- type Repo
- type ReviewComment
- type Server
- func (s *Server) Account(login string) (Account, bool)
- func (s *Server) AddInstallationRepos(id int64, repos ...string)
- func (s *Server) AddOrg(login string) Account
- func (s *Server) AddRepo(fullName string, private bool) Repo
- func (s *Server) AddUser(login string) Account
- func (s *Server) App() (service.GitHubAppCredentials, bool)
- func (s *Server) Calls(pattern string) int
- func (s *Server) CheckRunEvent(installationID int64, fullName string, pr PR, ...) map[string]any
- func (s *Server) CheckRuns(fullName string) []CheckRun
- func (s *Server) CheckSuiteEvent(installationID int64, fullName string, pr PR, app, conclusion string) map[string]any
- func (s *Server) Client(t testing.TB) *github.Client
- func (s *Server) Close()
- func (s *Server) Configure(t testing.TB, svc *service.Service) service.GitHubAppCredentials
- func (s *Server) Connector() func(service.GitHubAppCredentials) (service.GitHubAPI, error)
- func (s *Server) DeleteInstallation(id int64)
- func (s *Server) DeleteRepo(fullName string) Repo
- func (s *Server) Deliver(t testing.TB, hookURL, event string, payload any) int
- func (s *Server) DeliverRaw(t testing.TB, hookURL string, d Delivery) int
- func (s *Server) DenyAuthorization(deny bool)
- func (s *Server) ExpireTokens()
- func (s *Server) Fail(pattern string, status, n int)
- func (s *Server) Install(account string, repos ...string) int64
- func (s *Server) Installation(id int64) (Installation, bool)
- func (s *Server) InstallationEvent(action string, id int64) map[string]any
- func (s *Server) InstallationReposEvent(action string, id int64, repos ...string) map[string]any
- func (s *Server) InstallationTokenFor(id int64) string
- func (s *Server) IssueCommentEvent(installationID int64, fullName string, number int, action string, ...) map[string]any
- func (s *Server) Manifest() map[string]any
- func (s *Server) ManifestCode(manifestJSON []byte) string
- func (s *Server) MemberEvent(installationID int64, action, fullName, login string) map[string]any
- func (s *Server) MembershipEvent(installationID int64, action, org, team, login string) map[string]any
- func (s *Server) NewApp() service.GitHubAppCredentials
- func (s *Server) OAuthCode(login, redirectURI, codeChallenge string) string
- func (s *Server) OrgMemberEvent(installationID int64, action, org, login, role string) map[string]any
- func (s *Server) Prepare(t testing.TB, svc *service.Service)
- func (s *Server) PushEvent(installationID int64, fullName, ref, before, after string) map[string]any
- func (s *Server) RemoveInstallationRepos(id int64, repos ...string)
- func (s *Server) RenameAccount(login, newLogin string)
- func (s *Server) RenameRepo(fullName, newName string) Repo
- func (s *Server) Repo(fullName string) (Repo, bool)
- func (s *Server) RepoRenamedEvent(installationID int64, fullName, oldName string) map[string]any
- func (s *Server) RepoTransferredEvent(installationID int64, fullName, oldOwner string) map[string]any
- func (s *Server) RepositoryEvent(action string, installationID int64, r Repo) map[string]any
- func (s *Server) ReviewCommentEvent(installationID int64, fullName string, pr PR, action string, c ReviewComment) map[string]any
- func (s *Server) ReviewEvent(installationID int64, fullName string, pr PR, action string, reviewID int64, ...) map[string]any
- func (s *Server) SetAppEvents(events ...string)
- func (s *Server) SetAppPermission(name, level string)
- func (s *Server) SetDefaultBranch(fullName, branch string)
- func (s *Server) SetOrgMember(org, login, role string)
- func (s *Server) SetPermission(fullName, login, perm string)
- func (s *Server) SetPrivate(fullName string, private bool) Repo
- func (s *Server) SetProfile(login, name, email string)
- func (s *Server) SetupClient(t testing.TB) *github.Client
- func (s *Server) Sign(body []byte) string
- func (s *Server) SignIn(login string)
- func (s *Server) StatusEvent(installationID int64, fullName, sha, context, state, targetURL string) map[string]any
- func (s *Server) SuspendInstallation(id int64, suspended bool)
- func (s *Server) TeamAddEvent(installationID int64, org, team, fullName string) map[string]any
- func (s *Server) TeamEvent(installationID int64, action, org, team, fullName string) map[string]any
- func (s *Server) TransferRepo(fullName, newOwner string) Repo
- func (s *Server) UserToken(login string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeliveryIDAt ¶
DeliveryIDAt returns a delivery id made at t, for tests of stale deliveries.
func NewDeliveryID ¶
func NewDeliveryID() string
NewDeliveryID returns a delivery id like GitHub's: a version 1 UUID, which carries the time it was made (now).
Types ¶
type Account ¶
type Account struct {
ID int64
Login string
Type string // User or Organization
Name string
Email string // primary verified email; empty: none shared
}
Account is a GitHub user or organization.
type CheckRun ¶ added in v0.4.0
type CheckRun struct {
ID int64
Repo string // owner/name
Name string
HeadSHA string
Status string
Conclusion string
Title string
Summary string
Text string
DetailsURL string
ExternalID string
At time.Time
}
CheckRun is a check run posted to the fake.
type Delivery ¶
type Delivery struct {
Event string
ID string // X-GitHub-Delivery; empty: a new random id
Body []byte
Signature string // X-Hub-Signature-256; empty: signed with the webhook secret, "-": no header
}
Delivery is one webhook delivery.
type Installation ¶
type Installation struct {
ID int64
Account string // login
All bool // repository_selection all
Repos []int64
SuspendedAt *time.Time
}
Installation is an installation of the App.
type PR ¶ added in v0.4.0
type PR struct {
Number int
Head string // head branch, e.g. vx/rate-limiting
HeadSHA string
Base string // default: the repository's default branch
}
PR is a pull request as webhook payloads describe it.
type ReviewComment ¶ added in v0.4.0
type ReviewComment struct {
ID int64
Reviewer string
Body string
Path string
Line int // 0: a comment on the file
Side string // RIGHT (default) or LEFT
CommitID string // default: the pull request's head
// Outdated makes line null, as GitHub sends for a comment on a line a
// later push changed: Line becomes original_line.
Outdated bool
}
ReviewComment is a line comment of a review.
type Server ¶
type Server struct {
// URL is the browser base (WebURL), APIURL the REST API base.
URL string
APIURL string
// Now is the fake's clock, for token expiry and App JWT checks.
Now func() time.Time
// TokenTTL is how long installation tokens live (default one hour).
TokenTTL time.Duration
// PageSize is the page size of paginated lists (default 30), so tests
// see pagination with few repositories.
PageSize int
// contains filtered or unexported fields
}
Server is a fake GitHub. Its methods are safe for concurrent use.
func (*Server) AddInstallationRepos ¶
AddInstallationRepos adds repositories to a "selected" installation.
func (*Server) AddRepo ¶
AddRepo creates owner/name (the owner must exist) with default branch main.
func (*Server) App ¶
func (s *Server) App() (service.GitHubAppCredentials, bool)
App returns the App's credentials; ok is false before it exists.
func (*Server) Calls ¶
Calls returns how many requests matched "METHOD /path" so far, with numbers and names in the path as sent, e.g. "POST /app/installations/1001/access_tokens". A pattern ending in "*" matches by prefix. API paths are given without the /api/v3 prefix.
func (*Server) CheckRunEvent ¶ added in v0.4.0
func (s *Server) CheckRunEvent(installationID int64, fullName string, pr PR, action, app, name, status, conclusion string) map[string]any
CheckRunEvent is a "check_run" payload: action created, completed or rerequested; status queued, in_progress or completed. app is the CI's App slug (e.g. github-actions).
func (*Server) CheckRuns ¶ added in v0.4.0
CheckRuns returns the check runs posted on fullName, oldest first.
func (*Server) CheckSuiteEvent ¶ added in v0.4.0
func (s *Server) CheckSuiteEvent(installationID int64, fullName string, pr PR, app, conclusion string) map[string]any
CheckSuiteEvent is a "check_suite" payload: action completed.
func (*Server) Client ¶
Client returns a github.Client for the App against the fake, creating the App first if it does not exist yet.
func (*Server) Close ¶
func (s *Server) Close()
Close stops the server early; New already closes it at the end of the test.
func (*Server) Configure ¶
Configure is Prepare plus an App: it creates the App on the fake (if it does not exist) and saves it in svc, so svc.GitHubClient, svc.GitHubApp and webhook verification work.
func (*Server) Connector ¶
Connector returns a service.GitHubSettings.Connect function that makes clients against the fake.
func (*Server) DeleteInstallation ¶
DeleteInstallation uninstalls the App; its tokens stop working.
func (*Server) DeleteRepo ¶
DeleteRepo deletes a repository and returns what it was.
func (*Server) Deliver ¶
Deliver posts a webhook event signed with the App's webhook secret, with a new delivery id made at s.Now(), and returns the response status code.
func (*Server) DeliverRaw ¶
DeliverRaw posts a delivery as given, for tests of bad signatures and replays, and returns the response status code.
func (*Server) DenyAuthorization ¶
DenyAuthorization makes the authorize page redirect back with error=access_denied, as when the person cancels.
func (*Server) ExpireTokens ¶
func (s *Server) ExpireTokens()
ExpireTokens makes every installation and user token invalid.
func (*Server) Fail ¶
Fail makes the next n requests matching pattern (see Calls) fail with status. 403 and 429 come back as rate limits, as GitHub sends them.
func (*Server) Install ¶
Install installs the App on an account. With no repositories it gets all of the account's repositories, now and later.
func (*Server) Installation ¶
func (s *Server) Installation(id int64) (Installation, bool)
Installation returns an installation.
func (*Server) InstallationEvent ¶
InstallationEvent is an "installation" payload: created, deleted, suspend, unsuspend, new_permissions_accepted.
func (*Server) InstallationReposEvent ¶
InstallationReposEvent is an "installation_repositories" payload: added or removed, listing repos (full names).
func (*Server) InstallationTokenFor ¶
InstallationTokenFor mints an installation token directly.
func (*Server) IssueCommentEvent ¶ added in v0.4.0
func (s *Server) IssueCommentEvent(installationID int64, fullName string, number int, action string, commentID int64, author, body string) map[string]any
IssueCommentEvent is an "issue_comment" payload on pull request number: action created, edited or deleted.
func (*Server) ManifestCode ¶
ManifestCode registers a manifest as if a site admin had posted it to GitHub and confirmed, and returns the one-time code GitHub sends back.
func (*Server) MemberEvent ¶
MemberEvent is a "member" payload (a repository collaborator): added, removed or edited.
func (*Server) MembershipEvent ¶
func (s *Server) MembershipEvent(installationID int64, action, org, team, login string) map[string]any
MembershipEvent is a "membership" payload (team membership): added or removed.
func (*Server) NewApp ¶
func (s *Server) NewApp() service.GitHubAppCredentials
NewApp creates the App directly, as if the manifest flow had run, and returns its credentials.
func (*Server) OAuthCode ¶
OAuthCode mints a sign-in code for login, as GitHub's authorize page would after the person approved. codeChallenge is the PKCE S256 challenge ("" for none); redirectURI must match the exchange's.
func (*Server) OrgMemberEvent ¶
func (s *Server) OrgMemberEvent(installationID int64, action, org, login, role string) map[string]any
OrgMemberEvent is an "organization" payload: member_added, member_removed or member_invited.
func (*Server) Prepare ¶
Prepare points svc at the fake without creating an App: a random server key, the fake's URLs, Connect, and PublicURL http://vx.test when empty. The manifest flow can then run.
func (*Server) PushEvent ¶
func (s *Server) PushEvent(installationID int64, fullName, ref, before, after string) map[string]any
PushEvent is a "push" payload to ref (e.g. refs/heads/main).
func (*Server) RemoveInstallationRepos ¶
RemoveInstallationRepos removes repositories from an installation (an "all" installation becomes "selected").
func (*Server) RenameAccount ¶
RenameAccount changes a user's or organization's login (the id stays).
func (*Server) RenameRepo ¶
RenameRepo renames a repository (its id stays).
func (*Server) RepoRenamedEvent ¶
RepoRenamedEvent is a "repository" renamed payload; call it after RenameRepo with the old name.
func (*Server) RepoTransferredEvent ¶
func (s *Server) RepoTransferredEvent(installationID int64, fullName, oldOwner string) map[string]any
RepoTransferredEvent is a "repository" transferred payload; call it after TransferRepo with the old owner.
func (*Server) RepositoryEvent ¶
RepositoryEvent is a "repository" payload for r (from Repo, or the value DeleteRepo returned): deleted, privatized, publicized, edited, archived.
func (*Server) ReviewCommentEvent ¶ added in v0.4.0
func (s *Server) ReviewCommentEvent(installationID int64, fullName string, pr PR, action string, c ReviewComment) map[string]any
ReviewCommentEvent is a "pull_request_review_comment" payload: action created, edited or deleted.
func (*Server) ReviewEvent ¶ added in v0.4.0
func (s *Server) ReviewEvent(installationID int64, fullName string, pr PR, action string, reviewID int64, reviewer, state, body string) map[string]any
ReviewEvent is a "pull_request_review" payload: action submitted, edited or dismissed; state approved, changes_requested or commented.
func (*Server) SetAppEvents ¶ added in v0.4.0
SetAppEvents replaces the App's webhook events.
func (*Server) SetAppPermission ¶ added in v0.4.0
SetAppPermission sets one of the App's permissions ("" removes it).
func (*Server) SetDefaultBranch ¶
SetDefaultBranch changes a repository's default branch.
func (*Server) SetOrgMember ¶
SetOrgMember sets login's role in an organization: admin, member, or "" to remove them.
func (*Server) SetPermission ¶
SetPermission sets login's permission on a repository: admin, maintain, write, triage, read, or none/"" to remove it.
func (*Server) SetPrivate ¶
SetPrivate changes a repository's visibility.
func (*Server) SetProfile ¶
SetProfile sets a user's name and primary verified email ("" for none).
func (*Server) SetupClient ¶
SetupClient returns a client without App credentials, which can only complete the manifest flow.
func (*Server) Sign ¶
Sign returns the X-Hub-Signature-256 value of body with the App's webhook secret.
func (*Server) SignIn ¶
SignIn makes login the person signed in at the fake's web pages, so /login/oauth/authorize approves for them right away. "" signs out.
func (*Server) StatusEvent ¶ added in v0.4.0
func (s *Server) StatusEvent(installationID int64, fullName, sha, context, state, targetURL string) map[string]any
StatusEvent is a "status" payload: state pending, success, failure or error, for commit sha.
func (*Server) SuspendInstallation ¶
SuspendInstallation suspends or unsuspends an installation.
func (*Server) TeamAddEvent ¶
TeamAddEvent is a "team_add" payload: a team got access to a repository.
func (*Server) TeamEvent ¶
TeamEvent is a "team" payload: created, deleted, edited, added_to_repository or removed_from_repository (fullName may be "").
func (*Server) TransferRepo ¶
TransferRepo moves a repository to another owner (its id stays).