drone/app/gitspace/orchestrator/git/git_impl.go
Dhruv e92962b06d
fix: [CDE-540]: Adding a check for user name and email during code clone operation. (#3596)
### Description

This PR introduces two changes
1. Adding a check for user's name and email before cloning the code
inside the gitspace container. This will non-block non-harness repos.
2. Adding an explicit check for the media folder's path during VSCode
web setup.

---------
2024-12-05 13:50:34 +05:30

122 lines
3.5 KiB
Go

// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package git
import (
"context"
"fmt"
"net/url"
"github.com/harness/gitness/app/gitspace/orchestrator/devcontainer"
"github.com/harness/gitness/app/gitspace/orchestrator/template"
"github.com/harness/gitness/app/gitspace/scm"
_ "embed"
)
var _ Service = (*ServiceImpl)(nil)
//go:embed script/install_git.sh
var installScript string
const templateSetupGitCredentials = "setup_git_credentials.sh" // nolint:gosec
const templateCloneCode = "clone_code.sh"
type ServiceImpl struct {
}
func NewGitServiceImpl() Service {
return &ServiceImpl{}
}
func (g *ServiceImpl) Install(ctx context.Context, exec *devcontainer.Exec) ([]byte, error) {
output := "Setting up git inside container\n"
_, err := exec.ExecuteCommandInHomeDirectory(ctx, installScript, false, false)
if err != nil {
return nil, fmt.Errorf("failed to setup git: %w", err)
}
output += "Successfully setup git\n"
return []byte(output), nil
}
func (g *ServiceImpl) SetupCredentials(
ctx context.Context,
exec *devcontainer.Exec,
resolvedRepoDetails scm.ResolvedDetails,
) ([]byte, error) {
script, err := template.GenerateScriptFromTemplate(
templateSetupGitCredentials, &template.SetupGitCredentialsPayload{
CloneURLWithCreds: resolvedRepoDetails.CloneURL,
})
if err != nil {
return nil, fmt.Errorf(
"failed to generate scipt to setup git credentials from template %s: %w", templateSetupGitCredentials, err)
}
output := "Setting up git credentials inside container\n"
_, err = exec.ExecuteCommandInHomeDirectory(ctx, script, false, false)
if err != nil {
return nil, fmt.Errorf("failed to setup git credentials: %w", err)
}
output += "Successfully setup git credentials\n"
return []byte(output), nil
}
func (g *ServiceImpl) CloneCode(
ctx context.Context,
exec *devcontainer.Exec,
resolvedRepoDetails scm.ResolvedDetails,
defaultBaseImage string,
) ([]byte, error) {
cloneURL, err := url.Parse(resolvedRepoDetails.CloneURL)
if err != nil {
return nil, fmt.Errorf(
"failed to parse clone url %s: %w", resolvedRepoDetails.CloneURL, err)
}
cloneURL.User = nil
templatePayload := template.CloneCodePayload{
RepoURL: cloneURL.String(),
Image: defaultBaseImage,
Branch: resolvedRepoDetails.Branch,
RepoName: resolvedRepoDetails.RepoName,
}
if resolvedRepoDetails.Credentials != nil {
templatePayload.Email = resolvedRepoDetails.Credentials.Email
templatePayload.Name = resolvedRepoDetails.Credentials.Name
}
script, err := template.GenerateScriptFromTemplate(templateCloneCode, &templatePayload)
if err != nil {
return nil, fmt.Errorf(
"failed to generate scipt to clone code from template %s: %w", templateCloneCode, err)
}
output := "Cloning code inside container\n"
_, err = exec.ExecuteCommandInHomeDirectory(ctx, script, false, false)
if err != nil {
return nil, fmt.Errorf("failed to clone code: %w", err)
}
output += "Successfully clone code\n"
return []byte(output), nil
}