37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
source_code_repository.go 2.01 KB
一键复制 编辑 原始数据 按行查看 历史
phli 提交于 2018-03-07 14:25 . support export/import pipeline
package pipeline
import (
"encoding/json"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/controllers/user/pipeline/remote"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/types/apis/management.cattle.io/v3"
)
type SourceCodeRepositoryHandler struct {
SourceCodeCredentialLister v3.SourceCodeCredentialLister
SourceCodeRepositoryLister v3.SourceCodeRepositoryLister
ClusterPipelineLister v3.ClusterPipelineLister
}
func SourceCodeRepositoryFormatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.Links["pipeline"] = apiContext.URLBuilder.Link("pipeline", resource)
}
func (h SourceCodeRepositoryHandler) LinkHandler(apiContext *types.APIContext, next types.RequestHandler) error {
if apiContext.Link == "pipeline" {
ns, name := ref.Parse(apiContext.ID)
repo, err := h.SourceCodeRepositoryLister.Get(ns, name)
if err != nil {
return err
}
ns, name = ref.Parse(repo.Spec.SourceCodeCredentialName)
cred, err := h.SourceCodeCredentialLister.Get(ns, name)
if err != nil {
return err
}
clusterPipeline, err := h.ClusterPipelineLister.Get(cred.Spec.ClusterName, cred.Spec.ClusterName)
if err != nil {
return err
}
remote, err := remote.New(*clusterPipeline, repo.Spec.SourceCodeType)
if err != nil {
return err
}
m := map[string]interface{}{}
branch := apiContext.Request.URL.Query().Get("branch")
if branch == "" {
branch, err = remote.GetDefaultBranch(repo.Spec.URL, cred.Spec.AccessToken)
if err != nil {
return err
}
}
m["branch"] = branch
content, err := remote.GetPipelineFileInRepo(repo.Spec.URL, branch, cred.Spec.AccessToken)
if err != nil {
return err
}
if content != nil {
spec, err := fromYaml(content)
if err != nil {
return err
}
m["pipeline"] = spec
}
bytes, err := json.Marshal(m)
if err != nil {
return err
}
apiContext.Response.Write(bytes)
return nil
}
return httperror.NewAPIError(httperror.NotFound, "Link not found")
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.5

搜索帮助

0d507c66 1850385 C8b1a773 1850385