Add commit name and commit email to git client

This commit is contained in:
2024-11-30 12:54:47 +01:00
parent 453c022959
commit 325cedc235
5 changed files with 35 additions and 19 deletions

View File

@@ -59,6 +59,7 @@ type mockFS struct {
StatError error
}
//revive:disable:unexported-return
func NewMockFS() *mockFS {
return &mockFS{
ReadCalls: make(map[string]int),

View File

@@ -7,7 +7,7 @@ import (
// RepositoryManager defines the interface for managing Git repositories.
type RepositoryManager interface {
SetupGitRepo(userID, workspaceID int, gitURL, gitUser, gitToken string) error
SetupGitRepo(userID, workspaceID int, gitURL, gitUser, gitToken, commitName, commitEmail string) error
DisableGitRepo(userID, workspaceID int)
StageCommitAndPush(userID, workspaceID int, message string) error
Pull(userID, workspaceID int) error
@@ -15,12 +15,12 @@ type RepositoryManager interface {
// SetupGitRepo sets up a Git repository for the given userID and workspaceID.
// The repository is cloned from the given gitURL using the given gitUser and gitToken.
func (s *Service) SetupGitRepo(userID, workspaceID int, gitURL, gitUser, gitToken string) error {
func (s *Service) SetupGitRepo(userID, workspaceID int, gitURL, gitUser, gitToken, commitName, commitEmail string) error {
workspacePath := s.GetWorkspacePath(userID, workspaceID)
if _, ok := s.GitRepos[userID]; !ok {
s.GitRepos[userID] = make(map[int]git.Client)
}
s.GitRepos[userID][workspaceID] = s.newGitClient(gitURL, gitUser, gitToken, workspacePath)
s.GitRepos[userID][workspaceID] = s.newGitClient(gitURL, gitUser, gitToken, workspacePath, commitName, commitEmail)
return s.GitRepos[userID][workspaceID].EnsureRepo()
}

View File

@@ -55,6 +55,7 @@ func TestSetupGitRepo(t *testing.T) {
gitURL string
gitUser string
gitToken string
commitEmail string
mockErr error
wantErr bool
}{
@@ -65,6 +66,7 @@ func TestSetupGitRepo(t *testing.T) {
gitURL: "https://github.com/user/repo",
gitUser: "user",
gitToken: "token",
commitEmail: "test@example.com",
mockErr: nil,
wantErr: false,
},
@@ -75,6 +77,7 @@ func TestSetupGitRepo(t *testing.T) {
gitURL: "https://github.com/user/repo",
gitUser: "user",
gitToken: "token",
commitEmail: "test@example.com",
mockErr: errors.New("git initialization failed"),
wantErr: true,
},
@@ -86,7 +89,7 @@ func TestSetupGitRepo(t *testing.T) {
mockClient := &MockGitClient{ReturnError: tc.mockErr}
// Create a client factory that returns our configured mock
mockClientFactory := func(_, _, _, _ string) git.Client {
mockClientFactory := func(_, _, _, _, _, _ string) git.Client {
return mockClient
}
@@ -96,7 +99,7 @@ func TestSetupGitRepo(t *testing.T) {
})
// Setup the git repo
err := s.SetupGitRepo(tc.userID, tc.workspaceID, tc.gitURL, tc.gitUser, tc.gitToken)
err := s.SetupGitRepo(tc.userID, tc.workspaceID, tc.gitURL, tc.gitUser, tc.gitToken, tc.gitUser, tc.commitEmail)
if tc.wantErr {
if err == nil {
@@ -131,7 +134,7 @@ func TestGitOperations(t *testing.T) {
mockFS := NewMockFS()
s := storage.NewServiceWithOptions("test-root", storage.Options{
Fs: mockFS,
NewGitClient: func(_, _, _, _ string) git.Client { return &MockGitClient{} },
NewGitClient: func(_, _, _, _, _, _ string) git.Client { return &MockGitClient{} },
})
t.Run("operations on non-configured workspace", func(t *testing.T) {
@@ -203,7 +206,7 @@ func TestDisableGitRepo(t *testing.T) {
mockFS := NewMockFS()
s := storage.NewServiceWithOptions("test-root", storage.Options{
Fs: mockFS,
NewGitClient: func(_, _, _, _ string) git.Client { return &MockGitClient{} },
NewGitClient: func(_, _, _, _, _, _ string) git.Client { return &MockGitClient{} },
})
testCases := []struct {

View File

@@ -14,7 +14,7 @@ type Manager interface {
// Service represents the file system structure.
type Service struct {
fs fileSystem
newGitClient func(url, user, token, path string) git.Client
newGitClient func(url, user, token, path, commitName, commitEmail string) git.Client
RootDir string
GitRepos map[int]map[int]git.Client // map[userID]map[workspaceID]*git.Client
}
@@ -22,7 +22,7 @@ type Service struct {
// Options represents the options for the storage service.
type Options struct {
Fs fileSystem
NewGitClient func(url, user, token, path string) git.Client
NewGitClient func(url, user, token, path, commitName, commitEmail string) git.Client
}
// NewService creates a new Storage instance with the default options and the given rootDir root directory.