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: make pr-based promotions work with kargo render #1674

Merged
merged 7 commits into from
Mar 26, 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
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ RUN GRPC_HEALTH_PROBE_VERSION=v0.4.15 && \
# - supports development
# - not used for official image builds
####################################################################################################
FROM ghcr.io/akuity/kargo-render:v0.1.0-rc.35 as back-end-dev
FROM ghcr.io/akuity/kargo-render:v0.1.0-rc.38 as back-end-dev

USER root

Expand Down Expand Up @@ -103,7 +103,7 @@ CMD ["pnpm", "dev"]
# - the official image we publish
# - purposefully last so that it is the default target when building
####################################################################################################
FROM ghcr.io/akuity/kargo-render:v0.1.0-rc.35 as final
FROM ghcr.io/akuity/kargo-render:v0.1.0-rc.38 as final

USER root

Expand Down
24 changes: 19 additions & 5 deletions internal/controller/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
Password string `json:"password,omitempty"`
}

// CommitOptions represents options for committing changes to a git repository.
type CommitOptions struct {
// AllowEmpty indicates whether an empty commit should be allowed.
AllowEmpty bool
}

// Repo is an interface for interacting with a git repository.
type Repo interface {
// AddAll stages pending changes for commit.
Expand All @@ -46,7 +52,7 @@
// Checkout checks out the specified branch.
Checkout(branch string) error
// Commit commits staged changes to the current branch.
Commit(message string) error
Commit(message string, opts *CommitOptions) error
// CreateChildBranch creates a new branch that is a child of the current
// branch.
CreateChildBranch(branch string) error
Expand Down Expand Up @@ -135,7 +141,7 @@
repoCreds RepoCredentials,
opts *CloneOptions,
) (Repo, error) {
homeDir, err := os.MkdirTemp("", "")
homeDir, err := os.MkdirTemp("", "repo-")
if err != nil {
return nil, fmt.Errorf("error creating home directory for repo %q: %w", repoURL, err)
}
Expand All @@ -162,7 +168,7 @@
if err := r.AddAll(); err != nil {
return err
}
return r.Commit(message)
return r.Commit(message, nil)

Check warning on line 171 in internal/controller/git/git.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/git/git.go#L171

Added line #L171 was not covered by tests
}

func (r *repo) Clean() error {
Expand Down Expand Up @@ -226,8 +232,16 @@
return nil
}

func (r *repo) Commit(message string) error {
if _, err := libExec.Exec(r.buildCommand("commit", "-m", message)); err != nil {
func (r *repo) Commit(message string, opts *CommitOptions) error {
if opts == nil {
opts = &CommitOptions{}

Check warning on line 237 in internal/controller/git/git.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/git/git.go#L235-L237

Added lines #L235 - L237 were not covered by tests
}
cmdTokens := []string{"commit", "-m", message}
if opts.AllowEmpty {
cmdTokens = append(cmdTokens, "--allow-empty")

Check warning on line 241 in internal/controller/git/git.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/git/git.go#L239-L241

Added lines #L239 - L241 were not covered by tests
}

if _, err := libExec.Exec(r.buildCommand(cmdTokens...)); err != nil {

Check warning on line 244 in internal/controller/git/git.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/git/git.go#L244

Added line #L244 was not covered by tests
return fmt.Errorf("error committing changes to branch %q: %w", r.currentBranch, err)
}
return nil
Expand Down
12 changes: 11 additions & 1 deletion internal/controller/promotion/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"github.com/akuity/kargo/internal/logging"
)

const tmpPrefix = "repo-scrap-"

// gitMechanism is an implementation of the Mechanism interface that uses Git to
// update configuration in a repository. It is easily configured to support
// different types of configuration management tools.
Expand Down Expand Up @@ -45,6 +47,7 @@
applyConfigManagementFn func(
update kargoapi.GitRepoUpdate,
newFreight kargoapi.FreightReference,
sourceCommit string,
homeDir string,
workingDir string,
) ([]string, error)
Expand All @@ -61,6 +64,7 @@
applyConfigManagementFn func(
update kargoapi.GitRepoUpdate,
newFreight kargoapi.FreightReference,
sourceCommit string,
homeDir string,
workingDir string,
) ([]string, error),
Expand Down Expand Up @@ -294,11 +298,17 @@
}
}

sourceCommitID, err := repo.LastCommitID()
if err != nil {
return "", err // TODO: Wrap this

Check warning on line 303 in internal/controller/promotion/git.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotion/git.go#L301-L303

Added lines #L301 - L303 were not covered by tests
}

var changes []string
if g.applyConfigManagementFn != nil {
if changes, err = g.applyConfigManagementFn(
update,
newFreight,
sourceCommitID,

Check warning on line 311 in internal/controller/promotion/git.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotion/git.go#L311

Added line #L311 was not covered by tests
repo.HomeDir(),
repo.WorkingDir(),
); err != nil {
Expand All @@ -310,7 +320,7 @@
// Sometimes we don't write to the same branch we read from...
if readRef != writeBranch {
var tempDir string
tempDir, err = os.MkdirTemp("", "")
tempDir, err = os.MkdirTemp("", tmpPrefix)

Check warning on line 323 in internal/controller/promotion/git.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotion/git.go#L323

Added line #L323 was not covered by tests
if err != nil {
return "", fmt.Errorf("error creating temp directory for pending changes: %w", err)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/controller/promotion/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func TestNewGitMechanism(t *testing.T) {
func([]kargoapi.GitRepoUpdate) []kargoapi.GitRepoUpdate {
return nil
},
func(kargoapi.GitRepoUpdate, kargoapi.FreightReference, string, string) ([]string, error) {
func(
kargoapi.GitRepoUpdate,
kargoapi.FreightReference,
string, string, string,
) ([]string, error) {
return nil, nil
},
)
Expand Down
1 change: 1 addition & 0 deletions internal/controller/promotion/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type helmer struct {
func (h *helmer) apply(
update kargoapi.GitRepoUpdate,
newFreight kargoapi.FreightReference,
_ string, // TODO: sourceCommit would be a nice addition to the commit message
homeDir string,
workingDir string,
) ([]string, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/controller/promotion/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func TestHelmerApply(t *testing.T) {
kargoapi.FreightReference{}, // The way the tests are structured, this value doesn't matter
"",
"",
"",
)
testCase.assertions(t, changes, err)
})
Expand Down
1 change: 1 addition & 0 deletions internal/controller/promotion/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type kustomizer struct {
func (k *kustomizer) apply(
update kargoapi.GitRepoUpdate,
newFreight kargoapi.FreightReference,
_ string, // TODO: sourceCommit would be a nice addition to the commit message
_ string,
workingDir string,
) ([]string, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/controller/promotion/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func TestKustomizerApply(t *testing.T) {
},
"",
"",
"",
)
testCase.assertions(t, changes, err)
})
Expand Down
22 changes: 21 additions & 1 deletion internal/controller/promotion/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,27 @@
// we like (i.e. not a descendant of base), recreate it.
func preparePullRequestBranch(repo git.Repo, prBranch string, base string) error {
origBranch := repo.CurrentBranch()
if err := repo.Checkout(base); err != nil {
baseBranchExists, err := repo.RemoteBranchExists(base)
if err != nil {
return err

Check warning on line 25 in internal/controller/promotion/pullrequest.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotion/pullrequest.go#L23-L25

Added lines #L23 - L25 were not covered by tests
}
if !baseBranchExists {

Check warning on line 27 in internal/controller/promotion/pullrequest.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotion/pullrequest.go#L27

Added line #L27 was not covered by tests
// Base branch doesn't exist. Create it!
if err = repo.CreateOrphanedBranch(base); err != nil {
return err

Check warning on line 30 in internal/controller/promotion/pullrequest.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotion/pullrequest.go#L29-L30

Added lines #L29 - L30 were not covered by tests
}
if err = repo.Commit(
"Initial commit",
&git.CommitOptions{
AllowEmpty: true,
},
); err != nil {
return err

Check warning on line 38 in internal/controller/promotion/pullrequest.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotion/pullrequest.go#L32-L38

Added lines #L32 - L38 were not covered by tests
}
if err = repo.Push(false); err != nil {
return err

Check warning on line 41 in internal/controller/promotion/pullrequest.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotion/pullrequest.go#L40-L41

Added lines #L40 - L41 were not covered by tests
}
} else if err = repo.Checkout(base); err != nil {

Check warning on line 43 in internal/controller/promotion/pullrequest.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/promotion/pullrequest.go#L43

Added line #L43 was not covered by tests
return err
}
prBranchExists, err := repo.RemoteBranchExists(prBranch)
Expand Down
Loading
Loading