feat(integrations): single workspace switcher with deep links and copy-config dialog (#1616)
This commit is contained in:
@@ -92,6 +92,7 @@ func (c *Controller) RegisterHTTPRoutes(router *gin.Engine) {
|
||||
api.POST("/action-presets/reset", c.httpResetActionPresets)
|
||||
api.GET("/workspace-settings", c.httpGetWorkspaceSettings)
|
||||
api.PUT("/workspace-settings", c.httpUpdateWorkspaceSettings)
|
||||
api.POST("/workspace-settings/copy", c.httpCopyWorkspaceSettings)
|
||||
|
||||
api.GET("/stats", c.httpGetStats)
|
||||
}
|
||||
@@ -898,6 +899,41 @@ func (c *Controller) httpUpdateWorkspaceSettings(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, settings)
|
||||
}
|
||||
|
||||
// copyWorkspaceSettingsRequest carries the target workspace for a copy. The
|
||||
// source workspace comes from the workspace_id query param, matching the other
|
||||
// integration copy endpoints.
|
||||
type copyWorkspaceSettingsRequest struct {
|
||||
TargetWorkspaceID string `json:"targetWorkspaceId"`
|
||||
}
|
||||
|
||||
func (c *Controller) httpCopyWorkspaceSettings(ctx *gin.Context) {
|
||||
source := strings.TrimSpace(ctx.Query("workspace_id"))
|
||||
if source == "" {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "workspace_id query parameter required"})
|
||||
return
|
||||
}
|
||||
var req copyWorkspaceSettingsRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid payload"})
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(req.TargetWorkspaceID) == "" {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "targetWorkspaceId required"})
|
||||
return
|
||||
}
|
||||
settings, err := c.service.CopyWorkspaceSettingsToWorkspace(ctx.Request.Context(), source, req.TargetWorkspaceID)
|
||||
if err != nil {
|
||||
status := http.StatusInternalServerError
|
||||
switch {
|
||||
case errors.Is(err, ErrSameWorkspace), errors.Is(err, ErrWorkspaceSettingsValidation):
|
||||
status = http.StatusBadRequest
|
||||
}
|
||||
ctx.JSON(status, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, settings)
|
||||
}
|
||||
|
||||
// parsePaginationQuery reads `page` and `per_page` query params. Missing or
|
||||
// non-positive values fall back to defaults (page=1, perPage=50). The upper
|
||||
// bound for perPage (100) is applied later by clampSearchPage in the client
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrSameWorkspace is returned when a copy targets the same workspace it reads
|
||||
// from.
|
||||
var ErrSameWorkspace = errors.New("github: source and target workspaces are the same")
|
||||
|
||||
// CopyWorkspaceSettingsToWorkspace copies the per-workspace GitHub operational
|
||||
// settings (repo scope + saved/default query presets) and the workspace's
|
||||
// quick-action presets from sourceWorkspaceID to targetWorkspaceID. GitHub
|
||||
// authentication is install-wide, so there are no credentials to copy — only the
|
||||
// workspace-scoped settings. Watchers are intentionally out of scope.
|
||||
func (s *Service) CopyWorkspaceSettingsToWorkspace(ctx context.Context, sourceWorkspaceID, targetWorkspaceID string) (*WorkspaceSettings, error) {
|
||||
sourceWorkspaceID = strings.TrimSpace(sourceWorkspaceID)
|
||||
targetWorkspaceID = strings.TrimSpace(targetWorkspaceID)
|
||||
if sourceWorkspaceID == "" || targetWorkspaceID == "" {
|
||||
return nil, fmt.Errorf("%w: source and target workspace ids are required", ErrWorkspaceSettingsValidation)
|
||||
}
|
||||
if sourceWorkspaceID == targetWorkspaceID {
|
||||
return nil, ErrSameWorkspace
|
||||
}
|
||||
if s.store == nil {
|
||||
return nil, fmt.Errorf("github store not configured")
|
||||
}
|
||||
// GetWorkspaceSettings returns defaults (never nil) for a missing row.
|
||||
source, err := s.store.GetWorkspaceSettings(ctx, sourceWorkspaceID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read source github settings: %w", err)
|
||||
}
|
||||
target := &WorkspaceSettings{
|
||||
WorkspaceID: targetWorkspaceID,
|
||||
RepoScopeMode: source.RepoScopeMode,
|
||||
RepoScopeOrgs: append([]string(nil), source.RepoScopeOrgs...),
|
||||
RepoScopeRepos: append([]RepoFilter(nil), source.RepoScopeRepos...),
|
||||
SavedPresets: cloneRawMessage(source.SavedPresets),
|
||||
DefaultQueryPresets: cloneRawMessage(source.DefaultQueryPresets),
|
||||
}
|
||||
// Copy the action presets before the workspace-settings write. These are two
|
||||
// separate store writes without a shared transaction, so ordering the
|
||||
// preset copy first means a preset failure leaves the target completely
|
||||
// untouched; only a failure of the final settings write can leave a partial
|
||||
// copy, and a full retry overwrites both.
|
||||
if err := s.copyActionPresets(ctx, sourceWorkspaceID, targetWorkspaceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.store.UpsertWorkspaceSettings(ctx, target); err != nil {
|
||||
return nil, fmt.Errorf("write target github settings: %w", err)
|
||||
}
|
||||
return s.store.GetWorkspaceSettings(ctx, targetWorkspaceID)
|
||||
}
|
||||
|
||||
// copyActionPresets duplicates the source workspace's stored quick-action
|
||||
// presets onto the target. When the source has no stored row it relies on
|
||||
// built-in defaults, so the target's row is cleared as well — otherwise a copy
|
||||
// would leave the target on its own customised presets while reporting success,
|
||||
// diverging from the source. Both then fall back to the same defaults on read.
|
||||
func (s *Service) copyActionPresets(ctx context.Context, sourceWorkspaceID, targetWorkspaceID string) error {
|
||||
stored, err := s.store.GetActionPresets(ctx, sourceWorkspaceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read source github action presets: %w", err)
|
||||
}
|
||||
if stored == nil {
|
||||
if err := s.store.DeleteActionPresets(ctx, targetWorkspaceID); err != nil {
|
||||
return fmt.Errorf("clear target github action presets: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
target := &ActionPresets{
|
||||
WorkspaceID: targetWorkspaceID,
|
||||
PR: append([]ActionPreset(nil), stored.PR...),
|
||||
Issue: append([]ActionPreset(nil), stored.Issue...),
|
||||
}
|
||||
if err := s.store.UpsertActionPresets(ctx, target); err != nil {
|
||||
return fmt.Errorf("write target github action presets: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newCopyTestService(t *testing.T) *Service {
|
||||
t.Helper()
|
||||
client := NewMockClient()
|
||||
store := newTestStore(t)
|
||||
return NewService(client, AuthMethodPAT, nil, store, nil, testLogger(t))
|
||||
}
|
||||
|
||||
func TestCopyWorkspaceSettingsToWorkspace_CopiesSettings(t *testing.T) {
|
||||
svc := newCopyTestService(t)
|
||||
ctx := context.Background()
|
||||
const src, dst = "ws-src", "ws-dst"
|
||||
|
||||
if err := svc.UpsertWorkspaceSettings(ctx, &WorkspaceSettings{
|
||||
WorkspaceID: src,
|
||||
RepoScopeMode: RepoScopeModeRepos,
|
||||
RepoScopeRepos: []RepoFilter{{Owner: "kdlbs", Name: "kandev"}},
|
||||
SavedPresets: []byte(`[{"id":"p1","kind":"pr","label":"Mine"}]`),
|
||||
DefaultQueryPresets: []byte(`{"pr":[],"issue":[]}`),
|
||||
}); err != nil {
|
||||
t.Fatalf("seed source: %v", err)
|
||||
}
|
||||
if _, err := svc.UpdateActionPresets(ctx, &UpdateActionPresetsRequest{
|
||||
WorkspaceID: src,
|
||||
PR: &[]ActionPreset{{ID: "custom", Label: "Custom", PromptTemplate: "do {{url}}"}},
|
||||
}); err != nil {
|
||||
t.Fatalf("seed source action presets: %v", err)
|
||||
}
|
||||
|
||||
got, err := svc.CopyWorkspaceSettingsToWorkspace(ctx, src, dst)
|
||||
if err != nil {
|
||||
t.Fatalf("copy: %v", err)
|
||||
}
|
||||
if got.WorkspaceID != dst || got.RepoScopeMode != RepoScopeModeRepos {
|
||||
t.Errorf("copied settings identity/scope mismatch: %+v", got)
|
||||
}
|
||||
if len(got.RepoScopeRepos) != 1 || got.RepoScopeRepos[0].Owner != "kdlbs" ||
|
||||
got.RepoScopeRepos[0].Name != "kandev" {
|
||||
t.Errorf("repo scope not copied: %+v", got.RepoScopeRepos)
|
||||
}
|
||||
if string(got.SavedPresets) != `[{"id":"p1","kind":"pr","label":"Mine"}]` {
|
||||
t.Errorf("saved presets not copied: %s", got.SavedPresets)
|
||||
}
|
||||
if string(got.DefaultQueryPresets) != `{"pr":[],"issue":[]}` {
|
||||
t.Errorf("default query presets not copied: %s", got.DefaultQueryPresets)
|
||||
}
|
||||
|
||||
presets, err := svc.GetActionPresets(ctx, dst)
|
||||
if err != nil {
|
||||
t.Fatalf("get copied action presets: %v", err)
|
||||
}
|
||||
if len(presets.PR) != 1 || presets.PR[0].ID != "custom" {
|
||||
t.Errorf("action presets not copied: %+v", presets.PR)
|
||||
}
|
||||
}
|
||||
|
||||
// When the source relies on default presets (no stored row), the copy must
|
||||
// clear the target's customised presets so both fall back to the same defaults —
|
||||
// otherwise the copy silently leaves the target diverged from the source.
|
||||
func TestCopyWorkspaceSettingsToWorkspace_ClearsTargetPresetsWhenSourceHasNone(t *testing.T) {
|
||||
svc := newCopyTestService(t)
|
||||
ctx := context.Background()
|
||||
const src, dst = "ws-src", "ws-dst"
|
||||
|
||||
// Target has customised presets; source has none stored.
|
||||
if _, err := svc.UpdateActionPresets(ctx, &UpdateActionPresetsRequest{
|
||||
WorkspaceID: dst,
|
||||
PR: &[]ActionPreset{{ID: "target-custom", Label: "Target", PromptTemplate: "x {{url}}"}},
|
||||
}); err != nil {
|
||||
t.Fatalf("seed target action presets: %v", err)
|
||||
}
|
||||
|
||||
if _, err := svc.CopyWorkspaceSettingsToWorkspace(ctx, src, dst); err != nil {
|
||||
t.Fatalf("copy: %v", err)
|
||||
}
|
||||
|
||||
defaults, err := svc.GetActionPresets(ctx, "ws-fresh")
|
||||
if err != nil {
|
||||
t.Fatalf("get default presets: %v", err)
|
||||
}
|
||||
got, err := svc.GetActionPresets(ctx, dst)
|
||||
if err != nil {
|
||||
t.Fatalf("get target presets: %v", err)
|
||||
}
|
||||
if len(got.PR) != len(defaults.PR) {
|
||||
t.Errorf("target presets not reset to defaults: got %d, want %d", len(got.PR), len(defaults.PR))
|
||||
}
|
||||
for _, p := range got.PR {
|
||||
if p.ID == "target-custom" {
|
||||
t.Errorf("target retained its customised preset after copy from a source with none")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyWorkspaceSettingsToWorkspace_SameWorkspace(t *testing.T) {
|
||||
svc := newCopyTestService(t)
|
||||
if _, err := svc.CopyWorkspaceSettingsToWorkspace(context.Background(), "ws-1", "ws-1"); !errors.Is(err, ErrSameWorkspace) {
|
||||
t.Fatalf("expected ErrSameWorkspace, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyWorkspaceSettingsToWorkspace_MissingIDs(t *testing.T) {
|
||||
svc := newCopyTestService(t)
|
||||
if _, err := svc.CopyWorkspaceSettingsToWorkspace(context.Background(), "", "ws-dst"); !errors.Is(err, ErrWorkspaceSettingsValidation) {
|
||||
t.Fatalf("expected ErrWorkspaceSettingsValidation for empty source, got %v", err)
|
||||
}
|
||||
if _, err := svc.CopyWorkspaceSettingsToWorkspace(context.Background(), "ws-src", ""); !errors.Is(err, ErrWorkspaceSettingsValidation) {
|
||||
t.Fatalf("expected ErrWorkspaceSettingsValidation for empty destination, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user