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

1062 - Use the correct default config branch for auto-merge #1063

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions pkg/services/gitopswriter/gitopswriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ func (dw *gitOpsDirectoryWriterSvc) AddApplication(ctx context.Context, app mode
return fmt.Errorf("could not generate application GitOps Automation manifests: %w", err)
}

remover, repoDir, err := dw.RepoWriter.CloneRepo(ctx, app.Branch)
defaultBranch, err := dw.RepoWriter.GetDefaultBranch(ctx)
if err != nil {
return fmt.Errorf("failed to retrieve default branch for repository: %w", err)
}

remover, repoDir, err := dw.RepoWriter.CloneRepo(ctx, defaultBranch)
if err != nil {
return fmt.Errorf("failed to clone repo: %w", err)
}
Expand Down Expand Up @@ -79,11 +84,6 @@ func (dw *gitOpsDirectoryWriterSvc) AddApplication(ctx context.Context, app mode
files = append(files, gitprovider.CommitFile{Path: &manifestPath, Content: &content})
}

defaultBranch, err := dw.RepoWriter.GetDefaultBranch(ctx)
if err != nil {
return fmt.Errorf("failed to retrieve default branch for repository: %w", err)
}

prInfo := gitproviders.PullRequestInfo{
Title: fmt.Sprintf("Gitops add %s", app.Name),
Description: fmt.Sprintf("Added yamls for %s", app.Name),
Expand Down
61 changes: 45 additions & 16 deletions pkg/services/gitopswriter/writer_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ var _ = Describe("Add", func() {
})
})

Context("when creating a pull request", func() {
Context("when using auto-merge", func() {
BeforeEach(func() {
gitProviders.GetDefaultBranchStub = func(_ context.Context, repoUrl gitproviders.RepoURL) (string, error) {
addUrl := app.GitSourceURL
Expand All @@ -248,8 +248,6 @@ var _ = Describe("Add", func() {
return "default-config-branch", nil
}

gitProviders.CreatePullRequestReturns(testutils.DummyPullRequest{}, nil)

app.GitSourceURL = createRepoURL("ssh://github.com/user/repo.git")
})

Expand All @@ -259,38 +257,69 @@ var _ = Describe("Add", func() {
gitOpsDirWriter = createDirWriter()
})

It("creates the pull request against the default branch for an org app repository", func() {
err := gitOpsDirWriter.AddApplication(ctx, app, "test-cluster", false)
It("merges into the app default branch", func() {
err := gitOpsDirWriter.AddApplication(ctx, app, "test-cluster", true)
Expect(err).ShouldNot(HaveOccurred())

_, _, prInfo := gitProviders.CreatePullRequestArgsForCall(0)
Expect(prInfo.TargetBranch).To(Equal("default-app-branch"))
_, _, _, branch := gitClient.CloneArgsForCall(0)
Expect(branch).To(Equal("default-app-branch"))
})
})

It("creates the pull request against the default branch for a user app repository", func() {
err := gitOpsDirWriter.AddApplication(ctx, app, "test-cluster", false)
Context("uses the default config branch for external config", func() {
BeforeEach(func() {
app.ConfigURL = createRepoURL("https://github.com/foo/bar")
gitOpsDirWriter = createDirWriter()
})

It("merges into the config default branch", func() {
err := gitOpsDirWriter.AddApplication(ctx, app, "test-cluster", true)
Expect(err).ShouldNot(HaveOccurred())

_, _, prInfo := gitProviders.CreatePullRequestArgsForCall(0)
Expect(prInfo.TargetBranch).To(Equal("default-app-branch"))
_, _, _, branch := gitClient.CloneArgsForCall(0)
Expect(branch).To(Equal("default-config-branch"))
})
})
})

Context("uses the default config branch for external config", func() {
Context("when creating a pull request", func() {
BeforeEach(func() {
gitProviders.GetDefaultBranchStub = func(_ context.Context, repoUrl gitproviders.RepoURL) (string, error) {
addUrl := app.GitSourceURL

if repoUrl.String() == addUrl.String() {
return "default-app-branch", nil
}
return "default-config-branch", nil
}

gitProviders.CreatePullRequestReturns(testutils.DummyPullRequest{}, nil)

app.GitSourceURL = createRepoURL("ssh://github.com/user/repo.git")
})

Context("uses the default app branch for config in app repository", func() {
BeforeEach(func() {
app.ConfigURL = createRepoURL("https://github.com/foo/bar")
app.ConfigURL = app.GitSourceURL
gitOpsDirWriter = createDirWriter()
})

It("creates the pull request against the default branch for an org config repository", func() {
It("creates the pull request against the app default branch", func() {
err := gitOpsDirWriter.AddApplication(ctx, app, "test-cluster", false)
Expect(err).ShouldNot(HaveOccurred())

_, _, prInfo := gitProviders.CreatePullRequestArgsForCall(0)
Expect(prInfo.TargetBranch).To(Equal("default-config-branch"))
Expect(prInfo.TargetBranch).To(Equal("default-app-branch"))
})
})

Context("uses the default config branch for external config", func() {
BeforeEach(func() {
app.ConfigURL = createRepoURL("https://github.com/foo/bar")
gitOpsDirWriter = createDirWriter()
})

It("creates the pull request against the default branch for a user config repository", func() {
It("creates the pull request against the config default branch", func() {
err := gitOpsDirWriter.AddApplication(ctx, app, "test-cluster", false)
Expect(err).ShouldNot(HaveOccurred())

Expand Down