chore: add logs for better feedback

This commit is contained in:
Michael Smith 2025-04-08 16:31:25 +00:00
parent 902b32fd29
commit 1906520b4a

View File

@ -15,7 +15,7 @@ import (
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
) )
const rootRegistryPath = "./registry" const rootRegistryPath = "../../../registry"
type directoryReadme struct { type directoryReadme struct {
FilePath string FilePath string
@ -70,7 +70,7 @@ type workflowPhaseError struct {
} }
func (wpe workflowPhaseError) Error() string { func (wpe workflowPhaseError) Error() string {
msg := fmt.Sprintf("Error during phase %q of README validation:", wpe.Phase) msg := fmt.Sprintf("Error during %q phase of README validation:", wpe.Phase)
for _, e := range wpe.Errors { for _, e := range wpe.Errors {
msg += fmt.Sprintf("\n- %v", e) msg += fmt.Sprintf("\n- %v", e)
} }
@ -456,59 +456,75 @@ func parseContributorFiles(input []directoryReadme) (
return structured, nil return structured, nil
} }
func backfillAvatarUrls(contributors map[string]contributorProfile) error { // backfillAvatarUrls takes a map of contributor information, each keyed by
// GitHub username, and tries to mutate each entry to fill in its missing avatar
// URL. The first integer indicates the number of avatars that needed to be
// backfilled, while the second indicates the number that could be backfilled
// without any errors.
//
// The function will collect all request errors, rather than return the first
// one found.
func backfillAvatarUrls(contributors map[string]contributorProfile) (int, int, error) {
if contributors == nil { if contributors == nil {
return errors.New("provided map is nil") return 0, 0, errors.New("provided map is nil")
} }
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
mtx := sync.Mutex{}
errors := []error{} errors := []error{}
errorsMutex := sync.Mutex{} successfulBackfills := 0
// Todo: Add actual fetching logic once everything else has been verified // Todo: Add actual fetching logic once everything else has been verified
requestAvatarUrl := func(string) (string, error) { requestAvatarUrl := func(string) (string, error) {
return "", nil return "", nil
} }
for ghUsername, conCopy := range contributors { avatarsThatNeedBackfill := 0
if conCopy.AvatarUrl != "" { for ghUsername, con := range contributors {
if con.AvatarUrl != "" {
continue continue
} }
avatarsThatNeedBackfill++
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
url, err := requestAvatarUrl(ghUsername) url, err := requestAvatarUrl(ghUsername)
mtx.Lock()
defer mtx.Unlock()
if err != nil { if err != nil {
errorsMutex.Lock()
errors = append(errors, err) errors = append(errors, err)
errorsMutex.Unlock()
return return
} }
conCopy.AvatarUrl = url
contributors[ghUsername] = conCopy successfulBackfills++
con.AvatarUrl = url
contributors[ghUsername] = con
}() }()
} }
wg.Wait() wg.Wait()
if len(errors) == 0 { if len(errors) == 0 {
return nil return avatarsThatNeedBackfill, successfulBackfills, nil
} }
slices.SortFunc(errors, func(e1 error, e2 error) int { slices.SortFunc(errors, func(e1 error, e2 error) int {
return strings.Compare(e1.Error(), e2.Error()) return strings.Compare(e1.Error(), e2.Error())
}) })
return workflowPhaseError{ return avatarsThatNeedBackfill, successfulBackfills, workflowPhaseError{
Phase: "Avatar Backfill", Phase: "Avatar Backfill",
Errors: errors, Errors: errors,
} }
} }
func main() { func main() {
log.Println("Starting README validation")
dirEntries, err := os.ReadDir(rootRegistryPath) dirEntries, err := os.ReadDir(rootRegistryPath)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
log.Printf("Identified %d top-level directory entries\n", len(dirEntries))
allReadmeFiles := []directoryReadme{} allReadmeFiles := []directoryReadme{}
fsErrors := workflowPhaseError{ fsErrors := workflowPhaseError{
@ -543,14 +559,29 @@ func main() {
log.Panic(fsErrors) log.Panic(fsErrors)
} }
log.Printf("Processing %d README files\n", len(allReadmeFiles))
contributors, err := parseContributorFiles(allReadmeFiles) contributors, err := parseContributorFiles(allReadmeFiles)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
err = backfillAvatarUrls(contributors) log.Printf(
"Processed %d README files as valid contributor profiles",
len(contributors),
)
backfillsNeeded, successCount, err := backfillAvatarUrls(contributors)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
if backfillsNeeded == 0 {
log.Println("No GitHub avatar backfills needed")
} else {
log.Printf(
"Backfilled %d/%d missing GitHub avatars",
backfillsNeeded,
successCount,
)
}
log.Printf( log.Printf(
"Processed all READMEs in the %q directory\n", "Processed all READMEs in the %q directory\n",