Skip to content

Commit 02dadfd

Browse files
authored
Merge pull request #581 from fluxcd/fix-status
fix push branch reporting if its equal to checkout branch
2 parents 390a972 + db8a257 commit 02dadfd

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

internal/controller/imageupdateautomation_controller.go

Lines changed: 11 additions & 22 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.
@@ -433,17 +422,17 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
433422
return failWithError(err)
434423
}
435424
log.Info("pushed commit to origin", "revision", rev, "branch", pushBranch)
436-
statusMessage.WriteString(fmt.Sprintf("commited and pushed commit '%s' to branch '%s'", rev, pushBranch))
425+
statusMessage.WriteString(fmt.Sprintf("committed 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))

internal/controller/update_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func TestImageAutomationReconciler_commitMessage(t *testing.T) {
210210
updateStrategy := &imagev1.UpdateStrategy{
211211
Strategy: imagev1.UpdateStrategySetters,
212212
}
213-
err := createImageUpdateAutomation(testEnv, "update-test", s.namespace, s.gitRepoName, s.gitRepoNamespace, s.branch, "", "", testCommitTemplate, "", updateStrategy)
213+
err := createImageUpdateAutomation(testEnv, "update-test", s.namespace, s.gitRepoName, s.gitRepoNamespace, s.branch, s.branch, "", testCommitTemplate, "", updateStrategy)
214214
g.Expect(err).ToNot(HaveOccurred())
215215

216216
// Wait for a new commit to be made by the controller.
@@ -225,6 +225,17 @@ func TestImageAutomationReconciler_commitMessage(t *testing.T) {
225225
g.Expect(signature).NotTo(BeNil())
226226
g.Expect(signature.Name).To(Equal(testAuthorName))
227227
g.Expect(signature.Email).To(Equal(testAuthorEmail))
228+
229+
// Regression check to ensure the status message contains the branch name
230+
// if checkout branch is the same as push branch.
231+
imageUpdateKey := types.NamespacedName{
232+
Namespace: s.namespace,
233+
Name: "update-test",
234+
}
235+
var imageUpdate imagev1.ImageUpdateAutomation
236+
_ = testEnv.Get(context.TODO(), imageUpdateKey, &imageUpdate)
237+
ready := apimeta.FindStatusCondition(imageUpdate.Status.Conditions, meta.ReadyCondition)
238+
g.Expect(ready.Message).To(Equal(fmt.Sprintf("committed and pushed commit '%s' to branch '%s'", head.Hash().String(), s.branch)))
228239
},
229240
)
230241
})
@@ -517,6 +528,17 @@ func TestImageAutomationReconciler_push_refspec(t *testing.T) {
517528
refspecHash := getRemoteRef(g, repoURL, "smth/else")
518529
g.Expect(pushBranchHash.String()).ToNot(Equal(preChangeCommitId))
519530
g.Expect(pushBranchHash.String()).To(Equal(refspecHash.String()))
531+
532+
imageUpdateKey := types.NamespacedName{
533+
Namespace: s.namespace,
534+
Name: "push-refspec",
535+
}
536+
var imageUpdate imagev1.ImageUpdateAutomation
537+
_ = testEnv.Get(context.TODO(), imageUpdateKey, &imageUpdate)
538+
ready := apimeta.FindStatusCondition(imageUpdate.Status.Conditions, meta.ReadyCondition)
539+
g.Expect(ready.Message).To(Equal(
540+
fmt.Sprintf("committed and pushed commit '%s' to branch '%s' and using refspec '%s'",
541+
pushBranchHash.String(), pushBranch, refspec)))
520542
},
521543
)
522544
})

0 commit comments

Comments
 (0)