fix: remove parsing bugs

This commit is contained in:
Michael Smith 2025-04-08 17:12:28 +00:00
parent 1906520b4a
commit da735dafd6
3 changed files with 42 additions and 33 deletions

View File

@ -4,4 +4,7 @@ go 1.23.2
require github.com/ghodss/yaml v1.0.0 require github.com/ghodss/yaml v1.0.0
require gopkg.in/yaml.v2 v2.4.0 // indirect require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@ -4,3 +4,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -12,7 +12,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/ghodss/yaml" "gopkg.in/yaml.v3"
) )
const rootRegistryPath = "../../../registry" const rootRegistryPath = "../../../registry"
@ -23,18 +23,18 @@ type directoryReadme struct {
} }
type rawContributorProfileFrontmatter struct { type rawContributorProfileFrontmatter struct {
DisplayName string `yaml:"display_name"` DisplayName string `yaml:"display_name"`
Bio string `yaml:"bio"` Bio string `yaml:"bio"`
GithubUsername string `yaml:"github"` GithubUsername string `yaml:"github"`
AvatarUrl *string `yaml:"avatar"` AvatarUrl *string `yaml:"avatar"`
LinkedinURL *string `yaml:"linkedin"` LinkedinURL *string `yaml:"linkedin"`
WebsiteURL *string `yaml:"website"` WebsiteURL *string `yaml:"website"`
SupportEmail *string `yaml:"support_email"` SupportEmail *string `yaml:"support_email"`
CompanyGithub *string `yaml:"company_github"` EmployerGithubUsername *string `yaml:"employer_github"`
ContributorStatus *string `yaml:"status"` ContributorStatus *string `yaml:"status"`
} }
type trackableContributorFrontmatter struct { type contributorFrontmatterWithFilepath struct {
rawContributorProfileFrontmatter rawContributorProfileFrontmatter
FilePath string FilePath string
} }
@ -97,7 +97,7 @@ func extractFrontmatter(readmeText string) (string, error) {
} }
if nextLine != fence { if nextLine != fence {
fm += nextLine fm += nextLine + "\n"
continue continue
} }
@ -113,7 +113,7 @@ func extractFrontmatter(readmeText string) (string, error) {
return fm, nil return fm, nil
} }
func validateContributorYaml(yml trackableContributorFrontmatter) []error { func validateContributorYaml(yml contributorFrontmatterWithFilepath) []error {
// This function needs to aggregate a bunch of different errors, rather than // This function needs to aggregate a bunch of different errors, rather than
// stopping at the first one found, so using code blocks to section off // stopping at the first one found, so using code blocks to section off
// logic for different fields // logic for different fields
@ -145,8 +145,8 @@ func validateContributorYaml(yml trackableContributorFrontmatter) []error {
} }
// Company GitHub // Company GitHub
if yml.CompanyGithub != nil { if yml.EmployerGithubUsername != nil {
if *yml.CompanyGithub == "" { if *yml.EmployerGithubUsername == "" {
errors = append( errors = append(
errors, errors,
fmt.Errorf( fmt.Errorf(
@ -156,19 +156,19 @@ func validateContributorYaml(yml trackableContributorFrontmatter) []error {
) )
} }
lower := strings.ToLower(*yml.CompanyGithub) lower := strings.ToLower(*yml.EmployerGithubUsername)
if uriSafe := url.PathEscape(lower); uriSafe != lower { if uriSafe := url.PathEscape(lower); uriSafe != lower {
errors = append( errors = append(
errors, errors,
fmt.Errorf( fmt.Errorf(
"gitHub company username %q (%q) is not a valid URL path segment", "gitHub company username %q (%q) is not a valid URL path segment",
*yml.CompanyGithub, *yml.EmployerGithubUsername,
yml.FilePath, yml.FilePath,
), ),
) )
} }
if *yml.CompanyGithub == yml.GithubUsername { if *yml.EmployerGithubUsername == yml.GithubUsername {
errors = append( errors = append(
errors, errors,
fmt.Errorf( fmt.Errorf(
@ -315,7 +315,7 @@ website:
} }
func remapContributorProfile( func remapContributorProfile(
frontmatter trackableContributorFrontmatter, frontmatter contributorFrontmatterWithFilepath,
employeeGitHubNames []string, employeeGitHubNames []string,
) contributorProfile { ) contributorProfile {
// Function assumes that fields are previously validated and are safe to // Function assumes that fields are previously validated and are safe to
@ -354,20 +354,20 @@ func remapContributorProfile(
return remapped return remapped
} }
func parseContributorFiles(input []directoryReadme) ( func parseContributorFiles(readmeEntries []directoryReadme) (
map[string]contributorProfile, map[string]contributorProfile,
error, error,
) { ) {
frontmatterByGithub := map[string]trackableContributorFrontmatter{} frontmatterByGithub := map[string]contributorFrontmatterWithFilepath{}
yamlParsingErrors := workflowPhaseError{ yamlParsingErrors := workflowPhaseError{
Phase: "YAML parsing", Phase: "YAML parsing",
} }
for _, dirReadme := range input { for _, rm := range readmeEntries {
fmText, err := extractFrontmatter(dirReadme.RawText) fmText, err := extractFrontmatter(rm.RawText)
if err != nil { if err != nil {
yamlParsingErrors.Errors = append( yamlParsingErrors.Errors = append(
yamlParsingErrors.Errors, yamlParsingErrors.Errors,
fmt.Errorf("failed to parse %q: %v", dirReadme.FilePath, err), fmt.Errorf("failed to parse %q: %v", rm.FilePath, err),
) )
continue continue
} }
@ -376,12 +376,13 @@ func parseContributorFiles(input []directoryReadme) (
if err := yaml.Unmarshal([]byte(fmText), &yml); err != nil { if err := yaml.Unmarshal([]byte(fmText), &yml); err != nil {
yamlParsingErrors.Errors = append( yamlParsingErrors.Errors = append(
yamlParsingErrors.Errors, yamlParsingErrors.Errors,
fmt.Errorf("failed to parse %q: %v", dirReadme.FilePath, err), fmt.Errorf("failed to parse %q: %v", rm.FilePath, err),
) )
continue continue
} }
trackable := trackableContributorFrontmatter{
FilePath: dirReadme.FilePath, trackable := contributorFrontmatterWithFilepath{
FilePath: rm.FilePath,
rawContributorProfileFrontmatter: yml, rawContributorProfileFrontmatter: yml,
} }
@ -391,8 +392,8 @@ func parseContributorFiles(input []directoryReadme) (
fmt.Errorf( fmt.Errorf(
"GitHub name conflict for %q for files %q and %q", "GitHub name conflict for %q for files %q and %q",
trackable.GithubUsername, trackable.GithubUsername,
trackable.FilePath,
prev.FilePath, prev.FilePath,
trackable.FilePath,
), ),
) )
continue continue
@ -400,6 +401,9 @@ func parseContributorFiles(input []directoryReadme) (
frontmatterByGithub[trackable.GithubUsername] = trackable frontmatterByGithub[trackable.GithubUsername] = trackable
} }
if len(yamlParsingErrors.Errors) != 0 {
return nil, yamlParsingErrors
}
employeeGithubGroups := map[string][]string{} employeeGithubGroups := map[string][]string{}
yamlValidationErrors := workflowPhaseError{ yamlValidationErrors := workflowPhaseError{
@ -415,9 +419,9 @@ func parseContributorFiles(input []directoryReadme) (
continue continue
} }
if yml.CompanyGithub != nil { if yml.EmployerGithubUsername != nil {
employeeGithubGroups[*yml.CompanyGithub] = append( employeeGithubGroups[*yml.EmployerGithubUsername] = append(
employeeGithubGroups[*yml.CompanyGithub], employeeGithubGroups[*yml.EmployerGithubUsername],
yml.GithubUsername, yml.GithubUsername,
) )
} }
@ -443,8 +447,8 @@ func parseContributorFiles(input []directoryReadme) (
contributorError.Errors, contributorError.Errors,
fmt.Errorf( fmt.Errorf(
"company %q does not exist in %q directory but is referenced by these profiles: [%s]", "company %q does not exist in %q directory but is referenced by these profiles: [%s]",
rootRegistryPath,
companyName, companyName,
rootRegistryPath,
strings.Join(group, ", "), strings.Join(group, ", "),
), ),
) )