Skip to content

Commit ffd9861

Browse files
committed
refactor: extract pseudo-constants
1 parent 5c45642 commit ffd9861

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,6 @@ dist
134134
.yarn/build-state.yml
135135
.yarn/install-state.gz
136136
.pnp.*
137+
138+
# Script output
139+
/validate-contributor-readmes

scripts/validate-contributor-readmes/main.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ import (
2121

2222
const rootRegistryPath = "./registry"
2323

24+
var (
25+
validContributorStatuses = []string{"official", "partner", "community"}
26+
supportedAvatarFileFormats = []string{".png", ".jpeg", ".jpg", ".gif", ".svg"}
27+
)
28+
2429
type readme struct {
25-
FilePath string
26-
RawText string
30+
filePath string
31+
rawText string
2732
}
2833

2934
type contributorProfileFrontmatter struct {
@@ -48,18 +53,18 @@ type contributorFrontmatterWithFilePath struct {
4853
var _ error = validationPhaseError{}
4954

5055
type validationPhaseError struct {
51-
Phase string
52-
Errors []error
56+
phase string
57+
errors []error
5358
}
5459

5560
func (vpe validationPhaseError) Error() string {
5661
validationStrs := []string{}
57-
for _, e := range vpe.Errors {
62+
for _, e := range vpe.errors {
5863
validationStrs = append(validationStrs, fmt.Sprintf("- %v", e))
5964
}
6065
slices.Sort(validationStrs)
6166

62-
msg := fmt.Sprintf("Error during %q phase of README validation:", vpe.Phase)
67+
msg := fmt.Sprintf("Error during %q phase of README validation:", vpe.phase)
6368
msg += strings.Join(validationStrs, "\n")
6469
msg += "\n"
6570

@@ -193,7 +198,7 @@ func validateContributorSupportEmail(email *string) []error {
193198
problems = append(problems, fmt.Errorf("email address %q is missing top-level domain", *email))
194199
}
195200
if strings.Contains(*email, "?") {
196-
problems = append(problems, errors.New("email is not allowed to contain search parameters"))
201+
problems = append(problems, errors.New("email is not allowed to contain query parameters"))
197202
}
198203

199204
return problems
@@ -216,8 +221,7 @@ func validateContributorStatus(status *string) error {
216221
return nil
217222
}
218223

219-
validStatuses := []string{"official", "partner", "community"}
220-
if !slices.Contains(validStatuses, *status) {
224+
if !slices.Contains(validContributorStatuses, *status) {
221225
return fmt.Errorf("contributor status %q is not valid", *status)
222226
}
223227

@@ -246,9 +250,8 @@ func validateContributorAvatarURL(avatarURL *string) []error {
246250
problems = append(problems, errors.New("avatar URL is not allowed to contain search parameters"))
247251
}
248252

249-
supportedFileFormats := []string{".png", ".jpeg", ".jpg", ".gif", ".svg"}
250253
matched := false
251-
for _, ff := range supportedFileFormats {
254+
for _, ff := range supportedAvatarFileFormats {
252255
matched = strings.HasSuffix(*avatarURL, ff)
253256
if matched {
254257
break
@@ -257,7 +260,7 @@ func validateContributorAvatarURL(avatarURL *string) []error {
257260
if !matched {
258261
segments := strings.Split(*avatarURL, ".")
259262
fileExtension := segments[len(segments)-1]
260-
problems = append(problems, fmt.Errorf("avatar URL '.%s' does not end in a supported file format: [%s]", fileExtension, strings.Join(supportedFileFormats, ", ")))
263+
problems = append(problems, fmt.Errorf("avatar URL '.%s' does not end in a supported file format: [%s]", fileExtension, strings.Join(supportedAvatarFileFormats, ", ")))
261264
}
262265

263266
return problems
@@ -299,18 +302,18 @@ func validateContributorYaml(yml contributorFrontmatterWithFilePath) []error {
299302
}
300303

301304
func parseContributor(rm readme) (contributorFrontmatterWithFilePath, error) {
302-
fm, err := extractFrontmatter(rm.RawText)
305+
fm, err := extractFrontmatter(rm.rawText)
303306
if err != nil {
304-
return contributorFrontmatterWithFilePath{}, fmt.Errorf("%q: failed to parse frontmatter: %v", rm.FilePath, err)
307+
return contributorFrontmatterWithFilePath{}, fmt.Errorf("%q: failed to parse frontmatter: %v", rm.filePath, err)
305308
}
306309

307310
yml := contributorProfileFrontmatter{}
308311
if err := yaml.Unmarshal([]byte(fm), &yml); err != nil {
309-
return contributorFrontmatterWithFilePath{}, fmt.Errorf("%q: failed to parse: %v", rm.FilePath, err)
312+
return contributorFrontmatterWithFilePath{}, fmt.Errorf("%q: failed to parse: %v", rm.filePath, err)
310313
}
311314

312315
return contributorFrontmatterWithFilePath{
313-
FilePath: rm.FilePath,
316+
FilePath: rm.filePath,
314317
contributorProfileFrontmatter: yml,
315318
}, nil
316319
}
@@ -336,8 +339,8 @@ func parseContributorFiles(readmeEntries []readme) (
336339
}
337340
if len(yamlParsingErrors) != 0 {
338341
return nil, validationPhaseError{
339-
Phase: "YAML parsing",
340-
Errors: yamlParsingErrors,
342+
phase: "YAML parsing",
343+
errors: yamlParsingErrors,
341344
}
342345
}
343346

@@ -365,8 +368,8 @@ func parseContributorFiles(readmeEntries []readme) (
365368
}
366369
if len(yamlValidationErrors) != 0 {
367370
return nil, validationPhaseError{
368-
Phase: "Raw YAML Validation",
369-
Errors: yamlValidationErrors,
371+
phase: "Raw YAML Validation",
372+
errors: yamlValidationErrors,
370373
}
371374
}
372375

@@ -395,15 +398,15 @@ func aggregateContributorReadmeFiles() ([]readme, error) {
395398
continue
396399
}
397400
allReadmeFiles = append(allReadmeFiles, readme{
398-
FilePath: readmePath,
399-
RawText: string(rmBytes),
401+
filePath: readmePath,
402+
rawText: string(rmBytes),
400403
})
401404
}
402405

403406
if len(problems) != 0 {
404407
return nil, validationPhaseError{
405-
Phase: "FileSystem reading",
406-
Errors: problems,
408+
phase: "FileSystem reading",
409+
errors: problems,
407410
}
408411
}
409412

@@ -445,8 +448,8 @@ func validateRelativeUrls(
445448
return nil
446449
}
447450
return validationPhaseError{
448-
Phase: "Relative URL validation",
449-
Errors: problems,
451+
phase: "Relative URL validation",
452+
errors: problems,
450453
}
451454
}
452455

0 commit comments

Comments
 (0)