refactor: extract pseudo-constants

This commit is contained in:
Michael Smith
2025-04-14 13:37:17 +00:00
parent 5c45642e4b
commit ffd9861e03
2 changed files with 32 additions and 26 deletions
+3
View File
@@ -134,3 +134,6 @@ dist
.yarn/build-state.yml .yarn/build-state.yml
.yarn/install-state.gz .yarn/install-state.gz
.pnp.* .pnp.*
# Script output
/validate-contributor-readmes
+29 -26
View File
@@ -21,9 +21,14 @@ import (
const rootRegistryPath = "./registry" const rootRegistryPath = "./registry"
var (
validContributorStatuses = []string{"official", "partner", "community"}
supportedAvatarFileFormats = []string{".png", ".jpeg", ".jpg", ".gif", ".svg"}
)
type readme struct { type readme struct {
FilePath string filePath string
RawText string rawText string
} }
type contributorProfileFrontmatter struct { type contributorProfileFrontmatter struct {
@@ -48,18 +53,18 @@ type contributorFrontmatterWithFilePath struct {
var _ error = validationPhaseError{} var _ error = validationPhaseError{}
type validationPhaseError struct { type validationPhaseError struct {
Phase string phase string
Errors []error errors []error
} }
func (vpe validationPhaseError) Error() string { func (vpe validationPhaseError) Error() string {
validationStrs := []string{} validationStrs := []string{}
for _, e := range vpe.Errors { for _, e := range vpe.errors {
validationStrs = append(validationStrs, fmt.Sprintf("- %v", e)) validationStrs = append(validationStrs, fmt.Sprintf("- %v", e))
} }
slices.Sort(validationStrs) slices.Sort(validationStrs)
msg := fmt.Sprintf("Error during %q phase of README validation:", vpe.Phase) msg := fmt.Sprintf("Error during %q phase of README validation:", vpe.phase)
msg += strings.Join(validationStrs, "\n") msg += strings.Join(validationStrs, "\n")
msg += "\n" msg += "\n"
@@ -193,7 +198,7 @@ func validateContributorSupportEmail(email *string) []error {
problems = append(problems, fmt.Errorf("email address %q is missing top-level domain", *email)) problems = append(problems, fmt.Errorf("email address %q is missing top-level domain", *email))
} }
if strings.Contains(*email, "?") { if strings.Contains(*email, "?") {
problems = append(problems, errors.New("email is not allowed to contain search parameters")) problems = append(problems, errors.New("email is not allowed to contain query parameters"))
} }
return problems return problems
@@ -216,8 +221,7 @@ func validateContributorStatus(status *string) error {
return nil return nil
} }
validStatuses := []string{"official", "partner", "community"} if !slices.Contains(validContributorStatuses, *status) {
if !slices.Contains(validStatuses, *status) {
return fmt.Errorf("contributor status %q is not valid", *status) return fmt.Errorf("contributor status %q is not valid", *status)
} }
@@ -246,9 +250,8 @@ func validateContributorAvatarURL(avatarURL *string) []error {
problems = append(problems, errors.New("avatar URL is not allowed to contain search parameters")) problems = append(problems, errors.New("avatar URL is not allowed to contain search parameters"))
} }
supportedFileFormats := []string{".png", ".jpeg", ".jpg", ".gif", ".svg"}
matched := false matched := false
for _, ff := range supportedFileFormats { for _, ff := range supportedAvatarFileFormats {
matched = strings.HasSuffix(*avatarURL, ff) matched = strings.HasSuffix(*avatarURL, ff)
if matched { if matched {
break break
@@ -257,7 +260,7 @@ func validateContributorAvatarURL(avatarURL *string) []error {
if !matched { if !matched {
segments := strings.Split(*avatarURL, ".") segments := strings.Split(*avatarURL, ".")
fileExtension := segments[len(segments)-1] fileExtension := segments[len(segments)-1]
problems = append(problems, fmt.Errorf("avatar URL '.%s' does not end in a supported file format: [%s]", fileExtension, strings.Join(supportedFileFormats, ", "))) problems = append(problems, fmt.Errorf("avatar URL '.%s' does not end in a supported file format: [%s]", fileExtension, strings.Join(supportedAvatarFileFormats, ", ")))
} }
return problems return problems
@@ -299,18 +302,18 @@ func validateContributorYaml(yml contributorFrontmatterWithFilePath) []error {
} }
func parseContributor(rm readme) (contributorFrontmatterWithFilePath, error) { func parseContributor(rm readme) (contributorFrontmatterWithFilePath, error) {
fm, err := extractFrontmatter(rm.RawText) fm, err := extractFrontmatter(rm.rawText)
if err != nil { if err != nil {
return contributorFrontmatterWithFilePath{}, fmt.Errorf("%q: failed to parse frontmatter: %v", rm.FilePath, err) return contributorFrontmatterWithFilePath{}, fmt.Errorf("%q: failed to parse frontmatter: %v", rm.filePath, err)
} }
yml := contributorProfileFrontmatter{} yml := contributorProfileFrontmatter{}
if err := yaml.Unmarshal([]byte(fm), &yml); err != nil { if err := yaml.Unmarshal([]byte(fm), &yml); err != nil {
return contributorFrontmatterWithFilePath{}, fmt.Errorf("%q: failed to parse: %v", rm.FilePath, err) return contributorFrontmatterWithFilePath{}, fmt.Errorf("%q: failed to parse: %v", rm.filePath, err)
} }
return contributorFrontmatterWithFilePath{ return contributorFrontmatterWithFilePath{
FilePath: rm.FilePath, FilePath: rm.filePath,
contributorProfileFrontmatter: yml, contributorProfileFrontmatter: yml,
}, nil }, nil
} }
@@ -336,8 +339,8 @@ func parseContributorFiles(readmeEntries []readme) (
} }
if len(yamlParsingErrors) != 0 { if len(yamlParsingErrors) != 0 {
return nil, validationPhaseError{ return nil, validationPhaseError{
Phase: "YAML parsing", phase: "YAML parsing",
Errors: yamlParsingErrors, errors: yamlParsingErrors,
} }
} }
@@ -365,8 +368,8 @@ func parseContributorFiles(readmeEntries []readme) (
} }
if len(yamlValidationErrors) != 0 { if len(yamlValidationErrors) != 0 {
return nil, validationPhaseError{ return nil, validationPhaseError{
Phase: "Raw YAML Validation", phase: "Raw YAML Validation",
Errors: yamlValidationErrors, errors: yamlValidationErrors,
} }
} }
@@ -395,15 +398,15 @@ func aggregateContributorReadmeFiles() ([]readme, error) {
continue continue
} }
allReadmeFiles = append(allReadmeFiles, readme{ allReadmeFiles = append(allReadmeFiles, readme{
FilePath: readmePath, filePath: readmePath,
RawText: string(rmBytes), rawText: string(rmBytes),
}) })
} }
if len(problems) != 0 { if len(problems) != 0 {
return nil, validationPhaseError{ return nil, validationPhaseError{
Phase: "FileSystem reading", phase: "FileSystem reading",
Errors: problems, errors: problems,
} }
} }
@@ -445,8 +448,8 @@ func validateRelativeUrls(
return nil return nil
} }
return validationPhaseError{ return validationPhaseError{
Phase: "Relative URL validation", phase: "Relative URL validation",
Errors: problems, errors: problems,
} }
} }