fix: seed empty app repositories

This commit is contained in:
eding 2026-07-26 20:49:44 +02:00
parent 7ccadae8dc
commit 8ca8e88ca0
3 changed files with 34 additions and 1 deletions

View file

@ -62,7 +62,11 @@ func runBootstrap(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err 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 { if err := manager.PushRef(bootstrapPublishAppFrom, cfg.Delivery.AppRepoURL, "HEAD", cfg.Delivery.AppRepoRef); err != nil {
return err return err
} }

View file

@ -206,6 +206,23 @@ func (rm *RepoManager) PushRef(dir, repoURL, sourceRef, targetBranch string) err
return runGit(dir, environment, "push", repoURL, sourceRef+":refs/heads/"+targetBranch) 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) { func CurrentBranch(dir string) (string, error) {
command := exec.Command("git", "branch", "--show-current") command := exec.Command("git", "branch", "--show-current")
command.Dir = dir command.Dir = dir

View file

@ -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) { func TestRepoExistsReturnsFalseOnNotFound(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusNotFound) writer.WriteHeader(http.StatusNotFound)