-
Notifications
You must be signed in to change notification settings - Fork 28
/
update.go
182 lines (155 loc) · 4.6 KB
/
update.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// SPDX-License-Identifier: Apache-2.0
package pipeline
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"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/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)
// swagger:operation PUT /api/v1/pipelines/{org}/{repo}/{pipeline} pipelines UpdatePipeline
//
// Update a pipeline in the configured backend
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: org
// description: Name of the org
// required: true
// type: string
// - in: path
// name: repo
// description: Name of the repo
// required: true
// type: string
// - in: path
// name: pipeline
// description: Commit SHA for pipeline to update
// required: true
// type: string
// - in: body
// name: body
// description: Payload containing the pipeline to update
// required: true
// schema:
// "$ref": "#/definitions/Pipeline"
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully updated the pipeline
// schema:
// "$ref": "#/definitions/Pipeline"
// '404':
// description: Unable to update the pipeline
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to update the pipeline
// schema:
// "$ref": "#/definitions/Error"
// UpdatePipeline represents the API handler to update
// a pipeline for a repo in the configured backend.
func UpdatePipeline(c *gin.Context) {
// capture middleware values
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("updating pipeline %s", entry)
// capture body from API request
input := new(library.Pipeline)
err := c.Bind(input)
if err != nil {
retErr := fmt.Errorf("unable to decode JSON for pipeline %s: %w", entry, err)
util.HandleError(c, http.StatusNotFound, retErr)
return
}
// check if the Flavor field in the pipeline was provided
if len(input.GetFlavor()) > 0 {
// update the Flavor field
p.SetFlavor(input.GetFlavor())
}
// check if the Platform field in the pipeline was provided
if len(input.GetPlatform()) > 0 {
// update the Platform field
p.SetPlatform(input.GetPlatform())
}
// check if the Ref field in the pipeline was provided
if len(input.GetRef()) > 0 {
// update the Ref field
p.SetRef(input.GetRef())
}
// check if the Type field in the pipeline was provided
if len(input.GetType()) > 0 {
// update the Type field
p.SetType(input.GetType())
}
// check if the Version field in the pipeline was provided
if len(input.GetVersion()) > 0 {
// update the Version field
p.SetVersion(input.GetVersion())
}
// check if the ExternalSecrets field in the pipeline was provided
if input.ExternalSecrets != nil {
// update the ExternalSecrets field
p.SetExternalSecrets(input.GetExternalSecrets())
}
// check if the InternalSecrets field in the pipeline was provided
if input.InternalSecrets != nil {
// update the InternalSecrets field
p.SetInternalSecrets(input.GetInternalSecrets())
}
// check if the Services field in the pipeline was provided
if input.Services != nil {
// update the Services field
p.SetServices(input.GetServices())
}
// check if the Stages field in the pipeline was provided
if input.Stages != nil {
// update the Stages field
p.SetStages(input.GetStages())
}
// check if the Steps field in the pipeline was provided
if input.Steps != nil {
// update the Steps field
p.SetSteps(input.GetSteps())
}
// check if the Templates field in the pipeline was provided
if input.Templates != nil {
// update the Templates field
p.SetTemplates(input.GetTemplates())
}
// check if the Data field in the pipeline was provided
if len(input.GetData()) > 0 {
// update the data field
p.SetData(input.GetData())
}
// send API call to update the pipeline
p, err = database.FromContext(c).UpdatePipeline(ctx, p)
if err != nil {
retErr := fmt.Errorf("unable to update pipeline %s: %w", entry, err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, p)
}