Skip to content

Commit 0ba90d8

Browse files
committed
fix: wait in parallel
but don't wait at all if PR shouldn't merge automatically Signed-off-by: Mårten Svantesson <[email protected]>
1 parent 0d6e3e7 commit 0ba90d8

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

pkg/promote/promote.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"strconv"
1010
"strings"
11+
"sync"
1112
"time"
1213

1314
"github.com/jenkins-x/jx-helpers/v3/pkg/requirements"
@@ -575,6 +576,8 @@ func (o *Options) PromoteAll(pred func(*jxcore.EnvironmentConfig) bool) error {
575576
}
576577
groups = append(groups, []*jxcore.EnvironmentConfig{env})
577578
}
579+
var errorChannel = make(chan error, len(groups))
580+
var wg sync.WaitGroup
578581
for _, group := range groups {
579582
firstEnv := group[0]
580583

@@ -585,14 +588,24 @@ func (o *Options) PromoteAll(pred func(*jxcore.EnvironmentConfig) bool) error {
585588
return err
586589
}
587590
o.ReleaseInfo = releaseInfo
588-
if !o.NoPoll {
589-
err = o.WaitForPromotion(firstEnv, releaseInfo)
590-
if err != nil {
591-
return err
592-
}
591+
// TODO: Add test
592+
if !o.NoPoll && group[0].PromotionStrategy == v1.PromotionStrategyTypeAutomatic {
593+
wg.Add(1)
594+
// Wait for multiple PRs in parallel
595+
go o.WaitForPromotion(firstEnv, releaseInfo, errorChannel, &wg)
593596
}
594597
}
595-
return nil
598+
wg.Wait()
599+
close(errorChannel)
600+
var err error
601+
for err2 := range errorChannel {
602+
if err == nil {
603+
err = err2
604+
} else {
605+
err = fmt.Errorf("%w; %v", err, err2)
606+
}
607+
}
608+
return err
596609
}
597610

598611
// EnvironmentNamespace returns the namespace for the environment
@@ -833,14 +846,15 @@ func (o *Options) GetTargetNamespace(ns, env string) (string, *jxcore.Environmen
833846
return targetNS, envResource, nil
834847
}
835848

836-
func (o *Options) WaitForPromotion(env *jxcore.EnvironmentConfig, releaseInfo *ReleaseInfo) error {
849+
func (o *Options) WaitForPromotion(env *jxcore.EnvironmentConfig, releaseInfo *ReleaseInfo, errorChannel chan<- error, wg *sync.WaitGroup) {
850+
defer wg.Done()
837851
if o.TimeoutDuration == nil {
838852
log.Logger().Infof("No --%s option specified on the 'jx promote' command so not waiting for the promotion to succeed", optionTimeout)
839-
return nil
853+
return
840854
}
841855
if o.PullRequestPollDuration == nil {
842856
log.Logger().Infof("No --%s option specified on the 'jx promote' command so not waiting for the promotion to succeed", optionPullRequestPollTime)
843-
return nil
857+
return
844858
}
845859
duration := *o.TimeoutDuration
846860
end := time.Now().Add(duration)
@@ -856,12 +870,11 @@ func (o *Options) WaitForPromotion(env *jxcore.EnvironmentConfig, releaseInfo *R
856870
// TODO based on if the PR completed or not fail the PR or the Promote?
857871
err2 := promoteKey.OnPromotePullRequest(kubeClient, jxClient, o.Namespace, activities.FailedPromotionPullRequest)
858872
if err2 != nil {
859-
return err2
873+
errorChannel <- err2
860874
}
861-
return err
875+
errorChannel <- err
862876
}
863877
}
864-
return nil
865878
}
866879

867880
// TODO This could do with a refactor and some tests...
@@ -985,7 +998,7 @@ func (o *Options) waitForGitOpsPullRequest(env *jxcore.EnvironmentConfig, releas
985998
}
986999
}
9871000
}
988-
if !pr.Mergeable {
1001+
if pr.MergeableState == scm.MergeableStateConflicting {
9891002
log.Logger().Info("Rebasing PullRequest due to conflict")
9901003

9911004
err = o.PromoteViaPullRequest([]*jxcore.EnvironmentConfig{env}, releaseInfo, false)

0 commit comments

Comments
 (0)