Skip to content

Commit 1e2a26a

Browse files
authored
chore(refactor): less nesting (#400)
Signed-off-by: Michael Crenshaw <[email protected]>
1 parent f74d429 commit 1e2a26a

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed

internal/controller/changetransferpolicy_controller.go

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ func (r *ChangeTransferPolicyReconciler) mergeOrPullRequestPromote(ctx context.C
445445
func (r *ChangeTransferPolicyReconciler) creatOrUpdatePullRequest(ctx context.Context, ctp *promoterv1alpha1.ChangeTransferPolicy) error {
446446
logger := log.FromContext(ctx)
447447
if ctp.Status.Proposed.Dry.Sha == ctp.Status.Active.Dry.Sha {
448-
// If the proposed dry sha is different from the active dry sha, create a pull request
448+
// If the proposed dry sha is the same as the active dry sha, no need to create a pull request
449449
return nil
450450
}
451451

@@ -485,65 +485,65 @@ func (r *ChangeTransferPolicyReconciler) creatOrUpdatePullRequest(ctx context.Co
485485
Name: prName,
486486
}, &pr)
487487
if err != nil {
488-
if k8s_errors.IsNotFound(err) {
489-
// TODO: move some of the below code into a utility function. It's a bit verbose for being nested this deeply.
490-
// The code below sets the ownership for the PullRequest Object
491-
kind := reflect.TypeOf(promoterv1alpha1.ChangeTransferPolicy{}).Name()
492-
gvk := promoterv1alpha1.GroupVersion.WithKind(kind)
493-
controllerRef := metav1.NewControllerRef(ctp, gvk)
494-
495-
pr = promoterv1alpha1.PullRequest{
496-
ObjectMeta: metav1.ObjectMeta{
497-
Name: prName,
498-
Namespace: ctp.Namespace,
499-
OwnerReferences: []metav1.OwnerReference{*controllerRef},
500-
Labels: map[string]string{
501-
promoterv1alpha1.PromotionStrategyLabel: utils.KubeSafeLabel(ctp.Labels[promoterv1alpha1.PromotionStrategyLabel]),
502-
promoterv1alpha1.ChangeTransferPolicyLabel: utils.KubeSafeLabel(ctp.Name),
503-
promoterv1alpha1.EnvironmentLabel: utils.KubeSafeLabel(ctp.Spec.ActiveBranch),
504-
},
505-
},
506-
Spec: promoterv1alpha1.PullRequestSpec{
507-
RepositoryReference: ctp.Spec.RepositoryReference,
508-
Title: title,
509-
TargetBranch: ctp.Spec.ActiveBranch,
510-
SourceBranch: ctp.Spec.ProposedBranch,
511-
Description: description,
512-
State: "open",
513-
},
514-
}
515-
err = r.Create(ctx, &pr)
516-
if err != nil {
517-
return fmt.Errorf("failed to create PR from branch %q to %q: %w", ctp.Spec.ProposedBranch, ctp.Spec.ActiveBranch, err)
518-
}
519-
r.Recorder.Event(ctp, "Normal", constants.PullRequestCreatedReason, fmt.Sprintf(constants.PullRequestCreatedMessage, pr.Name))
520-
logger.V(4).Info("Created pull request")
521-
} else {
488+
if !k8s_errors.IsNotFound(err) {
522489
return fmt.Errorf("failed to get PR %q: %w", prName, err)
523490
}
524-
} else {
525-
// Pull Request already exists, update it.
526-
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
527-
prUpdated := promoterv1alpha1.PullRequest{}
528-
// TODO: consider skipping this Get on the first attempt, the object we already got might be up to date.
529-
err = r.Get(ctx, client.ObjectKey{Namespace: pr.Namespace, Name: pr.Name}, &prUpdated)
530-
if err != nil {
531-
return fmt.Errorf("failed to get PR %q: %w", pr.Name, err)
532-
}
533-
prUpdated.Spec.RepositoryReference = ctp.Spec.RepositoryReference
534-
prUpdated.Spec.Title = title
535-
prUpdated.Spec.TargetBranch = ctp.Spec.ActiveBranch
536-
prUpdated.Spec.SourceBranch = ctp.Spec.ProposedBranch
537-
prUpdated.Spec.Description = description
538-
return r.Update(ctx, &prUpdated)
539-
})
491+
492+
// TODO: move some of the below code into a utility function. It's a bit verbose for being nested this deeply.
493+
// The code below sets the ownership for the PullRequest Object
494+
kind := reflect.TypeOf(promoterv1alpha1.ChangeTransferPolicy{}).Name()
495+
gvk := promoterv1alpha1.GroupVersion.WithKind(kind)
496+
controllerRef := metav1.NewControllerRef(ctp, gvk)
497+
498+
pr = promoterv1alpha1.PullRequest{
499+
ObjectMeta: metav1.ObjectMeta{
500+
Name: prName,
501+
Namespace: ctp.Namespace,
502+
OwnerReferences: []metav1.OwnerReference{*controllerRef},
503+
Labels: map[string]string{
504+
promoterv1alpha1.PromotionStrategyLabel: utils.KubeSafeLabel(ctp.Labels[promoterv1alpha1.PromotionStrategyLabel]),
505+
promoterv1alpha1.ChangeTransferPolicyLabel: utils.KubeSafeLabel(ctp.Name),
506+
promoterv1alpha1.EnvironmentLabel: utils.KubeSafeLabel(ctp.Spec.ActiveBranch),
507+
},
508+
},
509+
Spec: promoterv1alpha1.PullRequestSpec{
510+
RepositoryReference: ctp.Spec.RepositoryReference,
511+
Title: title,
512+
TargetBranch: ctp.Spec.ActiveBranch,
513+
SourceBranch: ctp.Spec.ProposedBranch,
514+
Description: description,
515+
State: "open",
516+
},
517+
}
518+
err = r.Create(ctx, &pr)
540519
if err != nil {
541-
return fmt.Errorf("failed to update PR %q: %w", pr.Name, err)
520+
return fmt.Errorf("failed to create PR from branch %q to %q: %w", ctp.Spec.ProposedBranch, ctp.Spec.ActiveBranch, err)
542521
}
543-
// r.Recorder.Event(ctp, "Normal", "PullRequestUpdated", fmt.Sprintf("Pull Request %s updated", pr.Name))
544-
logger.V(4).Info("Updated pull request resource")
522+
r.Recorder.Event(ctp, "Normal", constants.PullRequestCreatedReason, fmt.Sprintf(constants.PullRequestCreatedMessage, pr.Name))
523+
logger.V(4).Info("Created pull request")
524+
return nil
545525
}
546526

527+
// Pull Request already exists, update it.
528+
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
529+
prUpdated := promoterv1alpha1.PullRequest{}
530+
// TODO: consider skipping this Get on the first attempt, the object we already got might be up to date.
531+
err = r.Get(ctx, client.ObjectKey{Namespace: pr.Namespace, Name: pr.Name}, &prUpdated)
532+
if err != nil {
533+
return fmt.Errorf("failed to get PR %q: %w", pr.Name, err)
534+
}
535+
prUpdated.Spec.RepositoryReference = ctp.Spec.RepositoryReference
536+
prUpdated.Spec.Title = title
537+
prUpdated.Spec.TargetBranch = ctp.Spec.ActiveBranch
538+
prUpdated.Spec.SourceBranch = ctp.Spec.ProposedBranch
539+
prUpdated.Spec.Description = description
540+
return r.Update(ctx, &prUpdated)
541+
})
542+
if err != nil {
543+
return fmt.Errorf("failed to update PR %q: %w", pr.Name, err)
544+
}
545+
// r.Recorder.Event(ctp, "Normal", "PullRequestUpdated", fmt.Sprintf("Pull Request %s updated", pr.Name))
546+
logger.V(4).Info("Updated pull request resource")
547547
return nil
548548
}
549549

0 commit comments

Comments
 (0)