-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathupdate_service.go
129 lines (109 loc) · 3.05 KB
/
update_service.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
// SPDX-License-Identifier: Apache-2.0
package log
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/go-vela/server/api/types"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/build"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/service"
"github.com/go-vela/server/util"
)
// swagger:operation PUT /api/v1/repos/{org}/{repo}/builds/{build}/services/{service}/logs services UpdateServiceLog
//
// Update service logs for a build
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: org
// description: Name of the organization
// required: true
// type: string
// - in: path
// name: repo
// description: Name of the repository
// required: true
// type: string
// - in: path
// name: build
// description: Build number
// required: true
// type: integer
// - in: path
// name: service
// description: Service number
// required: true
// type: integer
// - in: body
// name: body
// description: The log object with the fields to be updated
// required: true
// schema:
// "$ref": "#/definitions/Log"
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully updated the service logs
// '400':
// description: Invalid request payload or path
// schema:
// "$ref": "#/definitions/Error"
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Not found
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unexpected server error
// schema:
// "$ref": "#/definitions/Error"
// UpdateServiceLog represents the API handler to update
// the logs for a service.
func UpdateServiceLog(c *gin.Context) {
// capture middleware values
l := c.MustGet("logger").(*logrus.Entry)
b := build.Retrieve(c)
r := repo.Retrieve(c)
s := service.Retrieve(c)
ctx := c.Request.Context()
entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())
l.Debugf("updating logs for service %s", entry)
// send API call to capture the service logs
sl, err := database.FromContext(c).GetLogForService(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
// capture body from API request
input := new(types.Log)
err = c.Bind(input)
if err != nil {
retErr := fmt.Errorf("unable to decode JSON for service %s: %w", entry, err)
util.HandleError(c, http.StatusBadRequest, retErr)
return
}
// update log fields if provided
if len(input.GetData()) > 0 {
// update data if set
sl.SetData(input.GetData())
}
// send API call to update the log
err = database.FromContext(c).UpdateLog(ctx, sl)
if err != nil {
retErr := fmt.Errorf("unable to update logs for service %s: %w", entry, err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, nil)
}