37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
utils.go 3.23 KB
一键复制 编辑 原始数据 按行查看 历史
Alena Prokharchyk 提交于 2019-02-15 15:26 . ineffassign fixes
package providers
import (
"github.com/rancher/rancher/pkg/pipeline/remote"
"github.com/rancher/rancher/pkg/pipeline/utils"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/satori/go.uuid"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
func GetPipelineConfigByBranch(sourceCodeCredentials v3.SourceCodeCredentialInterface, sourceCodeCredentialLister v3.SourceCodeCredentialLister, pipeline *v3.Pipeline, branch string) (*v3.PipelineConfig, error) {
credentialName := pipeline.Spec.SourceCodeCredentialName
repoURL := pipeline.Spec.RepositoryURL
_, projID := ref.Parse(pipeline.Spec.ProjectName)
var scpConfig interface{}
var credential *v3.SourceCodeCredential
var err error
if credentialName != "" {
ns, name := ref.Parse(credentialName)
credential, err = sourceCodeCredentialLister.Get(ns, name)
if err != nil {
return nil, err
}
sourceCodeType := credential.Spec.SourceCodeType
scpConfig, err = GetSourceCodeProviderConfig(sourceCodeType, projID)
if err != nil {
return nil, err
}
}
remote, err := remote.New(scpConfig)
if err != nil {
return nil, err
}
accessToken, err := utils.EnsureAccessToken(sourceCodeCredentials, remote, credential)
if err != nil {
return nil, err
}
content, err := remote.GetPipelineFileInRepo(repoURL, branch, accessToken)
if err != nil {
return nil, err
}
if content != nil {
pipelineConfig, err := utils.PipelineConfigFromYaml(content)
if err != nil {
return nil, err
}
return pipelineConfig, nil
}
return nil, nil
}
func RefreshReposByCredential(sourceCodeRepositories v3.SourceCodeRepositoryInterface, sourceCodeRepositoryLister v3.SourceCodeRepositoryLister, sourceCodeCredentials v3.SourceCodeCredentialInterface, credential *v3.SourceCodeCredential, sourceCodeProviderConfig interface{}) ([]*v3.SourceCodeRepository, error) {
namespace := credential.Namespace
credentialID := ref.Ref(credential)
remote, err := remote.New(sourceCodeProviderConfig)
if err != nil {
return nil, err
}
accessToken, err := utils.EnsureAccessToken(sourceCodeCredentials, remote, credential)
if err != nil {
return nil, err
} else if accessToken != credential.Spec.AccessToken {
credential.Spec.AccessToken = accessToken
}
repos, err := remote.Repos(credential)
if err != nil {
return nil, err
}
//remove old repos
repositories, err := sourceCodeRepositoryLister.List(namespace, labels.Everything())
if err != nil {
return nil, err
}
for _, repo := range repositories {
if repo.Spec.SourceCodeCredentialName == credentialID {
if err := sourceCodeRepositories.DeleteNamespaced(namespace, repo.Name, &metav1.DeleteOptions{}); err != nil {
return nil, err
}
}
}
//store new repos
for _, repo := range repos {
if !repo.Spec.Permissions.Admin {
//store only admin repos
continue
}
repo.Spec.SourceCodeCredentialName = credentialID
repo.Spec.UserName = credential.Spec.UserName
repo.Spec.SourceCodeType = credential.Spec.SourceCodeType
repo.Name = uuid.NewV4().String()
repo.Namespace = namespace
repo.Spec.ProjectName = credential.Spec.ProjectName
if _, err := sourceCodeRepositories.Create(&repo); err != nil {
return nil, err
}
}
return repositories, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.2-rc13

搜索帮助

0d507c66 1850385 C8b1a773 1850385