github

package
v0.0.80 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Overview

Package github provides a client for interacting with the GitHub API

Index

Constants

View Source
const (
	// ExpensiveRestCallTimeout is the timeout for expensive REST calls
	ExpensiveRestCallTimeout = 15 * time.Second
	// MaxRateLimitWait is the maximum time to wait for a rate limit to reset
	MaxRateLimitWait = 5 * time.Minute
	// MaxRateLimitRetries is the maximum number of retries for rate limit errors after waiting
	MaxRateLimitRetries = 1
	// DefaultRateLimitWaitTime is the default time to wait for a rate limit to reset
	DefaultRateLimitWaitTime = 1 * time.Minute
)

Variables

View Source
var (
	// ErrNotFound denotes if the call returned a 404
	ErrNotFound = errors.New("not found")
	// ErrBranchNotFound denotes if the branch was not found
	ErrBranchNotFound = errors.New("branch not found")
	// ErrNoPackageListingClient denotes if there is no package listing client available
	ErrNoPackageListingClient = errors.New("no package listing client available")
	// ErroNoCheckPermissions is a fixed error returned when the credentialed
	// identity has not been authorized to use the checks API
	ErroNoCheckPermissions = errors.New("missing permissions: check")
	// ErrBranchNameEmpty is a fixed error returned when the branch name is empty
	ErrBranchNameEmpty = errors.New("branch name cannot be empty")
)

Functions

func CanHandleOwner

func CanHandleOwner(_ context.Context, prov db.Provider, owner string) bool

CanHandleOwner checks if the GitHub provider has the right credentials to handle the owner

func IsMinderHook

func IsMinderHook(hook *github.Hook, hostURL string) (bool, error)

IsMinderHook checks if a GitHub hook is a Minder hook

func NewFallbackTokenClient

func NewFallbackTokenClient(appConfig config.ProviderConfig) *github.Client

NewFallbackTokenClient creates a new GitHub client that uses the GitHub App's fallback token

Types

type ClientService

type ClientService interface {
	GetInstallation(ctx context.Context, id int64, jwt string) (*github.Installation, *github.Response, error)
	GetUserIdFromToken(ctx context.Context, token *oauth2.Token) (*int64, error)
	ListUserInstallations(ctx context.Context, token *oauth2.Token) ([]*github.Installation, error)
	DeleteInstallation(ctx context.Context, id int64, jwt string) (*github.Response, error)
	GetOrgMembership(ctx context.Context, token *oauth2.Token, org string) (*github.Membership, *github.Response, error)
}

ClientService is an interface for GitHub operations It is used to mock GitHub operations in tests, but in order to generate mocks, the interface must be exported

type ClientServiceImplementation

type ClientServiceImplementation struct{}

ClientServiceImplementation is the implementation of the ClientService interface

func (ClientServiceImplementation) DeleteInstallation

func (ClientServiceImplementation) DeleteInstallation(ctx context.Context, id int64, jwt string) (*github.Response, error)

DeleteInstallation is a wrapper for the GitHub API to delete an installation

func (ClientServiceImplementation) GetInstallation

func (ClientServiceImplementation) GetInstallation(
	ctx context.Context,
	installationID int64,
	jwt string,
) (*github.Installation, *github.Response, error)

GetInstallation is a wrapper for the GitHub API to get an installation

func (ClientServiceImplementation) GetOrgMembership

func (ClientServiceImplementation) GetOrgMembership(
	ctx context.Context, token *oauth2.Token, org string,
) (*github.Membership, *github.Response, error)

GetOrgMembership is a wrapper for the GitHub API to get users' organization membership

func (ClientServiceImplementation) GetUserIdFromToken

func (ClientServiceImplementation) GetUserIdFromToken(ctx context.Context, token *oauth2.Token) (*int64, error)

GetUserIdFromToken is a wrapper for the GitHub API to get the user id from a token

func (ClientServiceImplementation) ListUserInstallations

func (ClientServiceImplementation) ListUserInstallations(
	ctx context.Context, token *oauth2.Token,
) ([]*github.Installation, error)

ListUserInstallations is a wrapper for the GitHub API to list user installations

type Delegate

type Delegate interface {
	GetCredential() provifv1.GitHubCredential
	ListAllRepositories(context.Context) ([]*minderv1.Repository, error)
	GetUserId(ctx context.Context) (int64, error)
	GetName(ctx context.Context) (string, error)
	GetLogin(ctx context.Context) (string, error)
	GetPrimaryEmail(ctx context.Context) (string, error)
	GetOwner() string
	IsOrg() bool
}

Delegate is the interface that contains operations that differ between different GitHub actors (user vs app)

type GitHub

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

GitHub is the struct that contains the shared GitHub client operations

func NewGitHub

func NewGitHub(
	client *github.Client,
	packageListingClient *github.Client,
	cache ratecache.RestClientCache,
	delegate Delegate,
	cfg *config.ProviderConfig,
	whcfg *config.WebhookConfig,
	propertyFetchers properties.GhPropertyFetcherFactory,
) *GitHub

NewGitHub creates a new GitHub client

func (*GitHub) AddAuthToPushOptions

func (c *GitHub) AddAuthToPushOptions(ctx context.Context, pushOptions *git.PushOptions) error

AddAuthToPushOptions adds authorization to the push options

func (*GitHub) CanImplement

func (_ *GitHub) CanImplement(trait minderv1.ProviderType) bool

CanImplement returns true/false depending on whether the Provider can implement the specified trait

func (*GitHub) Clone

func (c *GitHub) Clone(ctx context.Context, cloneUrl string, branch string) (*git.Repository, error)

Clone clones a GitHub repository

func (*GitHub) ClosePullRequest

func (c *GitHub) ClosePullRequest(ctx context.Context, owner, repo string, number int) (*github.PullRequest, error)

ClosePullRequest closes a pull request in a repository.

func (*GitHub) CloseSecurityAdvisory

func (c *GitHub) CloseSecurityAdvisory(ctx context.Context, owner, repo, id string) error

CloseSecurityAdvisory closes a security advisory

func (*GitHub) CreateHook

func (c *GitHub) CreateHook(ctx context.Context, owner, repo string, hook *github.Hook) (*github.Hook, error)

CreateHook creates a new Hook.

func (*GitHub) CreateIssueComment

func (c *GitHub) CreateIssueComment(
	ctx context.Context, owner, repo string, number int, comment string,
) (*github.IssueComment, error)

CreateIssueComment creates a comment on a pull request or an issue

func (*GitHub) CreatePullRequest

func (c *GitHub) CreatePullRequest(
	ctx context.Context,
	owner, repo, title, body, head, base string,
) (*github.PullRequest, error)

CreatePullRequest creates a pull request in a repository.

func (*GitHub) CreateReview

func (c *GitHub) CreateReview(
	ctx context.Context, owner, repo string, number int, reviewRequest *github.PullRequestReviewRequest,
) (*github.PullRequestReview, error)

CreateReview is a wrapper for the GitHub API to create a review

func (*GitHub) CreateSecurityAdvisory

func (c *GitHub) CreateSecurityAdvisory(ctx context.Context, owner, repo, severity, summary, description string,
	v []*github.AdvisoryVulnerability) (string, error)

CreateSecurityAdvisory creates a new security advisory

func (*GitHub) DeleteHook

func (c *GitHub) DeleteHook(ctx context.Context, owner, repo string, id int64) error

DeleteHook deletes a specified Hook.

func (*GitHub) DeregisterEntity

func (c *GitHub) DeregisterEntity(ctx context.Context, entityType minderv1.Entity, props *properties.Properties) error

DeregisterEntity implements the Provider interface

func (*GitHub) DismissReview

func (c *GitHub) DismissReview(
	ctx context.Context,
	owner, repo string,
	prId int,
	reviewId int64,
	dismissalRequest *github.PullRequestReviewDismissalRequest,
) (*github.PullRequestReview, error)

DismissReview is a wrapper for the GitHub API to dismiss a review

func (*GitHub) Do

func (c *GitHub) Do(ctx context.Context, req *http.Request) (*http.Response, error)

Do sends an API request and returns the API response.

func (*GitHub) EditHook

func (c *GitHub) EditHook(ctx context.Context, owner, repo string, id int64, hook *github.Hook) (*github.Hook, error)

EditHook edits an existing Hook.

func (*GitHub) FetchAllProperties

func (c *GitHub) FetchAllProperties(
	ctx context.Context, getByProps *properties.Properties, entType minderv1.Entity, cachedProps *properties.Properties,
) (*properties.Properties, error)

FetchAllProperties fetches all properties for the given entity

func (*GitHub) FetchProperty

func (c *GitHub) FetchProperty(
	ctx context.Context, getByProps *properties.Properties, entType minderv1.Entity, key string,
) (*properties.Property, error)

FetchProperty fetches a single property for the given entity

func (*GitHub) GetArtifactVersions

func (gv *GitHub) GetArtifactVersions(
	ctx context.Context, artifact *minderv1.Artifact,
	filter provifv1.GetArtifactVersionsFilter,
) ([]*minderv1.ArtifactVersion, error)

GetArtifactVersions returns a list of all versions for a specific artifact

func (*GitHub) GetBaseURL

func (c *GitHub) GetBaseURL() string

GetBaseURL returns the base URL for the REST API.

func (*GitHub) GetBranchProtection

func (c *GitHub) GetBranchProtection(ctx context.Context, owner string,
	repo_name string, branch_name string) (*github.Protection, error)

GetBranchProtection returns the branch protection for a given branch

func (*GitHub) GetCredential

func (c *GitHub) GetCredential() provifv1.GitHubCredential

GetCredential returns the credential used to authenticate with the GitHub API

func (*GitHub) GetEntityName

func (c *GitHub) GetEntityName(entType minderv1.Entity, props *properties.Properties) (string, error)

GetEntityName implements the Provider interface

func (*GitHub) GetLogin

func (c *GitHub) GetLogin(ctx context.Context) (string, error)

GetLogin returns the login for the acting user

func (*GitHub) GetName

func (c *GitHub) GetName(ctx context.Context) (string, error)

GetName returns the username for the acting user

func (*GitHub) GetNamespaceURL

func (c *GitHub) GetNamespaceURL() string

GetNamespaceURL returns the URL for the repository

func (*GitHub) GetPackageByName

func (c *GitHub) GetPackageByName(ctx context.Context, owner string, package_type string, package_name string,
) (*github.Package, error)

GetPackageByName returns a single package for the authenticated user or for the org

func (*GitHub) GetPackageVersionById

func (c *GitHub) GetPackageVersionById(ctx context.Context, owner string, packageType string, packageName string,
	version int64) (*github.PackageVersion, error)

GetPackageVersionById returns a single package version for the specific id

func (*GitHub) GetPrimaryEmail

func (c *GitHub) GetPrimaryEmail(ctx context.Context) (string, error)

GetPrimaryEmail returns the primary email for the acting user

func (*GitHub) GetPullRequest

func (c *GitHub) GetPullRequest(
	ctx context.Context,
	owner string,
	repo string,
	number int,
) (*github.PullRequest, error)

GetPullRequest is a wrapper for the GitHub API to get a pull request

func (*GitHub) GetRepository

func (c *GitHub) GetRepository(ctx context.Context, owner string, name string) (*github.Repository, error)

GetRepository returns a single repository for the authenticated user

func (*GitHub) GetUserId

func (c *GitHub) GetUserId(ctx context.Context) (int64, error)

GetUserId returns the user id for the acting user

func (*GitHub) IsOrg

func (c *GitHub) IsOrg() bool

IsOrg returns true if the owner is an organization

func (*GitHub) ListAllRepositories

func (c *GitHub) ListAllRepositories(ctx context.Context) ([]*minderv1.Repository, error)

ListAllRepositories lists all repositories the credential has access to

func (*GitHub) ListFiles

func (c *GitHub) ListFiles(
	ctx context.Context,
	owner string,
	repo string,
	prNumber int,
	perPage int,
	pageNumber int,
) ([]*github.CommitFile, *github.Response, error)

ListFiles is a wrapper for the GitHub API to list files in a pull request

func (*GitHub) ListHooks

func (c *GitHub) ListHooks(ctx context.Context, owner, repo string) ([]*github.Hook, error)

ListHooks lists all Hooks for the specified repository.

func (*GitHub) ListImages

func (c *GitHub) ListImages(ctx context.Context) ([]string, error)

ListImages lists all containers in the GitHub Container Registry

func (*GitHub) ListIssueComments

func (c *GitHub) ListIssueComments(
	ctx context.Context, owner, repo string, number int, opts *github.IssueListCommentsOptions,
) ([]*github.IssueComment, error)

ListIssueComments is a wrapper for the GitHub API to get all comments in a review

func (*GitHub) ListPackagesByRepository

func (c *GitHub) ListPackagesByRepository(
	ctx context.Context,
	owner string,
	artifactType string,
	repositoryId int64,
	pageNumber int,
	itemsPerPage int,
) ([]*github.Package, error)

ListPackagesByRepository returns a list of all packages for a specific repository

func (*GitHub) ListPullRequests

func (c *GitHub) ListPullRequests(
	ctx context.Context,
	owner, repo string,
	opt *github.PullRequestListOptions,
) ([]*github.PullRequest, error)

ListPullRequests lists all pull requests in a repository.

func (*GitHub) ListReviews

func (c *GitHub) ListReviews(
	ctx context.Context,
	owner, repo string,
	number int,
	opt *github.ListOptions,
) ([]*github.PullRequestReview, error)

ListReviews is a wrapper for the GitHub API to list reviews

func (*GitHub) NewRequest

func (c *GitHub) NewRequest(method, requestUrl string, body any) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlStr, which will be resolved to the BaseURL of the Client. Relative URLS should always be specified without a preceding slash. If specified, the value pointed to by body is JSON encoded and included as the request body.

func (*GitHub) PropertiesToProtoMessage

func (c *GitHub) PropertiesToProtoMessage(
	entType minderv1.Entity, props *properties.Properties,
) (protoreflect.ProtoMessage, error)

PropertiesToProtoMessage implements the ProtoMessageConverter interface

func (*GitHub) RegisterEntity

func (c *GitHub) RegisterEntity(
	ctx context.Context, entityType minderv1.Entity, props *properties.Properties,
) (*properties.Properties, error)

RegisterEntity implements the Provider interface

func (*GitHub) ReregisterEntity

func (c *GitHub) ReregisterEntity(
	ctx context.Context, entityType minderv1.Entity, props *properties.Properties,
) error

ReregisterEntity implements the Provider interface

func (*GitHub) SetCommitStatus

func (c *GitHub) SetCommitStatus(
	ctx context.Context, owner, repo, ref string, status *github.RepoStatus,
) (*github.RepoStatus, error)

SetCommitStatus is a wrapper for the GitHub API to set a commit status

func (*GitHub) StartCheckRun

func (c *GitHub) StartCheckRun(
	ctx context.Context, owner, repo string, opts *github.CreateCheckRunOptions,
) (*github.CheckRun, error)

StartCheckRun calls the GitHub API to initialize a new check using the supplied options.

func (*GitHub) SupportsEntity

func (c *GitHub) SupportsEntity(entType minderv1.Entity) bool

SupportsEntity implements the Provider interface

func (*GitHub) UpdateBranchProtection

func (c *GitHub) UpdateBranchProtection(
	ctx context.Context, owner, repo, branch string, preq *github.ProtectionRequest,
) error

UpdateBranchProtection updates the branch protection for a given branch

func (*GitHub) UpdateCheckRun

func (c *GitHub) UpdateCheckRun(
	ctx context.Context, owner, repo string, checkRunID int64, opts *github.UpdateCheckRunOptions,
) (*github.CheckRun, error)

UpdateCheckRun updates an existing check run in GitHub. The check run is referenced using its run ID. This function returns the updated CheckRun srtuct.

func (*GitHub) UpdateIssueComment

func (c *GitHub) UpdateIssueComment(ctx context.Context, owner, repo string, number int64, comment string) error

UpdateIssueComment updates a comment on a pull request or an issue

func (*GitHub) UpdateReview

func (c *GitHub) UpdateReview(
	ctx context.Context, owner, repo string, number int, reviewId int64, body string,
) (*github.PullRequestReview, error)

UpdateReview is a wrapper for the GitHub API to update a review

Directories

Path Synopsis
Package clients contains github client logic
Package clients contains github client logic
mock
Package mock_clients is a generated GoMock package.
Package mock_clients is a generated GoMock package.
Package common provides common utilities for the GitHub provider
Package common provides common utilities for the GitHub provider
Package ghcr provides a client for interacting with the GitHub Container Registry
Package ghcr provides a client for interacting with the GitHub Container Registry
Package installations contains logic relating to GitHub provider installations
Package installations contains logic relating to GitHub provider installations
Package manager contains the GitHubProviderClassManager
Package manager contains the GitHubProviderClassManager
Package mock_github is a generated GoMock package.
Package mock_github is a generated GoMock package.
fixtures
Package fixtures contains code for creating ProfileService fixtures and is used in various parts of the code.
Package fixtures contains code for creating ProfileService fixtures and is used in various parts of the code.
Package properties provides utility functions for fetching and managing properties
Package properties provides utility functions for fetching and managing properties
Package service contains the GitHubProviderService
Package service contains the GitHubProviderService
mock
Package mock_service is a generated GoMock package.
Package mock_service is a generated GoMock package.
Package webhook implements github webhook handlers for the github provider
Package webhook implements github webhook handlers for the github provider

Jump to

Keyboard shortcuts

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