-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathcompile.go
More file actions
105 lines (92 loc) · 2.87 KB
/
compile.go
File metadata and controls
105 lines (92 loc) · 2.87 KB
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
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
// nolint: dupl // ignore similar code with expand
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}/compile pipelines CompilePipeline
//
// Get, expand and compile a pipeline configuration from the database
//
// ---
// 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: query
// name: ref
// description: Ref for retrieving pipeline configuration file
// type: string
// - in: query
// name: output
// description: Output string for specifying output format
// type: string
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved and compiled the pipeline
// schema:
// "$ref": "#/definitions/PipelineBuild"
// '400':
// description: Unable to validate the pipeline configuration
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Unable to retrieve the pipeline configuration
// schema:
// "$ref": "#/definitions/Error"
// CompilePipeline represents the API handler to capture,
// expand and compile a pipeline configuration.
func CompilePipeline(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/%d", r.GetFullName(), p.GetNumber())
// 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.GetNumber(),
"repo": r.GetName(),
"user": u.GetName(),
}).Infof("compiling 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().WithMetadata(m).WithRepo(r).WithUser(u)
// compile the pipeline
pipeline, _, err := compiler.CompileLite(p.GetData(), true, true, nil)
if err != nil {
retErr := fmt.Errorf("unable to compile pipeline %s: %w", entry, err)
util.HandleError(c, http.StatusBadRequest, retErr)
return
}
writeOutput(c, pipeline)
}