diff --git a/controllers/codebasebranch/chain/clean_tmp_directory/clean_tmp_directory.go b/controllers/codebasebranch/chain/clean_tmp_directory/clean_tmp_directory.go deleted file mode 100644 index f8a93b8a..00000000 --- a/controllers/codebasebranch/chain/clean_tmp_directory/clean_tmp_directory.go +++ /dev/null @@ -1,57 +0,0 @@ -package clean_tmp_directory - -import ( - "context" - "fmt" - - metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" - ctrl "sigs.k8s.io/controller-runtime" - - codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1" - "github.com/epam/edp-codebase-operator/v2/controllers/codebasebranch/chain" - "github.com/epam/edp-codebase-operator/v2/pkg/util" -) - -type CleanTempDirectory struct{} - -func (*CleanTempDirectory) ServeRequest(ctx context.Context, cb *codebaseApi.CodebaseBranch) error { - log := ctrl.LoggerFrom(ctx).WithName("clean-temp-directory") - - log.Info("Start CleanTempDirectory method") - - wd := chain.GetCodebaseBranchWorkingDirectory(cb) - - if err := deleteWorkDirectory(wd); err != nil { - setFailedFields(cb, codebaseApi.CleanData, err.Error()) - - return err - } - - log.Info("End cleaning temp directory") - - return nil -} - -func deleteWorkDirectory(dir string) error { - if err := util.RemoveDirectory(dir); err != nil { - return fmt.Errorf("failed to delete directory %v: %w", dir, err) - } - - return nil -} - -func setFailedFields(cb *codebaseApi.CodebaseBranch, a codebaseApi.ActionType, message string) { - cb.Status = codebaseApi.CodebaseBranchStatus{ - Status: util.StatusFailed, - LastTimeUpdated: metaV1.Now(), - Username: "system", - Action: a, - Result: codebaseApi.Error, - DetailedMessage: message, - Value: "failed", - Git: cb.Status.Git, - VersionHistory: cb.Status.VersionHistory, - Build: cb.Status.Build, - Conditions: cb.Status.Conditions, - } -} diff --git a/controllers/codebasebranch/chain/clean_tmp_directory/clean_tmp_directory_test.go b/controllers/codebasebranch/chain/clean_tmp_directory/clean_tmp_directory_test.go deleted file mode 100644 index 0a79f3bd..00000000 --- a/controllers/codebasebranch/chain/clean_tmp_directory/clean_tmp_directory_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package clean_tmp_directory - -import ( - "context" - "testing" - - "github.com/go-logr/logr" - "github.com/stretchr/testify/assert" - metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" - ctrl "sigs.k8s.io/controller-runtime" - - codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1" -) - -func TestCleanTempDirectory_ShouldRemoveWithSuccessStatus(t *testing.T) { - t.Setenv("WORKING_DIR", "/tmp/1") - - cb := &codebaseApi.CodebaseBranch{ - ObjectMeta: metaV1.ObjectMeta{ - Name: "stub-name", - Namespace: "stub-namespace", - }, - Spec: codebaseApi.CodebaseBranchSpec{ - CodebaseName: "stub-codebase-name", - BranchName: "stub-branch-name", - }, - } - directory := &CleanTempDirectory{} - - err := directory.ServeRequest(ctrl.LoggerInto(context.Background(), logr.Discard()), cb) - assert.NoError(t, err) -} - -func TestCleanTempDirectory_setFailedFields_ShouldPass(t *testing.T) { - cb := &codebaseApi.CodebaseBranch{} - setFailedFields(cb, codebaseApi.AcceptCodebaseBranchRegistration, "test") - assert.Equal(t, cb.Status.DetailedMessage, "test") -} diff --git a/controllers/codebasebranch/chain/factory/factory.go b/controllers/codebasebranch/chain/factory/factory.go index a2059cc5..0bad8cdc 100644 --- a/controllers/codebasebranch/chain/factory/factory.go +++ b/controllers/codebasebranch/chain/factory/factory.go @@ -4,7 +4,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "github.com/epam/edp-codebase-operator/v2/controllers/codebasebranch/chain" - "github.com/epam/edp-codebase-operator/v2/controllers/codebasebranch/chain/clean_tmp_directory" "github.com/epam/edp-codebase-operator/v2/controllers/codebasebranch/chain/empty" "github.com/epam/edp-codebase-operator/v2/controllers/codebasebranch/chain/handler" "github.com/epam/edp-codebase-operator/v2/controllers/codebasebranch/chain/put_branch_in_git" @@ -28,7 +27,6 @@ func GetChain(c client.Client) handler.CodebaseBranchHandler { Client: c, Next: put_codebase_image_stream.PutCodebaseImageStream{ Client: c, - Next: &clean_tmp_directory.CleanTempDirectory{}, }, }, Service: &service.CodebaseBranchServiceProvider{ diff --git a/controllers/codebasebranch/chain/util.go b/controllers/codebasebranch/chain/util.go index 5c74304d..f40ebc5f 100644 --- a/controllers/codebasebranch/chain/util.go +++ b/controllers/codebasebranch/chain/util.go @@ -2,11 +2,9 @@ package chain import ( "fmt" - "path" "slices" codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1" - "github.com/epam/edp-codebase-operator/v2/pkg/util" ) // HasNewVersion checks if codebase branch has new version. @@ -17,11 +15,3 @@ func HasNewVersion(codebaseBranch *codebaseApi.CodebaseBranch) (bool, error) { return !slices.Contains(codebaseBranch.Status.VersionHistory, *codebaseBranch.Spec.Version), nil } - -func GetCodebaseBranchWorkingDirectory(codebaseBranch *codebaseApi.CodebaseBranch) string { - return path.Join( - util.GetWorkDir(codebaseBranch.Spec.CodebaseName, codebaseBranch.Namespace), - "codebase-branches", - codebaseBranch.Name, - ) -} diff --git a/controllers/codebasebranch/codebasebranch_controller.go b/controllers/codebasebranch/codebasebranch_controller.go index 3b406e42..be5a7f36 100644 --- a/controllers/codebasebranch/codebasebranch_controller.go +++ b/controllers/codebasebranch/codebasebranch_controller.go @@ -282,10 +282,6 @@ func (r *ReconcileCodebaseBranch) tryToDeleteCodebaseBranch(ctx context.Context, } } - if err := removeDirectoryIfExists(cb.Spec.CodebaseName, cb.Name, cb.Namespace); err != nil { - return &reconcile.Result{}, fmt.Errorf("failed to remove codebase branch directory: %w", err) - } - err := retry.RetryOnConflict(retry.DefaultRetry, func() error { // Fetch the resource here; we need to refetch it on every try, since // if we got a conflict on the last update attempt then we need to get @@ -311,15 +307,6 @@ func (r *ReconcileCodebaseBranch) tryToDeleteCodebaseBranch(ctx context.Context, return &reconcile.Result{}, nil } -func removeDirectoryIfExists(codebaseName, branchName, namespace string) error { - wd := util.GetWorkDir(codebaseName, fmt.Sprintf("%v-%v", namespace, branchName)) - if err := util.RemoveDirectory(wd); err != nil { - return fmt.Errorf("failed to remove directory %q: %w", wd, err) - } - - return nil -} - // setFailureCount increments failure count and returns delay for next reconciliation. func (r *ReconcileCodebaseBranch) setFailureCount(c *codebaseApi.CodebaseBranch) time.Duration { const defaultDuration = 10 * time.Second diff --git a/pkg/git/git.go b/pkg/git/git.go index 1e0b9588..436ec365 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -48,19 +48,9 @@ type Git interface { // Init initializes a new git repository. Init(ctx context.Context, directory string) error - // Fetch fetches changes from the remote repository. - // branchName: specific branch to fetch (empty string fetches all). - Fetch(ctx context.Context, directory, branchName string) error - // AddRemoteLink adds or updates the remote origin URL. AddRemoteLink(ctx context.Context, directory, remoteURL string) error - // CommitExists checks if a commit with the given hash exists in the repository. - CommitExists(ctx context.Context, directory, hash string) (bool, error) - // CheckoutRemoteBranch fetches from remote and checks out the specified branch. CheckoutRemoteBranch(ctx context.Context, directory, branchName string) error - - // CreateRemoteTag creates a tag from a branch and pushes it to the remote repository. - CreateRemoteTag(ctx context.Context, directory, branchName, tagName string) error } diff --git a/pkg/git/mocks/git_generated.mock.go b/pkg/git/mocks/git_generated.mock.go index d8ca1445..ab4a21cc 100644 --- a/pkg/git/mocks/git_generated.mock.go +++ b/pkg/git/mocks/git_generated.mock.go @@ -435,78 +435,6 @@ func (_c *MockGit_Commit_Call) RunAndReturn(run func(ctx context.Context, direct return _c } -// CommitExists provides a mock function for the type MockGit -func (_mock *MockGit) CommitExists(ctx context.Context, directory string, hash string) (bool, error) { - ret := _mock.Called(ctx, directory, hash) - - if len(ret) == 0 { - panic("no return value specified for CommitExists") - } - - var r0 bool - var r1 error - if returnFunc, ok := ret.Get(0).(func(context.Context, string, string) (bool, error)); ok { - return returnFunc(ctx, directory, hash) - } - if returnFunc, ok := ret.Get(0).(func(context.Context, string, string) bool); ok { - r0 = returnFunc(ctx, directory, hash) - } else { - r0 = ret.Get(0).(bool) - } - if returnFunc, ok := ret.Get(1).(func(context.Context, string, string) error); ok { - r1 = returnFunc(ctx, directory, hash) - } else { - r1 = ret.Error(1) - } - return r0, r1 -} - -// MockGit_CommitExists_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CommitExists' -type MockGit_CommitExists_Call struct { - *mock.Call -} - -// CommitExists is a helper method to define mock.On call -// - ctx context.Context -// - directory string -// - hash string -func (_e *MockGit_Expecter) CommitExists(ctx interface{}, directory interface{}, hash interface{}) *MockGit_CommitExists_Call { - return &MockGit_CommitExists_Call{Call: _e.mock.On("CommitExists", ctx, directory, hash)} -} - -func (_c *MockGit_CommitExists_Call) Run(run func(ctx context.Context, directory string, hash string)) *MockGit_CommitExists_Call { - _c.Call.Run(func(args mock.Arguments) { - var arg0 context.Context - if args[0] != nil { - arg0 = args[0].(context.Context) - } - var arg1 string - if args[1] != nil { - arg1 = args[1].(string) - } - var arg2 string - if args[2] != nil { - arg2 = args[2].(string) - } - run( - arg0, - arg1, - arg2, - ) - }) - return _c -} - -func (_c *MockGit_CommitExists_Call) Return(b bool, err error) *MockGit_CommitExists_Call { - _c.Call.Return(b, err) - return _c -} - -func (_c *MockGit_CommitExists_Call) RunAndReturn(run func(ctx context.Context, directory string, hash string) (bool, error)) *MockGit_CommitExists_Call { - _c.Call.Return(run) - return _c -} - // CreateChildBranch provides a mock function for the type MockGit func (_mock *MockGit) CreateChildBranch(ctx context.Context, directory string, parentBranch string, newBranch string) error { ret := _mock.Called(ctx, directory, parentBranch, newBranch) @@ -645,138 +573,6 @@ func (_c *MockGit_CreateRemoteBranchViaRefUpdate_Call) RunAndReturn(run func(ctx return _c } -// CreateRemoteTag provides a mock function for the type MockGit -func (_mock *MockGit) CreateRemoteTag(ctx context.Context, directory string, branchName string, tagName string) error { - ret := _mock.Called(ctx, directory, branchName, tagName) - - if len(ret) == 0 { - panic("no return value specified for CreateRemoteTag") - } - - var r0 error - if returnFunc, ok := ret.Get(0).(func(context.Context, string, string, string) error); ok { - r0 = returnFunc(ctx, directory, branchName, tagName) - } else { - r0 = ret.Error(0) - } - return r0 -} - -// MockGit_CreateRemoteTag_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CreateRemoteTag' -type MockGit_CreateRemoteTag_Call struct { - *mock.Call -} - -// CreateRemoteTag is a helper method to define mock.On call -// - ctx context.Context -// - directory string -// - branchName string -// - tagName string -func (_e *MockGit_Expecter) CreateRemoteTag(ctx interface{}, directory interface{}, branchName interface{}, tagName interface{}) *MockGit_CreateRemoteTag_Call { - return &MockGit_CreateRemoteTag_Call{Call: _e.mock.On("CreateRemoteTag", ctx, directory, branchName, tagName)} -} - -func (_c *MockGit_CreateRemoteTag_Call) Run(run func(ctx context.Context, directory string, branchName string, tagName string)) *MockGit_CreateRemoteTag_Call { - _c.Call.Run(func(args mock.Arguments) { - var arg0 context.Context - if args[0] != nil { - arg0 = args[0].(context.Context) - } - var arg1 string - if args[1] != nil { - arg1 = args[1].(string) - } - var arg2 string - if args[2] != nil { - arg2 = args[2].(string) - } - var arg3 string - if args[3] != nil { - arg3 = args[3].(string) - } - run( - arg0, - arg1, - arg2, - arg3, - ) - }) - return _c -} - -func (_c *MockGit_CreateRemoteTag_Call) Return(err error) *MockGit_CreateRemoteTag_Call { - _c.Call.Return(err) - return _c -} - -func (_c *MockGit_CreateRemoteTag_Call) RunAndReturn(run func(ctx context.Context, directory string, branchName string, tagName string) error) *MockGit_CreateRemoteTag_Call { - _c.Call.Return(run) - return _c -} - -// Fetch provides a mock function for the type MockGit -func (_mock *MockGit) Fetch(ctx context.Context, directory string, branchName string) error { - ret := _mock.Called(ctx, directory, branchName) - - if len(ret) == 0 { - panic("no return value specified for Fetch") - } - - var r0 error - if returnFunc, ok := ret.Get(0).(func(context.Context, string, string) error); ok { - r0 = returnFunc(ctx, directory, branchName) - } else { - r0 = ret.Error(0) - } - return r0 -} - -// MockGit_Fetch_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Fetch' -type MockGit_Fetch_Call struct { - *mock.Call -} - -// Fetch is a helper method to define mock.On call -// - ctx context.Context -// - directory string -// - branchName string -func (_e *MockGit_Expecter) Fetch(ctx interface{}, directory interface{}, branchName interface{}) *MockGit_Fetch_Call { - return &MockGit_Fetch_Call{Call: _e.mock.On("Fetch", ctx, directory, branchName)} -} - -func (_c *MockGit_Fetch_Call) Run(run func(ctx context.Context, directory string, branchName string)) *MockGit_Fetch_Call { - _c.Call.Run(func(args mock.Arguments) { - var arg0 context.Context - if args[0] != nil { - arg0 = args[0].(context.Context) - } - var arg1 string - if args[1] != nil { - arg1 = args[1].(string) - } - var arg2 string - if args[2] != nil { - arg2 = args[2].(string) - } - run( - arg0, - arg1, - arg2, - ) - }) - return _c -} - -func (_c *MockGit_Fetch_Call) Return(err error) *MockGit_Fetch_Call { - _c.Call.Return(err) - return _c -} - -func (_c *MockGit_Fetch_Call) RunAndReturn(run func(ctx context.Context, directory string, branchName string) error) *MockGit_Fetch_Call { - _c.Call.Return(run) - return _c -} - // GetCurrentBranchName provides a mock function for the type MockGit func (_mock *MockGit) GetCurrentBranchName(ctx context.Context, directory string) (string, error) { ret := _mock.Called(ctx, directory) diff --git a/pkg/git/provider.go b/pkg/git/provider.go index 9e9b6ec6..c2fe4081 100644 --- a/pkg/git/provider.go +++ b/pkg/git/provider.go @@ -546,42 +546,6 @@ func (p *GitProvider) Init(ctx context.Context, directory string) error { return nil } -// Fetch fetches changes from the remote repository. -func (p *GitProvider) Fetch(ctx context.Context, directory, branchName string) error { - log := ctrl.LoggerFrom(ctx).WithValues("directory", directory, "branch", branchName) - log.Info("Fetching changes") - - repo, err := git.PlainOpen(directory) - if err != nil { - return fmt.Errorf("failed to open repository at %q: %w", directory, err) - } - - auth, err := p.getAuth() - if err != nil { - return fmt.Errorf("failed to get authentication: %w", err) - } - - fetchOptions := &git.FetchOptions{ - RemoteName: "origin", - Auth: auth, - Progress: os.Stdout, - } - - if branchName != "" { - refSpec := fmt.Sprintf("refs/heads/%s:refs/heads/%s", branchName, branchName) - fetchOptions.RefSpecs = []config.RefSpec{config.RefSpec(refSpec)} - } - - err = repo.FetchContext(ctx, fetchOptions) - if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { - return fmt.Errorf("failed to fetch: %w", err) - } - - log.Info("Changes fetched successfully") - - return nil -} - // AddRemoteLink adds or updates the remote origin URL. func (p *GitProvider) AddRemoteLink(ctx context.Context, directory, remoteURL string) error { log := ctrl.LoggerFrom(ctx).WithValues("directory", directory, "remoteURL", remoteURL) @@ -612,32 +576,6 @@ func (p *GitProvider) AddRemoteLink(ctx context.Context, directory, remoteURL st return nil } -// CommitExists checks if a commit with the given hash exists in the repository. -func (p *GitProvider) CommitExists(ctx context.Context, directory, hash string) (bool, error) { - log := ctrl.LoggerFrom(ctx).WithValues("directory", directory, "hash", hash) - log.Info("Checking if commit exists") - - repo, err := git.PlainOpen(directory) - if err != nil { - return false, fmt.Errorf("failed to open repository at %q: %w", directory, err) - } - - commitHash := plumbing.NewHash(hash) - - _, err = repo.CommitObject(commitHash) - if err != nil { - if errors.Is(err, plumbing.ErrObjectNotFound) { - return false, nil - } - - return false, fmt.Errorf("failed to get commit: %w", err) - } - - log.Info("Commit exists") - - return true, nil -} - // CheckoutRemoteBranch fetches from remote and checks out the specified branch. // This is a convenience method that fetches and checks out a remote branch. func (p *GitProvider) CheckoutRemoteBranch(ctx context.Context, directory, branchName string) error { @@ -688,62 +626,3 @@ func (p *GitProvider) CheckoutRemoteBranch(ctx context.Context, directory, branc return nil } - -// CreateRemoteTag creates a tag from a branch and pushes it to the remote repository. -func (p *GitProvider) CreateRemoteTag(ctx context.Context, directory, branchName, tagName string) error { - log := ctrl.LoggerFrom(ctx).WithValues("directory", directory, "branch", branchName, "tag", tagName) - log.Info("Creating remote tag") - - repo, err := git.PlainOpen(directory) - if err != nil { - return fmt.Errorf("failed to open repository at %q: %w", directory, err) - } - - // Check if tag already exists - tags, err := repo.Tags() - if err != nil { - return fmt.Errorf("failed to get tags: %w", err) - } - - exists := false - - err = tags.ForEach(func(ref *plumbing.Reference) error { - if ref.Name().Short() == tagName { - exists = true - } - - return nil - }) - if err != nil { - return fmt.Errorf("failed to iterate tags: %w", err) - } - - if exists { - log.Info("Tag already exists, skipping creation") - return nil - } - - // Get the branch reference - branchRef, err := repo.Reference(plumbing.NewBranchReferenceName(branchName), false) - if err != nil { - return fmt.Errorf("failed to get branch reference: %w", err) - } - - // Create the tag reference - tagRef := plumbing.NewHashReference(plumbing.NewTagReferenceName(tagName), branchRef.Hash()) - - err = repo.Storer.SetReference(tagRef) - if err != nil { - return fmt.Errorf("failed to create tag reference: %w", err) - } - - // Push the tag - err = p.Push(ctx, directory, RefSpecPushAllTags) - if err != nil { - return fmt.Errorf("failed to push tag: %w", err) - } - - log.Info("Remote tag created successfully") - - return nil -} diff --git a/pkg/git/provider_test.go b/pkg/git/provider_test.go index 1768990d..fbaefd1d 100644 --- a/pkg/git/provider_test.go +++ b/pkg/git/provider_test.go @@ -733,124 +733,6 @@ func TestGitProvider_GetCurrentBranchName(t *testing.T) { } } -func TestGitProvider_CommitExists(t *testing.T) { - tests := []struct { - name string - initRepo func(t *testing.T) (string, string) - commitHash string - wantExists bool - wantErr bool - }{ - { - name: "commit exists", - initRepo: func(t *testing.T) (string, string) { - dir := t.TempDir() - r, err := gogit.PlainInit(dir, false) - require.NoError(t, err) - - w, err := r.Worktree() - require.NoError(t, err) - - f, err := os.Create(path.Join(dir, "test.txt")) - require.NoError(t, err) - _, err = f.WriteString("test") - require.NoError(t, err) - require.NoError(t, f.Close()) - - _, err = w.Add("test.txt") - require.NoError(t, err) - - hash, err := w.Commit("initial", &gogit.CommitOptions{ - Author: &object.Signature{ - Name: "test", - Email: "test@example.com", - When: time.Now(), - }, - }) - require.NoError(t, err) - - return dir, hash.String() - }, - wantExists: true, - wantErr: false, - }, - { - name: "commit does not exist", - initRepo: func(t *testing.T) (string, string) { - dir := t.TempDir() - _, err := gogit.PlainInit(dir, false) - require.NoError(t, err) - - // Return a valid-looking but non-existent commit hash - return dir, "0000000000000000000000000000000000000000" - }, - wantExists: false, - wantErr: false, - }, - { - name: "invalid hash - treated as not found", - initRepo: func(t *testing.T) (string, string) { - dir := t.TempDir() - r, err := gogit.PlainInit(dir, false) - require.NoError(t, err) - - // Create a commit so the repo has valid objects - w, err := r.Worktree() - require.NoError(t, err) - - f, err := os.Create(path.Join(dir, "test.txt")) - require.NoError(t, err) - _, err = f.WriteString("test") - require.NoError(t, err) - require.NoError(t, f.Close()) - - _, err = w.Add("test.txt") - require.NoError(t, err) - - _, err = w.Commit("initial", &gogit.CommitOptions{ - Author: &object.Signature{ - Name: "test", - Email: "test@example.com", - When: time.Now(), - }, - }) - require.NoError(t, err) - - // Invalid hash format is treated as not found, not an error - return dir, "invalid-hash" - }, - wantExists: false, - wantErr: false, - }, - { - name: "repository error", - initRepo: func(t *testing.T) (string, string) { - // Return a non-git directory - return t.TempDir(), "0000000000000000000000000000000000000000" - }, - wantExists: false, - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gp := NewGitProvider(Config{}) - dir, hash := tt.initRepo(t) - - exists, err := gp.CommitExists(context.Background(), dir, hash) - - if tt.wantErr { - require.Error(t, err) - return - } - - require.NoError(t, err) - assert.Equal(t, tt.wantExists, exists) - }) - } -} - func TestGitProvider_Checkout_LocalMode(t *testing.T) { tests := []struct { name string @@ -931,55 +813,6 @@ func TestGitProvider_Checkout_LocalMode(t *testing.T) { } } -func TestGitProvider_CreateRemoteTag_Errors(t *testing.T) { - tests := []struct { - name string - initRepo func(t *testing.T) string - tagName string - branchName string - wantErr bool - }{ - { - name: "repository not found", - initRepo: func(t *testing.T) string { - return t.TempDir() - }, - tagName: "v1.0.0", - branchName: "master", - wantErr: true, - }, - { - name: "branch not found", - initRepo: func(t *testing.T) string { - dir := t.TempDir() - _, err := gogit.PlainInit(dir, false) - require.NoError(t, err) - - return dir - }, - tagName: "v1.0.0", - branchName: "non-existent-branch", - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gp := NewGitProvider(Config{}) - dir := tt.initRepo(t) - - err := gp.CreateRemoteTag(context.Background(), dir, tt.tagName, tt.branchName) - - if tt.wantErr { - require.Error(t, err) - return - } - - require.NoError(t, err) - }) - } -} - func TestGitProvider_Clone_Errors(t *testing.T) { tests := []struct { name string @@ -1312,98 +1145,6 @@ func TestGitProvider_Push_NoRemote(t *testing.T) { } } -func TestGitProvider_Fetch_NoRemote(t *testing.T) { - tests := []struct { - name string - initRepo func(t *testing.T) string - branchName string - wantErr bool - }{ - { - name: "fetch without remote configured", - initRepo: func(t *testing.T) string { - dir := t.TempDir() - r, err := gogit.PlainInit(dir, false) - require.NoError(t, err) - - w, err := r.Worktree() - require.NoError(t, err) - - f, err := os.Create(path.Join(dir, "test.txt")) - require.NoError(t, err) - _, err = f.WriteString("test") - require.NoError(t, err) - require.NoError(t, f.Close()) - - _, err = w.Add("test.txt") - require.NoError(t, err) - - _, err = w.Commit("initial", &gogit.CommitOptions{ - Author: &object.Signature{ - Name: "test", - Email: "test@example.com", - When: time.Now(), - }, - }) - require.NoError(t, err) - - return dir - }, - branchName: "", - wantErr: true, - }, - { - name: "fetch specific branch without remote", - initRepo: func(t *testing.T) string { - dir := t.TempDir() - r, err := gogit.PlainInit(dir, false) - require.NoError(t, err) - - w, err := r.Worktree() - require.NoError(t, err) - - f, err := os.Create(path.Join(dir, "test.txt")) - require.NoError(t, err) - _, err = f.WriteString("test") - require.NoError(t, err) - require.NoError(t, f.Close()) - - _, err = w.Add("test.txt") - require.NoError(t, err) - - _, err = w.Commit("initial", &gogit.CommitOptions{ - Author: &object.Signature{ - Name: "test", - Email: "test@example.com", - When: time.Now(), - }, - }) - require.NoError(t, err) - - return dir - }, - branchName: "master", - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gp := NewGitProvider(Config{}) - dir := tt.initRepo(t) - - err := gp.Fetch(context.Background(), dir, tt.branchName) - - if tt.wantErr { - require.Error(t, err) - return - } - - require.NoError(t, err) - }) - } -} - func TestGitProvider_CheckoutRemoteBranch_NoRemote(t *testing.T) { tests := []struct { name string @@ -1462,64 +1203,3 @@ func TestGitProvider_CheckoutRemoteBranch_NoRemote(t *testing.T) { }) } } - -func TestGitProvider_CreateRemoteTag_NoRemote(t *testing.T) { - tests := []struct { - name string - initRepo func(t *testing.T) string - tagName string - branchName string - wantErr bool - }{ - { - name: "create tag without remote", - initRepo: func(t *testing.T) string { - dir := t.TempDir() - r, err := gogit.PlainInit(dir, false) - require.NoError(t, err) - - w, err := r.Worktree() - require.NoError(t, err) - - f, err := os.Create(path.Join(dir, "test.txt")) - require.NoError(t, err) - _, err = f.WriteString("test") - require.NoError(t, err) - require.NoError(t, f.Close()) - - _, err = w.Add("test.txt") - require.NoError(t, err) - - _, err = w.Commit("initial", &gogit.CommitOptions{ - Author: &object.Signature{ - Name: "test", - Email: "test@example.com", - When: time.Now(), - }, - }) - require.NoError(t, err) - - return dir - }, - tagName: "v1.0.0", - branchName: "master", - wantErr: true, // Will fail on push - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gp := NewGitProvider(Config{}) - dir := tt.initRepo(t) - - err := gp.CreateRemoteTag(context.Background(), dir, tt.tagName, tt.branchName) - - if tt.wantErr { - require.Error(t, err) - return - } - - require.NoError(t, err) - }) - } -}