mirror of https://github.com/harness/drone.git
116 lines
3.3 KiB
Go
116 lines
3.3 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"
|
|
|
|
"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{
|
|
Email: resolvedRepoDetails.Credentials.Email,
|
|
Name: resolvedRepoDetails.Credentials.Name,
|
|
Password: resolvedRepoDetails.Credentials.Password,
|
|
Host: resolvedRepoDetails.Credentials.Host,
|
|
Protocol: resolvedRepoDetails.Credentials.Protocol,
|
|
Path: resolvedRepoDetails.Credentials.Path,
|
|
})
|
|
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) {
|
|
script, err := template.GenerateScriptFromTemplate(
|
|
templateCloneCode, &template.CloneCodePayload{
|
|
RepoURL: resolvedRepoDetails.CloneURL,
|
|
Image: defaultBaseImage,
|
|
Branch: resolvedRepoDetails.Branch,
|
|
RepoName: resolvedRepoDetails.RepoName,
|
|
})
|
|
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
|
|
}
|