-
Notifications
You must be signed in to change notification settings - Fork 7
/
spec.go
248 lines (218 loc) · 6.5 KB
/
spec.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
package artifact
import (
"encoding/json"
"io"
"os"
"strings"
"syscall"
"time"
"github.com/pkg/errors"
)
var (
// ErrFileNotFound indicates that an artifact was not found.
ErrFileNotFound = errors.New("file not found")
// ErrNotParsable indicates that an artifact could not be parsed against the
// artifact specification.
ErrNotParsable = errors.New("artifact not parsable")
// ErrUnknownFields indicates that an artifact contains an unknown field.
ErrUnknownFields = errors.New("artifact contains unknown fields")
)
type Spec struct {
ID string `json:"id,omitempty"`
Service string `json:"service,omitempty"`
Namespace string `json:"namespace,omitempty"`
Application Repository `json:"application,omitempty"`
CI CI `json:"ci,omitempty"`
Squad string `json:"squad,omitempty"`
Shuttle Shuttle `json:"shuttle,omitempty"`
Stages []Stage `json:"stages,omitempty"`
}
type Repository struct {
Branch string `json:"branch,omitempty"`
SHA string `json:"sha,omitempty"`
AuthorName string `json:"authorName,omitempty"`
AuthorEmail string `json:"authorEmail,omitempty"`
CommitterName string `json:"committerName,omitempty"`
CommitterEmail string `json:"committerEmail,omitempty"`
Message string `json:"message,omitempty"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Provider string `json:"provider,omitempty"`
}
type Shuttle struct {
Plan Repository `json:"plan,omitempty"`
ShuttleVersion string `json:"shuttleVersion,omitempty"`
}
type CI struct {
JobURL string `json:"jobUrl,omitempty"`
Start time.Time `json:"start,omitempty"`
End time.Time `json:"end,omitempty"`
}
type StageID string
const (
StageIDBuild StageID = "build"
StageIDTest StageID = "test"
StageIDPush StageID = "push"
StageIDSnykCode StageID = "snyk-code"
StageIDSnykDocker StageID = "snyk-docker"
)
type Stage struct {
ID StageID `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Data interface{} `json:"data,omitempty"`
}
// UnmarshalJSON implements a custom JSON unmarshal method that sets the
// concrete Data types for each stage type.
func (s *Stage) UnmarshalJSON(data []byte) error {
type genericStage struct {
ID StageID `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
}
var gStage genericStage
err := json.Unmarshal(data, &gStage)
if err != nil {
return err
}
s.ID = gStage.ID
s.Name = gStage.Name
switch s.ID {
case StageIDBuild:
data := BuildData{}
err = json.Unmarshal(gStage.Data, &data)
s.Data = data
case StageIDPush:
data := PushData{}
err = json.Unmarshal(gStage.Data, &data)
s.Data = data
case StageIDTest:
data := TestData{}
err = json.Unmarshal(gStage.Data, &data)
s.Data = data
case StageIDSnykCode:
data := SnykCodeData{}
err = json.Unmarshal(gStage.Data, &data)
s.Data = data
case StageIDSnykDocker:
data := SnykDockerData{}
err = json.Unmarshal(gStage.Data, &data)
s.Data = data
}
if err != nil {
return err
}
return nil
}
type BuildData struct {
Image string `json:"image,omitempty"`
Tag string `json:"tag,omitempty"`
DockerVersion string `json:"dockerVersion,omitempty"`
}
type PushData struct {
Image string `json:"image,omitempty"`
Tag string `json:"tag,omitempty"`
DockerVersion string `json:"dockerVersion,omitempty"`
}
type TestData struct {
URL string `json:"url,omitempty"`
Results TestResult `json:"results,omitempty"`
}
type TestResult struct {
Passed int `json:"passed"`
Failed int `json:"failed"`
Skipped int `json:"skipped"`
}
type SnykDockerData struct {
Tag string `json:"tag,omitempty"`
SnykVersion string `json:"snykVersion,omitempty"`
URL string `json:"url,omitempty"`
BaseImage string `json:"baseImage,omitempty"`
Vulnerabilities VulnerabilityResult `json:"vulnerabilities,omitempty"`
}
type SnykCodeData struct {
SnykVersion string `json:"snykVersion,omitempty"`
URL string `json:"url,omitempty"`
Language string `json:"language,omitempty"`
Vulnerabilities VulnerabilityResult `json:"vulnerabilities,omitempty"`
}
type VulnerabilityResult struct {
High int `json:"high"`
Medium int `json:"medium"`
Low int `json:"low"`
}
func Get(path string) (Spec, error) {
s, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return Spec{}, ErrFileNotFound
}
// handle "not a directory" errors that can be returned when looking into
// non-existing nested folders.
var pathErr *os.PathError
if errors.As(err, &pathErr) && pathErr.Err == syscall.ENOTDIR {
return Spec{}, ErrFileNotFound
}
return Spec{}, err
}
defer s.Close()
return Decode(s)
}
func Persist(path string, spec Spec) error {
s, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
return errors.WithMessage(err, "open file")
}
defer s.Close()
encode := json.NewEncoder(s)
encode.SetIndent("", " ")
err = encode.Encode(spec)
if err != nil {
return errors.WithMessage(err, "encode spec to json")
}
return nil
}
func Update(path string, f func(Spec) Spec) error {
s, err := Get(path)
if err != nil {
return errors.WithMessagef(err, "read artifact '%s'", path)
}
s = f(s)
// Persist back to the file
err = Persist(path, s)
if err != nil {
return errors.WithMessagef(err, "persiste artifact to '%s'", path)
}
return nil
}
func Encode(spec Spec, pretty bool) (string, error) {
var jsonOutput []byte
var err error
if pretty {
jsonOutput, err = json.MarshalIndent(spec, "", " ")
} else {
jsonOutput, err = json.Marshal(spec)
}
if err != nil {
return "", errors.WithMessage(err, "encode spec to json")
}
return string(jsonOutput), nil
}
func Decode(reader io.Reader) (Spec, error) {
var fileSpec Spec
decoder := json.NewDecoder(reader)
decoder.DisallowUnknownFields()
err := decoder.Decode(&fileSpec)
if err != nil {
_, ok := err.(*json.SyntaxError)
if ok {
return Spec{}, ErrNotParsable
}
// there is no other way to detect this error type unfortunately
// https://github.com/golang/go/blob/277609f844ed9254d25e975f7cf202d042beecc6/src/encoding/json/decode.go#L739
if strings.HasPrefix(err.Error(), "json: unknown field") {
return Spec{}, errors.WithMessagef(ErrUnknownFields, "%v", err)
}
return Spec{}, err
}
return fileSpec, nil
}