You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: run.go
    path of file: ./repos/erda/internal/tools/pipeline/providers/run
    
    the code of the file until where you have to start completion: // Copyright (c) 2021 Terminus, 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 run

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/sirupsen/logrus"

	commonpb "github.com/erda-project/erda-proto-go/common/pb"
	basepb "github.com/erda-project/erda-proto-go/core/pipeline/base/pb"
	pipelinepb "github.com/erda-project/erda-proto-go/core/pipeline
    
func (s *provider) RunOnePipeline(ctx context.Context, req *pipelinepb.PipelineRunRequest) (*spec.Pipeline, error) {
	p, err := s.dbClient.GetPipeline(req.PipelineID)
	if err != nil {
		return nil, apierrors.ErrGetPipeline.InvalidParameter(err)
	}

	if req.UserID != "" {
		p.Extra.RunUser = s.User.TryGetUser(ctx, req.UserID)
	}
	if req.InternalClient != "" {
		p.Extra.InternalClient = req.InternalClient
	}

	reason, canManualRun := s.CanManualRun(ctx, &p)
	if !canManualRun {
		return nil, apierrors.ErrRunPipeline.InvalidState(reason)
	}
	if req.ForceRun {
		err := s.Cancel.StopRelatedRunningPipelinesOfOnePipeline(ctx, &p, &commonpb.IdentityInfo{
			UserID:         req.UserID,
			InternalClient: req.InternalClient,
		})
		if err != nil {
			return nil, err
		}
	} else {
		// 校验已运行的 pipeline
		if err := s.limitParallelRunningPipelines(&p); err != nil {
			return nil, err
		}
	}

	p.Extra.ConfigManageNamespaces = append(p.Extra.ConfigManageNamespaces, req.ConfigManageNamespaces...)

	var (
		secrets, cmsDiceFiles         map[string]string
		holdOnKeys, encryptSecretKeys []string
		platformSecrets               map[string]string
	)
	secretCache := s.Cache.GetPipelineSecretByPipelineID(p.PipelineID)
	defer s.Cache.ClearPipelineSecretByPipelineID(p.PipelineID)
	// only autoRun can use cache
	if secretCache != nil {
		secrets = secretCache.Secrets
		cmsDiceFiles = secretCache.CmsDiceFiles
		holdOnKeys = secretCache.HoldOnKeys
		encryptSecretKeys = secretCache.EncryptSecretKeys
	} else {
		// fetch secrets
		secrets, cmsDiceFiles, holdOnKeys, encryptSecretKeys, err = s.Secret.FetchSecrets(ctx, &p)
		if err != nil {
			return nil, apierrors.ErrRunPipeline.InternalError(err)
		}
	}

	// fetch platform secrets
	platformSecrets, err = s.Secret.FetchPlatformSecrets(ctx, &p, holdOnKeys)
	if err != nil {
		return nil, apierrors.ErrRunPipeline.InternalError(err)
	}

	for k, v := range req.Secrets {
		secrets[k] = v
	}
	// replace global config use same random value
	for k, v := range secrets {
		secrets[k] = expression.ReplaceRandomParams(v)
	}

	// 校验私有配置转换出来的 envs
	secretsEnvs := make(map[string]string)
	for k, v := range secrets {
		newK := strings.Replace(strings.Replace(strings.ToUpper(k), ".", "_", -1), "-", "_", -1)
		secretsEnvs[newK] = v
	}
	errs := pipelineyml.CheckEnvs(secretsEnvs)
	if len(errs) > 0 {
		var errMsgs []string
		for _, checkErr := range errs {
			errMsgs = append(errMsgs, checkErr.Error())
		}
		return nil, apierrors.ErrCheckSecrets.InvalidParameter(strutil.Join(errMsgs, "\n", true))
	}

	// Snapshot 快照用于记录
	p.Snapshot.PipelineYml = p.PipelineYml
	p.Snapshot.Secrets = secrets
	p.Snapshot.PlatformSecrets = platformSecrets
	p.Snapshot.CmsDiceFiles = cmsDiceFiles
	// pipeline 运行时的参数
	runParams, err := getRealRunParams(req.PipelineRunParams, p.PipelineYml)
	if err != nil {
		return nil, err
	}
	p.Snapshot.RunPipelineParams = runParams.ToPipelineRunParamsWithValue()
	p.Snapshot.EncryptSecretKeys = encryptSecretKeys

	now := time.Now()
	p.TimeBegin = &now

	cluster, err := s.ClusterInfo.GetClusterInfoByName(p.ClusterName)
	if err != nil {
		return nil, apierrors.ErrRunPipeline.InternalError(err)
	}
	container_provider.DealPipelineProviderBeforeRun(&p, cluster.CM)
	// update pipeline base
	if err := s.dbClient.UpdatePipelineBase(p.ID, &p.PipelineBase); err != nil {
		return nil, apierrors.ErrUpdatePipeline.InternalError(err)
	}

	// update pipeline extra
	if err := s.dbClient.UpdatePipelineExtraByPipelineID(p.ID, &p.PipelineExtra); err != nil {
		return nil, apierrors.ErrRunPipeline.InternalError(err)
	}

	// create pipeline labels
	if err = s.createPipelineRunLabels(p, req); err != nil {
		return nil, apierrors.ErrRunPipeline.InternalError(err)
	}

	// aop
	_ = aop.Handle(aop.NewContextForPipeline(p, aoptypes.TuneTriggerPipelineBeforeExec))

	// send to pipengine reconciler
	s.Engine.DistributedSendPipeline(context.Background(), p.ID)

	// update pipeline definition
	if err = s.updatePipelineDefinition(p); err != nil {
		s.Log.Errorf("failed to updatePipelineDefinition, pipelineID: %d, definitionID: %s, err: %s", p.PipelineID, p.PipelineDefinitionID, err.Error())
	}

	return &p, nil
}