feat: [CDE-742]: add common scm auth functionality (#3729)

* feat: [CDE-742]: add common scm auth functionality
* feat: [CDE-742]: add common scm auth functionality
* feat: [CDE-742]: add common scm auth functionality
* feat: [CDE-742]: add common scm auth functionality
* feat: [CDE-742]: add common scm auth functionality
This commit is contained in:
Ansuman Satapathy 2025-04-24 12:15:36 +00:00 committed by Harness
parent c3a70ca12e
commit 8d0c8b2f1e
4 changed files with 28 additions and 6 deletions

View File

@ -200,9 +200,8 @@ func (s *GitnessSCM) ResolveCredentials(
if err != nil {
return nil, fmt.Errorf("error while parsing the clone url: %s", gitURL)
}
userInfo := url.UserPassword("harness", jwtToken)
modifiedURL.User = userInfo
resolvedCredentails.CloneURL = types.NewMaskSecret(modifiedURL.String())
resolvedCredentails.CloneURL = types.NewMaskSecret(
BuildAuthenticatedCloneURL(modifiedURL, jwtToken, enum.CodeRepoTypeGitness).String())
credentials := &UserPasswordCredentials{
Email: user.Email,
Name: types.NewMaskSecret(user.DisplayName),

View File

@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/url"
"regexp"
"strings"
@ -27,6 +28,7 @@ import (
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
"github.com/rs/zerolog/log"
"github.com/tidwall/jsonc"
)
@ -133,7 +135,7 @@ func (s *SCM) GetBranchURL(
repoURL string,
branch string,
) (string, error) {
scmProvider, err := s.scmProviderFactory.GetSCMProvider(repoType)
scmProvider, err := s.getSCMAuthAndFileProvider(repoType)
if err != nil {
return "", fmt.Errorf("failed to resolve scm provider while generating branch url: %w", err)
}
@ -198,3 +200,23 @@ func (s *SCM) getDevcontainerConfig(
return config, nil
}
func BuildAuthenticatedCloneURL(repoURL *url.URL, accessToken string, codeRepoType enum.GitspaceCodeRepoType) *url.URL {
switch codeRepoType {
case enum.CodeRepoTypeGithubEnterprise, enum.CodeRepoTypeGithub:
repoURL.User = url.UserPassword(accessToken, "x-oauth-basic")
case enum.CodeRepoTypeGitlab, enum.CodeRepoTypeGitlabOnPrem:
repoURL.User = url.UserPassword("oauth2", accessToken)
case enum.CodeRepoTypeBitbucket, enum.CodeRepoTypeBitbucketServer:
repoURL.User = url.UserPassword("x-token-auth", accessToken)
case enum.CodeRepoTypeGitness:
repoURL.User = url.UserPassword("harness", accessToken)
case enum.CodeRepoTypeHarnessCode:
repoURL.User = url.UserPassword("Basic", accessToken)
case enum.CodeRepoTypeUnknown:
log.Warn().Msgf("unknown repo type, cannot set credentials")
default:
log.Warn().Msgf("Unsupported repo type: %s", codeRepoType)
}
return repoURL
}

View File

@ -27,5 +27,8 @@ type AuthAndFileContentProvider interface {
filePath string,
credentials *ResolvedCredentials,
) ([]byte, error)
ResolveCredentials(ctx context.Context, gitspaceConfig types.GitspaceConfig) (*ResolvedCredentials, error)
GetBranchURL(spacePath string, repoURL string, branch string) (string, error)
}

View File

@ -30,6 +30,4 @@ type ListingProvider interface {
filter *BranchFilter,
credentials *ResolvedCredentials,
) ([]Branch, error)
GetBranchURL(spacePath string, repoURL string, branch string) (string, error)
}