From 135d5c811155a538447790c0392751c40db27b6d Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Wed, 16 Apr 2025 01:44:21 +0000 Subject: [PATCH] fix: remove potential race condition --- cmd/readmevalidation/contributors.go | 13 ++++++------- cmd/readmevalidation/main.go | 21 +++++++++++++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/cmd/readmevalidation/contributors.go b/cmd/readmevalidation/contributors.go index f8447c5b..aa3676db 100644 --- a/cmd/readmevalidation/contributors.go +++ b/cmd/readmevalidation/contributors.go @@ -373,24 +373,23 @@ func validateContributorRelativeUrls(contributors map[string]contributorProfile) } } -func validateAllContributors(errChan chan<- error) { +func validateAllContributors() error { allReadmeFiles, err := aggregateContributorReadmeFiles() if err != nil { - errChan <- err - return + return err } log.Printf("Processing %d README files\n", len(allReadmeFiles)) contributors, err := parseContributorFiles(allReadmeFiles) log.Printf("Processed %d README files as valid contributor profiles", len(contributors)) if err != nil { - errChan <- err - return + return err } err = validateContributorRelativeUrls(contributors) if err != nil { - errChan <- err - return + return err } log.Println("All relative URLs for READMEs are valid") log.Printf("Processed all READMEs in the %q directory\n", rootRegistryPath) + + return nil } diff --git a/cmd/readmevalidation/main.go b/cmd/readmevalidation/main.go index 1471a654..b4f108d2 100644 --- a/cmd/readmevalidation/main.go +++ b/cmd/readmevalidation/main.go @@ -14,6 +14,7 @@ import ( "coder.com/coder-registry/cmd/github" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "github.com/joho/godotenv" ) @@ -66,7 +67,10 @@ func main() { // Start main validation log.Println("Starting README validation") - // Validate file structure of main README directory + // Validate file structure of main README directory. Have to do this + // synchronously and before everything else, or else there's no way to for + // the other main validation functions can't make any safe assumptions + // about where they should look in the repo log.Println("Validating directory structure of the README directory") err = validateRepoStructure() if err != nil { @@ -76,18 +80,22 @@ func main() { // Set up concurrency for validating each category of README file var readmeValidationErrors []error errChan := make(chan error, 1) + doneChan := make(chan struct{}) wg := sync.WaitGroup{} go func() { for err := range errChan { readmeValidationErrors = append(readmeValidationErrors, err) } + close(doneChan) }() // Validate contributor README files wg.Add(1) go func() { defer wg.Done() - validateAllContributors(errChan) + if err := validateAllContributors(); err != nil { + errChan <- fmt.Errorf("contributor validation: %v", err) + } }() // Validate modules @@ -117,7 +125,11 @@ func main() { return } activeBranchName := head.Name().Short() - fmt.Println("-----", activeBranchName) + _, err = repo.Reference(plumbing.ReferenceName(activeBranchName), true) + if err != nil { + errChan <- err + return + } }() // Validate templates @@ -126,9 +138,10 @@ func main() { defer wg.Done() }() - // Clean up and log errors + // Clean up and then log errors wg.Wait() close(errChan) + <-doneChan for _, err := range readmeValidationErrors { log.Println(err) }