Documentation
¶
Overview ¶
Package scm reaches a source-code host through one interface. GitHub and GitLab are the adapters so far.
Index ¶
- Constants
- Variables
- func AppJWT(ctx context.Context, signer Signer, appID string, now time.Time) (string, error)
- func Verify(secret, body []byte, header string) bool
- type AppTokens
- type Approval
- type ApprovalRequest
- type CheckRun
- type DeliveryStore
- type Event
- type GitHub
- func (g *GitHub) CanWrite(ctx context.Context, login string) (bool, error)
- func (g *GitHub) PostCheck(ctx context.Context, check CheckRun) (int64, error)
- func (g *GitHub) PullRequest(ctx context.Context, number int) (PullRequest, error)
- func (g *GitHub) ReadFile(ctx context.Context, path, ref string) ([]byte, error)
- func (g *GitHub) UpsertComment(ctx context.Context, pullRequest int, marker, body string) error
- func (g *GitHub) Visibility(ctx context.Context) (Visibility, error)
- type GitHubOptions
- type GitLab
- func (g *GitLab) PostCheck(context.Context, CheckRun) (int64, error)
- func (g *GitLab) ReadFile(ctx context.Context, path, ref string) ([]byte, error)
- func (g *GitLab) UpsertComment(ctx context.Context, mergeRequest int, marker, body string) error
- func (g *GitLab) Visibility(ctx context.Context) (Visibility, error)
- type GitLabOptions
- type Host
- type LimitError
- type Limiter
- type MemoryDeliveries
- type PullRequest
- type Queue
- type Reporter
- type Signer
- type StaticToken
- type StatusError
- type TokenSource
- type Visibility
- type Webhook
Constants ¶
const ( EventPullRequest = "pull_request" EventCheckRun = "check_run" )
The events the App queues. Every other event is acknowledged and dropped.
const ( // ReplayWindow is how far an event's signed instant may sit from now. ReplayWindow = 10 * time.Minute // DeliveryRetention is how long a delivery id stays claimed. It outlasts // the replay window because a check_run states no instant, so the claim // is what refuses its replay. DeliveryRetention = 72 * time.Hour )
const (
// ApproveAction is the identifier of the check's approve button.
ApproveAction = "approve"
)
const (
// DefaultAPI is github.com's API root.
DefaultAPI = "https://api.github.com"
)
Variables ¶
var ( ErrAuthorApproval = errors.New("scm: the pull request's author cannot approve it") ErrNoWriteRole = errors.New("scm: the approver holds no write role") ErrStaleHead = errors.New("scm: the pull request's head has moved") ErrNotAnApproval = errors.New("scm: the event is not an approval") )
The refusals an approval can answer.
var ErrIgnoredEvent = errors.New("scm: event not processed")
ErrIgnoredEvent reports an event the App does not process.
var ErrNoCheck = errors.New("scm: this host takes no check from this token")
ErrNoCheck reports a host where the adapter posts no check.
var ErrNotFound = errors.New("scm: not found")
ErrNotFound reports a file or an object the host does not have.
Functions ¶
Types ¶
type AppTokens ¶
type AppTokens struct {
API string
AppID string
Installation int64
Signer Signer
Now func() time.Time
// Client is as in GitHubOptions.
Client *http.Client
// contains filtered or unexported fields
}
AppTokens mints installation tokens with the App's minimum permissions and holds each until it nears its expiry.
type Approval ¶
Approval records who approved which commit. It flips nothing yet: the check stays neutral until a policy gives it a conclusion to flip.
type ApprovalRequest ¶
type ApprovalRequest struct {
PullRequest int
Approver string
HeadSHA string
// AllowAuthor lets a pull request's author approve it. It is off by default.
AllowAuthor bool
}
ApprovalRequest is one press of the approve action.
func RequestFrom ¶
func RequestFrom(event Event) (ApprovalRequest, error)
RequestFrom reads an approval out of a queued event.
type CheckRun ¶
type CheckRun struct {
Name string
HeadSHA string
Conclusion string
Title string
Summary string
Text string
Completed time.Time
}
CheckRun is one completed check.
type DeliveryStore ¶
type DeliveryStore interface {
// Claim reports true the first time an id is seen.
Claim(ctx context.Context, delivery string, now time.Time) (bool, error)
// Release forgets an id whose event could not be queued, so a redelivery
// is processed.
Release(ctx context.Context, delivery string) error
}
DeliveryStore gives each delivery id one claim.
type Event ¶
type Event struct {
Delivery string
Name string
Action string
Installation int64
Repository string
Sender string
PullRequest int
HeadSHA string
RequestedAction string
// At is the instant the signed payload states for this event. It is zero
// for a check_run, whose payload holds no instant of the click.
At time.Time
}
Event is the part of a webhook the App acts on. Every string passed a check against its own format, because the rest of the App treats them as safe.
type GitHub ¶
type GitHub struct {
// contains filtered or unexported fields
}
GitHub is the Host for one repository.
func NewGitHub ¶
func NewGitHub(options GitHubOptions) (*GitHub, error)
NewGitHub returns the adapter. The token never travels over plain HTTP, except to a loopback address.
func (*GitHub) PullRequest ¶
PullRequest reads the head and the author.
func (*GitHub) UpsertComment ¶
UpsertComment edits the sticky comment in place. Only a comment a bot account wrote qualifies, so a person who pastes the marker into their own comment cannot have it rewritten.
func (*GitHub) Visibility ¶
func (g *GitHub) Visibility(ctx context.Context) (Visibility, error)
Visibility asks the host, and an answer that omits the field is an error.
type GitHubOptions ¶
type GitHubOptions struct {
// API is the API root, DefaultAPI when empty.
API string
// Repository is owner/name.
Repository string
Tokens TokenSource
// Client makes the requests, under this package's timeout and redirect
// rule. Required, so that a service can pass one bound by its allowlist.
Client *http.Client
// Limits and Installation apply the App's write limits. A token from CI
// has neither.
Limits *Limiter
Installation int64
}
GitHubOptions configures a GitHub adapter.
type GitLab ¶
type GitLab struct {
// contains filtered or unexported fields
}
GitLab is the Reporter for one project. It reaches GitLab's REST API v4, and it has run against a stub built from GitLab's documentation, never a real instance.
func NewGitLab ¶
func NewGitLab(options GitLabOptions) (*GitLab, error)
NewGitLab returns the adapter. The token never travels over plain HTTP, except to a loopback address.
func (*GitLab) PostCheck ¶
PostCheck posts nothing. A commit status needs the Developer role and a note needs less, so the note's token cannot set one, and a status has no neutral state to state.
func (*GitLab) UpsertComment ¶
UpsertComment edits the sticky note in place. Only a note this token's own user wrote qualifies, because a note's author carries no bot field. A failed search posts nothing, so a merge request never holds a second note.
func (*GitLab) Visibility ¶
func (g *GitLab) Visibility(ctx context.Context) (Visibility, error)
Visibility asks the host. An answer outside GitLab's three values is an error, and internal counts as private.
type GitLabOptions ¶
type GitLabOptions struct {
// API is the instance's REST root, CI_API_V4_URL in a job. It has no
// default, so a self-managed instance's token never reaches gitlab.com.
API string
// Project is the project's numeric id.
Project string
Tokens TokenSource
// Client is as in GitHubOptions.
Client *http.Client
}
GitLabOptions configures a GitLab adapter.
type Host ¶
type Host interface {
Reporter
PullRequest(ctx context.Context, number int) (PullRequest, error)
// CanWrite reports whether login holds a write role on the repository.
CanWrite(ctx context.Context, login string) (bool, error)
}
Host is one repository a person can approve a change on.
type LimitError ¶
LimitError says a request was held back, and for how long to wait.
func (*LimitError) Error ¶
func (e *LimitError) Error() string
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter holds GitHub's limits per installation: two token buckets for content writes, and a ceiling on requests in flight.
func NewLimiter ¶
NewLimiter returns a Limiter reading the given clock.
type MemoryDeliveries ¶
type MemoryDeliveries struct {
// contains filtered or unexported fields
}
MemoryDeliveries is a DeliveryStore for one process. Only verified deliveries reach it, and it holds each for DeliveryRetention.
func NewMemoryDeliveries ¶
func NewMemoryDeliveries() *MemoryDeliveries
NewMemoryDeliveries returns an empty store.
type PullRequest ¶
PullRequest is what an approval needs to know about a pull request.
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue carries events from the handler to the workers.
type Reporter ¶
type Reporter interface {
// PostCheck returns the check's id. A host without one answers ErrNoCheck.
PostCheck(ctx context.Context, check CheckRun) (int64, error)
// UpsertComment edits the host's own comment that opens with marker, or
// creates it when there is none.
UpsertComment(ctx context.Context, pullRequest int, marker, body string) error
// ReadFile reads a file at a ref. A missing file is ErrNotFound.
ReadFile(ctx context.Context, path, ref string) ([]byte, error)
Visibility(ctx context.Context) (Visibility, error)
}
Reporter is what a report needs from one repository on one source-code host.
type Signer ¶
Signer signs a SHA-256 digest with RSASSA-PKCS1-v1_5. The production signer is a KMS call, so the private key never reaches this process.
type StatusError ¶
StatusError is an answer the host refused. It never carries the body, which is the host's text and can echo the request.
func (*StatusError) Error ¶
func (e *StatusError) Error() string
func (*StatusError) Is ¶
func (e *StatusError) Is(target error) bool
Is makes a 404 match ErrNotFound.
type TokenSource ¶
TokenSource supplies the bearer token of each request.
type Visibility ¶
type Visibility int
Visibility is who can read a repository.
const ( Private Visibility = iota Public )
A repository is public or it is not. Internal counts as private.