-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathvalidate.go
107 lines (94 loc) · 2.79 KB
/
validate.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
// SPDX-License-Identifier: Apache-2.0
package pipeline
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-vela/server/compiler"
"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/util"
"github.com/go-vela/types"
"github.com/sirupsen/logrus"
)
// swagger:operation POST /api/v1/pipelines/{org}/{repo}/{pipeline}/validate pipelines ValidatePipeline
//
// Get, expand and validate 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, expanded and validated the pipeline
// schema:
// type: string
// '400':
// description: Unable to validate the pipeline configuration
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Unable to retrieve the pipeline configuration
// schema:
// "$ref": "#/definitions/Error"
// ValidatePipeline represents the API handler to capture,
// expand and validate a pipeline configuration.
func ValidatePipeline(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)
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("validating pipeline %s", entry)
// ensure we use the expected pipeline type when compiling
r.SetPipelineType(p.GetType())
// create the compiler object
compiler := compiler.FromContext(c).Duplicate().WithCommit(p.GetCommit()).WithMetadata(m).WithRepo(r).WithUser(u)
// validate the pipeline
pipeline, _, err := compiler.CompileLite(p.GetData(), false)
if err != nil {
retErr := fmt.Errorf("unable to validate pipeline %s: %w", entry, err)
util.HandleError(c, http.StatusBadRequest, retErr)
return
}
writeOutput(c, pipeline)
}