-
Notifications
You must be signed in to change notification settings - Fork 28
/
template.go
159 lines (135 loc) · 4.67 KB
/
template.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SPDX-License-Identifier: Apache-2.0
package pipeline
import (
"fmt"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/compiler/registry/github"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/pipeline"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/util"
"github.com/go-vela/types"
"github.com/go-vela/types/library"
"github.com/go-vela/types/yaml"
"github.com/sirupsen/logrus"
)
// swagger:operation GET /api/v1/pipelines/{org}/{repo}/{pipeline}/templates pipelines GetTemplates
//
// Get a map of templates utilized by a pipeline from the configured backend
//
// ---
// produces:
// - application/x-yaml
// - application/json
// parameters:
// - in: path
// name: repo
// description: Name of the repo
// required: true
// type: string
// - in: path
// name: org
// description: Name of the org
// required: true
// type: string
// - in: path
// name: pipeline
// description: Commit SHA for pipeline to retrieve
// required: true
// type: string
// - in: query
// name: output
// description: Output string for specifying output format
// type: string
// default: yaml
// enum:
// - json
// - yaml
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved the map of pipeline templates
// schema:
// "$ref": "#/definitions/Template"
// '400':
// description: Unable to retrieve the pipeline configuration templates
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Unable to retrieve the pipeline configuration templates
// schema:
// "$ref": "#/definitions/Error"
// GetTemplates represents the API handler to capture a
// map of templates utilized by a pipeline configuration.
func GetTemplates(c *gin.Context) {
// capture middleware values
m := c.MustGet("metadata").(*types.Metadata)
o := org.Retrieve(c)
p := pipeline.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()
entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit())
// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"org": o,
"pipeline": p.GetCommit(),
"repo": r.GetName(),
"user": u.GetName(),
}).Infof("reading templates from pipeline %s", entry)
// create the compiler object
compiler := compiler.FromContext(c).Duplicate().WithCommit(p.GetCommit()).WithMetadata(m).WithRepo(r).WithUser(u)
// parse the pipeline configuration
pipeline, _, err := compiler.Parse(p.GetData(), p.GetType(), new(yaml.Template))
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("unable to parse pipeline %s: %w", entry, err))
return
}
// send API call to capture the repo owner
user, err := database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err))
return
}
baseErr := fmt.Sprintf("unable to set template links for %s", entry)
templates := make(map[string]*library.Template)
for name, template := range pipeline.Templates.Map() {
templates[name] = template.ToLibrary()
// create a compiler registry client for parsing (no address or token needed for Parse)
registry, err := github.New("", "")
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("%s: unable to create compiler github client: %w", baseErr, err))
return
}
// capture source path to template
source := template.Source
// if type is file, compose a source string so the template can be found
if strings.EqualFold(template.Type, "file") {
source = fmt.Sprintf("%s%s/%s/%s@%s", registry.URL, o, r.GetName(), source, p.GetCommit())
}
// parse the source for the template using the compiler registry client
src, err := registry.Parse(source)
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("%s: unable to parse source for %s: %w", baseErr, template.Source, err))
return
}
// retrieve link to template file from github
link, err := scm.FromContext(c).GetHTMLURL(ctx, user, src.Org, src.Repo, src.Name, src.Ref)
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("%s: unable to get html url for %s/%s/%s/@%s: %w", baseErr, src.Org, src.Repo, src.Name, src.Ref, err))
return
}
// set link to file for template
templates[name].SetLink(link)
}
writeOutput(c, templates)
}