1
1
package main
2
2
3
3
import (
4
- "bufio"
5
4
"errors"
6
5
"fmt"
7
6
"net/url"
@@ -13,18 +12,10 @@ import (
13
12
"gopkg.in/yaml.v3"
14
13
)
15
14
16
- const rootRegistryPath = "./registry"
17
-
18
15
var (
19
- validContributorStatuses = []string {"official" , "partner" , "community" }
20
- supportedAvatarFileFormats = []string {".png" , ".jpeg" , ".jpg" , ".gif" , ".svg" }
16
+ validContributorStatuses = []string {"official" , "partner" , "community" }
21
17
)
22
18
23
- type readme struct {
24
- filePath string
25
- rawText string
26
- }
27
-
28
19
type contributorProfileFrontmatter struct {
29
20
DisplayName string `yaml:"display_name"`
30
21
Bio string `yaml:"bio"`
@@ -44,61 +35,6 @@ type contributorProfile struct {
44
35
filePath string
45
36
}
46
37
47
- var _ error = validationPhaseError {}
48
-
49
- type validationPhaseError struct {
50
- phase string
51
- errors []error
52
- }
53
-
54
- func (vpe validationPhaseError ) Error () string {
55
- validationStrs := []string {}
56
- for _ , e := range vpe .errors {
57
- validationStrs = append (validationStrs , fmt .Sprintf ("- %v" , e ))
58
- }
59
- slices .Sort (validationStrs )
60
-
61
- msg := fmt .Sprintf ("Error during %q phase of README validation:" , vpe .phase )
62
- msg += strings .Join (validationStrs , "\n " )
63
- msg += "\n "
64
-
65
- return msg
66
- }
67
-
68
- func extractFrontmatter (readmeText string ) (string , error ) {
69
- if readmeText == "" {
70
- return "" , errors .New ("README is empty" )
71
- }
72
-
73
- const fence = "---"
74
- fm := ""
75
- fenceCount := 0
76
- lineScanner := bufio .NewScanner (
77
- strings .NewReader (strings .TrimSpace (readmeText )),
78
- )
79
- for lineScanner .Scan () {
80
- nextLine := lineScanner .Text ()
81
- if fenceCount == 0 && nextLine != fence {
82
- return "" , errors .New ("README does not start with frontmatter fence" )
83
- }
84
-
85
- if nextLine != fence {
86
- fm += nextLine + "\n "
87
- continue
88
- }
89
-
90
- fenceCount ++
91
- if fenceCount >= 2 {
92
- break
93
- }
94
- }
95
-
96
- if fenceCount == 1 {
97
- return "" , errors .New ("README does not have two sets of frontmatter fences" )
98
- }
99
- return fm , nil
100
- }
101
-
102
38
func validateContributorGithubUsername (githubUsername string ) error {
103
39
if githubUsername == "" {
104
40
return errors .New ("missing GitHub username" )
@@ -260,10 +196,6 @@ func validateContributorAvatarURL(avatarURL *string) []error {
260
196
return problems
261
197
}
262
198
263
- func addFilePathToError (filePath string , err error ) error {
264
- return fmt .Errorf ("%q: %v" , filePath , err )
265
- }
266
-
267
199
func validateContributorYaml (yml contributorProfile ) []error {
268
200
allProblems := []error {}
269
201
@@ -297,7 +229,7 @@ func validateContributorYaml(yml contributorProfile) []error {
297
229
}
298
230
299
231
func parseContributorProfile (rm readme ) (contributorProfile , error ) {
300
- fm , err := extractFrontmatter (rm .rawText )
232
+ fm , _ , err := separateFrontmatter (rm .rawText )
301
233
if err != nil {
302
234
return contributorProfile {}, fmt .Errorf ("%q: failed to parse frontmatter: %v" , rm .filePath , err )
303
235
}
@@ -331,7 +263,7 @@ func parseContributorFiles(readmeEntries []readme) (map[string]contributorProfil
331
263
}
332
264
if len (yamlParsingErrors ) != 0 {
333
265
return nil , validationPhaseError {
334
- phase : "YAML parsing" ,
266
+ phase : validationPhaseReadmeParsing ,
335
267
errors : yamlParsingErrors ,
336
268
}
337
269
}
@@ -356,11 +288,11 @@ func parseContributorFiles(readmeEntries []readme) (map[string]contributorProfil
356
288
if _ , found := profilesByUsername [companyName ]; found {
357
289
continue
358
290
}
359
- yamlValidationErrors = append (yamlValidationErrors , fmt .Errorf ("company %q does not exist in %q directory but is referenced by these profiles: [%s]" , companyName , rootRegistryPath , strings .Join (group , ", " )))
291
+ yamlValidationErrors = append (yamlValidationErrors , fmt .Errorf ("%q: company %q does not exist but is referenced by these profiles: [%s]" , rootRegistryPath , companyName , strings .Join (group , ", " )))
360
292
}
361
293
if len (yamlValidationErrors ) != 0 {
362
294
return nil , validationPhaseError {
363
- phase : "Raw YAML Validation" ,
295
+ phase : validationPhaseReadmeParsing ,
364
296
errors : yamlValidationErrors ,
365
297
}
366
298
}
@@ -397,15 +329,15 @@ func aggregateContributorReadmeFiles() ([]readme, error) {
397
329
398
330
if len (problems ) != 0 {
399
331
return nil , validationPhaseError {
400
- phase : "FileSystem reading" ,
332
+ phase : validationPhaseFileLoad ,
401
333
errors : problems ,
402
334
}
403
335
}
404
336
405
337
return allReadmeFiles , nil
406
338
}
407
339
408
- func validateRelativeUrls (
340
+ func validateContributorRelativeUrls (
409
341
contributors map [string ]contributorProfile ,
410
342
) error {
411
343
// This function only validates relative avatar URLs for now, but it can be
@@ -440,7 +372,7 @@ func validateRelativeUrls(
440
372
return nil
441
373
}
442
374
return validationPhaseError {
443
- phase : "Relative URL validation" ,
375
+ phase : validationPhaseAssetCrossReference ,
444
376
errors : problems ,
445
377
}
446
378
}
0 commit comments