From 8ca8e88ca0cdbc15cb375fed73c0e04d23964b47 Mon Sep 17 00:00:00 2001 From: eding Date: Sun, 26 Jul 2026 20:49:44 +0200 Subject: [PATCH] fix: seed empty app repositories --- cmd/bootstrap.go | 6 +++++- internal/forgejo/repo.go | 17 +++++++++++++++++ internal/forgejo/repo_test.go | 12 ++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cmd/bootstrap.go b/cmd/bootstrap.go index ccf0132..010c43c 100644 --- a/cmd/bootstrap.go +++ b/cmd/bootstrap.go @@ -62,7 +62,11 @@ func runBootstrap(cmd *cobra.Command, args []string) error { if err != nil { return err } - if created { + hasDeliveryBranch, err := manager.HasRemoteBranch(cfg.Delivery.AppRepoURL, cfg.Delivery.AppRepoRef) + if err != nil { + return err + } + if created || !hasDeliveryBranch { if err := manager.PushRef(bootstrapPublishAppFrom, cfg.Delivery.AppRepoURL, "HEAD", cfg.Delivery.AppRepoRef); err != nil { return err } diff --git a/internal/forgejo/repo.go b/internal/forgejo/repo.go index 408bcdb..df7bbb8 100644 --- a/internal/forgejo/repo.go +++ b/internal/forgejo/repo.go @@ -206,6 +206,23 @@ func (rm *RepoManager) PushRef(dir, repoURL, sourceRef, targetBranch string) err return runGit(dir, environment, "push", repoURL, sourceRef+":refs/heads/"+targetBranch) } +func (rm *RepoManager) HasRemoteBranch(repoURL, branch string) (bool, error) { + cleanupAskPass, environment, err := rm.gitEnvironment("") + if err != nil { + return false, err + } + defer cleanupAskPass() + command := exec.Command("git", "ls-remote", "--exit-code", repoURL, "refs/heads/"+branch) + command.Env = environment + if err := command.Run(); err != nil { + if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 2 { + return false, nil + } + return false, err + } + return true, nil +} + func CurrentBranch(dir string) (string, error) { command := exec.Command("git", "branch", "--show-current") command.Dir = dir diff --git a/internal/forgejo/repo_test.go b/internal/forgejo/repo_test.go index 735952e..a9bc43d 100644 --- a/internal/forgejo/repo_test.go +++ b/internal/forgejo/repo_test.go @@ -116,6 +116,18 @@ func TestEnsureRepositoryCreatesWithoutInitialization(t *testing.T) { } } +func TestHasRemoteBranchReturnsFalseForMissingBranch(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + writer.WriteHeader(http.StatusNotFound) + })) + defer server.Close() + manager := NewRepoManager(server.URL, "token", "owner", "user", "manifests", "cluster", "main", "maidn/bootstrap-test") + manager.HTTPClient = server.Client() + if _, err := manager.HasRemoteBranch(server.URL, "main"); err == nil { + t.Fatal("HasRemoteBranch() accepted a failed git remote lookup") + } +} + func TestRepoExistsReturnsFalseOnNotFound(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { writer.WriteHeader(http.StatusNotFound)