pullreq

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatApplyComment

func FormatApplyComment(commentData ApplyCommentData) (string, error)

FormatApplyComment generates the body of an apply comment result.

func FormatDiffComment

func FormatDiffComment(commentData DiffCommentData) (string, error)

FormatDiffComment generates the body of a diff comment result.

func FormatErrorComment

func FormatErrorComment(commentData ErrorCommentData) (string, error)

FormatErrorComment generates the body of an error comment result.

func FormatHelpComment

func FormatHelpComment(commentData HelpCommentData) (string, error)

FormatHelpComment generates the body of a help comment result.

func FormatStatusComment

func FormatStatusComment(commentData StatusCommentData) (string, error)

FormatStatusComment generates the body of a status comment result.

func GenerateJWT

func GenerateJWT(pemStr string, appID string) (string, error)

GenerateJWT generates a signed JWT for an app. See https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/ for more details.

func GetCoveredClusters

func GetCoveredClusters(
	repoRoot string,
	diffs []*github.CommitFile,
	env string,
	selectedClusterGlobStrs []string,
	subpathOverride string,
	multiSubpaths bool,
) ([]*config.ClusterConfig, error)

GetCoveredClusters returns the configs of all clusters that are "covered" by the provided diffs. The general approach followed is:

  1. Walk the repo from the root
  2. Find each cluster config that matches the argument environment
  3. Get all of the expanded files associated with each config
  4. Go over all the diffs, mapping them back to the cluster(s) that are configuring them
  5. Drop any clusters that are not associated with any diffs
  6. Find the lowest parent among each set of cluster diffs and use this to set the subpath in the associated cluster config or, if multiSubpaths is set to true, all changed subpaths.

There are a few overrides that adjust this behavior:

  1. selectedClusterGlobStrs: If set, then clusters in this slice are never dropped, even if they don't have have any diffs in them.
  2. subpathOverride: If set, then this is used for the cluster subpaths instead of the procedure in step 6 above.

Types

type AccessToken

type AccessToken struct {
	Token       string            `json:"token"`
	ExpiresAt   time.Time         `json:"expires_at"`
	Permissions map[string]string `json:"permissions"`
}

AccessToken wraps the results returned by the Github access_tokens API.

func GenerateAccessToken

func GenerateAccessToken(
	ctx context.Context,
	jwt string,
	installationID string,
) (*AccessToken, error)

GenerateAccessToken uses a JWT to generate an access token in a specific app installation (e.g., organization). See https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/ for more details.

type ApplyCommentData

type ApplyCommentData struct {
	ClusterApplies    []ClusterApply
	PullRequestClient PullRequestClient
	Env               string
}

ApplyCommentData stores data for templating out a "kubeapply apply" comment result.

type ClusterApply

type ClusterApply struct {
	ClusterConfig *config.ClusterConfig
	Results       []apply.Result
}

ClusterApply contains the results of applying in a single cluster.

func (ClusterApply) NumUpdates

func (c ClusterApply) NumUpdates() int

NumUpdates returns the number of updates that were made as part of the apply.

type ClusterDiff

type ClusterDiff struct {
	ClusterConfig *config.ClusterConfig
	Results       []diff.Result
}

ClusterDiff contains the results of a diff in a single cluster.

type ClusterStatus

type ClusterStatus struct {
	ClusterConfig *config.ClusterConfig
	HealthSummary string
}

ClusterStatus contains the results of getting the status of a cluster.

type DiffCommentData

type DiffCommentData struct {
	ClusterDiffs      []ClusterDiff
	PullRequestClient PullRequestClient
	Env               string
}

DiffCommentData stores data for templating out a "kubeapply diff" comment result.

type ErrorCommentData

type ErrorCommentData struct {
	Error error
	Env   string
	Notes []string
}

ErrorCommentData represents data for templating out a kubeapply error comment result.

type FakePullRequestClient

type FakePullRequestClient struct {
	ClusterConfigs  []*config.ClusterConfig
	Comments        []string
	RequestStatuses []PullRequestStatus
	BehindByVal     int
	ApprovedVal     bool
	Draft           bool
	Mergeable       bool
	Merged          bool
}

FakePullRequestClient is a fake implementation of a PullRequestClient. For testing purposes only.

func (*FakePullRequestClient) Approved

func (prc *FakePullRequestClient) Approved(ctx context.Context) bool

Approved returns whether this pull request is approved.

func (*FakePullRequestClient) Base

func (prc *FakePullRequestClient) Base() string

Base returns the base branch for this pull request.

func (*FakePullRequestClient) BehindBy

func (prc *FakePullRequestClient) BehindBy() int

BehindBy returns the number of commits that this pull request is behind the base by.

func (*FakePullRequestClient) Close

func (prc *FakePullRequestClient) Close() error

Close closes the client.

func (*FakePullRequestClient) GetCoveredClusters

func (prc *FakePullRequestClient) GetCoveredClusters(
	env string,
	selectedClusterGlobStrs []string,
	subpathOverride string,
) ([]*config.ClusterConfig, error)

GetCoveredClusters returns the configs of the clusters that are affected by this pull request.

func (*FakePullRequestClient) HeadSHA

func (prc *FakePullRequestClient) HeadSHA() string

HeadSHA returns the git SHA of the HEAD of the branch on which this pull request is based.

func (*FakePullRequestClient) Init

func (prc *FakePullRequestClient) Init(ctx context.Context) error

Init initializes this client.

func (*FakePullRequestClient) IsDraft

func (prc *FakePullRequestClient) IsDraft(ctx context.Context) bool

IsDraft returns whether this pull request is a draft.

func (*FakePullRequestClient) IsMergeable

func (prc *FakePullRequestClient) IsMergeable(ctx context.Context) bool

IsMergeable returns whether this pull request is mergeable.

func (*FakePullRequestClient) IsMerged

func (prc *FakePullRequestClient) IsMerged(ctx context.Context) bool

IsMerged returns whether this pull request has been merged.

func (*FakePullRequestClient) Merge

func (prc *FakePullRequestClient) Merge(
	ctx context.Context,
) error

Merge does a fake merge of this pull request.

func (*FakePullRequestClient) PostComment

func (prc *FakePullRequestClient) PostComment(ctx context.Context, body string) error

PostComment posts a fake comment.

func (*FakePullRequestClient) PostErrorComment

func (prc *FakePullRequestClient) PostErrorComment(
	ctx context.Context,
	env string,
	err error,
) error

PostErrorComment posts a fake error comment.

func (*FakePullRequestClient) Statuses

func (prc *FakePullRequestClient) Statuses(
	ctx context.Context,
) ([]PullRequestStatus, error)

Statuses returns all of the statuses associated with this pull request.

func (*FakePullRequestClient) UpdateStatus

func (prc *FakePullRequestClient) UpdateStatus(
	ctx context.Context,
	state string,
	stateContext string,
	description string,
	url string,
) error

UpdateStatus updates a status in this pull request.

type GHPullRequestClient

type GHPullRequestClient struct {
	*github.Client
	// contains filtered or unexported fields
}

GHPullRequestClient is an implementation of PullRequestClient that hits the Github API. The actual work of communicating with Github is handled by a go-github client instance.

func NewGHPullRequestClient

func NewGHPullRequestClient(
	token string,
	owner string,
	repo string,
	pullRequestNum int,
) *GHPullRequestClient

NewGHPullRequestClient returns a new GHPullRequestClient.

func (*GHPullRequestClient) Approved

func (prc *GHPullRequestClient) Approved(ctx context.Context) bool

Approved determines whether this pull request has been approved.

func (*GHPullRequestClient) Base

func (prc *GHPullRequestClient) Base() string

Base returns the base branch for this pull request.

func (*GHPullRequestClient) BehindBy

func (prc *GHPullRequestClient) BehindBy() int

BehindBy returns the number of commits this branch is behind the base by.

func (*GHPullRequestClient) Close

func (prc *GHPullRequestClient) Close() error

Close closes this client.

func (*GHPullRequestClient) GetCoveredClusters

func (prc *GHPullRequestClient) GetCoveredClusters(
	env string,
	selectedClusterGlobStrs []string,
	subpathOverride string,
) ([]*config.ClusterConfig, error)

GetCoveredClusters returns the configs of the clusters potentially affected by this pull request.

func (*GHPullRequestClient) HeadSHA

func (prc *GHPullRequestClient) HeadSHA() string

HeadSHA returns the git SHA of the HEAD of the branch that this pull request is using.

func (*GHPullRequestClient) Init

func (prc *GHPullRequestClient) Init(ctx context.Context) error

Init initializes this client by fetching information about the target pull request from the Github API.

func (*GHPullRequestClient) IsDraft

func (prc *GHPullRequestClient) IsDraft(ctx context.Context) bool

IsDraft returns whether this pull request is a draft.

func (*GHPullRequestClient) IsMergeable

func (prc *GHPullRequestClient) IsMergeable(ctx context.Context) bool

IsMergeable returns whether this pull request is mergeable according to Github.

func (*GHPullRequestClient) IsMerged

func (prc *GHPullRequestClient) IsMerged(ctx context.Context) bool

IsMerged returns whether this pull request has been merged.

func (*GHPullRequestClient) Merge

func (prc *GHPullRequestClient) Merge(
	ctx context.Context,
) error

Merge merges this pull request via the Github API.

func (*GHPullRequestClient) PostComment

func (prc *GHPullRequestClient) PostComment(ctx context.Context, body string) error

PostComment posts a comment to this pull request using the Github API.

func (*GHPullRequestClient) PostErrorComment

func (prc *GHPullRequestClient) PostErrorComment(
	ctx context.Context,
	env string,
	err error,
) error

PostErrorComment posts an error comment to this pull request using the Github API.

func (*GHPullRequestClient) Statuses

func (prc *GHPullRequestClient) Statuses(
	ctx context.Context,
) ([]PullRequestStatus, error)

Statuses returns the statuses of all checks for this pull request.

func (*GHPullRequestClient) UpdateStatus

func (prc *GHPullRequestClient) UpdateStatus(
	ctx context.Context,
	state string,
	stateContext string,
	description string,
	url string,
) error

UpdateStatus updates the status of the HEAD SHA of the branch in the pull request. Note that the "state" argument must be one of error, failure, pending, or success.

type HelpCommentData

type HelpCommentData struct {
	ClusterConfigs []*config.ClusterConfig
	Env            string
}

HelpCommentData stores data for templating out a "kubeapply help" comment result.

type PullRequestClient

type PullRequestClient interface {
	// Init initializes the client by cloning the repo, etc.
	Init(ctx context.Context) error

	// GetCoveredClusters gets the configs for all clusters "covered" by a pull request.
	GetCoveredClusters(
		env string,
		selectedClusterGlobStrs []string,
		subpathOverride string,
	) ([]*config.ClusterConfig, error)

	// PostComment posts a non-error comment in the discussion stream for a pull request.
	PostComment(ctx context.Context, body string) error

	// PostErrorComment posts an error comment in the discussion stream for a pull request.
	PostErrorComment(
		ctx context.Context,
		env string,
		err error,
	) error

	// UpdateStatus updates the status of pull request.
	UpdateStatus(
		ctx context.Context,
		state string,
		stateContext string,
		description string,
		url string,
	) error

	// Merge merges the client's pull request into the base branch.
	Merge(ctx context.Context) error

	// Statuses gets all statuses for the pull request.
	Statuses(ctx context.Context) ([]PullRequestStatus, error)

	// IsDraft returns whether the pull request is a draft.
	IsDraft(ctx context.Context) bool

	// IsMerged returns whether the pull request is already merged.
	IsMerged(ctx context.Context) bool

	// IsMergeable returns whether the pull request is mergeble.
	IsMergeable(ctx context.Context) bool

	// Approved returns whether the client pull request is approved.
	Approved(ctx context.Context) bool

	// Base returns the base branch for the pull request.
	Base() string

	// BehindBy returns the number of commits that the pull request is behind the base branch by.
	BehindBy() int

	// HeadSHA returns the SHA of the head of this pull request.
	HeadSHA() string

	// Close cleans up the resources behind this pull request.
	Close() error
}

PullRequestClient is an interface for communicating with a pull request management system, i.e. Github, for a single pull request.

type PullRequestStatus

type PullRequestStatus struct {
	Context     string
	Description string
	State       string
}

PullRequestStatus represents the status of a single check on a pull request.

func (PullRequestStatus) IsSuccess

func (prc PullRequestStatus) IsSuccess() bool

IsSuccess returns whether this PullRequestStatus is successful/green.

type StatusCommentData

type StatusCommentData struct {
	ClusterStatuses   []ClusterStatus
	PullRequestClient PullRequestClient
	Env               string
}

StatusCommentData stores data for templating out a "kubeapply status" comment result.

Jump to

Keyboard shortcuts

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