Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】合并 domain.workBranchSegment 与 engine.safeGitRefSegment 为 domain.SafeGitRefSegment (#657) (#658)

fx-kurumi75623852b65d
diff --git a/internal/domain/work_branch.go b/internal/domain/work_branch.go
index 23cd7404..d2dfa48d 100644
--- a/internal/domain/work_branch.go
+++ b/internal/domain/work_branch.go
@@ -17,7 +17,7 @@ func DefaultWorkBranch(issue Issue, now time.Time) string {
if source == "tapd" || strings.HasPrefix(strings.TrimSpace(issue.ID), "tapd-") {
if id := tapdStoryID(issue); id != "" {
- return "feature/story-" + workBranchSegment(id) + "-" + suffix
+ return "feature/story-" + SafeGitRefSegment(id) + "-" + suffix
}
}
if source == "gongfeng" || kind == "issue" {
@@ -34,7 +34,7 @@ func DefaultWorkBranch(issue Issue, now time.Time) string {
return fmt.Sprintf("feature/fix-issue-%d-%s", issue.Number, suffix)
}
if issue.ID != "" {
- return "feature/" + workBranchSegment(issue.ID) + "-" + suffix
+ return "feature/" + SafeGitRefSegment(issue.ID) + "-" + suffix
}
return "feature/fix-" + suffix
}
@@ -55,7 +55,12 @@ func tapdStoryID(issue Issue) string {
return strings.TrimPrefix(strings.TrimSpace(issue.ID), "tapd-")
}
-func workBranchSegment(value string) string {
+// SafeGitRefSegment turns an arbitrary value into a single git ref segment:
+// lowercase letters, digits, dash and underscore, with every other rune
+// replaced by a dash and the segment cut at 64 characters. Leading and
+// trailing dashes/dots are trimmed; values that sanitize to nothing fall back
+// to "run" so callers always get a usable branch name.
+func SafeGitRefSegment(value string) string {
var b strings.Builder
for _, r := range strings.ToLower(value) {
if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-' || r == '_' {
diff --git a/internal/domain/work_branch_test.go b/internal/domain/work_branch_test.go
index 0e24401c..0e75286d 100644
--- a/internal/domain/work_branch_test.go
+++ b/internal/domain/work_branch_test.go
@@ -60,3 +60,30 @@ func TestDefaultWorkBranch(t *testing.T) {
})
}
}
+
+func TestSafeGitRefSegment(t *testing.T) {
+ long := ""
+ for len(long) < 80 {
+ long += "a"
+ }
+ tests := []struct {
+ name string
+ value string
+ want string
+ }{
+ {name: "trims surrounding blank and slash", value: " feature/hello world ", want: "feature-hello-world"},
+ {name: "strips leading dots and lock suffix", value: "..bad.lock", want: "bad-lock"},
+ {name: "collapses each separator rune to a dash", value: "a / /b", want: "a-----b"},
+ {name: "keeps upper case as lower case", value: "Feature-ABC_9", want: "feature-abc_9"},
+ {name: "empty falls back to run", value: "", want: "run"},
+ {name: "separators only fall back to run", value: " / . ", want: "run"},
+ {name: "caps the segment at 64 characters", value: long, want: long[:64]},
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := SafeGitRefSegment(tc.value); got != tc.want {
+ t.Fatalf("SafeGitRefSegment(%q) = %q, want %q", tc.value, got, tc.want)
+ }
+ })
+ }
+}
diff --git a/internal/engine/repository_workspace.go b/internal/engine/repository_workspace.go
index 0f0ba4d0..718064b9 100644
--- a/internal/engine/repository_workspace.go
+++ b/internal/engine/repository_workspace.go
@@ -629,7 +629,7 @@ func defaultWorkBranch(issueNumber int, issueID string, now time.Time) string {
if issueNumber > 0 {
branchOwner = fmt.Sprintf("fix-issue-%d", issueNumber)
} else if issueID != "" {
- branchOwner = "fix-" + safeGitRefSegment(issueID)
+ branchOwner = "fix-" + domain.SafeGitRefSegment(issueID)
}
branch := "feature/" + branchOwner + "-" + now.In(time.Local).Format("20060102-1504")
if issueNumber <= 0 && issueID == "" {
@@ -649,25 +649,6 @@ func workBranchNonce() string {
return hex.EncodeToString(b[:])
}
-func safeGitRefSegment(value string) string {
- var b strings.Builder
- for _, r := range strings.ToLower(value) {
- if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-' || r == '_' {
- b.WriteRune(r)
- } else if b.Len() > 0 {
- b.WriteByte('-')
- }
- if b.Len() >= 64 {
- break
- }
- }
- result := strings.Trim(b.String(), "-.")
- if result == "" {
- return "run"
- }
- return result
-}
-
func recordRepositoryBaseline(workspace, baseBranch string) error {
return gitwork.RecordBaseline(workspace, baseBranch)
}
diff --git a/internal/engine/state_helpers_cases_test.go b/internal/engine/state_helpers_cases_test.go
index 042e5994..0196a145 100644
--- a/internal/engine/state_helpers_cases_test.go
+++ b/internal/engine/state_helpers_cases_test.go
@@ -333,8 +333,8 @@ func TestSafeGitRefAndNodeFormattingHelpers(t *testing.T) {
"..bad.lock": "bad-lock",
"": "run",
} {
- if got := safeGitRefSegment(in); got != want {
- t.Fatalf("safeGitRefSegment(%q)=%q want %q", in, got, want)
+ if got := domain.SafeGitRefSegment(in); got != want {
+ t.Fatalf("domain.SafeGitRefSegment(%q)=%q want %q", in, got, want)
}
}
if dockerImageBuildLabel(nil) != "沙箱镜像" {