gitproviders

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2021 License: MPL-2.0 Imports: 12 Imported by: 7

README

Cache system of gitproviders pkg

How it works

This cache system works through Cassettes. A Cassette is a yaml file that stores all the interactions made towards the github api where that same Cassette was involved. This file is generated the first time the tests using the same cassette are run. If the file exists, that cassette will be used to emulate the previously recorded responses.

How to implement a Cassette recorder

Set up environment variables
  • GITHUB_TOKEN is used for authentication.
  • GITHUB_ORG is used to specify the Github organization we want to test with.
  • GITHUB_USER is used to specify the Github account we want to test with.

Note: Only GITHUB_TOKEN is a must, the other can be set as needed.

Set up a recorder

To initialize the recorder, use the function getTestClientWithCassette:

client, recorder, err := getTestClientWithCassette("CASSETTE_NAME")
  • client is the object from fluxcd/go-git-providers that was injected with the recorder and makes the api calls.
  • recorder is the object from dnaeon/go-vcr that helps to save the yaml file which is triggered by using recorder.Done().
  • CASSETTE_NAME name of the Cassette.
Complete implementation example using ginkgo
var _ = Describe("create github repo", func() {
	accounts := getAccounts()  

    var gitProvider defaultGitProvider
	var recorder *recorder.Recorder
	var err error
	BeforeEach(func() {
	    var client gitprovider.Client
		client, recorder, err = getTestClientWithCassette("CASSETTE_ID")
		Expect(err).NotTo(HaveOccurred())
		gitProvider = defaultGitProvider{
			provider: client,
		}
	})

	It("should create a repository successfully", func() {
		err = gitProvider.CreateRepository(repoName, accounts.GithubOrgName, true)
		Expect(err).NotTo(HaveOccurred())
	})

	AfterEach(func() {
	    // delete org repo
		ctx := context.Background()
		orgRepoRef := NewOrgRepositoryRef(github.DefaultDomain, accounts.GithubOrgName, repoName)
		org, err := client.OrgRepositories().Get(ctx, orgRepoRef)
		Expect(err).NotTo(HaveOccurred())
		err = org.Delete(ctx)
		Expect(err).NotTo(HaveOccurred())
		// save Cassette in pkg/gitproviders/cache/CASSETTE_ID.yaml
		err = recorder.Stop()
		Expect(err).NotTo(HaveOccurred())
	})

})

Troubleshooting

  • If you face the error Requested interaction not found it means there is an api call that was not saved in the Cassette when it was created.
  • If you added more interactions to an existing test you will need to regenerate the Cassette yaml. To do so remove yaml file and rerun the test. Otherwise, you will get the previous error.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewOrgRepositoryRef

func NewOrgRepositoryRef(domain, org, repoName string) gitprovider.OrgRepositoryRef

func NewRepositoryInfo

func NewRepositoryInfo(description string, visibility gitprovider.RepositoryVisibility) gitprovider.RepositoryInfo

func NewUserRepositoryRef

func NewUserRepositoryRef(domain, user, repoName string) gitprovider.UserRepositoryRef

Types

type Config added in v0.2.2

type Config struct {
	// Provider defines the GitProvider.
	Provider GitProviderName

	// Hostname is the HTTP/S hostname of the Provider,
	// e.g. github.example.com.
	Hostname string

	// Token contains the token used to authenticate with the
	// Provider.
	Token string
}

Config defines the configuration for connecting to a GitProvider.

type GitProvider added in v0.2.2

type GitProvider interface {
	CreateRepository(name string, owner string, private bool) error
	RepositoryExists(name string, owner string) (bool, error)
	DeployKeyExists(owner, repoName string) (bool, error)
	GetRepoInfo(accountType ProviderAccountType, owner string, repoName string) (*gitprovider.RepositoryInfo, error)
	GetRepoInfoFromUrl(url string) (*gitprovider.RepositoryInfo, error)
	GetDefaultBranch(url string) (string, error)
	GetRepoVisibility(url string) (*gitprovider.RepositoryVisibility, error)
	UploadDeployKey(owner, repoName string, deployKey []byte) error
	CreatePullRequestToUserRepo(userRepRef gitprovider.UserRepositoryRef, targetBranch string, newBranch string, files []gitprovider.CommitFile, commitMessage string, prTitle string, prDescription string) (gitprovider.PullRequest, error)
	CreatePullRequestToOrgRepo(orgRepRef gitprovider.OrgRepositoryRef, targetBranch string, newBranch string, files []gitprovider.CommitFile, commitMessage string, prTitle string, prDescription string) (gitprovider.PullRequest, error)
	GetCommitsFromUserRepo(userRepRef gitprovider.UserRepositoryRef, targetBranch string, pageSize int, pageToken int) ([]gitprovider.Commit, error)
	GetCommitsFromOrgRepo(orgRepRef gitprovider.OrgRepositoryRef, targetBranch string, pageSize int, pageToken int) ([]gitprovider.Commit, error)
	GetAccountType(owner string) (ProviderAccountType, error)
}

GitProvider Handler

func New added in v0.1.0

func New(config Config) (GitProvider, error)

type GitProviderName added in v0.2.2

type GitProviderName string

GitProviderName holds a Git provider definition.

const (
	GitProviderGitHub GitProviderName = "github"
	GitProviderGitLab GitProviderName = "gitlab"
)

func DetectGitProviderFromUrl added in v0.2.3

func DetectGitProviderFromUrl(raw string) (GitProviderName, error)

DetectGitProviderFromUrl accepts a url related to a git repo and returns the name of the provider associated. The raw URL is assumed to be something like ssh://git@github.com/myorg/myrepo.git. The common `git clone` variant of `git@github.com:myorg/myrepo.git` is not supported.

type NormalizedRepoURL added in v0.2.5

type NormalizedRepoURL struct {
	// contains filtered or unexported fields
}

func NewNormalizedRepoURL added in v0.2.5

func NewNormalizedRepoURL(uri string) (NormalizedRepoURL, error)

func (NormalizedRepoURL) Owner added in v0.2.5

func (n NormalizedRepoURL) Owner() string

func (NormalizedRepoURL) Protocol added in v0.2.5

func (NormalizedRepoURL) Provider added in v0.2.5

func (n NormalizedRepoURL) Provider() GitProviderName

func (NormalizedRepoURL) RepositoryName added in v0.2.5

func (n NormalizedRepoURL) RepositoryName() string

func (NormalizedRepoURL) String added in v0.2.5

func (n NormalizedRepoURL) String() string

func (NormalizedRepoURL) URL added in v0.2.5

func (n NormalizedRepoURL) URL() *url.URL

type ProviderAccountType added in v0.0.4

type ProviderAccountType string
const (
	AccountTypeUser ProviderAccountType = "user"
	AccountTypeOrg  ProviderAccountType = "organization"
)

type RepositoryURLProtocol added in v0.2.5

type RepositoryURLProtocol string
const RepositoryURLProtocolHTTPS RepositoryURLProtocol = "https"
const RepositoryURLProtocolSSH RepositoryURLProtocol = "ssh"

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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