fix: remove potential race condition

This commit is contained in:
Michael Smith 2025-04-16 01:44:21 +00:00
parent f23bbca2e7
commit 135d5c8111
2 changed files with 23 additions and 11 deletions

View File

@ -373,24 +373,23 @@ func validateContributorRelativeUrls(contributors map[string]contributorProfile)
} }
} }
func validateAllContributors(errChan chan<- error) { func validateAllContributors() error {
allReadmeFiles, err := aggregateContributorReadmeFiles() allReadmeFiles, err := aggregateContributorReadmeFiles()
if err != nil { if err != nil {
errChan <- err return err
return
} }
log.Printf("Processing %d README files\n", len(allReadmeFiles)) log.Printf("Processing %d README files\n", len(allReadmeFiles))
contributors, err := parseContributorFiles(allReadmeFiles) contributors, err := parseContributorFiles(allReadmeFiles)
log.Printf("Processed %d README files as valid contributor profiles", len(contributors)) log.Printf("Processed %d README files as valid contributor profiles", len(contributors))
if err != nil { if err != nil {
errChan <- err return err
return
} }
err = validateContributorRelativeUrls(contributors) err = validateContributorRelativeUrls(contributors)
if err != nil { if err != nil {
errChan <- err return err
return
} }
log.Println("All relative URLs for READMEs are valid") log.Println("All relative URLs for READMEs are valid")
log.Printf("Processed all READMEs in the %q directory\n", rootRegistryPath) log.Printf("Processed all READMEs in the %q directory\n", rootRegistryPath)
return nil
} }

View File

@ -14,6 +14,7 @@ import (
"coder.com/coder-registry/cmd/github" "coder.com/coder-registry/cmd/github"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
@ -66,7 +67,10 @@ func main() {
// Start main validation // Start main validation
log.Println("Starting README 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") log.Println("Validating directory structure of the README directory")
err = validateRepoStructure() err = validateRepoStructure()
if err != nil { if err != nil {
@ -76,18 +80,22 @@ func main() {
// Set up concurrency for validating each category of README file // Set up concurrency for validating each category of README file
var readmeValidationErrors []error var readmeValidationErrors []error
errChan := make(chan error, 1) errChan := make(chan error, 1)
doneChan := make(chan struct{})
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
go func() { go func() {
for err := range errChan { for err := range errChan {
readmeValidationErrors = append(readmeValidationErrors, err) readmeValidationErrors = append(readmeValidationErrors, err)
} }
close(doneChan)
}() }()
// Validate contributor README files // Validate contributor README files
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
validateAllContributors(errChan) if err := validateAllContributors(); err != nil {
errChan <- fmt.Errorf("contributor validation: %v", err)
}
}() }()
// Validate modules // Validate modules
@ -117,7 +125,11 @@ func main() {
return return
} }
activeBranchName := head.Name().Short() activeBranchName := head.Name().Short()
fmt.Println("-----", activeBranchName) _, err = repo.Reference(plumbing.ReferenceName(activeBranchName), true)
if err != nil {
errChan <- err
return
}
}() }()
// Validate templates // Validate templates
@ -126,9 +138,10 @@ func main() {
defer wg.Done() defer wg.Done()
}() }()
// Clean up and log errors // Clean up and then log errors
wg.Wait() wg.Wait()
close(errChan) close(errChan)
<-doneChan
for _, err := range readmeValidationErrors { for _, err := range readmeValidationErrors {
log.Println(err) log.Println(err)
} }