Skip to content

Commit 5c45642

Browse files
committed
refactor: split off another function
1 parent 96fa5d4 commit 5c45642

File tree

1 file changed

+36
-37
lines changed
  • scripts/validate-contributor-readmes

1 file changed

+36
-37
lines changed

scripts/validate-contributor-readmes/main.go

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -298,59 +298,55 @@ func validateContributorYaml(yml contributorFrontmatterWithFilePath) []error {
298298
return allProblems
299299
}
300300

301+
func parseContributor(rm readme) (contributorFrontmatterWithFilePath, error) {
302+
fm, err := extractFrontmatter(rm.RawText)
303+
if err != nil {
304+
return contributorFrontmatterWithFilePath{}, fmt.Errorf("%q: failed to parse frontmatter: %v", rm.FilePath, err)
305+
}
306+
307+
yml := contributorProfileFrontmatter{}
308+
if err := yaml.Unmarshal([]byte(fm), &yml); err != nil {
309+
return contributorFrontmatterWithFilePath{}, fmt.Errorf("%q: failed to parse: %v", rm.FilePath, err)
310+
}
311+
312+
return contributorFrontmatterWithFilePath{
313+
FilePath: rm.FilePath,
314+
contributorProfileFrontmatter: yml,
315+
}, nil
316+
}
317+
301318
func parseContributorFiles(readmeEntries []readme) (
302319
map[string]contributorFrontmatterWithFilePath,
303320
error,
304321
) {
305322
frontmatterByUsername := map[string]contributorFrontmatterWithFilePath{}
306-
yamlParsingErrors := validationPhaseError{
307-
Phase: "YAML parsing",
308-
}
323+
yamlParsingErrors := []error{}
309324
for _, rm := range readmeEntries {
310-
fm, err := extractFrontmatter(rm.RawText)
325+
fm, err := parseContributor(rm)
311326
if err != nil {
312-
yamlParsingErrors.Errors = append(
313-
yamlParsingErrors.Errors,
314-
fmt.Errorf("%q: failed to parse: %v", rm.FilePath, err),
315-
)
327+
yamlParsingErrors = append(yamlParsingErrors, err)
316328
continue
317329
}
318330

319-
yml := contributorProfileFrontmatter{}
320-
if err := yaml.Unmarshal([]byte(fm), &yml); err != nil {
321-
yamlParsingErrors.Errors = append(
322-
yamlParsingErrors.Errors,
323-
fmt.Errorf("%q: failed to parse: %v", rm.FilePath, err),
324-
)
331+
if prev, isConflict := frontmatterByUsername[fm.GithubUsername]; isConflict {
332+
yamlParsingErrors = append(yamlParsingErrors, fmt.Errorf("%q: GitHub name %s conflicts with field defined in %q", fm.FilePath, fm.GithubUsername, prev.FilePath))
325333
continue
326334
}
327-
processed := contributorFrontmatterWithFilePath{
328-
FilePath: rm.FilePath,
329-
contributorProfileFrontmatter: yml,
330-
}
331-
332-
if prev, isConflict := frontmatterByUsername[processed.GithubUsername]; isConflict {
333-
yamlParsingErrors.Errors = append(yamlParsingErrors.Errors, fmt.Errorf("%q: GitHub name %s conflicts with field defined in %q", processed.FilePath, processed.GithubUsername, prev.FilePath))
334-
continue
335-
}
336-
337-
frontmatterByUsername[processed.GithubUsername] = processed
335+
frontmatterByUsername[fm.GithubUsername] = fm
338336
}
339-
if len(yamlParsingErrors.Errors) != 0 {
340-
return nil, yamlParsingErrors
337+
if len(yamlParsingErrors) != 0 {
338+
return nil, validationPhaseError{
339+
Phase: "YAML parsing",
340+
Errors: yamlParsingErrors,
341+
}
341342
}
342343

343344
employeeGithubGroups := map[string][]string{}
344-
yamlValidationErrors := validationPhaseError{
345-
Phase: "Raw YAML Validation",
346-
}
345+
yamlValidationErrors := []error{}
347346
for _, yml := range frontmatterByUsername {
348347
errors := validateContributorYaml(yml)
349348
if len(errors) > 0 {
350-
yamlValidationErrors.Errors = append(
351-
yamlValidationErrors.Errors,
352-
errors...,
353-
)
349+
yamlValidationErrors = append(yamlValidationErrors, errors...)
354350
continue
355351
}
356352

@@ -365,10 +361,13 @@ func parseContributorFiles(readmeEntries []readme) (
365361
if _, found := frontmatterByUsername[companyName]; found {
366362
continue
367363
}
368-
yamlValidationErrors.Errors = append(yamlValidationErrors.Errors, fmt.Errorf("company %q does not exist in %q directory but is referenced by these profiles: [%s]", companyName, rootRegistryPath, strings.Join(group, ", ")))
364+
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, ", ")))
369365
}
370-
if len(yamlValidationErrors.Errors) != 0 {
371-
return nil, yamlValidationErrors
366+
if len(yamlValidationErrors) != 0 {
367+
return nil, validationPhaseError{
368+
Phase: "Raw YAML Validation",
369+
Errors: yamlValidationErrors,
370+
}
372371
}
373372

374373
return frontmatterByUsername, nil

0 commit comments

Comments
 (0)