-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartifact.go
300 lines (267 loc) · 10.4 KB
/
artifact.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"encoding/xml"
"errors"
"fmt"
"mime"
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/iver-wharf/wharf-api/v5/internal/ctxparser"
"github.com/iver-wharf/wharf-api/v5/internal/wherefields"
"github.com/iver-wharf/wharf-api/v5/pkg/model/database"
"github.com/iver-wharf/wharf-api/v5/pkg/model/response"
"github.com/iver-wharf/wharf-api/v5/pkg/modelconv"
"github.com/iver-wharf/wharf-api/v5/pkg/orderby"
"github.com/iver-wharf/wharf-core/pkg/ginutil"
"gorm.io/gorm"
)
type artifactModule struct {
Database *gorm.DB
}
func (m artifactModule) Register(g *gin.RouterGroup) {
g.GET("/artifact", m.getBuildArtifactListHandler)
g.GET("/artifact/:artifactId", m.getBuildArtifactHandler)
g.POST("/artifact", m.createBuildArtifactHandler)
// deprecated
g.GET("/tests-results", m.getBuildTestResultListHandler)
}
var artifactJSONToColumns = map[string]database.SafeSQLName{
response.ArtifactJSONFields.ArtifactID: database.ArtifactColumns.ArtifactID,
response.ArtifactJSONFields.Name: database.ArtifactColumns.Name,
response.ArtifactJSONFields.FileName: database.ArtifactColumns.FileName,
}
var defaultGetArtifactsOrderBy = orderby.Column{Name: database.ArtifactColumns.ArtifactID, Direction: orderby.Desc}
// getBuildArtifactListHandler godoc
// @id getBuildArtifactList
// @summary Get list of build artifacts
// @description List all build artifacts, or a window of build artifacts using the `limit` and `offset` query parameters. Allows optional filtering parameters.
// @description Verbatim filters will match on the entire string used to find exact matches,
// @description while the matching filters are meant for searches by humans where it tries to find soft matches and is therefore inaccurate by nature.
// @description Added in v5.0.0.
// @tags artifact
// @produce json
// @param buildId path uint true "Build ID" minimum(0)
// @param limit query int false "Number of results to return. No limiting is applied if empty (`?limit=`) or non-positive (`?limit=0`). Required if `offset` is used." default(100)
// @param offset query int false "Skipped results, where 0 means from the start." minimum(0) default(0)
// @param orderby query []string false "Sorting orders. Takes the property name followed by either 'asc' or 'desc'. Can be specified multiple times for more granular sorting. Defaults to `?orderby=artifactId desc`"
// @param name query string false "Filter by verbatim artifact name."
// @param fileName query string false "Filter by verbatim artifact file name."
// @param nameMatch query string false "Filter by matching artifact name. Cannot be used with `name`."
// @param fileNameMatch query string false "Filter by matching artifact file name. Cannot be used with `fileName`."
// @param match query string false "Filter by matching on any supported fields."
// @param pretty query bool false "Pretty indented JSON output"
// @success 200 {object} response.PaginatedArtifacts
// @failure 400 {object} problem.Response "Bad request"
// @failure 401 {object} problem.Response "Unauthorized or missing jwt token"
// @failure 502 {object} problem.Response "Database is unreachable"
// @router /build/{buildId}/artifact [get]
func (m artifactModule) getBuildArtifactListHandler(c *gin.Context) {
buildID, ok := ginutil.ParseParamUint(c, "buildId")
if !ok {
return
}
var params = struct {
commonGetQueryParams
Name *string `form:"name"`
FileName *string `form:"fileName"`
NameMatch *string `form:"nameMatch" binding:"excluded_with=Name"`
FileNameMatch *string `form:"fileNameMatch" binding:"excluded_with=FileName"`
Match *string `form:"match"`
}{
commonGetQueryParams: defaultCommonGetQueryParams,
}
if !bindCommonGetQueryParams(c, ¶ms) {
return
}
orderBySlice, ok := parseCommonOrderBySlice(c, params.OrderBy, artifactJSONToColumns)
if !ok {
return
}
var where wherefields.Collection
where.AddFieldName(database.ArtifactFields.BuildID)
query := m.Database.
Clauses(orderBySlice.ClauseIfNone(defaultGetArtifactsOrderBy)).
Where(&database.Artifact{
BuildID: buildID,
Name: where.String(database.ArtifactFields.Name, params.Name),
FileName: where.String(database.ArtifactFields.FileName, params.FileName),
}, where.NonNilFieldNames()...).
Scopes(
whereLikeScope(map[database.SafeSQLName]*string{
database.ArtifactColumns.Name: params.NameMatch,
database.ArtifactColumns.FileName: params.FileNameMatch,
}),
whereAnyLikeScope(
params.Match,
database.ArtifactColumns.Name,
database.ArtifactColumns.FileName,
),
)
var dbArtifacts []database.Artifact
var totalCount int64
err := findDBPaginatedSliceAndTotalCount(query, params.Limit, params.Offset, &dbArtifacts, &totalCount)
if err != nil {
ginutil.WriteDBReadError(c, err, fmt.Sprintf(
"Failed fetching list of artifacts for build with ID %d from database.",
buildID,
))
return
}
renderJSON(c, http.StatusOK, response.PaginatedArtifacts{
List: modelconv.DBArtifactsToResponses(dbArtifacts),
TotalCount: totalCount,
})
}
// getBuildArtifactHandler godoc
// @id getBuildArtifact
// @summary Get build artifact
// @description Added in v0.7.1.
// @tags artifact
// @produce multipart/form-data
// @param buildId path uint true "Build ID" minimum(0)
// @param artifactId path uint true "Artifact ID" minimum(0)
// @success 200 {file} string "OK"
// @failure 400 {object} problem.Response "Bad request"
// @failure 401 {object} problem.Response "Unauthorized or missing jwt token"
// @failure 404 {object} problem.Response "Artifact not found"
// @failure 502 {object} problem.Response "Database is unreachable"
// @router /build/{buildId}/artifact/{artifactId} [get]
func (m artifactModule) getBuildArtifactHandler(c *gin.Context) {
buildID, ok := ginutil.ParseParamUint(c, "buildId")
if !ok {
return
}
artifactID, ok := ginutil.ParseParamUint(c, "artifactId")
if !ok {
return
}
var dbArtifact database.Artifact
err := m.Database.
Where(&database.Artifact{
BuildID: buildID,
ArtifactID: artifactID}).
Order(database.ArtifactColumns.ArtifactID + " DESC").
First(&dbArtifact).
Error
if errors.Is(err, gorm.ErrRecordNotFound) {
ginutil.WriteDBNotFound(c, fmt.Sprintf(
"Artifact with ID %d was not found on build with ID %d.",
artifactID, buildID))
return
} else if err != nil {
ginutil.WriteBodyReadError(c, err, fmt.Sprintf(
"Failed fetching artifact with ID %d on build with ID %d.",
artifactID, buildID))
return
}
extension := filepath.Ext(dbArtifact.FileName)
mimeType := mime.TypeByExtension(extension)
disposition := fmt.Sprintf("attachment; filename=\"%s\"", dbArtifact.FileName)
c.Header("Content-Disposition", disposition)
c.Data(http.StatusOK, mimeType, dbArtifact.Data)
}
// createBuildArtifactHandler godoc
// @id createBuildArtifact
// @summary Post build artifact
// @description Added in v0.4.9.
// @tags artifact
// @accept multipart/form-data
// @param buildId path uint true "Build ID" minimum(0)
// @param files formData file true "Build artifact file"
// @success 201 "Added new artifacts"
// @failure 400 {object} problem.Response "Bad request"
// @failure 401 {object} problem.Response "Unauthorized or missing jwt token"
// @failure 404 {object} problem.Response "Artifact not found"
// @failure 502 {object} problem.Response "Database is unreachable"
// @router /build/{buildId}/artifact [post]
func (m artifactModule) createBuildArtifactHandler(c *gin.Context) {
log.Debug().Message("Start of createBuildArtifactHandler")
buildID, ok := ginutil.ParseParamUint(c, "buildId")
if !ok {
return
}
files, err := ctxparser.ParseMultipartFormDataFiles(c, "files")
if err != nil {
ginutil.WriteMultipartFormReadError(c, err,
fmt.Sprintf("Failed reading multipart-form's file data from request body when uploading"+
" new artifact for build with ID %d.", buildID))
return
}
_, ok = createArtifacts(c, m.Database, files, buildID)
if !ok {
return
}
c.Status(http.StatusCreated)
}
// getBuildTestResultListHandler godoc
// @id getBuildTestResultList
// @deprecated
// @summary Get build tests results from .trx files.
// @description Deprecated, /build/{buildid}/test-result/list-summary should be used instead.
// @description Added in v0.7.0.
// @tags artifact
// @produce json
// @param buildId path uint true "Build ID" minimum(0)
// @param pretty query bool false "Pretty indented JSON output"
// @success 200 {object} response.TestsResults
// @failure 400 {object} problem.Response "Bad request"
// @failure 401 {object} problem.Response "Unauthorized or missing jwt token"
// @failure 502 {object} problem.Response "Database is unreachable"
// @router /build/{buildId}/tests-results [get]
func (m artifactModule) getBuildTestResultListHandler(c *gin.Context) {
buildID, ok := ginutil.ParseParamUint(c, "buildId")
if !ok {
return
}
testRunFiles := []database.Artifact{}
err := m.Database.
Where(&database.Artifact{BuildID: buildID}).
Where(database.ArtifactColumns.FileName+" LIKE ?", "%.trx").
Find(&testRunFiles).
Error
if err != nil {
ginutil.WriteDBReadError(c, err, fmt.Sprintf(
"Failed fetching test result artifacts for build with ID %d from database.",
buildID))
return
}
var resResults response.TestsResults
var run trxTestRun
for _, testRunFile := range testRunFiles {
xml.Unmarshal(testRunFile.Data, &run)
resResults.Passed += run.ResultSummary.Counters.Passed
resResults.Failed += run.ResultSummary.Counters.Failed
}
if resResults.Failed == 0 && resResults.Passed == 0 {
resResults.Status = response.TestStatusNoTests
} else if resResults.Failed == 0 {
resResults.Status = response.TestStatusSuccess
} else {
resResults.Status = response.TestStatusFailed
}
renderJSON(c, http.StatusOK, resResults)
}
func createArtifacts(c *gin.Context, db *gorm.DB, files []ctxparser.File, buildID uint) ([]database.Artifact, bool) {
dbArtifacts := make([]database.Artifact, len(files))
for idx, f := range files {
artifactPtr := &dbArtifacts[idx]
artifactPtr.Data = f.Data
artifactPtr.Name = f.Name
artifactPtr.FileName = f.FileName
artifactPtr.BuildID = buildID
err := db.Create(artifactPtr).Error
if err != nil {
ginutil.WriteDBWriteError(c, err, fmt.Sprintf(
"Failed saving artifact with name %q for build with ID %d in database.",
artifactPtr.FileName, buildID))
return dbArtifacts, false
}
log.Debug().
WithString("filename", artifactPtr.FileName).
WithUint("build", buildID).
WithUint("artifact", artifactPtr.ArtifactID).
Message("File saved as artifact")
}
return dbArtifacts, true
}