-
Notifications
You must be signed in to change notification settings - Fork 3
/
lint.go
91 lines (78 loc) · 2.44 KB
/
lint.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
package handlers
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/replicatedhq/kots-lint/pkg/kots"
"github.com/replicatedhq/kots-lint/pkg/util"
log "github.com/sirupsen/logrus"
)
// LintReleaseParameters contains parameters to lint a release for an app
type LintReleaseParameters struct {
// Lint release parameters
// In: body
Body struct {
// The spec to lint
Spec string `json:"spec"`
}
}
// LintReleaseResponse contains the lint expressions
type LintReleaseResponse struct {
// JSON payload
// Required: true
// In: body
Body struct {
LintExpressions []kots.LintExpression `json:"lintExpressions"`
IsLintingComplete bool `json:"isLintingComplete"`
}
}
// LintRelease http handler for linting a release
func LintRelease(c *gin.Context) {
log.Infof("Received lint request with content-length=%s, content-type=%s, client-ip=%s", c.GetHeader("content-length"), c.ContentType(), c.ClientIP())
ctx := c.Request.Context()
// read before binding to check if body is a tar stream
data, err := io.ReadAll(c.Request.Body)
c.Request.Body.Close()
if err != nil {
log.Errorf("failed to read request body: %v", err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
specFiles := kots.SpecFiles{}
if util.IsTarFile(data) {
f, err := kots.SpecFilesFromTar(bytes.NewReader(data))
if err != nil {
log.Errorf("failed to get spec files from tar file: %v", err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
specFiles = f
} else {
// restore request body to its original state to be able to bind it
c.Request.Body = io.NopCloser(bytes.NewBuffer(data))
var request LintReleaseParameters
if err := c.Bind(&request.Body); err != nil {
log.Errorf("failed to bind to lint release parameters: %v", err)
c.AbortWithError(http.StatusBadRequest, err)
return
}
if err := json.Unmarshal([]byte(request.Body.Spec), &specFiles); err != nil {
log.Errorf("failed to unmarshal spec: %v", err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
lintExpressions, isComplete, err := kots.LintSpecFiles(ctx, specFiles)
if err != nil {
fmt.Printf("failed to lint spec files: %v", err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
response := LintReleaseResponse{}
response.Body.LintExpressions = lintExpressions
response.Body.IsLintingComplete = isComplete
c.JSON(http.StatusOK, response.Body)
}