Skip to content
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
5 changes: 4 additions & 1 deletion api/v1/codebasebranch_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ type CodebaseBranchSpec struct {
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
BranchName string `json:"branchName"`

// The new branch will be created starting from the selected commit hash.
// FromCommit is a commit hash or branch name.
// The new branch will be created starting from the selected commit hash or branch name.
// If a branch name is provided, the new branch will be created from the latest commit of that branch.
// +optional
FromCommit string `json:"fromCommit"`

// Version of the branch. It's required for versioning type "semver".
Expand Down
7 changes: 4 additions & 3 deletions config/crd/bases/v2.edp.epam.com_codebasebranches.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ spec:
description: Name of Codebase associated with.
type: string
fromCommit:
description: The new branch will be created starting from the selected
commit hash.
description: |-
FromCommit is a commit hash or branch name.
The new branch will be created starting from the selected commit hash or branch name.
If a branch name is provided, the new branch will be created from the latest commit of that branch.
type: string
pipelines:
additionalProperties:
Expand All @@ -95,7 +97,6 @@ spec:
required:
- branchName
- codebaseName
- fromCommit
- release
type: object
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (
"github.com/epam/edp-codebase-operator/v2/pkg/util"
)

// CheckCommitHashExists is chain element for checking commit hash existence.
type CheckCommitHashExists struct {
// CheckReferenceExists is chain element for checking if a reference (branch or commit) exists.
type CheckReferenceExists struct {
Next handler.CodebaseBranchHandler
Client client.Client
Git git.Git
}

// ServeRequest is a method for checking CodebaseBranch FromCommit hash existence.
func (c CheckCommitHashExists) ServeRequest(ctx context.Context, codebaseBranch *codebaseApi.CodebaseBranch) error {
log := ctrl.LoggerFrom(ctx).WithName("check-commit-hash-exists")
// ServeRequest is a method for checking if the reference (branch or commit) exists.
func (c CheckReferenceExists) ServeRequest(ctx context.Context, codebaseBranch *codebaseApi.CodebaseBranch) error {
log := ctrl.LoggerFrom(ctx).WithName("check-reference-exists")

if codebaseBranch.Status.Git == codebaseApi.CodebaseBranchGitStatusBranchCreated {
log.Info("Branch is already created in git. Skip checking commit hash existence")
log.Info("Branch is already created in git. Skip checking reference existence")

return c.next(ctx, codebaseBranch)
}
Expand Down Expand Up @@ -88,20 +88,16 @@ func (c CheckCommitHashExists) ServeRequest(ctx context.Context, codebaseBranch
}
}

exists, err := c.Git.CommitExists(workDir, codebaseBranch.Spec.FromCommit)
err := c.Git.CheckReference(workDir, codebaseBranch.Spec.FromCommit)
if err != nil {
return c.processErr(codebaseBranch, fmt.Errorf("failed to check commit hash %s: %w", codebaseBranch.Spec.FromCommit, err))
}

if !exists {
return c.processErr(codebaseBranch, fmt.Errorf("commit %s doesn't exist", codebaseBranch.Spec.FromCommit))
return c.processErr(codebaseBranch, fmt.Errorf("reference %s doesn't exist: %w", codebaseBranch.Spec.FromCommit, err))
}

return c.next(ctx, codebaseBranch)
}

// next is a method for serving next chain element.
func (c CheckCommitHashExists) next(ctx context.Context, codebaseBranch *codebaseApi.CodebaseBranch) error {
func (c CheckReferenceExists) next(ctx context.Context, codebaseBranch *codebaseApi.CodebaseBranch) error {
err := handler.NextServeOrNil(ctx, c.Next, codebaseBranch)
if err != nil {
return fmt.Errorf("failed to serve next chain element: %w", err)
Expand All @@ -111,7 +107,7 @@ func (c CheckCommitHashExists) next(ctx context.Context, codebaseBranch *codebas
}

// processErr is a method for processing error in chain.
func (c CheckCommitHashExists) processErr(codebaseBranch *codebaseApi.CodebaseBranch, err error) error {
func (c CheckReferenceExists) processErr(codebaseBranch *codebaseApi.CodebaseBranch, err error) error {
if err == nil {
return nil
}
Expand All @@ -121,7 +117,7 @@ func (c CheckCommitHashExists) processErr(codebaseBranch *codebaseApi.CodebaseBr
return err
}

func (CheckCommitHashExists) setFailedFields(
func (CheckReferenceExists) setFailedFields(
codebaseBranch *codebaseApi.CodebaseBranch,
actionType codebaseApi.ActionType,
message string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chain

import (
"context"
"errors"
"testing"

"github.com/go-logr/logr"
Expand All @@ -18,7 +19,7 @@ import (
gitServerMocks "github.com/epam/edp-codebase-operator/v2/pkg/git/mocks"
)

func TestCheckCommitHashExists_ServeRequest(t *testing.T) {
func TestCheckReferenceExists_ServeRequest(t *testing.T) {
scheme := runtime.NewScheme()
err := codebaseApi.AddToScheme(scheme)
require.NoError(t, err)
Expand Down Expand Up @@ -84,17 +85,77 @@ func TestCheckCommitHashExists_ServeRequest(t *testing.T) {
testifymock.Anything,
).Return(nil)
mGit.On(
"CommitExists",
"CheckReference",
testifymock.Anything,
testifymock.Anything,
).Return(true, nil)
).Return(nil)

return mGit
},
wantErr: require.NoError,
},
{
name: "failed, commit doesn't exist",
name: "success, branch reference exists",
codebaseBranch: &codebaseApi.CodebaseBranch{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
},
Spec: codebaseApi.CodebaseBranchSpec{
CodebaseName: "test-codebase",
BranchName: "feature",
FromCommit: "main",
},
},
objects: []runtime.Object{
&codebaseApi.Codebase{
ObjectMeta: metav1.ObjectMeta{
Name: "test-codebase",
Namespace: "default",
},
Spec: codebaseApi.CodebaseSpec{
GitServer: "test-git-server",
},
},
&codebaseApi.GitServer{
ObjectMeta: metav1.ObjectMeta{
Name: "test-git-server",
Namespace: "default",
},
Spec: codebaseApi.GitServerSpec{
NameSshKeySecret: "test-ssh-key",
},
},
&coreV1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ssh-key",
Namespace: "default",
},
},
},
gitClient: func() git.Git {
mGit := gitServerMocks.NewMockGit(t)
mGit.On(
"CloneRepositoryBySsh",
testifymock.Anything,
testifymock.Anything,
testifymock.Anything,
testifymock.Anything,
testifymock.Anything,
testifymock.Anything,
).Return(nil)
mGit.On(
"CheckReference",
testifymock.Anything,
testifymock.Anything,
).Return(nil)

return mGit
},
wantErr: require.NoError,
},
{
name: "failed, reference doesn't exist",
codebaseBranch: &codebaseApi.CodebaseBranch{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand All @@ -103,7 +164,7 @@ func TestCheckCommitHashExists_ServeRequest(t *testing.T) {
Spec: codebaseApi.CodebaseBranchSpec{
CodebaseName: "test-codebase",
BranchName: "main",
FromCommit: "bfba920bd3bdebc9ae1c4475d70391152645b2a4",
FromCommit: "non-existent",
},
},
objects: []runtime.Object{
Expand Down Expand Up @@ -144,21 +205,20 @@ func TestCheckCommitHashExists_ServeRequest(t *testing.T) {
testifymock.Anything,
).Return(nil)
mGit.On(
"CommitExists",
"CheckReference",
testifymock.Anything,
testifymock.Anything,
).Return(false, nil)
).Return(errors.New("reference not found"))

return mGit
},
wantErr: func(t require.TestingT, err error, _ ...any) {
require.Error(t, err)

require.Contains(t, err.Error(), "commit bfba920bd3bdebc9ae1c4475d70391152645b2a4 doesn't exist")
require.Contains(t, err.Error(), "reference non-existent doesn't exist")
},
},
{
name: "skip, commit hash is empty",
name: "skip, reference is empty",
codebaseBranch: &codebaseApi.CodebaseBranch{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand Down Expand Up @@ -194,7 +254,7 @@ func TestCheckCommitHashExists_ServeRequest(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := CheckCommitHashExists{
c := CheckReferenceExists{
Client: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(tt.objects...).Build(),
Git: tt.gitClient(),
}
Expand Down
7 changes: 4 additions & 3 deletions deploy-templates/crds/v2.edp.epam.com_codebasebranches.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ spec:
description: Name of Codebase associated with.
type: string
fromCommit:
description: The new branch will be created starting from the selected
commit hash.
description: |-
FromCommit is a commit hash or branch name.
The new branch will be created starting from the selected commit hash or branch name.
If a branch name is provided, the new branch will be created from the latest commit of that branch.
type: string
pipelines:
additionalProperties:
Expand All @@ -95,7 +97,6 @@ spec:
required:
- branchName
- codebaseName
- fromCommit
- release
type: object
status:
Expand Down
16 changes: 9 additions & 7 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,20 +332,22 @@ CodebaseBranchSpec defines the desired state of CodebaseBranch.
Name of Codebase associated with.<br/>
</td>
<td>true</td>
</tr><tr>
<td><b>fromCommit</b></td>
<td>string</td>
<td>
The new branch will be created starting from the selected commit hash.<br/>
</td>
<td>true</td>
</tr><tr>
<td><b>release</b></td>
<td>boolean</td>
<td>
Flag if branch is used as "release" branch.<br/>
</td>
<td>true</td>
</tr><tr>
<td><b>fromCommit</b></td>
<td>string</td>
<td>
FromCommit is a commit hash or branch name.
The new branch will be created starting from the selected commit hash or branch name.
If a branch name is provided, the new branch will be created from the latest commit of that branch.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>pipelines</b></td>
<td>map[string]string</td>
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0
github.com/epam/edp-cd-pipeline-operator/v2 v2.25.1
github.com/epam/edp-common v0.0.0-20230710145648-344bbce4120e
github.com/go-git/go-git/v5 v5.16.0
github.com/go-git/go-git/v5 v5.16.2
github.com/go-logr/logr v1.4.2
github.com/go-resty/resty/v2 v2.16.5
github.com/jarcoal/httpmock v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ github.com/go-git/go-billy/v5 v5.6.2 h1:6Q86EsPXMa7c3YZ3aLAQsMA0VlWmy43r6FHqa/UN
github.com/go-git/go-billy/v5 v5.6.2/go.mod h1:rcFC2rAsp/erv7CMz9GczHcuD0D32fWzH+MJAU+jaUU=
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
github.com/go-git/go-git/v5 v5.16.0 h1:k3kuOEpkc0DeY7xlL6NaaNg39xdgQbtH5mwCafHO9AQ=
github.com/go-git/go-git/v5 v5.16.0/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8=
github.com/go-git/go-git/v5 v5.16.2 h1:fT6ZIOjE5iEnkzKyxTHK1W4HGAsPhqEqiSAssSO77hM=
github.com/go-git/go-git/v5 v5.16.2/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down
Loading