Skip to content

Commit 135d5c8

Browse files
committed
fix: remove potential race condition
1 parent f23bbca commit 135d5c8

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

cmd/readmevalidation/contributors.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,24 +373,23 @@ func validateContributorRelativeUrls(contributors map[string]contributorProfile)
373373
}
374374
}
375375

376-
func validateAllContributors(errChan chan<- error) {
376+
func validateAllContributors() error {
377377
allReadmeFiles, err := aggregateContributorReadmeFiles()
378378
if err != nil {
379-
errChan <- err
380-
return
379+
return err
381380
}
382381
log.Printf("Processing %d README files\n", len(allReadmeFiles))
383382
contributors, err := parseContributorFiles(allReadmeFiles)
384383
log.Printf("Processed %d README files as valid contributor profiles", len(contributors))
385384
if err != nil {
386-
errChan <- err
387-
return
385+
return err
388386
}
389387
err = validateContributorRelativeUrls(contributors)
390388
if err != nil {
391-
errChan <- err
392-
return
389+
return err
393390
}
394391
log.Println("All relative URLs for READMEs are valid")
395392
log.Printf("Processed all READMEs in the %q directory\n", rootRegistryPath)
393+
394+
return nil
396395
}

cmd/readmevalidation/main.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"coder.com/coder-registry/cmd/github"
1616
"github.com/go-git/go-git/v5"
17+
"github.com/go-git/go-git/v5/plumbing"
1718
"github.com/joho/godotenv"
1819
)
1920

@@ -66,7 +67,10 @@ func main() {
6667
// Start main validation
6768
log.Println("Starting README validation")
6869

69-
// Validate file structure of main README directory
70+
// Validate file structure of main README directory. Have to do this
71+
// synchronously and before everything else, or else there's no way to for
72+
// the other main validation functions can't make any safe assumptions
73+
// about where they should look in the repo
7074
log.Println("Validating directory structure of the README directory")
7175
err = validateRepoStructure()
7276
if err != nil {
@@ -76,18 +80,22 @@ func main() {
7680
// Set up concurrency for validating each category of README file
7781
var readmeValidationErrors []error
7882
errChan := make(chan error, 1)
83+
doneChan := make(chan struct{})
7984
wg := sync.WaitGroup{}
8085
go func() {
8186
for err := range errChan {
8287
readmeValidationErrors = append(readmeValidationErrors, err)
8388
}
89+
close(doneChan)
8490
}()
8591

8692
// Validate contributor README files
8793
wg.Add(1)
8894
go func() {
8995
defer wg.Done()
90-
validateAllContributors(errChan)
96+
if err := validateAllContributors(); err != nil {
97+
errChan <- fmt.Errorf("contributor validation: %v", err)
98+
}
9199
}()
92100

93101
// Validate modules
@@ -117,7 +125,11 @@ func main() {
117125
return
118126
}
119127
activeBranchName := head.Name().Short()
120-
fmt.Println("-----", activeBranchName)
128+
_, err = repo.Reference(plumbing.ReferenceName(activeBranchName), true)
129+
if err != nil {
130+
errChan <- err
131+
return
132+
}
121133
}()
122134

123135
// Validate templates
@@ -126,9 +138,10 @@ func main() {
126138
defer wg.Done()
127139
}()
128140

129-
// Clean up and log errors
141+
// Clean up and then log errors
130142
wg.Wait()
131143
close(errChan)
144+
<-doneChan
132145
for _, err := range readmeValidationErrors {
133146
log.Println(err)
134147
}

0 commit comments

Comments
 (0)