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

feat: verify if targetBranch is present in git repo #511

Merged
merged 3 commits into from
Apr 13, 2023
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
8 changes: 8 additions & 0 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const maxNameLength = 63
//
// ValidateRepository checks that the current working directory is a valid git repository,
// and returns nil if valid.
//
// BranchExists checks whether a given branch exists in the git repository.
type Git interface {
FileExistsOnBranch(file string, remote string, branch string) bool
Show(file string, remote string, branch string) (string, error)
Expand All @@ -58,6 +60,7 @@ type Git interface {
ListChangedFilesInDirs(commit string, dirs ...string) ([]string, error)
GetURLForRemote(remote string) (string, error)
ValidateRepository() error
BranchExists(branch string) bool
}

// Helm is the interface that wraps Helm operations
Expand Down Expand Up @@ -703,6 +706,11 @@ func (t *Testing) computeMergeBase() (string, error) {
if err != nil {
return "", errors.New("must be in a git repository")
}

if !t.git.BranchExists(t.config.TargetBranch) {
return "", fmt.Errorf("targetBranch '%s' does not exist", t.config.TargetBranch)
}

return t.git.MergeBase(fmt.Sprintf("%s/%s", t.config.Remote, t.config.TargetBranch), t.config.Since)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/chart/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (g fakeGit) ValidateRepository() error {
return nil
}

func (g fakeGit) BranchExists(branch string) bool {
return true
}

type fakeAccountValidator struct{}

func (v fakeAccountValidator) Validate(repoDomain string, account string) error {
Expand Down
5 changes: 5 additions & 0 deletions pkg/tool/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ func (g Git) ValidateRepository() error {
_, err := g.exec.RunProcessAndCaptureOutput("git", "rev-parse", "--is-inside-work-tree")
return err
}

func (g Git) BranchExists(branch string) bool {
_, err := g.exec.RunProcessAndCaptureOutput("git", "rev-parse", "--verify", branch)
return err == nil
}