From 0ba90d8d5d932e2e23a92bcc573ff75d606246b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Svantesson?= Date: Sun, 19 May 2024 19:43:53 +0200 Subject: [PATCH] fix: wait in parallel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit but don't wait at all if PR shouldn't merge automatically Signed-off-by: MÃ¥rten Svantesson --- pkg/promote/promote.go | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/pkg/promote/promote.go b/pkg/promote/promote.go index 67b7f09..3439fd8 100644 --- a/pkg/promote/promote.go +++ b/pkg/promote/promote.go @@ -8,6 +8,7 @@ import ( "path/filepath" "strconv" "strings" + "sync" "time" "github.com/jenkins-x/jx-helpers/v3/pkg/requirements" @@ -575,6 +576,8 @@ func (o *Options) PromoteAll(pred func(*jxcore.EnvironmentConfig) bool) error { } groups = append(groups, []*jxcore.EnvironmentConfig{env}) } + var errorChannel = make(chan error, len(groups)) + var wg sync.WaitGroup for _, group := range groups { firstEnv := group[0] @@ -585,14 +588,24 @@ func (o *Options) PromoteAll(pred func(*jxcore.EnvironmentConfig) bool) error { return err } o.ReleaseInfo = releaseInfo - if !o.NoPoll { - err = o.WaitForPromotion(firstEnv, releaseInfo) - if err != nil { - return err - } + // TODO: Add test + if !o.NoPoll && group[0].PromotionStrategy == v1.PromotionStrategyTypeAutomatic { + wg.Add(1) + // Wait for multiple PRs in parallel + go o.WaitForPromotion(firstEnv, releaseInfo, errorChannel, &wg) } } - return nil + wg.Wait() + close(errorChannel) + var err error + for err2 := range errorChannel { + if err == nil { + err = err2 + } else { + err = fmt.Errorf("%w; %v", err, err2) + } + } + return err } // EnvironmentNamespace returns the namespace for the environment @@ -833,14 +846,15 @@ func (o *Options) GetTargetNamespace(ns, env string) (string, *jxcore.Environmen return targetNS, envResource, nil } -func (o *Options) WaitForPromotion(env *jxcore.EnvironmentConfig, releaseInfo *ReleaseInfo) error { +func (o *Options) WaitForPromotion(env *jxcore.EnvironmentConfig, releaseInfo *ReleaseInfo, errorChannel chan<- error, wg *sync.WaitGroup) { + defer wg.Done() if o.TimeoutDuration == nil { log.Logger().Infof("No --%s option specified on the 'jx promote' command so not waiting for the promotion to succeed", optionTimeout) - return nil + return } if o.PullRequestPollDuration == nil { log.Logger().Infof("No --%s option specified on the 'jx promote' command so not waiting for the promotion to succeed", optionPullRequestPollTime) - return nil + return } duration := *o.TimeoutDuration end := time.Now().Add(duration) @@ -856,12 +870,11 @@ func (o *Options) WaitForPromotion(env *jxcore.EnvironmentConfig, releaseInfo *R // TODO based on if the PR completed or not fail the PR or the Promote? err2 := promoteKey.OnPromotePullRequest(kubeClient, jxClient, o.Namespace, activities.FailedPromotionPullRequest) if err2 != nil { - return err2 + errorChannel <- err2 } - return err + errorChannel <- err } } - return nil } // TODO This could do with a refactor and some tests... @@ -985,7 +998,7 @@ func (o *Options) waitForGitOpsPullRequest(env *jxcore.EnvironmentConfig, releas } } } - if !pr.Mergeable { + if pr.MergeableState == scm.MergeableStateConflicting { log.Logger().Info("Rebasing PullRequest due to conflict") err = o.PromoteViaPullRequest([]*jxcore.EnvironmentConfig{env}, releaseInfo, false)