Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(controller)!: check create setting when attempting to clone non-existent remote branch #3012

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions internal/directives/git_cloner.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
switch {
case checkout.Branch != "":
ref = checkout.Branch
if err = ensureRemoteBranch(repo, ref); err != nil {
if err = ensureRemoteBranch(repo, ref, checkout.Create); err != nil {
return PromotionStepResult{Status: kargoapi.PromotionPhaseErrored},
fmt.Errorf("error ensuring existence of remote branch %s: %w", ref, err)
}
Expand Down Expand Up @@ -222,9 +222,12 @@
return false, nil
}

// ensureRemoteBranch ensures the existence of a remote branch. If the branch
// does not exist, an empty orphaned branch is created and pushed to the remote.
func ensureRemoteBranch(repo git.BareRepo, branch string) error {
// ensureRemoteBranch checks for the existence of a remote branch. If the remote
// branch exists, no action is taken and nil is returned. If the branch does not
// exist and create == true, an empty orphaned branch is created and pushed to
// the remote. If the branch does not exist and create == false, an error is
// returned.
func ensureRemoteBranch(repo git.BareRepo, branch string, create bool) error {
exists, err := repo.RemoteBranchExists(branch)
if err != nil {
return fmt.Errorf(
Expand All @@ -235,6 +238,15 @@
if exists {
return nil
}
if !create {
return fmt.Errorf(
"remote branch %q of repo %s does not exist; set create=true if you'd "+
"like a non-existent remote branch to be automatically created at "+
"checkout",
branch,
repo.URL(),
)
}

Check warning on line 249 in internal/directives/git_cloner.go

View check run for this annotation

Codecov / codecov/patch

internal/directives/git_cloner.go#L242-L249

Added lines #L242 - L249 were not covered by tests
tmpDir, err := os.MkdirTemp("", "repo-")
if err != nil {
return fmt.Errorf("error creating temporary directory: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/directives/git_cloner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ func Test_gitCloner_runPromotionStep(t *testing.T) {
{
Branch: "stage/dev",
Path: "out",
Create: true,
},
},
},
Expand Down