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.

【架构优化】从 importTAPD 中拆出仓库列表整理 tapdImportRepositoryIDs 与导入后仓库回写 applyTAPDImportRepositories (#718)

fx-kurumi559611cf0654
diff --git a/internal/httpapi/issues.go b/internal/httpapi/issues.go
index 147c18e1..2c68b928 100644
--- a/internal/httpapi/issues.go
+++ b/internal/httpapi/issues.go
@@ -64,19 +64,7 @@ func (h issuesHandlers) importTAPD(c *gin.Context) {
c.JSON(http.StatusBadRequest, map[string]string{"error": "project not found"})
return
}
- ids := make([]string, 0, len(req.RepositoryIDs)+1)
- seen := map[string]bool{}
- for _, id := range req.RepositoryIDs {
- id = strings.TrimSpace(id)
- if id == "" || seen[id] {
- continue
- }
- seen[id] = true
- ids = append(ids, id)
- }
- if id := strings.TrimSpace(req.RepositoryID); id != "" && !seen[id] {
- ids = append([]string{id}, ids...)
- }
+ ids := tapdImportRepositoryIDs(req.RepositoryID, req.RepositoryIDs)
if len(ids) == 0 {
c.JSON(http.StatusBadRequest, map[string]string{"error": "请至少选择一个代码仓库"})
return
@@ -109,17 +97,7 @@ func (h issuesHandlers) importTAPD(c *gin.Context) {
return
}
h.bindRequirementPlaybook(&issue)
- if req.BaseBranch != nil || req.RepositoryBranches != nil || issue.RepositoryID != probe.RepositoryID {
- issue.BaseBranch = probe.BaseBranch
- }
- issue.RepositoryIDs = probe.RepositoryIDs
- issue.RepositoryID = probe.RepositoryID
- if req.RepositoryBranches != nil || req.BaseBranch != nil {
- issue.RepositoryBranches = probe.RepositoryBranches
- } else {
- h.inheritIssueRepositoryBranches(&issue, previous)
- h.normalizeLegacyIssueBaseBranch(&issue)
- }
+ h.applyTAPDImportRepositories(&issue, previous, probe, req.BaseBranch != nil || req.RepositoryBranches != nil)
if err := h.bindIssueRepositories(&issue); err != nil {
c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
return
@@ -133,6 +111,43 @@ func (h issuesHandlers) importTAPD(c *gin.Context) {
c.JSON(http.StatusOK, stored)
}
+// tapdImportRepositoryIDs trims and deduplicates the requested repositories,
+// keeping their order. The legacy single repository goes first when it is not
+// already listed.
+func tapdImportRepositoryIDs(primary string, ids []string) []string {
+ out := make([]string, 0, len(ids)+1)
+ seen := map[string]bool{}
+ for _, id := range ids {
+ id = strings.TrimSpace(id)
+ if id == "" || seen[id] {
+ continue
+ }
+ seen[id] = true
+ out = append(out, id)
+ }
+ if id := strings.TrimSpace(primary); id != "" && !seen[id] {
+ out = append([]string{id}, out...)
+ }
+ return out
+}
+
+// applyTAPDImportRepositories writes the validated repository selection back
+// onto the imported issue. Explicit branches from the request win; otherwise
+// branches are inherited from the previously stored issue.
+func (h issuesHandlers) applyTAPDImportRepositories(issue *domain.Issue, previous, probe domain.Issue, explicitBranches bool) {
+ if explicitBranches || issue.RepositoryID != probe.RepositoryID {
+ issue.BaseBranch = probe.BaseBranch
+ }
+ issue.RepositoryIDs = probe.RepositoryIDs
+ issue.RepositoryID = probe.RepositoryID
+ if explicitBranches {
+ issue.RepositoryBranches = probe.RepositoryBranches
+ } else {
+ h.inheritIssueRepositoryBranches(issue, previous)
+ h.normalizeLegacyIssueBaseBranch(issue)
+ }
+}
+
func (h issuesHandlers) issues(c *gin.Context) {
r := c.Request
if r.Method == http.MethodGet {
diff --git a/internal/httpapi/issues_tapd_import_repositories_test.go b/internal/httpapi/issues_tapd_import_repositories_test.go
new file mode 100644
index 00000000..342c97dc
--- /dev/null
+++ b/internal/httpapi/issues_tapd_import_repositories_test.go
@@ -0,0 +1,29 @@
+package httpapi
+
+import (
+ "slices"
+ "testing"
+)
+
+func TestTAPDImportRepositoryIDs(t *testing.T) {
+ cases := []struct {
+ name string
+ primary string
+ ids []string
+ want []string
+ }{
+ {name: "empty", want: []string{}},
+ {name: "trims and dedupes in order", ids: []string{" repo-b ", "", "repo-a", "repo-b", " "}, want: []string{"repo-b", "repo-a"}},
+ {name: "primary only", primary: " repo-a ", want: []string{"repo-a"}},
+ {name: "primary goes first when missing", primary: "repo-c", ids: []string{"repo-a", "repo-b"}, want: []string{"repo-c", "repo-a", "repo-b"}},
+ {name: "primary already listed keeps list order", primary: " repo-b", ids: []string{"repo-a", "repo-b"}, want: []string{"repo-a", "repo-b"}},
+ {name: "blank primary ignored", primary: " ", ids: []string{"repo-a"}, want: []string{"repo-a"}},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := tapdImportRepositoryIDs(tc.primary, tc.ids); !slices.Equal(got, tc.want) {
+ t.Fatalf("tapdImportRepositoryIDs(%q, %q) = %q, want %q", tc.primary, tc.ids, got, tc.want)
+ }
+ })
+ }
+}
diff --git a/internal/httpapi/issues_tapd_import_repository_switch_test.go b/internal/httpapi/issues_tapd_import_repository_switch_test.go
new file mode 100644
index 00000000..f573fbd1
--- /dev/null
+++ b/internal/httpapi/issues_tapd_import_repository_switch_test.go
@@ -0,0 +1,50 @@
+package httpapi
+
+import (
+ "net/http"
+ "testing"
+
+ "agentflow/internal/domain"
+ "agentflow/internal/repo"
+)
+
+// Reimporting a TAPD story into a different single repository without an
+// explicit branch must not carry the old repository's work branch over: the
+// branch only exists in the repository it was chosen for.
+func TestImportTAPDRepositorySwitchDropsPreviousBaseBranch(t *testing.T) {
+ st, err := repo.OpenTest()
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(st.Close)
+ for _, id := range []string{"switch-old-repo", "switch-new-repo"} {
+ if err := st.PutRepository(domain.Repository{ID: id, ProjectID: "platform", Name: id, DefaultBranch: "master"}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ const storyID = "1010000000100007160"
+ h := newTAPDImportHandler(t, st, t.TempDir(), `{"data":[{"id":"`+storyID+`","name":"切换仓库重新导入","status":"open"}]}`)
+ payload := map[string]any{"projectId": "platform", "repositoryId": "switch-old-repo", "baseBranch": "feature/old-repo-work",
+ "url": "https://tapd.example.com/tapd_fe/10000000/story/detail/" + storyID}
+ imported := requestJSON[domain.Issue](t, h, http.MethodPost, "/api/issues/import-tapd", payload, http.StatusOK)
+ if imported.RepositoryID != "switch-old-repo" || imported.BaseBranch != "feature/old-repo-work" || imported.RepositoryBranches != nil {
+ t.Fatalf("initial single-repository import must keep the selected branch: %+v", imported)
+ }
+
+ delete(payload, "baseBranch")
+ payload["repositoryId"] = "switch-new-repo"
+ switched := requestJSON[domain.Issue](t, h, http.MethodPost, "/api/issues/import-tapd", payload, http.StatusOK)
+ if switched.ID != imported.ID {
+ t.Fatalf("reimport must update the existing issue: got %q, want %q", switched.ID, imported.ID)
+ }
+ if switched.RepositoryID != "switch-new-repo" || len(switched.RepositoryIDs) > 1 {
+ t.Fatalf("reimport must bind the newly selected repository: %+v", switched)
+ }
+ if switched.BaseBranch != "" || switched.WorkBranchForRepository("switch-new-repo") != "" {
+ t.Fatalf("switching repositories without a branch must drop the old repository's branch: %+v", switched)
+ }
+ stored, ok := st.Issue(imported.ID)
+ if !ok || stored.BaseBranch != "" || stored.RepositoryID != "switch-new-repo" {
+ t.Fatalf("persisted issue kept the old repository's branch: %+v", stored)
+ }
+}