mirror of https://github.com/harness/drone.git
add methods in gitness scm (#2540)
* fix lint issue * fix lint issue * add credentials in methods * add methods in gitness scmpull/3545/head
parent
01e26bdb82
commit
e888f8c481
|
@ -31,6 +31,7 @@ import (
|
|||
urlprovider "github.com/harness/gitness/app/url"
|
||||
"github.com/harness/gitness/git"
|
||||
"github.com/harness/gitness/types"
|
||||
"github.com/harness/gitness/types/enum"
|
||||
)
|
||||
|
||||
var _ Provider = (*GitnessSCM)(nil)
|
||||
|
@ -47,6 +48,83 @@ type GitnessSCM struct {
|
|||
urlProvider urlprovider.Provider
|
||||
}
|
||||
|
||||
// ListBranches implements Provider.
|
||||
func (s *GitnessSCM) ListBranches(ctx context.Context,
|
||||
filter *BranchFilter,
|
||||
_ *ResolvedCredentials) ([]Branch, error) {
|
||||
repo, err := s.repoStore.FindByRef(ctx, filter.Repository)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find repo: %w", err)
|
||||
}
|
||||
rpcOut, err := s.git.ListBranches(ctx, &git.ListBranchesParams{
|
||||
ReadParams: git.CreateReadParams(repo),
|
||||
IncludeCommit: false,
|
||||
Query: filter.Query,
|
||||
Sort: git.BranchSortOptionDate,
|
||||
Order: git.SortOrderDesc,
|
||||
Page: int32(filter.Page),
|
||||
PageSize: int32(filter.Size),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
branches := make([]Branch, len(rpcOut.Branches))
|
||||
for i := range rpcOut.Branches {
|
||||
branches[i] = mapBranch(rpcOut.Branches[i])
|
||||
}
|
||||
|
||||
return branches, nil
|
||||
}
|
||||
|
||||
func mapBranch(b git.Branch) Branch {
|
||||
return Branch{
|
||||
Name: b.Name,
|
||||
SHA: b.SHA.String(),
|
||||
}
|
||||
}
|
||||
|
||||
// ListReporisotries implements Provider.
|
||||
func (s *GitnessSCM) ListReporisotries(ctx context.Context,
|
||||
filter *RepositoryFilter,
|
||||
_ *ResolvedCredentials) ([]Repository, error) {
|
||||
repos, err := s.repoStore.List(ctx, filter.SpaceID, &types.RepoFilter{
|
||||
Page: filter.Page,
|
||||
Size: filter.Size,
|
||||
Query: filter.Query,
|
||||
Sort: enum.RepoAttrUpdated,
|
||||
Order: enum.OrderDesc,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list child repos: %w", err)
|
||||
}
|
||||
var reposOut []Repository
|
||||
for _, repo := range repos {
|
||||
// backfill URLs
|
||||
repo.GitURL = s.urlProvider.GenerateGITCloneURL(ctx, repo.Path)
|
||||
repo.GitSSHURL = s.urlProvider.GenerateGITCloneSSHURL(ctx, repo.Path)
|
||||
|
||||
repoOut, err := mapRepository(repo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get repo %q output: %w", repo.Path, err)
|
||||
}
|
||||
|
||||
reposOut = append(reposOut, repoOut)
|
||||
}
|
||||
return reposOut, nil
|
||||
}
|
||||
|
||||
func mapRepository(repo *types.Repository) (Repository, error) {
|
||||
if repo == nil {
|
||||
return Repository{}, fmt.Errorf("repository is null")
|
||||
}
|
||||
return Repository{
|
||||
Name: repo.Identifier,
|
||||
DefaultBranch: repo.DefaultBranch,
|
||||
GitURL: repo.GitURL,
|
||||
GitSSHURL: repo.GitSSHURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewGitnessSCM(repoStore store.RepoStore, git git.Interface,
|
||||
tokenStore store.TokenStore,
|
||||
principalStore store.PrincipalStore,
|
||||
|
|
|
@ -38,6 +38,20 @@ func NewGenericSCM() *GenericSCM {
|
|||
return &GenericSCM{}
|
||||
}
|
||||
|
||||
// ListBranches implements Provider.
|
||||
func (s *GenericSCM) ListBranches(_ context.Context,
|
||||
_ *BranchFilter,
|
||||
_ *ResolvedCredentials) ([]Branch, error) {
|
||||
return []Branch{}, nil
|
||||
}
|
||||
|
||||
// ListReporisotries implements Provider.
|
||||
func (s *GenericSCM) ListReporisotries(_ context.Context,
|
||||
_ *RepositoryFilter,
|
||||
_ *ResolvedCredentials) ([]Repository, error) {
|
||||
return []Repository{}, nil
|
||||
}
|
||||
|
||||
func (s GenericSCM) GetFileContent(ctx context.Context,
|
||||
gitspaceConfig types.GitspaceConfig,
|
||||
filePath string,
|
||||
|
|
|
@ -29,6 +29,12 @@ type Provider interface {
|
|||
gitspaceConfig types.GitspaceConfig,
|
||||
filePath string,
|
||||
) ([]byte, error)
|
||||
ListReporisotries(ctx context.Context,
|
||||
filter *RepositoryFilter,
|
||||
credentials *ResolvedCredentials) ([]Repository, error)
|
||||
ListBranches(ctx context.Context,
|
||||
filter *BranchFilter,
|
||||
credentials *ResolvedCredentials) ([]Branch, error)
|
||||
}
|
||||
|
||||
type Factory struct {
|
||||
|
|
|
@ -53,4 +53,32 @@ type (
|
|||
Credentials *Credentials
|
||||
RepoName string
|
||||
}
|
||||
|
||||
RepositoryFilter struct {
|
||||
SpaceID int64 `json:"space_id"`
|
||||
Page int `json:"page"`
|
||||
Size int `json:"size"`
|
||||
Query string `json:"query"`
|
||||
}
|
||||
|
||||
BranchFilter struct {
|
||||
SpaceID int64 `json:"space_id"`
|
||||
Repository string `json:"repo"`
|
||||
Query string `json:"query"`
|
||||
Page int `json:"page"`
|
||||
Size int `json:"size"`
|
||||
}
|
||||
|
||||
Repository struct {
|
||||
Name string `json:"name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
// git urls
|
||||
GitURL string `json:"git_url"`
|
||||
GitSSHURL string `json:"git_ssh_url,omitempty"`
|
||||
}
|
||||
|
||||
Branch struct {
|
||||
Name string `json:"name"`
|
||||
SHA string `json:"sha"`
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue