mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Add commit name and commit email to git client
This commit is contained in:
@@ -5,17 +5,21 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"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"
|
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config holds the configuration for a Git client
|
// Config holds the configuration for a Git client
|
||||||
type Config struct {
|
type Config struct {
|
||||||
URL string
|
URL string
|
||||||
Username string
|
Username string
|
||||||
Token string
|
Token string
|
||||||
WorkDir string
|
WorkDir string
|
||||||
|
CommitName string
|
||||||
|
CommitEmail string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client defines the interface for Git operations
|
// Client defines the interface for Git operations
|
||||||
@@ -34,13 +38,15 @@ type client struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new git Client instance
|
// 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{
|
return &client{
|
||||||
Config: Config{
|
Config: Config{
|
||||||
URL: url,
|
URL: url,
|
||||||
Username: username,
|
Username: username,
|
||||||
Token: token,
|
Token: token,
|
||||||
WorkDir: workDir,
|
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)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to commit changes: %w", err)
|
return fmt.Errorf("failed to commit changes: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ type mockFS struct {
|
|||||||
StatError error
|
StatError error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//revive:disable:unexported-return
|
||||||
func NewMockFS() *mockFS {
|
func NewMockFS() *mockFS {
|
||||||
return &mockFS{
|
return &mockFS{
|
||||||
ReadCalls: make(map[string]int),
|
ReadCalls: make(map[string]int),
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
// RepositoryManager defines the interface for managing Git repositories.
|
// RepositoryManager defines the interface for managing Git repositories.
|
||||||
type RepositoryManager interface {
|
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)
|
DisableGitRepo(userID, workspaceID int)
|
||||||
StageCommitAndPush(userID, workspaceID int, message string) error
|
StageCommitAndPush(userID, workspaceID int, message string) error
|
||||||
Pull(userID, workspaceID int) 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.
|
// 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.
|
// 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)
|
workspacePath := s.GetWorkspacePath(userID, workspaceID)
|
||||||
if _, ok := s.GitRepos[userID]; !ok {
|
if _, ok := s.GitRepos[userID]; !ok {
|
||||||
s.GitRepos[userID] = make(map[int]git.Client)
|
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()
|
return s.GitRepos[userID][workspaceID].EnsureRepo()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ func TestSetupGitRepo(t *testing.T) {
|
|||||||
gitURL string
|
gitURL string
|
||||||
gitUser string
|
gitUser string
|
||||||
gitToken string
|
gitToken string
|
||||||
|
commitEmail string
|
||||||
mockErr error
|
mockErr error
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
@@ -65,6 +66,7 @@ func TestSetupGitRepo(t *testing.T) {
|
|||||||
gitURL: "https://github.com/user/repo",
|
gitURL: "https://github.com/user/repo",
|
||||||
gitUser: "user",
|
gitUser: "user",
|
||||||
gitToken: "token",
|
gitToken: "token",
|
||||||
|
commitEmail: "test@example.com",
|
||||||
mockErr: nil,
|
mockErr: nil,
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
@@ -75,6 +77,7 @@ func TestSetupGitRepo(t *testing.T) {
|
|||||||
gitURL: "https://github.com/user/repo",
|
gitURL: "https://github.com/user/repo",
|
||||||
gitUser: "user",
|
gitUser: "user",
|
||||||
gitToken: "token",
|
gitToken: "token",
|
||||||
|
commitEmail: "test@example.com",
|
||||||
mockErr: errors.New("git initialization failed"),
|
mockErr: errors.New("git initialization failed"),
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
@@ -86,7 +89,7 @@ func TestSetupGitRepo(t *testing.T) {
|
|||||||
mockClient := &MockGitClient{ReturnError: tc.mockErr}
|
mockClient := &MockGitClient{ReturnError: tc.mockErr}
|
||||||
|
|
||||||
// Create a client factory that returns our configured mock
|
// Create a client factory that returns our configured mock
|
||||||
mockClientFactory := func(_, _, _, _ string) git.Client {
|
mockClientFactory := func(_, _, _, _, _, _ string) git.Client {
|
||||||
return mockClient
|
return mockClient
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +99,7 @@ func TestSetupGitRepo(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Setup the git repo
|
// 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 tc.wantErr {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -131,7 +134,7 @@ func TestGitOperations(t *testing.T) {
|
|||||||
mockFS := NewMockFS()
|
mockFS := NewMockFS()
|
||||||
s := storage.NewServiceWithOptions("test-root", storage.Options{
|
s := storage.NewServiceWithOptions("test-root", storage.Options{
|
||||||
Fs: mockFS,
|
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) {
|
t.Run("operations on non-configured workspace", func(t *testing.T) {
|
||||||
@@ -203,7 +206,7 @@ func TestDisableGitRepo(t *testing.T) {
|
|||||||
mockFS := NewMockFS()
|
mockFS := NewMockFS()
|
||||||
s := storage.NewServiceWithOptions("test-root", storage.Options{
|
s := storage.NewServiceWithOptions("test-root", storage.Options{
|
||||||
Fs: mockFS,
|
Fs: mockFS,
|
||||||
NewGitClient: func(_, _, _, _ string) git.Client { return &MockGitClient{} },
|
NewGitClient: func(_, _, _, _, _, _ string) git.Client { return &MockGitClient{} },
|
||||||
})
|
})
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ type Manager interface {
|
|||||||
// Service represents the file system structure.
|
// Service represents the file system structure.
|
||||||
type Service struct {
|
type Service struct {
|
||||||
fs fileSystem
|
fs fileSystem
|
||||||
newGitClient func(url, user, token, path string) git.Client
|
newGitClient func(url, user, token, path, commitName, commitEmail string) git.Client
|
||||||
RootDir string
|
RootDir string
|
||||||
GitRepos map[int]map[int]git.Client // map[userID]map[workspaceID]*git.Client
|
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.
|
// Options represents the options for the storage service.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Fs fileSystem
|
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.
|
// NewService creates a new Storage instance with the default options and the given rootDir root directory.
|
||||||
|
|||||||
Reference in New Issue
Block a user