Skip to content

Commit

Permalink
fix: Updated nrversions to properly clone repos concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 committed May 6, 2024
1 parent 8e8f9f7 commit 236cc51
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ func run(args []string) error {

repoChan := make(chan repoIterChan)
cloneWg := &sync.WaitGroup{}
go cloneRepos(repos, repoChan, cloneWg)
cloneRepos(repos, repoChan, cloneWg)
go func() {
cloneWg.Wait()
close(repoChan)
}()

testDirs := make([]string, 0)
data := make([]ReleaseData, 0)
Expand All @@ -74,9 +78,9 @@ func run(args []string) error {
var repoDir = repo.repoDir
var testDir = repo.testPath
versionedTestsDir := filepath.Join(repoDir, testDir)
fmt.Println("Adding test dir")
testDirs = append(testDirs, versionedTestsDir)
}
cloneWg.Wait()

wg := sync.WaitGroup{}
logger.Debug("Processing data ...")
Expand Down Expand Up @@ -266,48 +270,45 @@ func readPackageJson(pkgJsonFile *os.File) (*VersionedTestPackageJson, error) {
func cloneRepos(repos []nrRepo, repoChan chan repoIterChan, wg *sync.WaitGroup) {
for _, repo := range repos {
wg.Add(1)
if repo.repoDir != "" {
repoChan <- repoIterChan{
repoDir: repo.repoDir,
testPath: repo.testPath,
}
continue
}

repoDir, err := cloneRepo(repo.url, repo.branch, wg)
if err != nil {
repoChan <- repoIterChan{
err: fmt.Errorf("failed to clone repo `%s`: %w", repo.url, err),
}
}
go cloneRepo(repo, wg, repoChan)
}
}

func cloneRepo(repo nrRepo, wg *sync.WaitGroup, repoChan chan repoIterChan) {
defer wg.Done()
if repo.repoDir != "" {
repoChan <- repoIterChan{
repoDir: repoDir,
repoDir: repo.repoDir,
testPath: repo.testPath,
}
return
}

close(repoChan)
}

func cloneRepo(repo string, branch string, wg *sync.WaitGroup) (string, error) {
defer wg.Done()
repoDir, err := os.MkdirTemp("", "newrelic")
if err != nil {
return "", fmt.Errorf("failed to create temporary directory: %w", err)
repoChan <- repoIterChan{
err: fmt.Errorf("failed to create temporary directory: %w", err),
}
return
}

fmt.Println("Cloning repository ...")
_, err = git.PlainClone(repoDir, false, &git.CloneOptions{
URL: repo,
ReferenceName: plumbing.ReferenceName(branch),
URL: repo.url,
ReferenceName: plumbing.ReferenceName(repo.branch),
Depth: 1,
})
if err != nil {
return "", fmt.Errorf("failed to clone newrelic repo: %w", err)
repoChan <- repoIterChan{
err: fmt.Errorf("failed to clone repo `%s`: %w", repo.url, err),
}
return
}

return repoDir, nil
repoChan <- repoIterChan{
repoDir: repoDir,
testPath: repo.testPath,
}
}

// pruneData removes duplicate entries from the data set. A duplicate entry
Expand Down

0 comments on commit 236cc51

Please sign in to comment.