From 31d00681a1ccaaba8273d31ddbbd5311478fe7d5 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sat, 30 Nov 2024 16:12:51 +0100 Subject: [PATCH] Update handlers and db --- server/internal/db/migrations_test.go | 16 ++++----- server/internal/db/users.go | 6 ++-- server/internal/db/workspaces.go | 9 +++-- server/internal/handlers/integration_test.go | 2 +- .../internal/handlers/workspace_handlers.go | 8 ++++- .../workspace_handlers_integration_test.go | 34 ++++++++++++------- server/internal/models/workspace.go | 2 +- 7 files changed, 48 insertions(+), 29 deletions(-) diff --git a/server/internal/db/migrations_test.go b/server/internal/db/migrations_test.go index 0b966c0..7272793 100644 --- a/server/internal/db/migrations_test.go +++ b/server/internal/db/migrations_test.go @@ -27,8 +27,8 @@ func TestMigrate(t *testing.T) { t.Fatalf("failed to get migration version: %v", err) } - if version != 2 { // Current number of migrations in production code - t.Errorf("expected migration version 2, got %d", version) + if version != 1 { // Current number of migrations in production code + t.Errorf("expected migration version 1, got %d", version) } // Verify number of migration entries matches versions applied @@ -38,8 +38,8 @@ func TestMigrate(t *testing.T) { t.Fatalf("failed to count migrations: %v", err) } - if count != 2 { - t.Errorf("expected 2 migration entries, got %d", count) + if count != 1 { + t.Errorf("expected 1 migration entries, got %d", count) } }) @@ -82,8 +82,8 @@ func TestMigrate(t *testing.T) { t.Fatalf("failed to count migrations: %v", err) } - if count != 2 { - t.Errorf("expected 2 migration entries, got %d", count) + if count != 1 { + t.Errorf("expected 1 migration entries, got %d", count) } }) @@ -118,8 +118,8 @@ func TestMigrate(t *testing.T) { t.Fatalf("failed to get migration version: %v", err) } - if version != 2 { - t.Errorf("expected migration version to remain at 2, got %d", version) + if version != 1 { + t.Errorf("expected migration version to remain at 1, got %d", version) } }) } diff --git a/server/internal/db/users.go b/server/internal/db/users.go index 618dd3d..ecb2da1 100644 --- a/server/internal/db/users.go +++ b/server/internal/db/users.go @@ -68,12 +68,14 @@ func (db *database) createWorkspaceTx(tx *sql.Tx, workspace *models.Workspace) e user_id, name, theme, auto_save, show_hidden_files, git_enabled, git_url, git_user, git_token, - git_auto_commit, git_commit_msg_template - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + git_auto_commit, git_commit_msg_template, + git_commit_name, git_commit_email + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, workspace.UserID, workspace.Name, workspace.Theme, workspace.AutoSave, workspace.ShowHiddenFiles, workspace.GitEnabled, workspace.GitURL, workspace.GitUser, workspace.GitToken, workspace.GitAutoCommit, workspace.GitCommitMsgTemplate, + workspace.GitCommitName, workspace.GitCommitEmail, ) if err != nil { return err diff --git a/server/internal/db/workspaces.go b/server/internal/db/workspaces.go index e7aa296..667cf98 100644 --- a/server/internal/db/workspaces.go +++ b/server/internal/db/workspaces.go @@ -23,7 +23,8 @@ func (db *database) CreateWorkspace(workspace *models.Workspace) error { INSERT INTO workspaces ( user_id, name, theme, auto_save, show_hidden_files, git_enabled, git_url, git_user, git_token, - git_auto_commit, git_commit_msg_template, git_commit_name, git_commit_email + git_auto_commit, git_commit_msg_template, + git_commit_name, git_commit_email ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, workspace.UserID, workspace.Name, workspace.Theme, workspace.AutoSave, workspace.ShowHiddenFiles, workspace.GitEnabled, workspace.GitURL, workspace.GitUser, encryptedToken, @@ -51,7 +52,8 @@ func (db *database) GetWorkspaceByID(id int) (*models.Workspace, error) { id, user_id, name, created_at, theme, auto_save, show_hidden_files, git_enabled, git_url, git_user, git_token, - git_auto_commit, git_commit_msg_template, git_commit_name, git_commit_email + git_auto_commit, git_commit_msg_template, + git_commit_name, git_commit_email FROM workspaces WHERE id = ?`, id, @@ -93,7 +95,8 @@ func (db *database) GetWorkspaceByName(userID int, workspaceName string) (*model &workspace.ID, &workspace.UserID, &workspace.Name, &workspace.CreatedAt, &workspace.Theme, &workspace.AutoSave, &workspace.ShowHiddenFiles, &workspace.GitEnabled, &workspace.GitURL, &workspace.GitUser, &encryptedToken, - &workspace.GitAutoCommit, &workspace.GitCommitMsgTemplate, &workspace.GitCommitName, &workspace.GitCommitEmail, + &workspace.GitAutoCommit, &workspace.GitCommitMsgTemplate, + &workspace.GitCommitName, &workspace.GitCommitEmail, ) if err != nil { return nil, err diff --git a/server/internal/handlers/integration_test.go b/server/internal/handlers/integration_test.go index 5c65f16..9b380e9 100644 --- a/server/internal/handlers/integration_test.go +++ b/server/internal/handlers/integration_test.go @@ -70,7 +70,7 @@ func setupTestHarness(t *testing.T) *testHarness { // Create storage with mock git client storageOpts := storage.Options{ - NewGitClient: func(url, user, token, path string) git.Client { + NewGitClient: func(url, user, token, path, commitName, commitEmail string) git.Client { return mockGit }, } diff --git a/server/internal/handlers/workspace_handlers.go b/server/internal/handlers/workspace_handlers.go index dc4d94d..514c9ff 100644 --- a/server/internal/handlers/workspace_handlers.go +++ b/server/internal/handlers/workspace_handlers.go @@ -64,6 +64,8 @@ func (h *Handler) CreateWorkspace() http.HandlerFunc { workspace.GitURL, workspace.GitUser, workspace.GitToken, + workspace.GitCommitName, + workspace.GitCommitEmail, ); err != nil { http.Error(w, "Failed to setup git repo: "+err.Error(), http.StatusInternalServerError) return @@ -96,7 +98,9 @@ func gitSettingsChanged(new, old *models.Workspace) bool { if new.GitEnabled { return new.GitURL != old.GitURL || new.GitUser != old.GitUser || - new.GitToken != old.GitToken + new.GitToken != old.GitToken || + new.GitCommitName != old.GitCommitName || + new.GitCommitEmail != old.GitCommitEmail } return false @@ -135,6 +139,8 @@ func (h *Handler) UpdateWorkspace() http.HandlerFunc { workspace.GitURL, workspace.GitUser, workspace.GitToken, + workspace.GitCommitName, + workspace.GitCommitEmail, ); err != nil { http.Error(w, "Failed to setup git repo: "+err.Error(), http.StatusInternalServerError) return diff --git a/server/internal/handlers/workspace_handlers_integration_test.go b/server/internal/handlers/workspace_handlers_integration_test.go index 724350c..0d44b73 100644 --- a/server/internal/handlers/workspace_handlers_integration_test.go +++ b/server/internal/handlers/workspace_handlers_integration_test.go @@ -54,12 +54,14 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { t.Run("create with git settings", func(t *testing.T) { workspace := &models.Workspace{ - Name: "Git Workspace", - GitEnabled: true, - GitURL: "https://github.com/test/repo.git", - GitUser: "testuser", - GitToken: "testtoken", - GitAutoCommit: true, + Name: "Git Workspace", + GitEnabled: true, + GitURL: "https://github.com/test/repo.git", + GitUser: "testuser", + GitToken: "testtoken", + GitAutoCommit: true, + GitCommitName: "Test User", + GitCommitEmail: "test@example.com", } rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularToken, nil) @@ -73,6 +75,8 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { assert.Equal(t, workspace.GitUser, created.GitUser) assert.Equal(t, workspace.GitToken, created.GitToken) assert.Equal(t, workspace.GitAutoCommit, created.GitAutoCommit) + assert.Equal(t, workspace.GitCommitName, created.GitCommitName) + assert.Equal(t, workspace.GitCommitEmail, created.GitCommitEmail) }) t.Run("invalid workspace", func(t *testing.T) { @@ -161,13 +165,15 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { t.Run("enable git", func(t *testing.T) { update := &models.Workspace{ - Name: workspace.Name, - Theme: "dark", - GitEnabled: true, - GitURL: "https://github.com/test/repo.git", - GitUser: "testuser", - GitToken: "testtoken", - GitAutoCommit: true, + Name: workspace.Name, + Theme: "dark", + GitEnabled: true, + GitURL: "https://github.com/test/repo.git", + GitUser: "testuser", + GitToken: "testtoken", + GitAutoCommit: true, + GitCommitName: "Test User", + GitCommitEmail: "test@example.com", } rr := h.makeRequest(t, http.MethodPut, baseURL, update, h.RegularToken, nil) @@ -180,6 +186,8 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { assert.Equal(t, update.GitURL, updated.GitURL) assert.Equal(t, update.GitUser, updated.GitUser) assert.Equal(t, update.GitToken, updated.GitToken) + assert.Equal(t, update.GitAutoCommit, updated.GitAutoCommit) + assert.Equal(t, update.GitCommitName, updated.GitCommitName) // Mock should have been called to setup git assert.True(t, h.MockGit.IsInitialized()) diff --git a/server/internal/models/workspace.go b/server/internal/models/workspace.go index 858e5ef..6589957 100644 --- a/server/internal/models/workspace.go +++ b/server/internal/models/workspace.go @@ -23,7 +23,7 @@ type Workspace struct { GitAutoCommit bool `json:"gitAutoCommit"` GitCommitMsgTemplate string `json:"gitCommitMsgTemplate"` GitCommitName string `json:"gitCommitName"` - GitCommitEmail string `json:"gitCommitEmail" validate:"required_if=GitEnabled true,email"` + GitCommitEmail string `json:"gitCommitEmail" validate:"omitempty,required_if=GitEnabled true,email"` } // Validate validates the workspace struct