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

@@ -5,17 +5,21 @@ import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)
// Config holds the configuration for a Git client
type Config struct {
URL string
Username string
Token string
WorkDir string
URL string
Username string
Token string
WorkDir string
CommitName string
CommitEmail string
}
// Client defines the interface for Git operations
@@ -34,13 +38,15 @@ type client struct {
}
// New creates a new git Client instance
func New(url, username, token, workDir string) Client {
func New(url, username, token, workDir, commitName, commitEmail string) Client {
return &client{
Config: Config{
URL: url,
Username: username,
Token: token,
WorkDir: workDir,
URL: url,
Username: username,
Token: token,
WorkDir: workDir,
CommitName: commitName,
CommitEmail: commitEmail,
},
}
}
@@ -110,7 +116,13 @@ func (c *client) Commit(message string) error {
return fmt.Errorf("failed to add changes: %w", err)
}
_, err = w.Commit(message, &git.CommitOptions{})
_, err = w.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: c.CommitName,
Email: c.CommitEmail,
When: time.Now(),
},
})
if err != nil {
return fmt.Errorf("failed to commit changes: %w", err)
}

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.