wip: scaffold relative URL validation

This commit is contained in:
Michael Smith 2025-04-08 20:02:52 +00:00
parent 23f1cee640
commit e20e679e08
2 changed files with 59 additions and 13 deletions

View File

@ -63,6 +63,7 @@ func (status contributorProfileStatus) String() string {
type contributorProfile struct { type contributorProfile struct {
EmployeeGithubUsernames []string EmployeeGithubUsernames []string
GithubUsername string GithubUsername string
FilePath string
DisplayName string DisplayName string
Bio string Bio string
AvatarUrl *string AvatarUrl *string
@ -406,6 +407,7 @@ func remapContributorProfile(
// copy over verbatim when appropriate, and (2) any missing avatar URLs will // copy over verbatim when appropriate, and (2) any missing avatar URLs will
// be backfilled during the main Registry site build step // be backfilled during the main Registry site build step
remapped := contributorProfile{ remapped := contributorProfile{
FilePath: frontmatter.FilePath,
DisplayName: frontmatter.DisplayName, DisplayName: frontmatter.DisplayName,
GithubUsername: frontmatter.GithubUsername, GithubUsername: frontmatter.GithubUsername,
Bio: frontmatter.Bio, Bio: frontmatter.Bio,
@ -544,24 +546,19 @@ func parseContributorFiles(readmeEntries []directoryReadme) (
return structured, nil return structured, nil
} }
func main() { func aggregateReadmeFiles() ([]directoryReadme, error) {
log.Println("Starting README validation")
dirEntries, err := os.ReadDir(rootRegistryPath) dirEntries, err := os.ReadDir(rootRegistryPath)
if err != nil { if err != nil {
log.Panic(err) return nil, err
} }
log.Printf("Identified %d top-level directory entries\n", len(dirEntries))
allReadmeFiles := []directoryReadme{} allReadmeFiles := []directoryReadme{}
fsErrors := workflowPhaseError{ problems := []error{}
Phase: "FileSystem reading",
Errors: []error{},
}
for _, e := range dirEntries { for _, e := range dirEntries {
dirPath := path.Join(rootRegistryPath, e.Name()) dirPath := path.Join(rootRegistryPath, e.Name())
if !e.IsDir() { if !e.IsDir() {
fsErrors.Errors = append( problems = append(
fsErrors.Errors, problems,
fmt.Errorf( fmt.Errorf(
"Detected non-directory file %q at base of main Registry directory", "Detected non-directory file %q at base of main Registry directory",
dirPath, dirPath,
@ -573,7 +570,7 @@ func main() {
readmePath := path.Join(dirPath, "README.md") readmePath := path.Join(dirPath, "README.md")
rmBytes, err := os.ReadFile(readmePath) rmBytes, err := os.ReadFile(readmePath)
if err != nil { if err != nil {
fsErrors.Errors = append(fsErrors.Errors, err) problems = append(problems, err)
continue continue
} }
allReadmeFiles = append(allReadmeFiles, directoryReadme{ allReadmeFiles = append(allReadmeFiles, directoryReadme{
@ -581,8 +578,51 @@ func main() {
RawText: string(rmBytes), RawText: string(rmBytes),
}) })
} }
if len(fsErrors.Errors) != 0 {
log.Panic(fsErrors) if len(problems) != 0 {
return nil, workflowPhaseError{
Phase: "FileSystem reading",
Errors: problems,
}
}
return allReadmeFiles, nil
}
func validateRelativeUrls(
contributors map[string]contributorProfile,
) error {
// This function only validates relative avatar URLs for now, but it can be
// beefed up to validate more in the future
problems := []error{}
for _, con := range contributors {
if con.AvatarUrl == nil {
continue
}
isRelativeUrl := strings.HasPrefix(*con.AvatarUrl, ".") ||
strings.HasPrefix(*con.AvatarUrl, "/")
if !isRelativeUrl {
continue
}
fmt.Println(con.GithubUsername, con.AvatarUrl)
}
if len(problems) == 0 {
return nil
}
return workflowPhaseError{
Phase: "Relative URL validation",
Errors: problems,
}
}
func main() {
log.Println("Starting README validation")
allReadmeFiles, err := aggregateReadmeFiles()
if err != nil {
panic(err)
} }
log.Printf("Processing %d README files\n", len(allReadmeFiles)) log.Printf("Processing %d README files\n", len(allReadmeFiles))
@ -595,6 +635,12 @@ func main() {
len(contributors), len(contributors),
) )
err = validateRelativeUrls(contributors)
if err != nil {
log.Panic(err)
}
log.Println("all relative URLs for READMEs are valid")
log.Printf( log.Printf(
"Processed all READMEs in the %q directory\n", "Processed all READMEs in the %q directory\n",
rootRegistryPath, rootRegistryPath,