add scm provider in lookup repo (#2544)

* fix: [CDE-244]: added validation for missing URL ref
* fix: [CDE-244]: added validation for missing URL ref
* fix: [CDE-244]: added validation for missing URL ref
* treat empty input as unknown repo type
* add scm provider in lookup repo
pull/3545/head
Kapil Garg 2024-08-20 13:16:32 +00:00 committed by Harness
parent cd5de8fe62
commit 516fd5c1ca
3 changed files with 63 additions and 11 deletions

View File

@ -27,14 +27,16 @@ import (
)
type LookupRepoInput struct {
Identifier string `json:"-"`
SpaceRef string `json:"space_ref"` // Ref of the parent space
URL string `json:"url"`
SpaceRef string `json:"space_ref"` // Ref of the parent space
URL string `json:"url"`
RepoType enum.GitspaceCodeRepoType `json:"repo_type"`
}
var (
ErrInvalidURL = usererror.BadRequest(
"The URL specified is not valid format.")
ErrRepoMissing = usererror.BadRequest(
"There must be URL or Ref specified fir repo.")
ErrBadURLScheme = usererror.BadRequest("the URL is missing scheme, it must start with http or https")
)
@ -54,7 +56,13 @@ func (c *Controller) LookupRepo(
if err != nil {
return nil, fmt.Errorf("failed to authorize: %w", err)
}
repositoryRequest := scm.CodeRepositoryRequest{URL: in.URL}
repositoryRequest := scm.CodeRepositoryRequest{
URL: in.URL,
UserIdentifier: session.Principal.UID,
SpacePath: space.Path,
RepoType: in.RepoType,
UserID: session.Principal.ID,
}
codeRepositoryResponse, err := c.scm.CheckValidCodeRepo(ctx, repositoryRequest)
if err != nil {
return nil, err
@ -63,6 +71,9 @@ func (c *Controller) LookupRepo(
}
func (c *Controller) sanitizeLookupRepoInput(in *LookupRepoInput) error {
if in.RepoType == "" && in.URL == "" {
return ErrRepoMissing
}
parsedURL, err := url.Parse(in.URL)
if err != nil {
return ErrInvalidURL

View File

@ -56,21 +56,55 @@ func NewSCM(factory Factory) SCM {
return &scm{scmProviderFactory: factory}
}
func (s scm) CheckValidCodeRepo(ctx context.Context, request CodeRepositoryRequest) (*CodeRepositoryResponse, error) {
func (s scm) CheckValidCodeRepo(
ctx context.Context,
codeRepositoryRequest CodeRepositoryRequest,
) (*CodeRepositoryResponse, error) {
codeRepositoryResponse := &CodeRepositoryResponse{
URL: request.URL,
URL: codeRepositoryRequest.URL,
CodeRepoIsPrivate: true,
}
defaultBranch, err := detectDefaultGitBranch(ctx, request.URL)
defaultBranch, err := detectDefaultGitBranch(ctx, codeRepositoryRequest.URL)
if err == nil {
branch := "main"
if defaultBranch != "" {
branch = defaultBranch
}
return &CodeRepositoryResponse{
URL: codeRepositoryRequest.URL,
Branch: branch,
CodeRepoIsPrivate: false,
}, nil
}
repoType := enum.CodeRepoTypeUnknown
if codeRepositoryRequest.RepoType != "" {
repoType = codeRepositoryRequest.RepoType
}
scmProvider, err := s.scmProviderFactory.GetSCMProvider(repoType)
if err != nil {
return nil, fmt.Errorf("failed to resolve scm provider: %w", err)
}
codeRepo := types.CodeRepo{URL: codeRepositoryRequest.URL}
gitspaceUser := types.GitspaceUser{Identifier: codeRepositoryRequest.UserIdentifier}
gitspaceConfig := types.GitspaceConfig{
CodeRepo: codeRepo,
SpacePath: codeRepositoryRequest.SpacePath,
GitspaceUser: gitspaceUser,
}
resolvedCreds, err := scmProvider.ResolveCredentials(ctx, gitspaceConfig)
if err != nil {
return nil, fmt.Errorf("failed to resolve repo credentials and url: %w", err)
}
defaultBranch, err = detectDefaultGitBranch(ctx, resolvedCreds.CloneURL)
if err == nil {
branch := "main"
if defaultBranch != "" {
branch = defaultBranch
}
codeRepositoryResponse = &CodeRepositoryResponse{
URL: request.URL,
URL: codeRepositoryRequest.URL,
Branch: branch,
CodeRepoIsPrivate: false,
CodeRepoIsPrivate: true,
}
}
return codeRepositoryResponse, nil

View File

@ -14,10 +14,17 @@
package scm
import "github.com/harness/gitness/types"
import (
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
type CodeRepositoryRequest struct {
URL string `json:"url"`
URL string
RepoType enum.GitspaceCodeRepoType
UserIdentifier string
UserID int64
SpacePath string
}
type CodeRepositoryResponse struct {