git

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneIntoNewBranch

func CloneIntoNewBranch(base Base, local, branch string, opts CloneOptions) error

func CommitRenameBranchAndPush

func CommitRenameBranchAndPush(local string, commit ChangeSet, head Head) error

func WriteAndAddFile

func WriteAndAddFile(local string, f *File) error

func WriteAndAddFiles

func WriteAndAddFiles(local string, files []*File) error

Types

type Base

type Base struct {
	Owner  string
	Repo   string
	Branch string
}

Base represents a base repository and branch.

func (Base) CloneGitURL

func (b Base) CloneGitURL() string

func (Base) CloneHTTPSURL

func (b Base) CloneHTTPSURL(token string) string

type ChangeSet

type ChangeSet struct {
	// Files is a list of files to be created and pushed.
	Files []*File

	// Message is a commit message.
	Message string

	// UserName is user.name of the committer.
	UserName string
	// UserEmail is user.email of the committer.
	UserEmail string
}

ChangeSet represents a commit to be created and pushed.

type CloneOptions

type CloneOptions struct {
	// Token is a GitHub API token used to clone the repository.
	Token string
}

type CommitFound

type CommitFound struct {
	SHA       string
	Message   string
	ChangeSet ChangeSet
}

CommitFound contains the information about a commit that was found in the repository.

This contains both the subset of information obtained from GitHub API and some derived information about the files that were affected by the commit. The affected files, named ChangeSet, contains paths and contents of the affected files, so that the caller can use this information to validate the state of the files in the repository, after some operations are done.

func FindCommits

func FindCommits(local string, sinceSHA string) ([]*CommitFound, error)

type File

type File struct {
	// Path is a path to the file within the repository.
	Path string
	// ContentString is a string that contains the content of the file.
	ContentString string
	// ContentBytes is a byte slice that contains the content of the file.
	ContentBytes []byte
	// ContentFromLocalFile is a path to a local file that contains the content of the file.
	ContentFromLocalFile string

	// ContentFunc is a function that returns the content of the file.
	// The argument is the content of the file that already exists in the repository.
	// The argument is nil if the file does not exist.
	// The return value is the content of the file that should be committed.
	ContentBytesFunc func([]byte) ([]byte, error)
}

File represents the desired state of a file in a repository. The service should create a commit that contains the file described by this struct.

The service should not create a commit if the file already exists and the content of the file is the same as the content described by this struct.

The service should create a commit if the file already exists and the content of the file is different from the content described by this struct.

The Path field is required. There must be only one of ContentString, ContentBytes, and ContentFromLocalFile set.

type GitHubRepositories

type GitHubRepositories struct {
	// Token is a GitHub API token that is used to call various GitHub API
	// and interact with a private or an internal repository.
	// If empty, the service reads the token from the GITHUB_TOKEN environment variable.
	Token string

	// TempDir is a path to a temporary directory that is used to clone the repository.
	// The service creates a subdirectory under this directory and clones the repository into it.
	//
	// If empty, the service uses the system's temporary directory.
	TempDir string

	// RetainClonedRepository specifies whether the service should retain the cloned repository
	// after the service is done.
	// This is usually set to true when the consumer of the library wants to inspect the cloned
	// repository after the service is done, perhaps for debugging.
	RetainClonedRepository bool
	// contains filtered or unexported fields
}

GitHubRepositories provides a set of operations to interact with repositories. In an abstract sense, this is used to trigger a side effect in a repository, or to validate the outcome of a side effect.

The supported operations are: - Create and push a commit to the repository - Create and push a tag to the repository - Create and push a branch to the repository - Create and send a pull request to the repository - Create a release in the repository - Send a comment to a pull request in the repository - Send a repository dispatch event to the repository - Merge a pull request in the repository - Find a commit in the repository - Find a tag in the repository - Find a pull request in the repository - Find a file in the repository (This is used by the provider to validate the repository)

The unsupported operations are: - Validate the repository (e.g. if it's a repository supposed to be reset and used by a test) - Reset the repository (e.g. delete all the branches and tags) - Find a GitHub Actions workflow run in the repository (You check the outcome of the workflow run instead)

These unsupported operations are implemented in the provider, not this abstraction, because they are provider-specific. The consumer of this library should never directly do these operations because any mistake could cause catastrophic damage to an unintended repository.

This service uses the GitHub API and the git command under the hood. Unless otherwise noted, it's assumed that the GitHub API and the git command are available. If the GitHub API or the git command is not available, the service should return an error on any operation.

For calling various GitHub API and interacting with a private or an internal repository, the service should use the GitHub API token.

func (*GitHubRepositories) Comment

func (s *GitHubRepositories) Comment(ctx context.Context, base Base, prNumber int, body string) error

Comment sends a comment to the pull request in the repository.

func (*GitHubRepositories) Dispatch

func (s *GitHubRepositories) Dispatch(ctx context.Context, base Base, eventType string, clientPayload interface{}) error

func (*GitHubRepositories) FindCommits

func (s *GitHubRepositories) FindCommits(ctx context.Context, base Base, sinceSHA string) ([]*CommitFound, error)

FindCommits finds a commit in the repository.

This does so by cloning the base repository, and finding all the commits made since the given SHA. If the SHA is empty, it finds the latest commit in the given repository and branch.

An expected use case is to call this method twice, with the first call to find the commit before the change set, and the second call to find the commit after the change set. This way, you can see if the change set is reflected in the repository.

func (*GitHubRepositories) FindPullRequests

func (s *GitHubRepositories) FindPullRequests(ctx context.Context, base Base, sinceNumber int) ([]*PullRequestFound, error)

FindPullRequests finds pull requests in the repository.

The last argument, sinceNumber is the pull request number, and the method finds the pull requests that are greater than or equal to the given number. If sinceNumber is 0, it finds the latest pull request in the given repository. The latest pull request is the pull request that is the greatest in the pull request number order.

Either way, the caller can use the first pull request in the returned list to find the latest pull request in the repository, that is at least greater than or equal to the given sinceNumber.

func (*GitHubRepositories) FindSemverTags

func (s *GitHubRepositories) FindSemverTags(ctx context.Context, base Base, sinceVer string) ([]string, error)

FindSemverTags finds semver tags in the repository.

This does so by using GitHub API to find the tags in the repository. sinceVer is a semver version string, and the method finds the tags that are greater than or equal to the given version. If sinceVer is empty, it finds the latest tag in the given repository. The latest tag is the tag that is the greatest in the semver order.

Either way, the caller can use the first tag in the returned list to find the latest tag in the repository, that is at least greater than or equal to the given sinceVer.

func (*GitHubRepositories) GetFileContent

func (s *GitHubRepositories) GetFileContent(ctx context.Context, base Base, path string) ([]byte, error)

GetFileContent finds a file in the repository.

This does so by cloning the base repository, and finding the file in the given branch.

func (*GitHubRepositories) Merge

func (s *GitHubRepositories) Merge(ctx context.Context, base Base, prNumber int) error

Merge merges the pull request in the repository.

func (*GitHubRepositories) Push

func (s *GitHubRepositories) Push(ctx context.Context, base Base, head Head, commit ChangeSet) error

Push the changeset to a repository.

This does so by cloning the base repository, writes a commit representing the change set, and pushes it to the branch, optionally tagging it.

func (*GitHubRepositories) Release

func (s *GitHubRepositories) Release(ctx context.Context, base Base, name string) error

Release creates a release in the repository, along with a lightweight tag.

func (*GitHubRepositories) Send

func (s *GitHubRepositories) Send(ctx context.Context, base Base, head Head, commit ChangeSet, pr PullRequest) (*PullRequestCreated, error)

Send pushes the given commit to the repository and creates a pull request.

type Head struct {
	Branch string
	Tag    string
}

Head represents a head branch to be created and optionally a tag to be created.

type PullRequest

type PullRequest struct {
	Title  string
	Body   string
	Labels []string
}

PullRequest contains the information about a pull request to be created and sent to a repository.

type PullRequestCreated

type PullRequestCreated struct {
	Number int
}

PullRequestCreated contains the information about a pull request that was created and sent to a repository.

type PullRequestFound

type PullRequestFound struct {
	Number  int
	BaseSHA string
	Commits []*CommitFound
}

PullRequestFound contains the information about a pull request found in the repository.

type RepoService

type RepoService struct {
	Repos *GitHubRepositories
	Owner string
	Repo  string
}

func (*RepoService) FindCommits

func (s *RepoService) FindCommits(t *testing.T, branch string, sinceSHA string) []*CommitFound

func (*RepoService) WriteFile

func (s *RepoService) WriteFile(t *testing.T, path, content, message string)

func (*RepoService) WriteFileE

func (s *RepoService) WriteFileE(path, content, message string) error

type Service

type Service interface {
	FindCommits(t *testing.T, branch string, sinceSHA string) []*CommitFound
	WriteFile(t *testing.T, path, content, message string)
	WriteFileE(path, content, message string) error
}

Jump to

Keyboard shortcuts

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