host

package
v0.29.1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2025 License: AGPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFileNotFound        = errors.New("file not found")
	ErrPullRequestNotFound = errors.New("pull request not found")
)

Functions

func CreatePullRequestCommentWithIdentifier

func CreatePullRequestCommentWithIdentifier(body string, identifier string, pr *PullRequest, repo Repository) error

func DeletePullRequestCommentByIdentifier

func DeletePullRequestCommentByIdentifier(identifier string, pr *PullRequest, repo Repository) error

func UpdatePullRequestCache added in v0.27.0

func UpdatePullRequestCache(clock clock.Clock, hosts []Host, prCache PullRequestCache) error

UpdatePullRequestCache iterates over all hosts and updates prCache. It performs a full update if no previous update ran for a host and performs a partial update otherwise.

Types

type Cacher added in v0.27.0

type Cacher interface {
	Delete(key string) error
	DeleteAllByTag(tagName string) error
	Get(key string) ([]byte, error)
	GetAllByTag(tag string) ([][]byte, error)
	Set(key string, value []byte) error
	SetWithTags(key string, value []byte, tags ...string) error
}

Cacher defines functions expected by a cache. github.com/wndhydrnt/saturn-bot/pkg/cache.Cache implements this interface.

type GitHubHost

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

func NewGitHubHost

func NewGitHubHost(address, token string, cacheDisabled bool) (*GitHubHost, error)

func (*GitHubHost) AuthenticatedUser added in v0.10.0

func (g *GitHubHost) AuthenticatedUser() (*UserInfo, error)

func (*GitHubHost) CreateFromJson added in v0.18.0

func (g *GitHubHost) CreateFromJson(dec *json.Decoder) (Repository, error)

CreateFromJson implements Host.

func (*GitHubHost) CreateFromName

func (g *GitHubHost) CreateFromName(name string) (Repository, error)

func (*GitHubHost) Name added in v0.10.0

func (g *GitHubHost) Name() string

func (*GitHubHost) PullRequestFactory added in v0.27.0

func (g *GitHubHost) PullRequestFactory() PullRequestFactory

PullRequestFactory implements Host.

func (*GitHubHost) PullRequestIterator added in v0.27.0

func (g *GitHubHost) PullRequestIterator() PullRequestIterator

PullRequestIterator implements Host.

func (*GitHubHost) RepositoryIterator added in v0.27.0

func (g *GitHubHost) RepositoryIterator() RepositoryIterator

func (*GitHubHost) Type added in v0.27.0

func (g *GitHubHost) Type() Type

Type implements Host.

type GitHubRepository

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

func (*GitHubRepository) BaseBranch

func (g *GitHubRepository) BaseBranch() string

func (*GitHubRepository) CanMergePullRequest

func (g *GitHubRepository) CanMergePullRequest(pr *PullRequest) (bool, error)

func (*GitHubRepository) CloneUrlHttp

func (g *GitHubRepository) CloneUrlHttp() string

func (*GitHubRepository) CloneUrlSsh

func (g *GitHubRepository) CloneUrlSsh() string

func (*GitHubRepository) ClosePullRequest

func (g *GitHubRepository) ClosePullRequest(msg string, pr *PullRequest) (*PullRequest, error)

func (*GitHubRepository) CreatePullRequest

func (g *GitHubRepository) CreatePullRequest(branch string, data PullRequestData) (*PullRequest, error)

func (*GitHubRepository) CreatePullRequestComment

func (g *GitHubRepository) CreatePullRequestComment(body string, pr *PullRequest) error

func (*GitHubRepository) DeleteBranch

func (g *GitHubRepository) DeleteBranch(pr *PullRequest) error

func (*GitHubRepository) DeletePullRequestComment

func (g *GitHubRepository) DeletePullRequestComment(comment PullRequestComment, _ *PullRequest) error

func (*GitHubRepository) FindPullRequest

func (g *GitHubRepository) FindPullRequest(branch string) (*PullRequest, error)

func (*GitHubRepository) FullName

func (g *GitHubRepository) FullName() string

func (*GitHubRepository) GetPullRequestBody

func (g *GitHubRepository) GetPullRequestBody(pr *PullRequest) string

func (*GitHubRepository) HasSuccessfulPullRequestBuild

func (g *GitHubRepository) HasSuccessfulPullRequestBuild(pr *PullRequest) (bool, error)

func (*GitHubRepository) Host

func (g *GitHubRepository) Host() HostDetail

func (*GitHubRepository) ID added in v0.18.0

func (g *GitHubRepository) ID() int64

ID implements Repository.

func (*GitHubRepository) IsArchived added in v0.18.0

func (g *GitHubRepository) IsArchived() bool

IsArchived implements Repository.

func (*GitHubRepository) IsPullRequestClosed

func (g *GitHubRepository) IsPullRequestClosed(pr PullRequestRaw) bool

IsPullRequestClosed implements Repository. Note: uses MergedAt attribute instead of Merged because Merge attribute isn't set when listing pull requests via the GitHub API.

func (*GitHubRepository) IsPullRequestMerged

func (g *GitHubRepository) IsPullRequestMerged(pr PullRequestRaw) bool

IsPullRequestMerged implements Repository. Note: uses MergedAt attribute instead of Merged because Merge attribute isn't set when listing pull requests via the GitHub API.

func (*GitHubRepository) IsPullRequestOpen

func (g *GitHubRepository) IsPullRequestOpen(pr PullRequestRaw) bool

IsPullRequestOpen implements Repository.

func (*GitHubRepository) ListPullRequestComments

func (g *GitHubRepository) ListPullRequestComments(pr *PullRequest) ([]PullRequestComment, error)

func (*GitHubRepository) MergePullRequest

func (g *GitHubRepository) MergePullRequest(deleteBranch bool, pr *PullRequest) error

func (*GitHubRepository) Name

func (g *GitHubRepository) Name() string

func (*GitHubRepository) Owner

func (g *GitHubRepository) Owner() string

func (*GitHubRepository) Raw added in v0.18.0

func (g *GitHubRepository) Raw() any

Raw implements Repository.

func (*GitHubRepository) UpdatePullRequest

func (g *GitHubRepository) UpdatePullRequest(data PullRequestData, pr *PullRequest) error

func (*GitHubRepository) UpdatedAt added in v0.19.0

func (g *GitHubRepository) UpdatedAt() time.Time

UpdatedAt implements Repository.

func (*GitHubRepository) WebUrl

func (g *GitHubRepository) WebUrl() string

type GitLabHost

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

func NewGitLabHost

func NewGitLabHost(addr, token string) (*GitLabHost, error)

func (*GitLabHost) AuthenticatedUser added in v0.10.0

func (g *GitLabHost) AuthenticatedUser() (*UserInfo, error)

func (*GitLabHost) CreateFromJson added in v0.18.0

func (g *GitLabHost) CreateFromJson(dec *json.Decoder) (Repository, error)

CreateFromJson implements Host.

func (*GitLabHost) CreateFromName

func (g *GitLabHost) CreateFromName(name string) (Repository, error)

func (*GitLabHost) Name added in v0.10.0

func (g *GitLabHost) Name() string

func (*GitLabHost) PullRequestFactory added in v0.27.0

func (g *GitLabHost) PullRequestFactory() PullRequestFactory

PullRequestFactory implements Host.

func (*GitLabHost) PullRequestIterator added in v0.27.0

func (g *GitLabHost) PullRequestIterator() PullRequestIterator

PullRequestIterator implements Host.

func (*GitLabHost) RepositoryIterator added in v0.27.0

func (g *GitLabHost) RepositoryIterator() RepositoryIterator

func (*GitLabHost) SearchCode added in v0.18.0

func (g *GitLabHost) SearchCode(gitlabGroupID any, query string) ([]int64, error)

SearchCode implements GitLabSearcher. It returns a list of unique IDs of all projects returned by the search query. The IDs are sorted in ascending order.

func (*GitLabHost) Type added in v0.27.0

func (g *GitLabHost) Type() Type

Type implements Host.

type GitLabRepository

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

func (*GitLabRepository) BaseBranch

func (g *GitLabRepository) BaseBranch() string

func (*GitLabRepository) CanMergePullRequest

func (g *GitLabRepository) CanMergePullRequest(_ *PullRequest) (bool, error)

func (*GitLabRepository) CloneUrlHttp

func (g *GitLabRepository) CloneUrlHttp() string

func (*GitLabRepository) CloneUrlSsh

func (g *GitLabRepository) CloneUrlSsh() string

func (*GitLabRepository) ClosePullRequest

func (g *GitLabRepository) ClosePullRequest(msg string, pr *PullRequest) (*PullRequest, error)

func (*GitLabRepository) CreatePullRequest

func (g *GitLabRepository) CreatePullRequest(branch string, data PullRequestData) (*PullRequest, error)

func (*GitLabRepository) CreatePullRequestComment

func (g *GitLabRepository) CreatePullRequestComment(body string, pr *PullRequest) error

func (*GitLabRepository) DeleteBranch

func (g *GitLabRepository) DeleteBranch(pr *PullRequest) error

func (*GitLabRepository) DeletePullRequestComment

func (g *GitLabRepository) DeletePullRequestComment(comment PullRequestComment, pr *PullRequest) error

func (*GitLabRepository) FindPullRequest

func (g *GitLabRepository) FindPullRequest(branch string) (*PullRequest, error)

func (*GitLabRepository) FullName

func (g *GitLabRepository) FullName() string

func (*GitLabRepository) GetPullRequestBody

func (g *GitLabRepository) GetPullRequestBody(pr *PullRequest) string

func (*GitLabRepository) HasSuccessfulPullRequestBuild

func (g *GitLabRepository) HasSuccessfulPullRequestBuild(pr *PullRequest) (bool, error)

func (*GitLabRepository) Host

func (g *GitLabRepository) Host() HostDetail

func (*GitLabRepository) ID added in v0.18.0

func (g *GitLabRepository) ID() int64

ID implements Host.

func (*GitLabRepository) IsArchived added in v0.18.0

func (g *GitLabRepository) IsArchived() bool

IsArchived implements Repository.

func (*GitLabRepository) IsPullRequestClosed

func (g *GitLabRepository) IsPullRequestClosed(pr interface{}) bool

func (*GitLabRepository) IsPullRequestMerged

func (g *GitLabRepository) IsPullRequestMerged(pr interface{}) bool

func (*GitLabRepository) IsPullRequestOpen

func (g *GitLabRepository) IsPullRequestOpen(pr interface{}) bool

func (*GitLabRepository) ListPullRequestComments

func (g *GitLabRepository) ListPullRequestComments(pr *PullRequest) ([]PullRequestComment, error)

func (*GitLabRepository) MergePullRequest

func (g *GitLabRepository) MergePullRequest(deleteBranch bool, pr *PullRequest) error

func (*GitLabRepository) Name

func (g *GitLabRepository) Name() string

func (*GitLabRepository) Owner

func (g *GitLabRepository) Owner() string

func (*GitLabRepository) PullRequest added in v0.2.0

func (g *GitLabRepository) PullRequest(pr any) *PullRequest

func (*GitLabRepository) Raw added in v0.18.0

func (g *GitLabRepository) Raw() any

Raw implements Repository.

func (*GitLabRepository) UpdatePullRequest

func (g *GitLabRepository) UpdatePullRequest(data PullRequestData, pr *PullRequest) error

func (*GitLabRepository) UpdatedAt added in v0.19.0

func (g *GitLabRepository) UpdatedAt() time.Time

UpdatedAt implements Repository.

func (*GitLabRepository) WebUrl

func (g *GitLabRepository) WebUrl() string

type GitLabSearcher added in v0.18.0

type GitLabSearcher interface {
	// SearchCode returns a list of GitLab project IDs that match the search query.
	// If gitlabGroupID is not nil, the search is limited to projects
	// in the specified GitLab group and its sub-groups.
	SearchCode(gitlabGroupID any, query string) ([]int64, error)
}

GitLabSearcher defines methods to search GitLab.

type Host

type Host interface {
	HostDetail
	CreateFromName(name string) (Repository, error)
	// CreateFromJson takes a JSON decoder from which to unmarshal a Repository and return it.
	CreateFromJson(dec *json.Decoder) (Repository, error)
	// RepositoryIterator returns an implementation of [RepositoryIterator] to iterate over repositories of the host.
	RepositoryIterator() RepositoryIterator
	// PullRequestFactory return a [PullRequestFactory] that creates a new data struct of a pull request for the host.
	PullRequestFactory() PullRequestFactory
	// PullRequestIterator returns an implementation of [PullRequestIterator] to iterate over pull requests of the host.
	PullRequestIterator() PullRequestIterator
	// Type returns the type of the host.
	Type() Type
}

type HostDetail added in v0.10.0

type HostDetail interface {
	AuthenticatedUser() (*UserInfo, error)
	Name() string
}

type PullRequest added in v0.2.0

type PullRequest struct {
	// CreatedAt is the time and date at which the pull request has been created.
	CreatedAt time.Time
	// Number is the identifier of the pull request.
	Number int64
	// WebURL is the URL humans visit to view the pull request.
	WebURL string
	// Raw is the raw data structure of the pull request.
	Raw PullRequestRaw
	// State denotes the current state of the pull request.
	State PullRequestState
	// HostName is the name of the [Host] that returned the pull request.
	HostName string
	// BranchName is the name of the source branch.
	BranchName string
	// RepositoryName is the full name of the repository for which the pull request has been created.
	RepositoryName string
	// Type indicates the type of host this pull request belongs to.
	Type Type
}

PullRequest holds data on an existing pull request.

type PullRequestCache added in v0.27.0

type PullRequestCache interface {
	// Delete deletes the data in the cache, identified by branchName and repo.
	Delete(branchName, repo string)
	// Get reads the data from the cache, identified by branchName and repo.
	// It returns nil if the cache doesn't contain data.
	Get(branchName, repoName string) *PullRequest
	// Set writes pr to the cache, identified by branchName and repoName.
	Set(branchName, repoName string, pr *PullRequest)
	// LastUpdatedAtFor returns the last time at which the cache was updated for host.
	LastUpdatedAtFor(host Host) *time.Time
	// SetLastUpdatedAtFor sets the last time at which the cache was updated for host.
	SetLastUpdatedAtFor(host Host, updatedAt time.Time)
	// SetPullRequestFactory adds a factory function that returns the data struct for hostType to unmarshal
	// a pull request struct.
	SetPullRequestFactory(hostType Type, fac PullRequestFactory)
}

A PullRequestCache caches pull request data.

func NewPullRequestCache added in v0.27.0

func NewPullRequestCache(c Cacher, factories map[Type]PullRequestFactory) PullRequestCache

func NewPullRequestCacheFromHosts added in v0.27.0

func NewPullRequestCacheFromHosts(c Cacher, hosts []Host) PullRequestCache

type PullRequestComment

type PullRequestComment struct {
	Body string
	ID   int64
}

type PullRequestData

type PullRequestData struct {
	Assignees      []string
	AutoMerge      bool
	AutoMergeAfter time.Duration
	Body           string
	Labels         []string
	MergeOnce      bool
	Reviewers      []string
	TaskName       string
	TemplateData   template.Data
	Title          string
}

func (PullRequestData) GetBody

func (prd PullRequestData) GetBody() (string, error)

type PullRequestFactory added in v0.27.0

type PullRequestFactory func() any

PullRequestFactory is a function that returns a new instance of the struct that represents a pull request of a host. This function is used to properly unmarshal cached data.

type PullRequestIterator added in v0.27.0

type PullRequestIterator interface {
	// ListPullRequests returns an iter.Seq to iterate over pull requests in a host.
	// If since is not nil, the function should return only the pull requests that have changed since the given time.
	ListPullRequests(since *time.Time) iter.Seq[*PullRequest]
	// Error returns an error that occurred during ListPullRequests.
	// Callers of ListPullRequests should call this function after the iterator has returned to check if there was an error.
	Error() error
}

PullRequestIterator is an iterator to iterate over pull requests in a host.

type PullRequestRaw added in v0.27.0

type PullRequestRaw any

PullRequestRaw is the raw, underlying struct of a Pull Request in a host.

type PullRequestState added in v0.20.0

type PullRequestState int

PullRequestState denotes the current state of the pull request.

const (
	PullRequestStateUnknown PullRequestState = iota
	PullRequestStateOpen
	PullRequestStateClosed
	PullRequestStateMerged
	PullRequestStateArchived
)

type Repository

type Repository interface {
	BaseBranch() string
	CanMergePullRequest(pr *PullRequest) (bool, error)
	CloneUrlHttp() string
	CloneUrlSsh() string
	ClosePullRequest(msg string, pr *PullRequest) (*PullRequest, error)
	CreatePullRequestComment(body string, pr *PullRequest) error
	CreatePullRequest(branch string, data PullRequestData) (*PullRequest, error)
	DeleteBranch(pr *PullRequest) error
	DeletePullRequestComment(comment PullRequestComment, pr *PullRequest) error
	FindPullRequest(branch string) (*PullRequest, error)
	FullName() string
	GetPullRequestBody(pr *PullRequest) string
	HasSuccessfulPullRequestBuild(pr *PullRequest) (bool, error)
	Host() HostDetail
	// ID returns the global, unique identifier of the repository in the host.
	ID() int64
	// IsArchived returns true if the repository has been archived on the host.
	IsArchived() bool
	ListPullRequestComments(pr *PullRequest) ([]PullRequestComment, error)
	MergePullRequest(deleteBranch bool, pr *PullRequest) error
	Name() string
	Owner() string
	UpdatePullRequest(data PullRequestData, pr *PullRequest) error
	WebUrl() string
	// Raw returns the underlying data structure of the Repository struct.
	// The raw struct is marshalled to JSON.
	Raw() any
	// UpdatedAt returns the last time the repository has been updated,
	// according to the host.
	// The time is used to decide if new commits need to be pulled from the host.
	UpdatedAt() time.Time
}

func NewRepositoryFromName added in v0.18.0

func NewRepositoryFromName(hosts []Host, repositoryName string) (Repository, error)

NewRepositoryFromName create a new Repository by finding the Host that serves the repository.

Returns an error if no Host can be identified.

type RepositoryCache added in v0.27.0

type RepositoryCache struct {
	Cacher Cacher
	Clock  clock.Clock
	Ttl    time.Duration
	// contains filtered or unexported fields
}

RepositoryCache reads all repositories from hosts and stores them in a file cache. It helps reduce API requests to the host.

func NewRepositoryCache added in v0.27.0

func NewRepositoryCache(c Cacher, clock clock.Clock, dir string, ttl time.Duration) *RepositoryCache

NewRepositoryCache returns a new RepositoryCache.

func (*RepositoryCache) List added in v0.27.0

func (rc *RepositoryCache) List(hosts []Host, result chan Repository, errChan chan error)

List implements RepositoryLister.

type RepositoryIterator added in v0.27.0

type RepositoryIterator interface {
	// ListRepositories returns an iter.Seq to iterate over repositories in a host.
	// If since is not nil, the function should return only the repositories that have been updated since the given time.
	ListRepositories(since *time.Time) iter.Seq[Repository]
	// Error returns an error that occurred during ListRepositories.
	// Callers of ListRepositories should call this function after the iterator has returned to check if there was an error.
	Error() error
}

RepositoryIterator is an iterator to iterate over repositories in a host.

type RepositoryLister added in v0.18.0

type RepositoryLister interface {
	List(hosts []Host, result chan Repository, errChan chan error)
}

RepositoryLister lists all repositories from the cache.

An implementation queries all hosts to gather the list of repositories. Every repository is then send to the result channel. If an error occurs, then the error is sent to the errChan channel.

type Type added in v0.27.0

type Type string
const (
	GitHubType Type = "github"
	GitLabType Type = "gitlab"
)

type UserInfo added in v0.10.0

type UserInfo struct {
	Email string
	Name  string
}

Jump to

Keyboard shortcuts

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