forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_code_repository.go
76 lines (66 loc) · 2.01 KB
/
source_code_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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")
}