From 0cc1f14680694c98db8fc272ca6bd891927e5858 Mon Sep 17 00:00:00 2001 From: Yunwen Zheng Date: Thu, 12 Jun 2025 15:58:46 -0400 Subject: [PATCH] ProvisionedDashboard: Fix backend fails to delete dashboard due to missing branch and file --- pkg/registry/apis/provisioning/repository/github.go | 9 +++++---- pkg/registry/apis/provisioning/resources/dualwriter.go | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/registry/apis/provisioning/repository/github.go b/pkg/registry/apis/provisioning/repository/github.go index 7265568d08e..73687d38a26 100644 --- a/pkg/registry/apis/provisioning/repository/github.go +++ b/pkg/registry/apis/provisioning/repository/github.go @@ -45,6 +45,7 @@ type GithubRepository interface { Owner() string Repo() string Client() pgh.Client + EnsureBranchExists(ctx context.Context, branchName string) error } func NewGitHub( @@ -279,7 +280,7 @@ func (r *githubRepository) Create(ctx context.Context, path, ref string, data [] } ctx, _ = r.logger(ctx, ref) - if err := r.ensureBranchExists(ctx, ref); err != nil { + if err := r.EnsureBranchExists(ctx, ref); err != nil { return err } @@ -314,7 +315,7 @@ func (r *githubRepository) Update(ctx context.Context, path, ref string, data [] } ctx, _ = r.logger(ctx, ref) - if err := r.ensureBranchExists(ctx, ref); err != nil { + if err := r.EnsureBranchExists(ctx, ref); err != nil { return err } @@ -365,7 +366,7 @@ func (r *githubRepository) Delete(ctx context.Context, path, ref, comment string } ctx, _ = r.logger(ctx, ref) - if err := r.ensureBranchExists(ctx, ref); err != nil { + if err := r.EnsureBranchExists(ctx, ref); err != nil { return err } @@ -482,7 +483,7 @@ func isValidGitBranchName(branch string) bool { return true } -func (r *githubRepository) ensureBranchExists(ctx context.Context, branchName string) error { +func (r *githubRepository) EnsureBranchExists(ctx context.Context, branchName string) error { if !isValidGitBranchName(branchName) { return &apierrors.StatusError{ ErrStatus: metav1.Status{ diff --git a/pkg/registry/apis/provisioning/resources/dualwriter.go b/pkg/registry/apis/provisioning/resources/dualwriter.go index b9f6e71c3a7..720cf16efff 100644 --- a/pkg/registry/apis/provisioning/resources/dualwriter.go +++ b/pkg/registry/apis/provisioning/resources/dualwriter.go @@ -81,6 +81,13 @@ func (r *DualReadWriter) Delete(ctx context.Context, opts DualWriteOptions) (*Pa return nil, fmt.Errorf("folder delete not supported") } + // First ensure the branch exists if we're using a GitHub repository + if githubRepo, ok := r.repo.(repository.GithubRepository); ok && opts.Ref != "" { + if err := githubRepo.EnsureBranchExists(ctx, opts.Ref); err != nil { + return nil, fmt.Errorf("ensure branch exists: %w", err) + } + } + file, err := r.repo.Read(ctx, opts.Path, opts.Ref) if err != nil { return nil, fmt.Errorf("read file: %w", err)