Skip to content

Commit da735da

Browse files
committed
fix: remove parsing bugs
1 parent 1906520 commit da735da

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

.github/scripts/readme-validation/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ go 1.23.2
44

55
require github.com/ghodss/yaml v1.0.0
66

7-
require gopkg.in/yaml.v2 v2.4.0 // indirect
7+
require (
8+
gopkg.in/yaml.v2 v2.4.0 // indirect
9+
gopkg.in/yaml.v3 v3.0.1 // indirect
10+
)

.github/scripts/readme-validation/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
44
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
55
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
66
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
7+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
8+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

.github/scripts/readme-validation/main.go

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"strings"
1313
"sync"
1414

15-
"github.com/ghodss/yaml"
15+
"gopkg.in/yaml.v3"
1616
)
1717

1818
const rootRegistryPath = "../../../registry"
@@ -23,18 +23,18 @@ type directoryReadme struct {
2323
}
2424

2525
type rawContributorProfileFrontmatter struct {
26-
DisplayName string `yaml:"display_name"`
27-
Bio string `yaml:"bio"`
28-
GithubUsername string `yaml:"github"`
29-
AvatarUrl *string `yaml:"avatar"`
30-
LinkedinURL *string `yaml:"linkedin"`
31-
WebsiteURL *string `yaml:"website"`
32-
SupportEmail *string `yaml:"support_email"`
33-
CompanyGithub *string `yaml:"company_github"`
34-
ContributorStatus *string `yaml:"status"`
26+
DisplayName string `yaml:"display_name"`
27+
Bio string `yaml:"bio"`
28+
GithubUsername string `yaml:"github"`
29+
AvatarUrl *string `yaml:"avatar"`
30+
LinkedinURL *string `yaml:"linkedin"`
31+
WebsiteURL *string `yaml:"website"`
32+
SupportEmail *string `yaml:"support_email"`
33+
EmployerGithubUsername *string `yaml:"employer_github"`
34+
ContributorStatus *string `yaml:"status"`
3535
}
3636

37-
type trackableContributorFrontmatter struct {
37+
type contributorFrontmatterWithFilepath struct {
3838
rawContributorProfileFrontmatter
3939
FilePath string
4040
}
@@ -97,7 +97,7 @@ func extractFrontmatter(readmeText string) (string, error) {
9797
}
9898

9999
if nextLine != fence {
100-
fm += nextLine
100+
fm += nextLine + "\n"
101101
continue
102102
}
103103

@@ -113,7 +113,7 @@ func extractFrontmatter(readmeText string) (string, error) {
113113
return fm, nil
114114
}
115115

116-
func validateContributorYaml(yml trackableContributorFrontmatter) []error {
116+
func validateContributorYaml(yml contributorFrontmatterWithFilepath) []error {
117117
// This function needs to aggregate a bunch of different errors, rather than
118118
// stopping at the first one found, so using code blocks to section off
119119
// logic for different fields
@@ -145,8 +145,8 @@ func validateContributorYaml(yml trackableContributorFrontmatter) []error {
145145
}
146146

147147
// Company GitHub
148-
if yml.CompanyGithub != nil {
149-
if *yml.CompanyGithub == "" {
148+
if yml.EmployerGithubUsername != nil {
149+
if *yml.EmployerGithubUsername == "" {
150150
errors = append(
151151
errors,
152152
fmt.Errorf(
@@ -156,19 +156,19 @@ func validateContributorYaml(yml trackableContributorFrontmatter) []error {
156156
)
157157
}
158158

159-
lower := strings.ToLower(*yml.CompanyGithub)
159+
lower := strings.ToLower(*yml.EmployerGithubUsername)
160160
if uriSafe := url.PathEscape(lower); uriSafe != lower {
161161
errors = append(
162162
errors,
163163
fmt.Errorf(
164164
"gitHub company username %q (%q) is not a valid URL path segment",
165-
*yml.CompanyGithub,
165+
*yml.EmployerGithubUsername,
166166
yml.FilePath,
167167
),
168168
)
169169
}
170170

171-
if *yml.CompanyGithub == yml.GithubUsername {
171+
if *yml.EmployerGithubUsername == yml.GithubUsername {
172172
errors = append(
173173
errors,
174174
fmt.Errorf(
@@ -315,7 +315,7 @@ website:
315315
}
316316

317317
func remapContributorProfile(
318-
frontmatter trackableContributorFrontmatter,
318+
frontmatter contributorFrontmatterWithFilepath,
319319
employeeGitHubNames []string,
320320
) contributorProfile {
321321
// Function assumes that fields are previously validated and are safe to
@@ -354,20 +354,20 @@ func remapContributorProfile(
354354
return remapped
355355
}
356356

357-
func parseContributorFiles(input []directoryReadme) (
357+
func parseContributorFiles(readmeEntries []directoryReadme) (
358358
map[string]contributorProfile,
359359
error,
360360
) {
361-
frontmatterByGithub := map[string]trackableContributorFrontmatter{}
361+
frontmatterByGithub := map[string]contributorFrontmatterWithFilepath{}
362362
yamlParsingErrors := workflowPhaseError{
363363
Phase: "YAML parsing",
364364
}
365-
for _, dirReadme := range input {
366-
fmText, err := extractFrontmatter(dirReadme.RawText)
365+
for _, rm := range readmeEntries {
366+
fmText, err := extractFrontmatter(rm.RawText)
367367
if err != nil {
368368
yamlParsingErrors.Errors = append(
369369
yamlParsingErrors.Errors,
370-
fmt.Errorf("failed to parse %q: %v", dirReadme.FilePath, err),
370+
fmt.Errorf("failed to parse %q: %v", rm.FilePath, err),
371371
)
372372
continue
373373
}
@@ -376,12 +376,13 @@ func parseContributorFiles(input []directoryReadme) (
376376
if err := yaml.Unmarshal([]byte(fmText), &yml); err != nil {
377377
yamlParsingErrors.Errors = append(
378378
yamlParsingErrors.Errors,
379-
fmt.Errorf("failed to parse %q: %v", dirReadme.FilePath, err),
379+
fmt.Errorf("failed to parse %q: %v", rm.FilePath, err),
380380
)
381381
continue
382382
}
383-
trackable := trackableContributorFrontmatter{
384-
FilePath: dirReadme.FilePath,
383+
384+
trackable := contributorFrontmatterWithFilepath{
385+
FilePath: rm.FilePath,
385386
rawContributorProfileFrontmatter: yml,
386387
}
387388

@@ -391,15 +392,18 @@ func parseContributorFiles(input []directoryReadme) (
391392
fmt.Errorf(
392393
"GitHub name conflict for %q for files %q and %q",
393394
trackable.GithubUsername,
394-
trackable.FilePath,
395395
prev.FilePath,
396+
trackable.FilePath,
396397
),
397398
)
398399
continue
399400
}
400401

401402
frontmatterByGithub[trackable.GithubUsername] = trackable
402403
}
404+
if len(yamlParsingErrors.Errors) != 0 {
405+
return nil, yamlParsingErrors
406+
}
403407

404408
employeeGithubGroups := map[string][]string{}
405409
yamlValidationErrors := workflowPhaseError{
@@ -415,9 +419,9 @@ func parseContributorFiles(input []directoryReadme) (
415419
continue
416420
}
417421

418-
if yml.CompanyGithub != nil {
419-
employeeGithubGroups[*yml.CompanyGithub] = append(
420-
employeeGithubGroups[*yml.CompanyGithub],
422+
if yml.EmployerGithubUsername != nil {
423+
employeeGithubGroups[*yml.EmployerGithubUsername] = append(
424+
employeeGithubGroups[*yml.EmployerGithubUsername],
421425
yml.GithubUsername,
422426
)
423427
}
@@ -443,8 +447,8 @@ func parseContributorFiles(input []directoryReadme) (
443447
contributorError.Errors,
444448
fmt.Errorf(
445449
"company %q does not exist in %q directory but is referenced by these profiles: [%s]",
446-
rootRegistryPath,
447450
companyName,
451+
rootRegistryPath,
448452
strings.Join(group, ", "),
449453
),
450454
)

0 commit comments

Comments
 (0)