feat: custom branch prefix (#58)
* feat: custom branch prefix * feat: improve ui
This commit is contained in:
@@ -71,10 +71,11 @@ func (a *lifecycleAdapter) LaunchAgent(ctx context.Context, req *executor.Launch
|
||||
ACPSessionID: req.ACPSessionID,
|
||||
Metadata: req.Metadata,
|
||||
// Worktree configuration for concurrent agent execution
|
||||
UseWorktree: req.UseWorktree,
|
||||
RepositoryID: req.RepositoryID,
|
||||
RepositoryPath: req.RepositoryPath,
|
||||
BaseBranch: req.BaseBranch,
|
||||
UseWorktree: req.UseWorktree,
|
||||
RepositoryID: req.RepositoryID,
|
||||
RepositoryPath: req.RepositoryPath,
|
||||
BaseBranch: req.BaseBranch,
|
||||
WorktreeBranchPrefix: req.WorktreeBranchPrefix,
|
||||
}
|
||||
|
||||
// Create the agentctl execution (does NOT start agent process)
|
||||
|
||||
@@ -618,13 +618,14 @@ func (m *Manager) getOrCreateWorktree(ctx context.Context, req *LaunchRequest) (
|
||||
|
||||
// Create request with optional WorktreeID for resumption
|
||||
createReq := worktree.CreateRequest{
|
||||
TaskID: req.TaskID,
|
||||
SessionID: req.SessionID,
|
||||
TaskTitle: req.TaskTitle,
|
||||
RepositoryID: req.RepositoryID,
|
||||
RepositoryPath: req.RepositoryPath,
|
||||
BaseBranch: req.BaseBranch,
|
||||
WorktreeID: worktreeID, // If set, will try to reuse this worktree
|
||||
TaskID: req.TaskID,
|
||||
SessionID: req.SessionID,
|
||||
TaskTitle: req.TaskTitle,
|
||||
RepositoryID: req.RepositoryID,
|
||||
RepositoryPath: req.RepositoryPath,
|
||||
BaseBranch: req.BaseBranch,
|
||||
WorktreeBranchPrefix: req.WorktreeBranchPrefix,
|
||||
WorktreeID: worktreeID, // If set, will try to reuse this worktree
|
||||
}
|
||||
|
||||
wt, err := m.worktreeMgr.Create(ctx, createReq)
|
||||
|
||||
@@ -84,10 +84,11 @@ type LaunchRequest struct {
|
||||
Metadata map[string]interface{}
|
||||
|
||||
// Worktree configuration
|
||||
UseWorktree bool // Whether to use a Git worktree for isolation
|
||||
RepositoryID string // Repository ID for worktree tracking
|
||||
RepositoryPath string // Path to the main repository (for worktree creation)
|
||||
BaseBranch string // Base branch for the worktree (e.g., "main")
|
||||
UseWorktree bool // Whether to use a Git worktree for isolation
|
||||
RepositoryID string // Repository ID for worktree tracking
|
||||
RepositoryPath string // Path to the main repository (for worktree creation)
|
||||
BaseBranch string // Base branch for the worktree (e.g., "main")
|
||||
WorktreeBranchPrefix string // Branch prefix for worktree branches
|
||||
}
|
||||
|
||||
// CredentialsManager interface for credential retrieval
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package worktree
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -23,11 +25,12 @@ type Config struct {
|
||||
MaxPerRepo int `mapstructure:"max_per_repo"`
|
||||
|
||||
// BranchPrefix is the prefix used for worktree branch names.
|
||||
// Default: kandev/
|
||||
// Default: feature/
|
||||
BranchPrefix string `mapstructure:"branch_prefix"`
|
||||
}
|
||||
|
||||
|
||||
// DefaultBranchPrefix is used when no repository-specific prefix is provided.
|
||||
const DefaultBranchPrefix = "feature/"
|
||||
|
||||
// Validate validates the configuration and returns an error if invalid.
|
||||
func (c *Config) Validate() error {
|
||||
@@ -35,7 +38,7 @@ func (c *Config) Validate() error {
|
||||
c.MaxPerRepo = 10
|
||||
}
|
||||
if c.BranchPrefix == "" {
|
||||
c.BranchPrefix = "kandev/"
|
||||
c.BranchPrefix = DefaultBranchPrefix
|
||||
}
|
||||
if c.BasePath == "" {
|
||||
c.BasePath = "~/.kandev/worktrees"
|
||||
@@ -66,15 +69,15 @@ func (c *Config) WorktreePath(worktreeID string) (string, error) {
|
||||
}
|
||||
|
||||
// BranchName returns the branch name for a given task ID and suffix.
|
||||
// Format: {prefix}{taskID}_{suffix} e.g. kandev/abc123_xy7z
|
||||
// Format: {prefix}{taskID}-{suffix} e.g. feature/abc123-xyz
|
||||
func (c *Config) BranchName(taskID, suffix string) string {
|
||||
return c.BranchPrefix + taskID + "_" + suffix
|
||||
return c.BranchPrefix + taskID + "-" + suffix
|
||||
}
|
||||
|
||||
// SemanticBranchName returns a branch name using a semantic name derived from task title.
|
||||
// Format: {prefix}{semanticName}_{suffix} e.g. kandev/fix-login-bug_ab12cd34
|
||||
// Format: {prefix}{semanticName}-{suffix} e.g. feature/fix-login-bug-abc
|
||||
func (c *Config) SemanticBranchName(semanticName, suffix string) string {
|
||||
return c.BranchPrefix + semanticName + "_" + suffix
|
||||
return c.BranchPrefix + semanticName + "-" + suffix
|
||||
}
|
||||
|
||||
// SanitizeForBranch converts a task title into a valid git branch name component.
|
||||
@@ -122,6 +125,53 @@ func SanitizeForBranch(title string, maxLen int) string {
|
||||
return result
|
||||
}
|
||||
|
||||
// NormalizeBranchPrefix trims and falls back to the default prefix.
|
||||
func NormalizeBranchPrefix(prefix string) string {
|
||||
trimmed := strings.TrimSpace(prefix)
|
||||
if trimmed == "" {
|
||||
return DefaultBranchPrefix
|
||||
}
|
||||
return trimmed
|
||||
}
|
||||
|
||||
// ValidateBranchPrefix ensures a prefix contains only safe branch characters.
|
||||
func ValidateBranchPrefix(prefix string) error {
|
||||
trimmed := strings.TrimSpace(prefix)
|
||||
if trimmed == "" {
|
||||
return nil
|
||||
}
|
||||
for _, r := range trimmed {
|
||||
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '/' || r == '-' || r == '_' || r == '.' {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("invalid branch prefix")
|
||||
}
|
||||
if strings.Contains(trimmed, "..") || strings.Contains(trimmed, "@{") {
|
||||
return fmt.Errorf("invalid branch prefix")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const branchSuffixAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
|
||||
// SmallSuffix returns a random suffix capped at 3 characters.
|
||||
func SmallSuffix(maxLen int) string {
|
||||
if maxLen <= 0 {
|
||||
return ""
|
||||
}
|
||||
if maxLen > 3 {
|
||||
maxLen = 3
|
||||
}
|
||||
buf := make([]byte, maxLen)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return strings.Repeat("x", maxLen)
|
||||
}
|
||||
for i := range buf {
|
||||
buf[i] = branchSuffixAlphabet[int(buf[i])%len(branchSuffixAlphabet)]
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
// SemanticWorktreeName generates a semantic worktree directory name from a task title.
|
||||
// Format: {sanitizedTitle}_{suffix} e.g. fix-login-bug_ab12cd34
|
||||
// The title is truncated to 20 characters before adding the suffix.
|
||||
@@ -133,4 +183,3 @@ func SemanticWorktreeName(taskTitle, suffix string) string {
|
||||
}
|
||||
return semanticName + "_" + suffix
|
||||
}
|
||||
|
||||
|
||||
@@ -181,21 +181,26 @@ func (m *Manager) Create(ctx context.Context, req CreateRequest) (*Worktree, err
|
||||
func (m *Manager) createWorktree(ctx context.Context, req CreateRequest) (*Worktree, error) {
|
||||
// Generate a unique worktree ID and random suffix
|
||||
worktreeID := uuid.New().String()
|
||||
suffix := worktreeID[:8] // Use first 8 chars of UUID as suffix
|
||||
dirSuffix := worktreeID[:8] // Use first 8 chars of UUID for worktree dir uniqueness
|
||||
branchSuffix := SmallSuffix(3)
|
||||
prefix := NormalizeBranchPrefix(req.WorktreeBranchPrefix)
|
||||
|
||||
// Generate semantic name from task title if available
|
||||
var worktreeDirName, branchName string
|
||||
if req.TaskTitle != "" {
|
||||
// Use semantic naming: {sanitized-title}_{suffix}
|
||||
semanticName := SemanticWorktreeName(req.TaskTitle, suffix)
|
||||
semanticName := SemanticWorktreeName(req.TaskTitle, dirSuffix)
|
||||
worktreeDirName = semanticName
|
||||
// For branch, use sanitized title (20 chars max) + suffix
|
||||
sanitizedTitle := SanitizeForBranch(req.TaskTitle, 20)
|
||||
branchName = m.config.SemanticBranchName(sanitizedTitle, suffix)
|
||||
if sanitizedTitle == "" {
|
||||
sanitizedTitle = SanitizeForBranch(req.TaskID, 20)
|
||||
}
|
||||
branchName = prefix + sanitizedTitle + "-" + branchSuffix
|
||||
} else {
|
||||
// Fallback to task ID based naming
|
||||
worktreeDirName = req.TaskID + "_" + suffix
|
||||
branchName = m.config.BranchName(req.TaskID, suffix)
|
||||
worktreeDirName = req.TaskID + "_" + dirSuffix
|
||||
branchName = prefix + SanitizeForBranch(req.TaskID, 20) + "-" + branchSuffix
|
||||
}
|
||||
|
||||
worktreePath, err := m.config.WorktreePath(worktreeDirName)
|
||||
@@ -241,13 +246,13 @@ func (m *Manager) createWorktree(ctx context.Context, req CreateRequest) (*Workt
|
||||
zap.String("task_id", req.TaskID),
|
||||
zap.String("worktree_id", wt.ID))
|
||||
} else {
|
||||
if err := m.store.CreateWorktree(ctx, wt); err != nil {
|
||||
// Cleanup on failure
|
||||
if cleanupErr := m.removeWorktreeDir(ctx, worktreePath, req.RepositoryPath); cleanupErr != nil {
|
||||
m.logger.Warn("failed to cleanup worktree after persist failure", zap.Error(cleanupErr))
|
||||
if err := m.store.CreateWorktree(ctx, wt); err != nil {
|
||||
// Cleanup on failure
|
||||
if cleanupErr := m.removeWorktreeDir(ctx, worktreePath, req.RepositoryPath); cleanupErr != nil {
|
||||
m.logger.Warn("failed to cleanup worktree after persist failure", zap.Error(cleanupErr))
|
||||
}
|
||||
return nil, fmt.Errorf("failed to persist worktree: %w", err)
|
||||
}
|
||||
return nil, fmt.Errorf("failed to persist worktree: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,8 +417,6 @@ func (m *Manager) removeWorktree(ctx context.Context, wt *Worktree, removeBranch
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
// OnTaskDeleted cleans up all worktrees for a task when it is deleted.
|
||||
func (m *Manager) OnTaskDeleted(ctx context.Context, taskID string) error {
|
||||
// Get all worktrees for this task
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
@@ -287,3 +288,54 @@ func TestSemanticWorktreeName(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSmallSuffix(t *testing.T) {
|
||||
suffix := SmallSuffix(3)
|
||||
if len(suffix) == 0 || len(suffix) > 3 {
|
||||
t.Fatalf("expected suffix length 1-3, got %d (%q)", len(suffix), suffix)
|
||||
}
|
||||
if !regexp.MustCompile(`^[a-z0-9]{1,3}$`).MatchString(suffix) {
|
||||
t.Fatalf("suffix contains invalid characters: %q", suffix)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSmallSuffix_MaxLenCap(t *testing.T) {
|
||||
suffix := SmallSuffix(10)
|
||||
if len(suffix) != 3 {
|
||||
t.Fatalf("expected suffix length 3, got %d (%q)", len(suffix), suffix)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeBranchPrefix(t *testing.T) {
|
||||
if got := NormalizeBranchPrefix(""); got != DefaultBranchPrefix {
|
||||
t.Fatalf("expected default prefix %q, got %q", DefaultBranchPrefix, got)
|
||||
}
|
||||
if got := NormalizeBranchPrefix(" feature/ "); got != "feature/" {
|
||||
t.Fatalf("expected trimmed prefix %q, got %q", "feature/", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateBranchPrefix(t *testing.T) {
|
||||
valid := []string{"feature/", "bugfix-", "release_1.0/", "team/alpha"}
|
||||
for _, prefix := range valid {
|
||||
if err := ValidateBranchPrefix(prefix); err != nil {
|
||||
t.Fatalf("expected prefix %q to be valid: %v", prefix, err)
|
||||
}
|
||||
}
|
||||
|
||||
invalid := []string{"bad prefix", "feature@{", "foo..bar"}
|
||||
for _, prefix := range invalid {
|
||||
if err := ValidateBranchPrefix(prefix); err == nil {
|
||||
t.Fatalf("expected prefix %q to be invalid", prefix)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSemanticBranchName(t *testing.T) {
|
||||
cfg := Config{BranchPrefix: "feature/"}
|
||||
got := cfg.SemanticBranchName("fix-login", "abc")
|
||||
want := "feature/fix-login-abc"
|
||||
if got != want {
|
||||
t.Fatalf("SemanticBranchName() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +70,10 @@ type CreateRequest struct {
|
||||
// Typically "main" or "master".
|
||||
BaseBranch string
|
||||
|
||||
// WorktreeBranchPrefix is the prefix to use for the worktree branch name.
|
||||
// If empty, the default prefix is used.
|
||||
WorktreeBranchPrefix string
|
||||
|
||||
// WorktreeID is the ID of an existing worktree to reuse (optional).
|
||||
// If provided and valid, the existing worktree is returned instead of creating a new one.
|
||||
WorktreeID string
|
||||
|
||||
@@ -81,10 +81,11 @@ type LaunchAgentRequest struct {
|
||||
ACPSessionID string // ACP session ID to resume, if available
|
||||
|
||||
// Worktree configuration for concurrent agent execution
|
||||
UseWorktree bool // Whether to use a Git worktree for isolation
|
||||
RepositoryID string // Repository ID for worktree tracking
|
||||
RepositoryPath string // Path to the main repository (for worktree creation)
|
||||
BaseBranch string // Base branch for the worktree (e.g., "main")
|
||||
UseWorktree bool // Whether to use a Git worktree for isolation
|
||||
RepositoryID string // Repository ID for worktree tracking
|
||||
RepositoryPath string // Path to the main repository (for worktree creation)
|
||||
BaseBranch string // Base branch for the worktree (e.g., "main")
|
||||
WorktreeBranchPrefix string // Branch prefix for worktree branches
|
||||
}
|
||||
|
||||
// LaunchAgentResponse contains the result of launching an agent
|
||||
@@ -233,6 +234,7 @@ func (e *Executor) ExecuteWithProfile(ctx context.Context, task *v1.Task, agentP
|
||||
var repositoryPath string
|
||||
var repositoryID string
|
||||
var baseBranch string
|
||||
var worktreeBranchPrefix string
|
||||
|
||||
// Get the primary repository for this task
|
||||
primaryTaskRepo, err := e.repo.GetPrimaryTaskRepository(ctx, task.ID)
|
||||
@@ -256,6 +258,7 @@ func (e *Executor) ExecuteWithProfile(ctx context.Context, task *v1.Task, agentP
|
||||
return nil, err
|
||||
}
|
||||
repositoryPath = repository.LocalPath
|
||||
worktreeBranchPrefix = repository.WorktreeBranchPrefix
|
||||
if repositoryPath != "" {
|
||||
req.RepositoryURL = repositoryPath
|
||||
}
|
||||
@@ -277,6 +280,7 @@ func (e *Executor) ExecuteWithProfile(ctx context.Context, task *v1.Task, agentP
|
||||
} else {
|
||||
req.BaseBranch = "main"
|
||||
}
|
||||
req.WorktreeBranchPrefix = worktreeBranchPrefix
|
||||
}
|
||||
|
||||
// Create agent session in database before launch so worktree associations can persist.
|
||||
|
||||
@@ -41,17 +41,18 @@ func (c *RepositoryController) GetRepository(ctx context.Context, req dto.GetRep
|
||||
|
||||
func (c *RepositoryController) CreateRepository(ctx context.Context, req dto.CreateRepositoryRequest) (dto.RepositoryDTO, error) {
|
||||
repository, err := c.service.CreateRepository(ctx, &service.CreateRepositoryRequest{
|
||||
WorkspaceID: req.WorkspaceID,
|
||||
Name: req.Name,
|
||||
SourceType: req.SourceType,
|
||||
LocalPath: req.LocalPath,
|
||||
Provider: req.Provider,
|
||||
ProviderRepoID: req.ProviderRepoID,
|
||||
ProviderOwner: req.ProviderOwner,
|
||||
ProviderName: req.ProviderName,
|
||||
DefaultBranch: req.DefaultBranch,
|
||||
SetupScript: req.SetupScript,
|
||||
CleanupScript: req.CleanupScript,
|
||||
WorkspaceID: req.WorkspaceID,
|
||||
Name: req.Name,
|
||||
SourceType: req.SourceType,
|
||||
LocalPath: req.LocalPath,
|
||||
Provider: req.Provider,
|
||||
ProviderRepoID: req.ProviderRepoID,
|
||||
ProviderOwner: req.ProviderOwner,
|
||||
ProviderName: req.ProviderName,
|
||||
DefaultBranch: req.DefaultBranch,
|
||||
WorktreeBranchPrefix: req.WorktreeBranchPrefix,
|
||||
SetupScript: req.SetupScript,
|
||||
CleanupScript: req.CleanupScript,
|
||||
})
|
||||
if err != nil {
|
||||
return dto.RepositoryDTO{}, err
|
||||
@@ -61,16 +62,17 @@ func (c *RepositoryController) CreateRepository(ctx context.Context, req dto.Cre
|
||||
|
||||
func (c *RepositoryController) UpdateRepository(ctx context.Context, req dto.UpdateRepositoryRequest) (dto.RepositoryDTO, error) {
|
||||
repository, err := c.service.UpdateRepository(ctx, req.ID, &service.UpdateRepositoryRequest{
|
||||
Name: req.Name,
|
||||
SourceType: req.SourceType,
|
||||
LocalPath: req.LocalPath,
|
||||
Provider: req.Provider,
|
||||
ProviderRepoID: req.ProviderRepoID,
|
||||
ProviderOwner: req.ProviderOwner,
|
||||
ProviderName: req.ProviderName,
|
||||
DefaultBranch: req.DefaultBranch,
|
||||
SetupScript: req.SetupScript,
|
||||
CleanupScript: req.CleanupScript,
|
||||
Name: req.Name,
|
||||
SourceType: req.SourceType,
|
||||
LocalPath: req.LocalPath,
|
||||
Provider: req.Provider,
|
||||
ProviderRepoID: req.ProviderRepoID,
|
||||
ProviderOwner: req.ProviderOwner,
|
||||
ProviderName: req.ProviderName,
|
||||
DefaultBranch: req.DefaultBranch,
|
||||
WorktreeBranchPrefix: req.WorktreeBranchPrefix,
|
||||
SetupScript: req.SetupScript,
|
||||
CleanupScript: req.CleanupScript,
|
||||
})
|
||||
if err != nil {
|
||||
return dto.RepositoryDTO{}, err
|
||||
|
||||
@@ -41,20 +41,21 @@ type ColumnDTO struct {
|
||||
}
|
||||
|
||||
type RepositoryDTO struct {
|
||||
ID string `json:"id"`
|
||||
WorkspaceID string `json:"workspace_id"`
|
||||
Name string `json:"name"`
|
||||
SourceType string `json:"source_type"`
|
||||
LocalPath string `json:"local_path,omitempty"`
|
||||
Provider string `json:"provider,omitempty"`
|
||||
ProviderRepoID string `json:"provider_repo_id,omitempty"`
|
||||
ProviderOwner string `json:"provider_owner,omitempty"`
|
||||
ProviderName string `json:"provider_name,omitempty"`
|
||||
DefaultBranch string `json:"default_branch,omitempty"`
|
||||
SetupScript string `json:"setup_script,omitempty"`
|
||||
CleanupScript string `json:"cleanup_script,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID string `json:"id"`
|
||||
WorkspaceID string `json:"workspace_id"`
|
||||
Name string `json:"name"`
|
||||
SourceType string `json:"source_type"`
|
||||
LocalPath string `json:"local_path"`
|
||||
Provider string `json:"provider"`
|
||||
ProviderRepoID string `json:"provider_repo_id"`
|
||||
ProviderOwner string `json:"provider_owner"`
|
||||
ProviderName string `json:"provider_name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
WorktreeBranchPrefix string `json:"worktree_branch_prefix"`
|
||||
SetupScript string `json:"setup_script"`
|
||||
CleanupScript string `json:"cleanup_script"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type RepositoryScriptDTO struct {
|
||||
@@ -293,20 +294,21 @@ func FromColumn(column *models.Column) ColumnDTO {
|
||||
|
||||
func FromRepository(repository *models.Repository) RepositoryDTO {
|
||||
return RepositoryDTO{
|
||||
ID: repository.ID,
|
||||
WorkspaceID: repository.WorkspaceID,
|
||||
Name: repository.Name,
|
||||
SourceType: repository.SourceType,
|
||||
LocalPath: repository.LocalPath,
|
||||
Provider: repository.Provider,
|
||||
ProviderRepoID: repository.ProviderRepoID,
|
||||
ProviderOwner: repository.ProviderOwner,
|
||||
ProviderName: repository.ProviderName,
|
||||
DefaultBranch: repository.DefaultBranch,
|
||||
SetupScript: repository.SetupScript,
|
||||
CleanupScript: repository.CleanupScript,
|
||||
CreatedAt: repository.CreatedAt,
|
||||
UpdatedAt: repository.UpdatedAt,
|
||||
ID: repository.ID,
|
||||
WorkspaceID: repository.WorkspaceID,
|
||||
Name: repository.Name,
|
||||
SourceType: repository.SourceType,
|
||||
LocalPath: repository.LocalPath,
|
||||
Provider: repository.Provider,
|
||||
ProviderRepoID: repository.ProviderRepoID,
|
||||
ProviderOwner: repository.ProviderOwner,
|
||||
ProviderName: repository.ProviderName,
|
||||
DefaultBranch: repository.DefaultBranch,
|
||||
WorktreeBranchPrefix: repository.WorktreeBranchPrefix,
|
||||
SetupScript: repository.SetupScript,
|
||||
CleanupScript: repository.CleanupScript,
|
||||
CreatedAt: repository.CreatedAt,
|
||||
UpdatedAt: repository.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -90,31 +90,33 @@ type GetRepositoryRequest struct {
|
||||
}
|
||||
|
||||
type CreateRepositoryRequest struct {
|
||||
WorkspaceID string
|
||||
Name string
|
||||
SourceType string
|
||||
LocalPath string
|
||||
Provider string
|
||||
ProviderRepoID string
|
||||
ProviderOwner string
|
||||
ProviderName string
|
||||
DefaultBranch string
|
||||
SetupScript string
|
||||
CleanupScript string
|
||||
WorkspaceID string
|
||||
Name string
|
||||
SourceType string
|
||||
LocalPath string
|
||||
Provider string
|
||||
ProviderRepoID string
|
||||
ProviderOwner string
|
||||
ProviderName string
|
||||
DefaultBranch string
|
||||
WorktreeBranchPrefix string
|
||||
SetupScript string
|
||||
CleanupScript string
|
||||
}
|
||||
|
||||
type UpdateRepositoryRequest struct {
|
||||
ID string
|
||||
Name *string
|
||||
SourceType *string
|
||||
LocalPath *string
|
||||
Provider *string
|
||||
ProviderRepoID *string
|
||||
ProviderOwner *string
|
||||
ProviderName *string
|
||||
DefaultBranch *string
|
||||
SetupScript *string
|
||||
CleanupScript *string
|
||||
ID string
|
||||
Name *string
|
||||
SourceType *string
|
||||
LocalPath *string
|
||||
Provider *string
|
||||
ProviderRepoID *string
|
||||
ProviderOwner *string
|
||||
ProviderName *string
|
||||
DefaultBranch *string
|
||||
WorktreeBranchPrefix *string
|
||||
SetupScript *string
|
||||
CleanupScript *string
|
||||
}
|
||||
|
||||
type DeleteRepositoryRequest struct {
|
||||
|
||||
@@ -135,16 +135,17 @@ func (h *RepositoryHandlers) httpListLocalRepositoryBranches(c *gin.Context) {
|
||||
}
|
||||
|
||||
type httpCreateRepositoryRequest struct {
|
||||
Name string `json:"name"`
|
||||
SourceType string `json:"source_type"`
|
||||
LocalPath string `json:"local_path"`
|
||||
Provider string `json:"provider"`
|
||||
ProviderRepoID string `json:"provider_repo_id"`
|
||||
ProviderOwner string `json:"provider_owner"`
|
||||
ProviderName string `json:"provider_name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
SetupScript string `json:"setup_script"`
|
||||
CleanupScript string `json:"cleanup_script"`
|
||||
Name string `json:"name"`
|
||||
SourceType string `json:"source_type"`
|
||||
LocalPath string `json:"local_path"`
|
||||
Provider string `json:"provider"`
|
||||
ProviderRepoID string `json:"provider_repo_id"`
|
||||
ProviderOwner string `json:"provider_owner"`
|
||||
ProviderName string `json:"provider_name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
WorktreeBranchPrefix string `json:"worktree_branch_prefix"`
|
||||
SetupScript string `json:"setup_script"`
|
||||
CleanupScript string `json:"cleanup_script"`
|
||||
}
|
||||
|
||||
func (h *RepositoryHandlers) httpCreateRepository(c *gin.Context) {
|
||||
@@ -158,19 +159,24 @@ func (h *RepositoryHandlers) httpCreateRepository(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
resp, err := h.controller.CreateRepository(c.Request.Context(), dto.CreateRepositoryRequest{
|
||||
WorkspaceID: c.Param("id"),
|
||||
Name: body.Name,
|
||||
SourceType: body.SourceType,
|
||||
LocalPath: body.LocalPath,
|
||||
Provider: body.Provider,
|
||||
ProviderRepoID: body.ProviderRepoID,
|
||||
ProviderOwner: body.ProviderOwner,
|
||||
ProviderName: body.ProviderName,
|
||||
DefaultBranch: body.DefaultBranch,
|
||||
SetupScript: body.SetupScript,
|
||||
CleanupScript: body.CleanupScript,
|
||||
WorkspaceID: c.Param("id"),
|
||||
Name: body.Name,
|
||||
SourceType: body.SourceType,
|
||||
LocalPath: body.LocalPath,
|
||||
Provider: body.Provider,
|
||||
ProviderRepoID: body.ProviderRepoID,
|
||||
ProviderOwner: body.ProviderOwner,
|
||||
ProviderName: body.ProviderName,
|
||||
DefaultBranch: body.DefaultBranch,
|
||||
WorktreeBranchPrefix: body.WorktreeBranchPrefix,
|
||||
SetupScript: body.SetupScript,
|
||||
CleanupScript: body.CleanupScript,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrInvalidRepositorySettings) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid repository settings"})
|
||||
return
|
||||
}
|
||||
h.logger.Error("failed to create repository", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create repository"})
|
||||
return
|
||||
@@ -202,16 +208,17 @@ func (h *RepositoryHandlers) httpListRepositoryBranches(c *gin.Context) {
|
||||
}
|
||||
|
||||
type httpUpdateRepositoryRequest struct {
|
||||
Name *string `json:"name"`
|
||||
SourceType *string `json:"source_type"`
|
||||
LocalPath *string `json:"local_path"`
|
||||
Provider *string `json:"provider"`
|
||||
ProviderRepoID *string `json:"provider_repo_id"`
|
||||
ProviderOwner *string `json:"provider_owner"`
|
||||
ProviderName *string `json:"provider_name"`
|
||||
DefaultBranch *string `json:"default_branch"`
|
||||
SetupScript *string `json:"setup_script"`
|
||||
CleanupScript *string `json:"cleanup_script"`
|
||||
Name *string `json:"name"`
|
||||
SourceType *string `json:"source_type"`
|
||||
LocalPath *string `json:"local_path"`
|
||||
Provider *string `json:"provider"`
|
||||
ProviderRepoID *string `json:"provider_repo_id"`
|
||||
ProviderOwner *string `json:"provider_owner"`
|
||||
ProviderName *string `json:"provider_name"`
|
||||
DefaultBranch *string `json:"default_branch"`
|
||||
WorktreeBranchPrefix *string `json:"worktree_branch_prefix"`
|
||||
SetupScript *string `json:"setup_script"`
|
||||
CleanupScript *string `json:"cleanup_script"`
|
||||
}
|
||||
|
||||
func (h *RepositoryHandlers) httpUpdateRepository(c *gin.Context) {
|
||||
@@ -221,19 +228,24 @@ func (h *RepositoryHandlers) httpUpdateRepository(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
resp, err := h.controller.UpdateRepository(c.Request.Context(), dto.UpdateRepositoryRequest{
|
||||
ID: c.Param("id"),
|
||||
Name: body.Name,
|
||||
SourceType: body.SourceType,
|
||||
LocalPath: body.LocalPath,
|
||||
Provider: body.Provider,
|
||||
ProviderRepoID: body.ProviderRepoID,
|
||||
ProviderOwner: body.ProviderOwner,
|
||||
ProviderName: body.ProviderName,
|
||||
DefaultBranch: body.DefaultBranch,
|
||||
SetupScript: body.SetupScript,
|
||||
CleanupScript: body.CleanupScript,
|
||||
ID: c.Param("id"),
|
||||
Name: body.Name,
|
||||
SourceType: body.SourceType,
|
||||
LocalPath: body.LocalPath,
|
||||
Provider: body.Provider,
|
||||
ProviderRepoID: body.ProviderRepoID,
|
||||
ProviderOwner: body.ProviderOwner,
|
||||
ProviderName: body.ProviderName,
|
||||
DefaultBranch: body.DefaultBranch,
|
||||
WorktreeBranchPrefix: body.WorktreeBranchPrefix,
|
||||
SetupScript: body.SetupScript,
|
||||
CleanupScript: body.CleanupScript,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrInvalidRepositorySettings) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid repository settings"})
|
||||
return
|
||||
}
|
||||
handleNotFound(c, h.logger, err, "repository not found")
|
||||
return
|
||||
}
|
||||
@@ -352,17 +364,18 @@ func (h *RepositoryHandlers) wsListRepositories(ctx context.Context, msg *ws.Mes
|
||||
}
|
||||
|
||||
type wsCreateRepositoryRequest struct {
|
||||
WorkspaceID string `json:"workspace_id"`
|
||||
Name string `json:"name"`
|
||||
SourceType string `json:"source_type"`
|
||||
LocalPath string `json:"local_path"`
|
||||
Provider string `json:"provider"`
|
||||
ProviderRepoID string `json:"provider_repo_id"`
|
||||
ProviderOwner string `json:"provider_owner"`
|
||||
ProviderName string `json:"provider_name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
SetupScript string `json:"setup_script"`
|
||||
CleanupScript string `json:"cleanup_script"`
|
||||
WorkspaceID string `json:"workspace_id"`
|
||||
Name string `json:"name"`
|
||||
SourceType string `json:"source_type"`
|
||||
LocalPath string `json:"local_path"`
|
||||
Provider string `json:"provider"`
|
||||
ProviderRepoID string `json:"provider_repo_id"`
|
||||
ProviderOwner string `json:"provider_owner"`
|
||||
ProviderName string `json:"provider_name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
WorktreeBranchPrefix string `json:"worktree_branch_prefix"`
|
||||
SetupScript string `json:"setup_script"`
|
||||
CleanupScript string `json:"cleanup_script"`
|
||||
}
|
||||
|
||||
func (h *RepositoryHandlers) wsCreateRepository(ctx context.Context, msg *ws.Message) (*ws.Message, error) {
|
||||
@@ -374,19 +387,23 @@ func (h *RepositoryHandlers) wsCreateRepository(ctx context.Context, msg *ws.Mes
|
||||
return ws.NewError(msg.ID, msg.Action, ws.ErrorCodeValidation, "workspace_id and name are required", nil)
|
||||
}
|
||||
resp, err := h.controller.CreateRepository(ctx, dto.CreateRepositoryRequest{
|
||||
WorkspaceID: req.WorkspaceID,
|
||||
Name: req.Name,
|
||||
SourceType: req.SourceType,
|
||||
LocalPath: req.LocalPath,
|
||||
Provider: req.Provider,
|
||||
ProviderRepoID: req.ProviderRepoID,
|
||||
ProviderOwner: req.ProviderOwner,
|
||||
ProviderName: req.ProviderName,
|
||||
DefaultBranch: req.DefaultBranch,
|
||||
SetupScript: req.SetupScript,
|
||||
CleanupScript: req.CleanupScript,
|
||||
WorkspaceID: req.WorkspaceID,
|
||||
Name: req.Name,
|
||||
SourceType: req.SourceType,
|
||||
LocalPath: req.LocalPath,
|
||||
Provider: req.Provider,
|
||||
ProviderRepoID: req.ProviderRepoID,
|
||||
ProviderOwner: req.ProviderOwner,
|
||||
ProviderName: req.ProviderName,
|
||||
DefaultBranch: req.DefaultBranch,
|
||||
WorktreeBranchPrefix: req.WorktreeBranchPrefix,
|
||||
SetupScript: req.SetupScript,
|
||||
CleanupScript: req.CleanupScript,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrInvalidRepositorySettings) {
|
||||
return ws.NewError(msg.ID, msg.Action, ws.ErrorCodeValidation, "Invalid repository settings", nil)
|
||||
}
|
||||
h.logger.Error("failed to create repository", zap.Error(err))
|
||||
return ws.NewError(msg.ID, msg.Action, ws.ErrorCodeInternalError, "Failed to create repository", nil)
|
||||
}
|
||||
@@ -413,17 +430,18 @@ func (h *RepositoryHandlers) wsGetRepository(ctx context.Context, msg *ws.Messag
|
||||
}
|
||||
|
||||
type wsUpdateRepositoryRequest struct {
|
||||
ID string `json:"id"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
SourceType *string `json:"source_type,omitempty"`
|
||||
LocalPath *string `json:"local_path,omitempty"`
|
||||
Provider *string `json:"provider,omitempty"`
|
||||
ProviderRepoID *string `json:"provider_repo_id,omitempty"`
|
||||
ProviderOwner *string `json:"provider_owner,omitempty"`
|
||||
ProviderName *string `json:"provider_name,omitempty"`
|
||||
DefaultBranch *string `json:"default_branch,omitempty"`
|
||||
SetupScript *string `json:"setup_script,omitempty"`
|
||||
CleanupScript *string `json:"cleanup_script,omitempty"`
|
||||
ID string `json:"id"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
SourceType *string `json:"source_type,omitempty"`
|
||||
LocalPath *string `json:"local_path,omitempty"`
|
||||
Provider *string `json:"provider,omitempty"`
|
||||
ProviderRepoID *string `json:"provider_repo_id,omitempty"`
|
||||
ProviderOwner *string `json:"provider_owner,omitempty"`
|
||||
ProviderName *string `json:"provider_name,omitempty"`
|
||||
DefaultBranch *string `json:"default_branch,omitempty"`
|
||||
WorktreeBranchPrefix *string `json:"worktree_branch_prefix,omitempty"`
|
||||
SetupScript *string `json:"setup_script,omitempty"`
|
||||
CleanupScript *string `json:"cleanup_script,omitempty"`
|
||||
}
|
||||
|
||||
func (h *RepositoryHandlers) wsUpdateRepository(ctx context.Context, msg *ws.Message) (*ws.Message, error) {
|
||||
@@ -435,19 +453,23 @@ func (h *RepositoryHandlers) wsUpdateRepository(ctx context.Context, msg *ws.Mes
|
||||
return ws.NewError(msg.ID, msg.Action, ws.ErrorCodeValidation, "id is required", nil)
|
||||
}
|
||||
resp, err := h.controller.UpdateRepository(ctx, dto.UpdateRepositoryRequest{
|
||||
ID: req.ID,
|
||||
Name: req.Name,
|
||||
SourceType: req.SourceType,
|
||||
LocalPath: req.LocalPath,
|
||||
Provider: req.Provider,
|
||||
ProviderRepoID: req.ProviderRepoID,
|
||||
ProviderOwner: req.ProviderOwner,
|
||||
ProviderName: req.ProviderName,
|
||||
DefaultBranch: req.DefaultBranch,
|
||||
SetupScript: req.SetupScript,
|
||||
CleanupScript: req.CleanupScript,
|
||||
ID: req.ID,
|
||||
Name: req.Name,
|
||||
SourceType: req.SourceType,
|
||||
LocalPath: req.LocalPath,
|
||||
Provider: req.Provider,
|
||||
ProviderRepoID: req.ProviderRepoID,
|
||||
ProviderOwner: req.ProviderOwner,
|
||||
ProviderName: req.ProviderName,
|
||||
DefaultBranch: req.DefaultBranch,
|
||||
WorktreeBranchPrefix: req.WorktreeBranchPrefix,
|
||||
SetupScript: req.SetupScript,
|
||||
CleanupScript: req.CleanupScript,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrInvalidRepositorySettings) {
|
||||
return ws.NewError(msg.ID, msg.Action, ws.ErrorCodeValidation, "Invalid repository settings", nil)
|
||||
}
|
||||
h.logger.Error("failed to update repository", zap.Error(err))
|
||||
return ws.NewError(msg.ID, msg.Action, ws.ErrorCodeInternalError, "Failed to update repository", nil)
|
||||
}
|
||||
|
||||
@@ -253,16 +253,17 @@ type Repository struct {
|
||||
// populated after the repo is cloned/synced on the agent host.
|
||||
LocalPath string `json:"local_path"`
|
||||
// Provider fields describe the upstream source (e.g. github/gitlab) for future syncing.
|
||||
Provider string `json:"provider"`
|
||||
ProviderRepoID string `json:"provider_repo_id"`
|
||||
ProviderOwner string `json:"provider_owner"`
|
||||
ProviderName string `json:"provider_name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
SetupScript string `json:"setup_script"`
|
||||
CleanupScript string `json:"cleanup_script"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `json:"deleted_at,omitempty"`
|
||||
Provider string `json:"provider"`
|
||||
ProviderRepoID string `json:"provider_repo_id"`
|
||||
ProviderOwner string `json:"provider_owner"`
|
||||
ProviderName string `json:"provider_name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
WorktreeBranchPrefix string `json:"worktree_branch_prefix"`
|
||||
SetupScript string `json:"setup_script"`
|
||||
CleanupScript string `json:"cleanup_script"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `json:"deleted_at,omitempty"`
|
||||
}
|
||||
|
||||
// RepositoryScript represents a custom script for a repository
|
||||
|
||||
@@ -168,6 +168,7 @@ func (r *sqliteRepository) initSchema() error {
|
||||
provider_owner TEXT DEFAULT '',
|
||||
provider_name TEXT DEFAULT '',
|
||||
default_branch TEXT DEFAULT '',
|
||||
worktree_branch_prefix TEXT DEFAULT 'feature/',
|
||||
setup_script TEXT DEFAULT '',
|
||||
cleanup_script TEXT DEFAULT '',
|
||||
created_at DATETIME NOT NULL,
|
||||
@@ -292,6 +293,9 @@ func (r *sqliteRepository) initSchema() error {
|
||||
if err := r.ensureColumn("repositories", "deleted_at", "DATETIME"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.ensureColumn("repositories", "worktree_branch_prefix", "TEXT DEFAULT 'feature/'"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.ensureColumn("executors", "deleted_at", "DATETIME"); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1590,10 +1594,10 @@ func (r *sqliteRepository) CreateRepository(ctx context.Context, repository *mod
|
||||
_, err := r.db.ExecContext(ctx, `
|
||||
INSERT INTO repositories (
|
||||
id, workspace_id, name, source_type, local_path, provider, provider_repo_id, provider_owner,
|
||||
provider_name, default_branch, setup_script, cleanup_script, created_at, updated_at, deleted_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
provider_name, default_branch, worktree_branch_prefix, setup_script, cleanup_script, created_at, updated_at, deleted_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, repository.ID, repository.WorkspaceID, repository.Name, repository.SourceType, repository.LocalPath, repository.Provider,
|
||||
repository.ProviderRepoID, repository.ProviderOwner, repository.ProviderName, repository.DefaultBranch,
|
||||
repository.ProviderRepoID, repository.ProviderOwner, repository.ProviderName, repository.DefaultBranch, repository.WorktreeBranchPrefix,
|
||||
repository.SetupScript, repository.CleanupScript, repository.CreatedAt, repository.UpdatedAt, repository.DeletedAt)
|
||||
|
||||
return err
|
||||
@@ -1605,12 +1609,12 @@ func (r *sqliteRepository) GetRepository(ctx context.Context, id string) (*model
|
||||
|
||||
err := r.db.QueryRowContext(ctx, `
|
||||
SELECT id, workspace_id, name, source_type, local_path, provider, provider_repo_id, provider_owner,
|
||||
provider_name, default_branch, setup_script, cleanup_script, created_at, updated_at, deleted_at
|
||||
provider_name, default_branch, worktree_branch_prefix, setup_script, cleanup_script, created_at, updated_at, deleted_at
|
||||
FROM repositories WHERE id = ? AND deleted_at IS NULL
|
||||
`, id).Scan(
|
||||
&repository.ID, &repository.WorkspaceID, &repository.Name, &repository.SourceType, &repository.LocalPath,
|
||||
&repository.Provider, &repository.ProviderRepoID, &repository.ProviderOwner, &repository.ProviderName,
|
||||
&repository.DefaultBranch, &repository.SetupScript, &repository.CleanupScript, &repository.CreatedAt, &repository.UpdatedAt, &repository.DeletedAt,
|
||||
&repository.DefaultBranch, &repository.WorktreeBranchPrefix, &repository.SetupScript, &repository.CleanupScript, &repository.CreatedAt, &repository.UpdatedAt, &repository.DeletedAt,
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -1626,11 +1630,11 @@ func (r *sqliteRepository) UpdateRepository(ctx context.Context, repository *mod
|
||||
result, err := r.db.ExecContext(ctx, `
|
||||
UPDATE repositories SET
|
||||
name = ?, source_type = ?, local_path = ?, provider = ?, provider_repo_id = ?, provider_owner = ?,
|
||||
provider_name = ?, default_branch = ?, setup_script = ?, cleanup_script = ?, updated_at = ?
|
||||
provider_name = ?, default_branch = ?, worktree_branch_prefix = ?, setup_script = ?, cleanup_script = ?, updated_at = ?
|
||||
WHERE id = ? AND deleted_at IS NULL
|
||||
`, repository.Name, repository.SourceType, repository.LocalPath, repository.Provider, repository.ProviderRepoID,
|
||||
repository.ProviderOwner, repository.ProviderName, repository.DefaultBranch, repository.SetupScript, repository.CleanupScript,
|
||||
repository.UpdatedAt, repository.ID)
|
||||
repository.ProviderOwner, repository.ProviderName, repository.DefaultBranch, repository.WorktreeBranchPrefix, repository.SetupScript,
|
||||
repository.CleanupScript, repository.UpdatedAt, repository.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1662,7 +1666,7 @@ func (r *sqliteRepository) DeleteRepository(ctx context.Context, id string) erro
|
||||
func (r *sqliteRepository) ListRepositories(ctx context.Context, workspaceID string) ([]*models.Repository, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
SELECT id, workspace_id, name, source_type, local_path, provider, provider_repo_id, provider_owner,
|
||||
provider_name, default_branch, setup_script, cleanup_script, created_at, updated_at, deleted_at
|
||||
provider_name, default_branch, worktree_branch_prefix, setup_script, cleanup_script, created_at, updated_at, deleted_at
|
||||
FROM repositories WHERE workspace_id = ? AND deleted_at IS NULL ORDER BY created_at DESC
|
||||
`, workspaceID)
|
||||
if err != nil {
|
||||
@@ -1676,7 +1680,7 @@ func (r *sqliteRepository) ListRepositories(ctx context.Context, workspaceID str
|
||||
err := rows.Scan(
|
||||
&repository.ID, &repository.WorkspaceID, &repository.Name, &repository.SourceType, &repository.LocalPath,
|
||||
&repository.Provider, &repository.ProviderRepoID, &repository.ProviderOwner, &repository.ProviderName,
|
||||
&repository.DefaultBranch, &repository.SetupScript, &repository.CleanupScript, &repository.CreatedAt, &repository.UpdatedAt, &repository.DeletedAt,
|
||||
&repository.DefaultBranch, &repository.WorktreeBranchPrefix, &repository.SetupScript, &repository.CleanupScript, &repository.CreatedAt, &repository.UpdatedAt, &repository.DeletedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
v1 "github.com/kandev/kandev/pkg/api/v1"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/kandev/kandev/internal/agent/worktree"
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
@@ -31,6 +32,7 @@ type TaskExecutionStopper interface {
|
||||
}
|
||||
|
||||
var ErrActiveTaskSessions = errors.New("active agent sessions exist")
|
||||
var ErrInvalidRepositorySettings = errors.New("invalid repository settings")
|
||||
|
||||
// Service provides task business logic
|
||||
type Service struct {
|
||||
@@ -149,31 +151,33 @@ type UpdateColumnRequest struct {
|
||||
|
||||
// CreateRepositoryRequest contains the data for creating a new repository
|
||||
type CreateRepositoryRequest struct {
|
||||
WorkspaceID string `json:"workspace_id"`
|
||||
Name string `json:"name"`
|
||||
SourceType string `json:"source_type"`
|
||||
LocalPath string `json:"local_path"`
|
||||
Provider string `json:"provider"`
|
||||
ProviderRepoID string `json:"provider_repo_id"`
|
||||
ProviderOwner string `json:"provider_owner"`
|
||||
ProviderName string `json:"provider_name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
SetupScript string `json:"setup_script"`
|
||||
CleanupScript string `json:"cleanup_script"`
|
||||
WorkspaceID string `json:"workspace_id"`
|
||||
Name string `json:"name"`
|
||||
SourceType string `json:"source_type"`
|
||||
LocalPath string `json:"local_path"`
|
||||
Provider string `json:"provider"`
|
||||
ProviderRepoID string `json:"provider_repo_id"`
|
||||
ProviderOwner string `json:"provider_owner"`
|
||||
ProviderName string `json:"provider_name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
WorktreeBranchPrefix string `json:"worktree_branch_prefix"`
|
||||
SetupScript string `json:"setup_script"`
|
||||
CleanupScript string `json:"cleanup_script"`
|
||||
}
|
||||
|
||||
// UpdateRepositoryRequest contains the data for updating a repository
|
||||
type UpdateRepositoryRequest struct {
|
||||
Name *string `json:"name,omitempty"`
|
||||
SourceType *string `json:"source_type,omitempty"`
|
||||
LocalPath *string `json:"local_path,omitempty"`
|
||||
Provider *string `json:"provider,omitempty"`
|
||||
ProviderRepoID *string `json:"provider_repo_id,omitempty"`
|
||||
ProviderOwner *string `json:"provider_owner,omitempty"`
|
||||
ProviderName *string `json:"provider_name,omitempty"`
|
||||
DefaultBranch *string `json:"default_branch,omitempty"`
|
||||
SetupScript *string `json:"setup_script,omitempty"`
|
||||
CleanupScript *string `json:"cleanup_script,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
SourceType *string `json:"source_type,omitempty"`
|
||||
LocalPath *string `json:"local_path,omitempty"`
|
||||
Provider *string `json:"provider,omitempty"`
|
||||
ProviderRepoID *string `json:"provider_repo_id,omitempty"`
|
||||
ProviderOwner *string `json:"provider_owner,omitempty"`
|
||||
ProviderName *string `json:"provider_name,omitempty"`
|
||||
DefaultBranch *string `json:"default_branch,omitempty"`
|
||||
WorktreeBranchPrefix *string `json:"worktree_branch_prefix,omitempty"`
|
||||
SetupScript *string `json:"setup_script,omitempty"`
|
||||
CleanupScript *string `json:"cleanup_script,omitempty"`
|
||||
}
|
||||
|
||||
// CreateExecutorRequest contains the data for creating an executor
|
||||
@@ -928,19 +932,27 @@ func (s *Service) CreateRepository(ctx context.Context, req *CreateRepositoryReq
|
||||
if sourceType == "" {
|
||||
sourceType = "local"
|
||||
}
|
||||
prefix := strings.TrimSpace(req.WorktreeBranchPrefix)
|
||||
if err := worktree.ValidateBranchPrefix(prefix); err != nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidRepositorySettings, err)
|
||||
}
|
||||
if prefix == "" {
|
||||
prefix = worktree.DefaultBranchPrefix
|
||||
}
|
||||
repository := &models.Repository{
|
||||
ID: uuid.New().String(),
|
||||
WorkspaceID: req.WorkspaceID,
|
||||
Name: req.Name,
|
||||
SourceType: sourceType,
|
||||
LocalPath: req.LocalPath,
|
||||
Provider: req.Provider,
|
||||
ProviderRepoID: req.ProviderRepoID,
|
||||
ProviderOwner: req.ProviderOwner,
|
||||
ProviderName: req.ProviderName,
|
||||
DefaultBranch: req.DefaultBranch,
|
||||
SetupScript: req.SetupScript,
|
||||
CleanupScript: req.CleanupScript,
|
||||
ID: uuid.New().String(),
|
||||
WorkspaceID: req.WorkspaceID,
|
||||
Name: req.Name,
|
||||
SourceType: sourceType,
|
||||
LocalPath: req.LocalPath,
|
||||
Provider: req.Provider,
|
||||
ProviderRepoID: req.ProviderRepoID,
|
||||
ProviderOwner: req.ProviderOwner,
|
||||
ProviderName: req.ProviderName,
|
||||
DefaultBranch: req.DefaultBranch,
|
||||
WorktreeBranchPrefix: prefix,
|
||||
SetupScript: req.SetupScript,
|
||||
CleanupScript: req.CleanupScript,
|
||||
}
|
||||
|
||||
if err := s.repo.CreateRepository(ctx, repository); err != nil {
|
||||
@@ -986,6 +998,13 @@ func (s *Service) UpdateRepository(ctx context.Context, id string, req *UpdateRe
|
||||
if req.DefaultBranch != nil {
|
||||
repository.DefaultBranch = *req.DefaultBranch
|
||||
}
|
||||
if req.WorktreeBranchPrefix != nil {
|
||||
prefix := strings.TrimSpace(*req.WorktreeBranchPrefix)
|
||||
if err := worktree.ValidateBranchPrefix(prefix); err != nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidRepositorySettings, err)
|
||||
}
|
||||
repository.WorktreeBranchPrefix = prefix
|
||||
}
|
||||
if req.SetupScript != nil {
|
||||
repository.SetupScript = *req.SetupScript
|
||||
}
|
||||
@@ -1717,20 +1736,21 @@ func (s *Service) publishRepositoryEvent(ctx context.Context, eventType string,
|
||||
return
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"id": repository.ID,
|
||||
"workspace_id": repository.WorkspaceID,
|
||||
"name": repository.Name,
|
||||
"source_type": repository.SourceType,
|
||||
"local_path": repository.LocalPath,
|
||||
"provider": repository.Provider,
|
||||
"provider_repo_id": repository.ProviderRepoID,
|
||||
"provider_owner": repository.ProviderOwner,
|
||||
"provider_name": repository.ProviderName,
|
||||
"default_branch": repository.DefaultBranch,
|
||||
"setup_script": repository.SetupScript,
|
||||
"cleanup_script": repository.CleanupScript,
|
||||
"created_at": repository.CreatedAt.Format(time.RFC3339),
|
||||
"updated_at": repository.UpdatedAt.Format(time.RFC3339),
|
||||
"id": repository.ID,
|
||||
"workspace_id": repository.WorkspaceID,
|
||||
"name": repository.Name,
|
||||
"source_type": repository.SourceType,
|
||||
"local_path": repository.LocalPath,
|
||||
"provider": repository.Provider,
|
||||
"provider_repo_id": repository.ProviderRepoID,
|
||||
"provider_owner": repository.ProviderOwner,
|
||||
"provider_name": repository.ProviderName,
|
||||
"default_branch": repository.DefaultBranch,
|
||||
"worktree_branch_prefix": repository.WorktreeBranchPrefix,
|
||||
"setup_script": repository.SetupScript,
|
||||
"cleanup_script": repository.CleanupScript,
|
||||
"created_at": repository.CreatedAt.Format(time.RFC3339),
|
||||
"updated_at": repository.UpdatedAt.Format(time.RFC3339),
|
||||
}
|
||||
event := bus.NewEvent(eventType, "task-service", data)
|
||||
if err := s.eventBus.Publish(ctx, eventType, event); err != nil {
|
||||
|
||||
@@ -154,6 +154,32 @@ func TestService_CreateTask(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_CreateRepository_DefaultWorktreeBranchPrefix(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
_ = repo.CreateWorkspace(ctx, &models.Workspace{ID: "ws-1", Name: "Workspace"})
|
||||
|
||||
created, err := svc.CreateRepository(ctx, &CreateRepositoryRequest{
|
||||
WorkspaceID: "ws-1",
|
||||
Name: "Test Repo",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateRepository failed: %v", err)
|
||||
}
|
||||
if created.WorktreeBranchPrefix != worktree.DefaultBranchPrefix {
|
||||
t.Fatalf("expected default prefix %q, got %q", worktree.DefaultBranchPrefix, created.WorktreeBranchPrefix)
|
||||
}
|
||||
|
||||
stored, err := repo.GetRepository(ctx, created.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetRepository failed: %v", err)
|
||||
}
|
||||
if stored.WorktreeBranchPrefix != worktree.DefaultBranchPrefix {
|
||||
t.Fatalf("expected stored prefix %q, got %q", worktree.DefaultBranchPrefix, stored.WorktreeBranchPrefix)
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_GetTask(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -50,6 +50,7 @@ export type Repository = {
|
||||
provider_owner: string;
|
||||
provider_name: string;
|
||||
default_branch: string;
|
||||
worktree_branch_prefix: string;
|
||||
setup_script: string;
|
||||
cleanup_script: string;
|
||||
created_at: string;
|
||||
|
||||
@@ -183,6 +183,7 @@ export async function createRepositoryAction(payload: {
|
||||
provider_owner: string;
|
||||
provider_name: string;
|
||||
default_branch: string;
|
||||
worktree_branch_prefix: string;
|
||||
setup_script: string;
|
||||
cleanup_script: string;
|
||||
}) {
|
||||
@@ -197,6 +198,7 @@ export async function createRepositoryAction(payload: {
|
||||
provider_owner: payload.provider_owner,
|
||||
provider_name: payload.provider_name,
|
||||
default_branch: payload.default_branch,
|
||||
worktree_branch_prefix: payload.worktree_branch_prefix,
|
||||
setup_script: payload.setup_script,
|
||||
cleanup_script: payload.cleanup_script,
|
||||
}),
|
||||
|
||||
@@ -96,6 +96,7 @@ export function WorkspaceRepositoriesClient({
|
||||
repo.provider_owner !== saved.provider_owner ||
|
||||
repo.provider_name !== saved.provider_name ||
|
||||
repo.default_branch !== saved.default_branch ||
|
||||
repo.worktree_branch_prefix !== saved.worktree_branch_prefix ||
|
||||
repo.setup_script !== saved.setup_script ||
|
||||
repo.cleanup_script !== saved.cleanup_script
|
||||
);
|
||||
@@ -196,6 +197,7 @@ export function WorkspaceRepositoriesClient({
|
||||
provider_owner: '',
|
||||
provider_name: '',
|
||||
default_branch: selectedRepo?.default_branch || '',
|
||||
worktree_branch_prefix: 'feature/',
|
||||
setup_script: '',
|
||||
cleanup_script: '',
|
||||
created_at: '',
|
||||
@@ -266,6 +268,7 @@ export function WorkspaceRepositoriesClient({
|
||||
provider_owner: repo.provider_owner,
|
||||
provider_name: repo.provider_name,
|
||||
default_branch: repo.default_branch,
|
||||
worktree_branch_prefix: repo.worktree_branch_prefix,
|
||||
setup_script: repo.setup_script,
|
||||
cleanup_script: repo.cleanup_script,
|
||||
});
|
||||
@@ -294,6 +297,7 @@ export function WorkspaceRepositoriesClient({
|
||||
provider_owner: repo.provider_owner,
|
||||
provider_name: repo.provider_name,
|
||||
default_branch: repo.default_branch,
|
||||
worktree_branch_prefix: repo.worktree_branch_prefix,
|
||||
setup_script: repo.setup_script,
|
||||
cleanup_script: repo.cleanup_script,
|
||||
});
|
||||
|
||||
@@ -49,6 +49,11 @@ export function RepositoryCard({
|
||||
const [deleteOpen, setDeleteOpen] = useState(false);
|
||||
const saveRequest = useRequest(() => onSave(repository.id));
|
||||
const deleteRequest = useRequest(async () => { await onDelete(repository.id); });
|
||||
const repositoryName = repository.name ?? '';
|
||||
const repositoryLocalPath = repository.local_path ?? '';
|
||||
const worktreeBranchPrefix = repository.worktree_branch_prefix ?? '';
|
||||
const setupScript = repository.setup_script ?? '';
|
||||
const cleanupScript = repository.cleanup_script ?? '';
|
||||
|
||||
const handleSave = async () => {
|
||||
try {
|
||||
@@ -99,28 +104,41 @@ export function RepositoryCard({
|
||||
<div className="space-y-2">
|
||||
<Label>Repository Name</Label>
|
||||
<Input
|
||||
value={repository.name}
|
||||
value={repositoryName}
|
||||
onChange={(e) => onUpdate(repository.id, { name: e.target.value })}
|
||||
placeholder="my-repo"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label>Local Path</Label>
|
||||
<Input
|
||||
value={repositoryLocalPath}
|
||||
onChange={(e) => onUpdate(repository.id, { local_path: e.target.value })}
|
||||
placeholder="/path/to/repository"
|
||||
disabled={repository.source_type !== 'local'}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Local Path</Label>
|
||||
<Input
|
||||
value={repository.local_path}
|
||||
onChange={(e) => onUpdate(repository.id, { local_path: e.target.value })}
|
||||
placeholder="/path/to/repository"
|
||||
disabled={repository.source_type !== 'local'}
|
||||
/>
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<div className="space-y-2">
|
||||
<Label>Worktree Branch Prefix</Label>
|
||||
<Input
|
||||
value={worktreeBranchPrefix}
|
||||
onChange={(e) => onUpdate(repository.id, { worktree_branch_prefix: e.target.value })}
|
||||
placeholder="feature/"
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Used for new worktree branches. Leave empty to use the default.
|
||||
Branches are generated as {'{prefix}{sanitized-title}-{rand}'}.</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<div className="space-y-2">
|
||||
<Label>Setup Script</Label>
|
||||
<Textarea
|
||||
value={repository.setup_script}
|
||||
value={setupScript}
|
||||
onChange={(e) => onUpdate(repository.id, { setup_script: e.target.value })}
|
||||
placeholder="#!/bin/bash # any manual setup you need"
|
||||
rows={3}
|
||||
@@ -133,7 +151,7 @@ export function RepositoryCard({
|
||||
<div className="space-y-2">
|
||||
<Label>Cleanup Script</Label>
|
||||
<Textarea
|
||||
value={repository.cleanup_script}
|
||||
value={cleanupScript}
|
||||
onChange={(e) => onUpdate(repository.id, { cleanup_script: e.target.value })}
|
||||
placeholder="#!/bin/bash # any manual clean up you need"
|
||||
rows={3}
|
||||
@@ -164,7 +182,7 @@ export function RepositoryCard({
|
||||
<div key={script.id} className="grid gap-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
value={script.name}
|
||||
value={script.name ?? ''}
|
||||
onChange={(e) =>
|
||||
onUpdateScript(repository.id, script.id, { name: e.target.value })
|
||||
}
|
||||
@@ -180,7 +198,7 @@ export function RepositoryCard({
|
||||
</Button>
|
||||
</div>
|
||||
<Textarea
|
||||
value={script.command}
|
||||
value={script.command ?? ''}
|
||||
onChange={(e) =>
|
||||
onUpdateScript(repository.id, script.id, { command: e.target.value })
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ export type Repository = {
|
||||
provider_owner: string;
|
||||
provider_name: string;
|
||||
default_branch: string;
|
||||
worktree_branch_prefix: string;
|
||||
setup_script: string;
|
||||
cleanup_script: string;
|
||||
created_at: string;
|
||||
|
||||
Reference in New Issue
Block a user