feat(gitops): support to store git credentials [EE-2683] (#7066)

pull/7602/head
Oscar Zhou 2022-09-15 16:32:05 +12:00 committed by GitHub
parent 9ef5636718
commit fa162cafc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -19,6 +19,10 @@ import (
"github.com/go-git/go-git/v5/storage/memory" "github.com/go-git/go-git/v5/storage/memory"
) )
var (
ErrAuthenticationFailure = errors.New("Authentication failed, please ensure that the git credentials are correct.")
)
type fetchOptions struct { type fetchOptions struct {
repositoryUrl string repositoryUrl string
username string username string
@ -57,6 +61,9 @@ func (c gitClient) download(ctx context.Context, dst string, opt cloneOptions) e
_, err := git.PlainCloneContext(ctx, dst, false, &gitOptions) _, err := git.PlainCloneContext(ctx, dst, false, &gitOptions)
if err != nil { if err != nil {
if err.Error() == "authentication required" {
return ErrAuthenticationFailure
}
return errors.Wrap(err, "failed to clone git repository") return errors.Wrap(err, "failed to clone git repository")
} }
@ -79,6 +86,9 @@ func (c gitClient) latestCommitID(ctx context.Context, opt fetchOptions) (string
refs, err := remote.List(listOptions) refs, err := remote.List(listOptions)
if err != nil { if err != nil {
if err.Error() == "authentication required" {
return "", ErrAuthenticationFailure
}
return "", errors.Wrap(err, "failed to list repository refs") return "", errors.Wrap(err, "failed to list repository refs")
} }

View File

@ -17,4 +17,8 @@ type RepoConfig struct {
type GitAuthentication struct { type GitAuthentication struct {
Username string Username string
Password string Password string
// Git credentials identifier when the value is not 0
// When the value is 0, Username and Password are set without using saved credential
// This is introduced since 2.15.0
GitCredentialID int `example:"0"`
} }