-
Notifications
You must be signed in to change notification settings - Fork 6
/
parser.go
436 lines (362 loc) · 11.1 KB
/
parser.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package publiccode
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"unicode/utf8"
"github.com/alranel/go-vcsurl/v2"
"github.com/go-playground/validator/v10"
"gopkg.in/yaml.v3"
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
publiccodeValidator "github.com/italia/publiccode-parser-go/v4/validators"
)
type ParserConfig struct {
// DisableNetwork disables all network tests (URL existence and Oembed). This
// results in much faster parsing.
DisableNetwork bool
// Domain will have domain specific settings, including basic auth if provided
// this will avoid strong quota limit imposed by code hosting platform
Domain Domain
// The name of the branch used to check for existence of the files referenced
// in the publiccode.yml
Branch string
// The URL used as base of relative files in publiccode.yml (eg. logo)
// It can be a local file with the 'file' scheme.
BaseURL string
}
// Parser is a helper class for parsing publiccode.yml files.
type Parser struct {
disableNetwork bool
domain Domain
branch string
baseURL *url.URL
}
// Domain is a single code hosting service.
type Domain struct {
// Domains.yml data
Host string `yaml:"host"`
UseTokenFor []string `yaml:"use-token-for"`
BasicAuth []string `yaml:"basic-auth"`
}
// NewParser initializes and returns a new Parser object following the settings in
// ParserConfig.
func NewParser(config ParserConfig) (*Parser, error) {
parser := Parser{
disableNetwork: config.DisableNetwork,
domain: config.Domain,
branch: config.Branch,
}
if config.BaseURL != "" {
var err error
if parser.baseURL, err = toURL(config.BaseURL); err != nil {
return nil, err
}
}
return &parser, nil
}
func NewDefaultParser() (*Parser, error) {
return NewParser(ParserConfig{})
}
// ParseStream reads the data and tries to parse it. Returns an error if fails.
func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
b, err := io.ReadAll(in)
if err != nil {
return nil, ValidationResults{newValidationError("", fmt.Sprintf("Can't read the stream: %v", err))}
}
if !utf8.Valid(b) {
return nil, ValidationResults{newValidationError("", "Invalid UTF-8")}
}
// First, decode the YAML into yaml.Node so we can access line and column
// numbers.
var node yaml.Node
d := yaml.NewDecoder(bytes.NewReader(b))
d.KnownFields(true)
err = d.Decode(&node)
if err == nil && len(node.Content) > 0 {
node = *node.Content[0]
} else {
// YAML is malformed
return nil, ValidationResults{toValidationError(err.Error(), nil)}
}
_, version := getNodes("publiccodeYmlVersion", &node)
if version == nil {
return nil, ValidationResults{newValidationError("publiccodeYmlVersion", "required")}
}
if version.ShortTag() != "!!str" {
line, column := getPositionInFile("publiccodeYmlVersion", node)
return nil, ValidationResults{ValidationError{
Key: "publiccodeYmlVersion",
Description: "wrong type for this field",
Line: line,
Column: column,
}}
}
if !slices.Contains(SupportedVersions, version.Value) {
return nil, ValidationResults{
newValidationError("publiccodeYmlVersion", fmt.Sprintf(
"unsupported version: '%s'. Supported versions: %s",
version.Value,
strings.Join(SupportedVersions, ", "),
)),
}
}
var ve ValidationResults
if slices.Contains(SupportedVersions, version.Value) && !strings.HasPrefix(version.Value, "0.4") {
latestVersion := SupportedVersions[len(SupportedVersions)-1]
line, column := getPositionInFile("publiccodeYmlVersion", node)
ve = append(ve, ValidationWarning{
Key: "publiccodeYmlVersion",
Description: fmt.Sprintf(
"v%s is not the latest version, use '%s'. Parsing this file as v%s.",
version.Value,
latestVersion,
latestVersion,
),
Line: line,
Column: column,
})
}
var publiccode PublicCode
var validateFields validateFn
var decodeResults ValidationResults
if version.Value[0] == '0' {
v0 := PublicCodeV0{}
validateFields = validateFieldsV0
decodeResults = decode(b, &v0, node)
publiccode = v0
}
// When publiccode.yml v1.x is release the code will look
// like this:
// } else {
// v1 := PublicCodeV1{}
// validateFields = validateFieldsV1
//
// decodeResults = decode(b, &v1, node)
// publiccode = v1
// }
if decodeResults != nil {
ve = append(ve, decodeResults...)
}
validate := publiccodeValidator.New()
err = validate.Struct(publiccode)
if err != nil {
tagMap := map[string]string{
"gt": "must be more than",
"oneof": "must be one of the following:",
"email": "must be a valid email",
"date": "must be a date with format 'YYYY-MM-DD'",
"umax": "must be less or equal than",
"umin": "must be more or equal than",
"url_http_url": "must be an HTTP URL",
"url_url": "must be a valid URL",
"is_category_v0": "must be a valid category",
"is_scope_v0": "must be a valid scope",
"is_italian_ipa_code": "must be a valid Italian Public Administration Code (iPA)",
"iso3166_1_alpha2_lowercase": "must be a valid lowercase ISO 3166-1 alpha-2 two-letter country code",
"bcp47_language_tag": "must be a valid BCP 47 language",
"bcp47_keys": "must use a valid BCP 47 language",
}
for _, err := range err.(validator.ValidationErrors) {
var sb strings.Builder
tag, ok := tagMap[err.ActualTag()]
if !ok {
tag = err.ActualTag()
}
sb.WriteString(tag)
// condition parameters, e.g. oneof=red blue -> red blue
if err.Param() != "" {
sb.WriteString(" " + err.Param())
}
// TODO: find a cleaner way
key := strings.Replace(
err.Namespace(),
fmt.Sprintf("PublicCodeV%d.", publiccode.Version()),
"",
1,
)
m := regexp.MustCompile(`\[([[:alpha:]]+)\]`)
key = m.ReplaceAllString(key, ".$1")
line, column := getPositionInFile(key, node)
ve = append(ve, ValidationError{
Key: key,
Description: sb.String(),
Line: line,
Column: column,
})
}
}
// baseURL was not set to a local path, let's autodetect it from the
// publiccode.yml url key
//
// We need the baseURL to perform network checks.
if p.baseURL == nil && !p.disableNetwork && publiccode.Url() != nil {
rawRoot, err := vcsurl.GetRawRoot((*url.URL)(publiccode.Url()), p.branch)
if err != nil {
line, column := getPositionInFile("url", node)
ve = append(ve, ValidationError{
Key: "url",
Description: fmt.Sprintf("failed to get raw URL for code repository at %s: %s", publiccode.Url(), err),
Line: line,
Column: column,
})
// Return early because proceeding with no baseURL would result in a lot
// of duplicate errors stemming from its absence.
return publiccode, ve
}
p.baseURL = rawRoot
}
if err = validateFields(publiccode, *p, !p.disableNetwork); err != nil {
for _, err := range err.(ValidationResults) {
switch err := err.(type) {
case ValidationError:
err.Line, err.Column = getPositionInFile(err.Key, node)
ve = append(ve, err)
case ValidationWarning:
err.Line, err.Column = getPositionInFile(err.Key, node)
ve = append(ve, err)
}
}
}
if len(ve) == 0 {
return publiccode, nil
}
return publiccode, ve
}
func (p *Parser) Parse(uri string) (PublicCode, error) {
var stream io.Reader
url, err := toURL(uri)
if err != nil {
return nil, fmt.Errorf("Invalid URL '%s': %w", uri, err)
}
if url.Scheme == "file" {
stream, err = os.Open(url.Path)
if err != nil {
return nil, fmt.Errorf("Can't open file '%s': %w", url.Path, err)
}
} else {
resp, err := http.Get(uri)
if err != nil {
return nil, fmt.Errorf("Can't GET '%s': %w", uri, err)
}
defer resp.Body.Close()
stream = resp.Body
}
return p.ParseStream(stream)
}
func getNodes(key string, node *yaml.Node) (*yaml.Node, *yaml.Node) {
for i := 0; i < len(node.Content); i += 2 {
childNode := *node.Content[i]
if childNode.Value == key {
return &childNode, node.Content[i+1]
}
}
return nil, nil
}
func getPositionInFile(key string, node yaml.Node) (int, int) {
var n *yaml.Node = &node
keys := strings.Split(key, ".")
for _, path := range keys[:len(keys)-1] {
_, n = getNodes(path, n)
// This should not happen, but let's be defensive
if n == nil {
return 0, 0
}
}
parentNode := n
n, _ = getNodes(keys[len(keys)-1], n)
if n != nil {
return n.Line, n.Column
} else {
return parentNode.Line, parentNode.Column
}
}
// getKeyAtLine returns the key name at line "line" for the YAML document
// represented at parentNode.
func getKeyAtLine(parentNode yaml.Node, line int, path string) string {
var key = path
for i, currNode := range parentNode.Content {
// If this node is a mapping and the index is odd it means
// we are not looking at a key, but at its value. Skip it.
if parentNode.Kind == yaml.MappingNode && i%2 != 0 && currNode.Kind == yaml.ScalarNode {
continue
}
// This node is a key of a mapping type
if parentNode.Kind == yaml.MappingNode && i%2 == 0 {
if path == "" {
key = currNode.Value
} else {
key = fmt.Sprintf("%s.%s", path, currNode.Value)
}
}
// We want the scalar node (ie. key) not the mapping node which
// doesn't have a tag name even if it has the same line number
if currNode.Line == line && parentNode.Kind == yaml.MappingNode && currNode.Kind == yaml.ScalarNode {
return key
}
if currNode.Kind != yaml.ScalarNode {
if k := getKeyAtLine(*currNode, line, key); k != "" {
return k
}
}
}
return ""
}
func toValidationError(errorText string, node *yaml.Node) ValidationError {
r := regexp.MustCompile(`(line ([0-9]+): )`)
matches := r.FindStringSubmatch(errorText)
line := 0
if len(matches) > 1 {
line, _ = strconv.Atoi(matches[2])
errorText = strings.ReplaceAll(errorText, matches[1], "")
}
// Transform unmarshalling errors messages to a user friendlier message
r = regexp.MustCompile("^cannot unmarshal")
if r.MatchString(errorText) {
errorText = "wrong type for this field"
}
var key string
if node != nil {
key = getKeyAtLine(*node, line, "")
}
return ValidationError{
Key: key,
Description: errorText,
Line: line,
Column: 1,
}
}
// Decode the YAML into a PublicCode structure, so we get type errors
func decode[T any](data []byte, publiccode *T, node yaml.Node) ValidationResults {
var ve ValidationResults
d := yaml.NewDecoder(bytes.NewReader(data))
d.KnownFields(true)
if err := d.Decode(&publiccode); err != nil {
switch err := err.(type) {
case *yaml.TypeError:
for _, errorText := range err.Errors {
ve = append(ve, toValidationError(errorText, &node))
}
default:
ve = append(ve, newValidationError("", err.Error()))
}
}
return ve
}
func toURL(file string) (*url.URL, error) {
if _, u := urlutil.IsValidURL(file); u != nil {
return u, nil
}
if path, err := filepath.Abs(file); err == nil {
return &url.URL{Scheme: "file", Path: path}, nil
} else {
return nil, err
}
}