fix: [CDE-244]: set user details automatically (#2632)

* fix: [CDE-244]: set user details automatically
* fix: [CDE-244]: set user details automatically
* fix: [CDE-244]: set user details automatically
pull/3545/head
Ansuman Satapathy 2024-09-02 07:16:07 +00:00 committed by Harness
parent 77e93d587c
commit 3b0c20ae53
6 changed files with 60 additions and 27 deletions

View File

@ -411,6 +411,9 @@ func (e *EmbeddedDockerOrchestrator) authenticateGit(
Email: resolvedRepoDetails.Credentials.Email,
Name: resolvedRepoDetails.Credentials.Name,
Password: resolvedRepoDetails.Credentials.Password,
Host: resolvedRepoDetails.Credentials.Host,
Protocol: resolvedRepoDetails.Credentials.Protocol,
Path: resolvedRepoDetails.Credentials.Path,
}
gitAuthenticateScript, err := template.GenerateScriptFromTemplate(
templateAuthenticateGit, data)
@ -491,6 +494,9 @@ func (e *EmbeddedDockerOrchestrator) cloneCode(
data.Email = resolvedRepoDetails.Credentials.Email
data.Name = resolvedRepoDetails.Credentials.Name
data.Password = resolvedRepoDetails.Credentials.Password
data.Host = resolvedRepoDetails.Credentials.Host
data.Protocol = resolvedRepoDetails.Credentials.Protocol
data.Path = resolvedRepoDetails.Credentials.Path
}
gitCloneScript, err := template.GenerateScriptFromTemplate(templateCloneGit, data)
if err != nil {

View File

@ -47,6 +47,9 @@ type AuthenticateGitPayload struct {
Email string
Name string
Password string
Host string
Protocol string
Path string
}
type RunVSCodeWebPayload struct {

View File

@ -2,17 +2,25 @@
name={{ .Name }}
password={{ .Password }}
email={{ .Email }}
host={{ .Host }}
protocol={{ .Protocol }}
path={{ .Path }}
# Create or overwrite the config file with new settings
touch $HOME/.git-askpass
cat > $HOME/.git-askpass <<EOF
echo $password
EOF
chmod 700 $HOME/.git-askpass
git config --global credential.helper 'cache --timeout=2592000'
git config --global user.email "$email"
git config --global user.name "$name"
#run git operation to cache the credential in memory
export GIT_ASKPASS=$HOME/.git-askpass
git ls-remote
rm $HOME/.git-askpass
if [ -z "$password" ]; then
echo "setting up without credentials"
else
git config --global credential.helper 'cache --timeout=2592000'
git config --global user.email "$email"
git config --global user.name "$name"
touch .gitcontext
echo "host="$host >> .gitcontext
echo "protocol="$protocol >> .gitcontext
echo "path="$path >> .gitcontext
echo "username="$email >> .gitcontext
echo "password="$password >> .gitcontext
echo "" >> .gitcontext
cat .gitcontext | git credential approve
rm .gitcontext
fi

View File

@ -8,15 +8,9 @@ repo_name={{ .RepoName }}
password={{ .Password }}
email={{ .Email }}
name={{ .Name }}
# Create or overwrite the config file with new settings
touch $HOME/.git-askpass
cat > $HOME/.git-askpass <<EOF
echo $password
EOF
chmod 700 $HOME/.git-askpass
export GIT_ASKPASS=$HOME/.git-askpass
git config --global credential.helper 'cache --timeout=2592000'
host={{ .Host }}
protocol={{ .Protocol }}
path={{ .Path }}
# Check if Git is installed
if ! command -v git >/dev/null 2>&1; then
@ -29,22 +23,34 @@ if ! command -v git >/dev/null 2>&1; then
echo "Git is not installed. Exiting..."
exit 1
fi
if [ -z "$password" ]; then
echo "setting up without credentials"
else
git config --global credential.helper 'cache --timeout=2592000'
git config --global user.email "$email"
git config --global user.name "$name"
touch .gitcontext
echo "host="$host >> .gitcontext
echo "protocol="$protocol >> .gitcontext
echo "path="$path >> .gitcontext
echo "username="$email >> .gitcontext
echo "password="$password >> .gitcontext
echo "" >> .gitcontext
cat .gitcontext | git credential approve
rm .gitcontext
fi
git config --global user.email "$email"
git config --global user.name "$name"
# Clone the repository inside the working directory if it doesn't exist
if [ ! -d "$HOME/$repo_name/.git" ]; then
echo "Cloning the repository..."
if ! git clone "$repo_url" --branch "$branch" "$HOME/$repo_name"; then
echo "Failed to clone the repository. Exiting..."
rm $HOME/.git-askpass
exit 1
fi
else
echo "Repository already exists. Skipping clone."
fi
git ls-remote
rm $HOME/.git-askpass
git config --global --add safe.directory "$HOME/$repo_name"

View File

@ -184,10 +184,17 @@ func (s GitnessSCM) ResolveCredentials(
if err != nil {
return nil, fmt.Errorf("failed to create JWT: %w", err)
}
modifiedURL, err := url.Parse(gitURL)
if err != nil {
return nil, fmt.Errorf("error while parsing the clone url: %s", gitURL)
}
credentials := &Credentials{
Password: jwtToken,
Email: user.Email,
Name: user.DisplayName,
Password: jwtToken,
Host: modifiedURL.Host,
Protocol: modifiedURL.Scheme,
Path: modifiedURL.Path,
}
resolvedCredentails.Credentials = credentials
return resolvedCredentails, nil

View File

@ -45,6 +45,9 @@ type (
Email string
Name string
Password string
Host string
Protocol string
Path string
}
ResolvedCredentials struct {