Skip to content

Commit 73466b6

Browse files
committed
fix push branch reporting if its equal to checkout branch
Fix the push branch reported in the logs and status if `.spec.git.push.branch==.spec.git.checkout.branch`. Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent 390a972 commit 73466b6

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

internal/controller/imageupdateautomation_controller.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,18 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
234234
}
235235
}()
236236

237+
// pushBranch contains the branch name the commit needs to be pushed to.
238+
// It takes the value of the push branch if one is specified or, if the push
239+
// config is nil, then it takes the value of the checkout branch if possible.
237240
var pushBranch string
238241
var switchBranch bool
239-
if gitSpec.Push != nil {
242+
if gitSpec.Push != nil && gitSpec.Push.Branch != "" {
243+
pushBranch = gitSpec.Push.Branch
244+
tracelog.Info("using push branch from .spec.push.branch", "branch", pushBranch)
240245
// We only need to switch branches when a branch has been specified in
241246
// the push spec and it is different than the one in the checkout ref.
242-
if gitSpec.Push.Branch != "" && gitSpec.Push.Branch != checkoutRef.Branch {
243-
pushBranch = gitSpec.Push.Branch
247+
if gitSpec.Push.Branch != checkoutRef.Branch {
244248
switchBranch = true
245-
tracelog.Info("using push branch from .spec.push.branch", "branch", pushBranch)
246249
}
247250
} else {
248251
// Here's where it gets constrained. If there's no push branch
@@ -402,25 +405,11 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
402405
pushCtx, cancel := context.WithTimeout(ctx, origin.Spec.Timeout.Duration)
403406
defer cancel()
404407

405-
var pushToBranch bool
406-
var pushWithRefspec bool
407-
// If a refspec is specified, then we need to perform a push using
408-
// that refspec.
409-
if gitSpec.Push != nil && gitSpec.Push.Refspec != "" {
410-
pushWithRefspec = true
411-
}
412-
// We need to push the commit to the push branch if one was specified, or if
413-
// no push config was specified, then we need to push to the branch we checked
414-
// out to.
415-
if (gitSpec.Push != nil && gitSpec.Push.Branch != "") || gitSpec.Push == nil {
416-
pushToBranch = true
417-
}
418-
419408
var pushConfig repository.PushConfig
420409
if gitSpec.Push != nil {
421410
pushConfig.Options = gitSpec.Push.Options
422411
}
423-
if pushToBranch {
412+
if pushBranch != "" {
424413
// If the force push feature flag is true and we are pushing to a
425414
// different branch than the one we checked out to, then force push
426415
// these changes.
@@ -436,14 +425,14 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
436425
statusMessage.WriteString(fmt.Sprintf("commited and pushed commit '%s' to branch '%s'", rev, pushBranch))
437426
}
438427

439-
if pushWithRefspec {
428+
if gitSpec.Push != nil && gitSpec.Push.Refspec != "" {
440429
pushConfig.Refspecs = []string{gitSpec.Push.Refspec}
441430
if err := gitClient.Push(pushCtx, pushConfig); err != nil {
442431
return failWithError(err)
443432
}
444433
log.Info("pushed commit to origin", "revision", rev, "refspec", gitSpec.Push.Refspec)
445434

446-
if pushToBranch {
435+
if statusMessage.Len() > 0 {
447436
statusMessage.WriteString(fmt.Sprintf(" and using refspec '%s'", gitSpec.Push.Refspec))
448437
} else {
449438
statusMessage.WriteString(fmt.Sprintf("committed and pushed commit '%s' using refspec '%s'", rev, gitSpec.Push.Refspec))

0 commit comments

Comments
 (0)