func syncContentFromIlyshin(userName string, args *commonmodels.Service) error {
	if args.Commit == nil {
		return nil
	}
	address, owner, repo, branch, path, pathType, err := GetOwnerRepoBranchPath(args.SrcPath)
	if err != nil {
		return fmt.Errorf("url format failed")
	}
	client, err := getIlyshinClientByAddress(address)
	if err != nil {
		return err
	}
	files, err := IlyshinGetRawFiles(client, owner, repo, branch, path, pathType)
	if err != nil {
		return err
	}
	if userName != setting.WebhookTaskCreator {
		if len(files) == 0 {
			return fmt.Errorf("没有检索到yml,yaml类型文件，请检查目录是否正确")
		}
	}
	args.KubeYamls = files
	args.Yaml = joinYamls(files)
	return nil
}
syncContentFromIlyshin ...
sync content with commit, args.Commit should not be nil
package webhook
import (
	"context"
	"encoding/base64"
	"errors"
	"fmt"
	"path"
	"strings"
	"github.com/google/go-github/v35/github"
	"github.com/hashicorp/go-multierror"
	"github.com/xanzy/go-gitlab"
	"go.uber.org/zap"
	"helm.sh/helm/v3/pkg/releaseutil"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"github.com/koderover/zadig/pkg/microservice/aslan/config"
	commonmodels "github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/models"
	commonrepo "github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/mongodb"
	"github.com/koderover/zadig/pkg/microservice/aslan/core/common/service/codehub"
	"github.com/koderover/zadig/pkg/setting"
	"github.com/koderover/zadig/pkg/shared/codehost"
	"github.com/koderover/zadig/pkg/shared/poetry"
	e "github.com/koderover/zadig/pkg/tool/errors"
	githubtool "github.com/koderover/zadig/pkg/tool/git/github"
	gitlabtool "github.com/koderover/zadig/pkg/tool/git/gitlab"
	"github.com/koderover/zadig/pkg/tool/ilyshin"
	"github.com/koderover/zadig/pkg/tool/kube/serializer"
	"github.com/koderover/zadig/pkg/tool/log"
	"github.com/koderover/zadig/pkg/util"
)
func syncContent(args *commonmodels.Service, logger *zap.SugaredLogger) error {
	address, _, repo, branch, path, pathType, err := GetOwnerRepoBranchPath(args.SrcPath)
	if err != nil {
		logger.Errorf("Failed to parse url %s, err: %s", args.SrcPath, err)
		return fmt.Errorf("url parse failure, err: %s", err)
	}
	var yamls []string
	switch args.Source {
	case setting.SourceFromCodeHub:
		client, err := getCodehubClientByAddress(address)
		if err != nil {
			logger.Errorf("Failed to get codehub client, error: %s", err)
			return err
		}
		repoUUID, err := client.GetRepoUUID(repo)
		if err != nil {
			logger.Errorf("Failed to get repoUUID, error: %s", err)
			return err
		}
		yamls, err = client.GetYAMLContents(repoUUID, branch, path, pathType == "tree", true)
		if err != nil {
			logger.Errorf("Failed to get yamls, error: %s", err)
			return err
		}
	}
	args.KubeYamls = yamls
	args.Yaml = util.CombineManifests(yamls)
	return nil
}
func fillServiceTmpl(userName string, args *commonmodels.Service, log *zap.SugaredLogger) error {
	if args == nil {
		return errors.New("service template arg is null")
	}
	if len(args.ServiceName) == 0 {
		return errors.New("service name is empty")
	}
	if !config.ServiceNameRegex.MatchString(args.ServiceName) {
		return fmt.Errorf("service name must match %s", config.ServiceNameRegexString)
	}
	if args.Type == setting.K8SDeployType {
		if args.Containers == nil {
			args.Containers = make([]*commonmodels.Container, 0)
		}
		if args.Source == setting.SourceFromGitlab {
			if err := syncLatestCommit(args); err != nil {
				log.Errorf("Sync change log from gitlab failed, error: %v", err)
				return err
			}
			if err := syncContentFromGitlab(userName, args); err != nil {
				log.Errorf("Sync content from gitlab failed, error: %v", err)
				return err
			}
		} else if args.Source == setting.SourceFromIlyshin {
			if err := syncIlyshinLatestCommit(args, log); err != nil {
				log.Errorf("Sync change log from ilyshin failed, error: %v", err)
				return err
			}
			if err := syncContentFromIlyshin(userName, args); err != nil {
				log.Errorf("Sync content from ilyshin failed, error: %v", err)
				return err
			}
		} else if args.Source == setting.SourceFromGithub {
			err := syncContentFromGithub(args, log)
			if err != nil {
				log.Errorf("Sync content from github failed, error: %v", err)
				return err
			}
		} else if args.Source == setting.SourceFromCodeHub {
			err := syncContent(args, log)
			if err != nil {
				log.Errorf("Sync content from codehub failed, error: %v", err)
				return err
			}
		} else {
			args.Yaml = util.ReplaceWrapLine(args.Yaml)
			args.KubeYamls = SplitYaml(args.Yaml)
		}
		if err := setCurrentContainerImages(args); err != nil {
			return err
		}
		log.Infof("find %d containers in service %s", len(args.Containers), args.ServiceName)
	}
	serviceTemplate := fmt.Sprintf(setting.ServiceTemplateCounterName, args.ServiceName, args.ProductName)
	rev, err := commonrepo.NewCounterColl().GetNextSeq(serviceTemplate)
	if err != nil {
		return fmt.Errorf("get next service template revision error: %v", err)
	}
	args.Revision = rev
	return nil
}
func syncLatestCommit(service *commonmodels.Service) error {
	if service.SrcPath == "" {
		return fmt.Errorf("url不能是空的")
	}
	address, owner, repo, branch, path, _, err := GetOwnerRepoBranchPath(service.SrcPath)
	if err != nil {
		return fmt.Errorf("url 必须包含 owner/repo/tree/branch/path，具体请参考 Placeholder 提示")
	}
	client, err := getGitlabClientByAddress(address)
	if err != nil {
		return err
	}
	commit, err := GitlabGetLatestCommit(client, owner, repo, branch, path)
	if err != nil {
		return err
	}
	service.Commit = &commonmodels.Commit{
		SHA:     commit.ID,
		Message: commit.Message,
	}
	return nil
}
func syncIlyshinLatestCommit(service *commonmodels.Service, log *zap.SugaredLogger) error {
	if service.SrcPath == "" {
		return fmt.Errorf("url不能是空的")
	}
	address, owner, repo, branch, path, _, err := GetOwnerRepoBranchPath(service.SrcPath)
	if err != nil {
		return fmt.Errorf("url 必须包含 owner/repo/tree/branch/path，具体请参考 Placeholder 提示")
	}
	client, err := getIlyshinClientByAddress(address)
	if err != nil {
		return err
	}
	commit, err := client.GetLatestCommit(owner, repo, branch, path, log)
	if err != nil {
		return err
	}
	service.Commit = &commonmodels.Commit{
		SHA:     commit.ID,
		Message: commit.Message,
	}
	return nil
}
func syncCodehubLatestCommit(service *commonmodels.Service) error {
	if service.SrcPath == "" {
		return fmt.Errorf("url不能是空的")
	}
	address, owner, repo, branch, _, _, err := GetOwnerRepoBranchPath(service.SrcPath)
	if err != nil {
		return fmt.Errorf("url 必须包含 owner/repo/tree/branch/path，具体请参考 Placeholder 提示")
	}
	client, err := getCodehubClientByAddress(address)
	if err != nil {
		return err
	}
	id, message, err := CodehubGetLatestCommit(client, owner, repo, branch)
	if err != nil {
		return err
	}
	service.Commit = &commonmodels.Commit{
		SHA:     id,
		Message: message,
	}
	return nil
}
func getCodehubClientByAddress(address string) (*codehub.Client, error) {
	opt := &codehost.Option{
		Address:      address,
		CodeHostType: codehost.CodeHubProvider,
	}
	codehost, err := codehost.GetCodeHostInfo(opt)
	if err != nil {
		log.Error(err)
		return nil, e.ErrCodehostListProjects.AddDesc("git client is nil")
	}
	client := codehub.NewClient(codehost.AccessKey, codehost.SecretKey, codehost.Region)
	return client, nil
}
func getIlyshinClientByAddress(address string) (*ilyshin.Client, error) {
	opt := &codehost.Option{
		Address:      address,
		CodeHostType: codehost.IlyshinProvider,
	}
	codehost, err := codehost.GetCodeHostInfo(opt)
	if err != nil {
		log.Error(err)
		return nil, e.ErrCodehostListProjects.AddDesc("git client is nil")
	}
	client := ilyshin.NewClient(codehost.Address, codehost.AccessToken)
	return client, nil
}
func getGitlabClientByAddress(address string) (*gitlabtool.Client, error) {
	opt := &codehost.Option{
		Address:      address,
		CodeHostType: codehost.GitLabProvider,
	}
	codehost, err := codehost.GetCodeHostInfo(opt)
	if err != nil {
		log.Error(err)
		return nil, e.ErrCodehostListProjects.AddDesc("git client is nil")
	}
	client, err := gitlabtool.NewClient(codehost.Address, codehost.AccessToken)
	if err != nil {
		log.Error(err)
		return nil, e.ErrCodehostListProjects.AddDesc(err.Error())
	}
	return client, nil
}
func GitlabGetLatestCommit(client *gitlabtool.Client, owner, repo string, ref, path string) (*gitlab.Commit, error) {
	commit, err := client.GetLatestRepositoryCommit(owner, repo, path, ref)
	if err != nil {
		return nil, fmt.Errorf("failed to get lastest commit with project %s/%s, ref: %s, path:%s, error: %v",
			owner, repo, ref, path, err)
	}
	return commit, nil
}
func CodehubGetLatestCommit(client *codehub.Client, owner, repo string, branch string) (string, string, error) {
	commit, err := client.GetLatestRepositoryCommit(owner, repo, branch)
	if err != nil {
		return "", "", fmt.Errorf("failed to get lastest commit with project %s/%s, ref: %s, error: %s",
			owner, repo, branch, err)
	}
	return commit.ID, commit.Message, nil
}
func GitlabGetRawFiles(client *gitlabtool.Client, owner, repo, ref, path, pathType string) (files []string, err error) {
	files = make([]string, 0)
	var errs *multierror.Error
	if pathType == "tree" {
		nodes, err := client.ListTree(owner, repo, path, ref, false, nil)
		if err != nil {
			return files, err
		}
		for _, node := range nodes {
			if node.Type == "tree" {
				continue
			}
			fileName := strings.ToLower(node.Name)
			if !strings.HasSuffix(fileName, ".yaml") && !strings.HasSuffix(fileName, ".yml") {
				continue
			}
			content, err := client.GetRawFile(owner, repo, ref, node.Path)
			if err != nil {
				errs = multierror.Append(errs, err)
			}
			contentStr := string(content)
			contentStr = util.ReplaceWrapLine(contentStr)
			files = append(files, contentStr)
		}
		return files, errs.ErrorOrNil()
	}
	content, err := client.GetFileContent(owner, repo, path, ref)
	if err != nil {
		return files, err
	}
	files = append(files, string(content))
	return files, errs.ErrorOrNil()
}
func syncContentFromGitlab(userName string, args *commonmodels.Service) error {
	if args.Commit == nil {
		return nil
	}
	address, owner, repo, branch, path, pathType, err := GetOwnerRepoBranchPath(args.SrcPath)
	if err != nil {
		return fmt.Errorf("url format failed")
	}
	client, err := getGitlabClientByAddress(address)
	if err != nil {
		return err
	}
	files, err := GitlabGetRawFiles(client, owner, repo, branch, path, pathType)
	if err != nil {
		return err
	}
	if userName != setting.WebhookTaskCreator {
		if len(files) == 0 {
			return fmt.Errorf("没有检索到yml,yaml类型文件，请检查目录是否正确")
		}
	}
	args.KubeYamls = files
	args.Yaml = joinYamls(files)
	return nil
}
func joinYamls(files []string) string {
	return strings.Join(files, setting.YamlFileSeperator)
}
func IlyshinGetRawFiles(client *ilyshin.Client, owner, repo, ref, path, pathType string) (files []string, err error) {
	files = make([]string, 0)
	var errs *multierror.Error
	if pathType == "tree" {
		nodes, err := client.ListTree(owner, repo, ref, path)
		if err != nil {
			return files, err
		}
		for _, node := range nodes {
			if node.Type == "tree" {
				continue
			}
			fileName := strings.ToLower(node.Name)
			if !strings.HasSuffix(fileName, ".yaml") && !strings.HasSuffix(fileName, ".yml") {
				continue
			}
			content, err := client.GetRawFile(owner, repo, ref, node.Path)
			if err != nil {
				errs = multierror.Append(errs, err)
			}
			contentStr := string(content)
			contentStr = util.ReplaceWrapLine(contentStr)
			files = append(files, contentStr)
		}
		return files, errs.ErrorOrNil()
	}
	fileInfo, err := client.GetFile(owner, repo, ref, path)
	if err != nil {
		return files, err
	}
	decodedFile, err := base64.StdEncoding.DecodeString(fileInfo.Content)
	if err != nil {
		return files, err
	}
	files = append(files, string(decodedFile))
	return files, errs.ErrorOrNil()
}