Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

refactor(cli): resolve doc and folder ids with the shared prefix resolver (MUL-9821) (#11164)

Jiayuan Zhangf6b4c9f79c9b
diff --git a/server/cmd/multica/cmd_doc.go b/server/cmd/multica/cmd_doc.go
index aac638c161..3c38a24b02 100644
--- a/server/cmd/multica/cmd_doc.go
+++ b/server/cmd/multica/cmd_doc.go
@@ -186,39 +186,29 @@ func fetchDocTree(ctx context.Context, client *cli.APIClient) (docTree, error) {
// resolveDocRef accepts a full id or an unambiguous id prefix from `doc list`.
func resolveDocRef(ctx context.Context, client *cli.APIClient, ref string, folders bool) (string, error) {
- kind := "doc"
+ kind, listHint := "doc", "run `multica doc list`"
if folders {
- kind = "folder"
+ kind, listHint = "folder", "run `multica doc folder list`"
}
- ref = strings.TrimSpace(ref)
- if ref == "" {
- return "", fmt.Errorf("%s id is required", kind)
- }
- if uuidRegexp.MatchString(ref) {
- return ref, nil
- }
- tree, err := fetchDocTree(ctx, client)
+ resolved, err := resolveIDByPrefixWithListHint(ctx, client, kind, ref, listHint, func(ctx context.Context, client *cli.APIClient) ([]idCandidate, error) {
+ tree, err := fetchDocTree(ctx, client)
+ if err != nil {
+ return nil, err
+ }
+ items := tree.Docs
+ if folders {
+ items = tree.Folders
+ }
+ candidates := make([]idCandidate, 0, len(items))
+ for _, item := range items {
+ candidates = append(candidates, idCandidate{ID: strVal(item, "id"), Display: strVal(item, "title")})
+ }
+ return candidates, nil
+ })
if err != nil {
return "", err
}
- items := tree.Docs
- if folders {
- items = tree.Folders
- }
- var matches []string
- for _, item := range items {
- if id := strVal(item, "id"); strings.HasPrefix(id, ref) {
- matches = append(matches, id)
- }
- }
- switch len(matches) {
- case 1:
- return matches[0], nil
- case 0:
- return "", fmt.Errorf("no %s matches %q", kind, ref)
- default:
- return "", fmt.Errorf("%q matches %d %ss; use a longer id", ref, len(matches), kind)
- }
+ return resolved.ID, nil
}
func resolveDocFolderTarget(ctx context.Context, client *cli.APIClient, ref string) (any, error) {
diff --git a/server/cmd/multica/cmd_doc_test.go b/server/cmd/multica/cmd_doc_test.go
index 5ad5765605..c68bec0aee 100644
--- a/server/cmd/multica/cmd_doc_test.go
+++ b/server/cmd/multica/cmd_doc_test.go
@@ -1,6 +1,7 @@
package main
import (
+ "context"
"encoding/json"
"errors"
"net/http"
@@ -380,10 +381,66 @@ func TestRunDocFolderUpdateRejectsEmptyAndNonUUIDRefs(t *testing.T) {
}
}
nonUUID := "aaaaaaaa/../../../docs?x=1#yyyyyyyyyy"[:36]
- if err := runDocFolderUpdate(docFolderUpdateCmd, []string{nonUUID}); err == nil || !strings.Contains(err.Error(), "no folder matches") {
+ if err := runDocFolderUpdate(docFolderUpdateCmd, []string{nonUUID}); err == nil || !strings.Contains(err.Error(), "resolve folder") {
t.Fatalf("36-char non-UUID ref must not be used as an id: %v", err)
}
if writes != 0 {
t.Fatalf("rejected refs must not reach a write endpoint, got %d writes", writes)
}
}
+
+func TestResolveDocRefMatchesSharedPrefixRules(t *testing.T) {
+ const docA = "abcd1234-0000-0000-0000-000000000001"
+ const docB = "abcd5678-0000-0000-0000-000000000002"
+ const folderID = "fedc1234-0000-0000-0000-000000000003"
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "folders": []any{map[string]any{"id": folderID, "title": "Guides"}},
+ "docs": []any{
+ map[string]any{"id": docA, "title": "Runbook"},
+ map[string]any{"id": docB, "title": "Roadmap"},
+ },
+ })
+ }))
+ defer srv.Close()
+ client := cli.NewAPIClient(srv.URL, "ws-1", "mat_test-token")
+ ctx := context.Background()
+
+ for _, tc := range []struct {
+ name string
+ ref string
+ folders bool
+ want string
+ wantErr []string
+ }{
+ {name: "uppercase prefix", ref: "ABCD1", want: docA},
+ {name: "prefix with dash", ref: "abcd5678-0", want: docB},
+ {name: "folder prefix", ref: "FEDC", folders: true, want: folderID},
+ {name: "doc prefix does not match folders", ref: "abcd1", folders: true, wantErr: []string{"no folder found", "`multica doc folder list`"}},
+ {name: "unmatched doc prefix", ref: "0000", wantErr: []string{"no doc found", "`multica doc list`"}},
+ {name: "too short prefix", ref: "abc", wantErr: []string{"resolve doc", "at least 4"}},
+ {name: "ambiguous prefix lists candidates", ref: "abcd", wantErr: []string{"ambiguous doc id prefix", docA, docB, "`multica doc list`"}},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := resolveDocRef(ctx, client, tc.ref, tc.folders)
+ if tc.wantErr == nil {
+ if err != nil || got != tc.want {
+ t.Fatalf("resolveDocRef(%q) = %q, %v; want %q", tc.ref, got, err, tc.want)
+ }
+ return
+ }
+ if err == nil {
+ t.Fatalf("resolveDocRef(%q) = %q; want error", tc.ref, got)
+ }
+ for _, want := range tc.wantErr {
+ if !strings.Contains(err.Error(), want) {
+ t.Fatalf("resolveDocRef(%q) error = %v; want it to contain %q", tc.ref, err, want)
+ }
+ }
+ if strings.Contains(err.Error(), "--full-id") {
+ t.Fatalf("resolveDocRef(%q) error = %v; doc list commands have no --full-id flag", tc.ref, err)
+ }
+ })
+ }
+}
diff --git a/server/cmd/multica/cmd_id_resolver.go b/server/cmd/multica/cmd_id_resolver.go
index a8596375b7..6ac203811e 100644
--- a/server/cmd/multica/cmd_id_resolver.go
+++ b/server/cmd/multica/cmd_id_resolver.go
@@ -81,7 +81,16 @@ func compactUUID(id string) string {
return strings.ToLower(strings.ReplaceAll(strings.TrimSpace(id), "-", ""))
}
+const defaultListHint = "run the list command with --full-id"
+
func resolveIDByPrefix(ctx context.Context, client *cli.APIClient, kind, input string, fetch func(context.Context, *cli.APIClient) ([]idCandidate, error)) (resolvedID, error) {
+ return resolveIDByPrefixWithListHint(ctx, client, kind, input, defaultListHint, fetch)
+}
+
+// resolveIDByPrefixWithListHint is resolveIDByPrefix for resources whose list
+// command needs a different hint, e.g. one that always prints full UUIDs and
+// has no --full-id flag. listHint completes "Use more characters or ...".
+func resolveIDByPrefixWithListHint(ctx context.Context, client *cli.APIClient, kind, input, listHint string, fetch func(context.Context, *cli.APIClient) ([]idCandidate, error)) (resolvedID, error) {
trimmed := strings.TrimSpace(input)
if trimmed == "" {
return resolvedID{}, fmt.Errorf("%s id is required", kind)
@@ -122,7 +131,7 @@ func resolveIDByPrefix(ctx context.Context, client *cli.APIClient, kind, input s
switch len(matches) {
case 0:
- return resolvedID{}, fmt.Errorf("no %s found matching id prefix %q; run the list command with --full-id to copy the full UUID", kind, input)
+ return resolvedID{}, fmt.Errorf("no %s found matching id prefix %q; %s to copy the full UUID", kind, input, listHint)
case 1:
display := matches[0].Display
if display == "" {
@@ -130,11 +139,15 @@ func resolveIDByPrefix(ctx context.Context, client *cli.APIClient, kind, input s
}
return resolvedID{ID: matches[0].ID, Display: display, Archived: matches[0].Archived}, nil
default:
- return resolvedID{}, ambiguousIDPrefixError(kind, input, matches)
+ return resolvedID{}, ambiguousIDPrefixErrorWithListHint(kind, input, listHint, matches)
}
}
func ambiguousIDPrefixError(kind, input string, matches []idCandidate) error {
+ return ambiguousIDPrefixErrorWithListHint(kind, input, defaultListHint, matches)
+}
+
+func ambiguousIDPrefixErrorWithListHint(kind, input, listHint string, matches []idCandidate) error {
sort.Slice(matches, func(i, j int) bool {
return matches[i].ID < matches[j].ID
})
@@ -142,7 +155,7 @@ func ambiguousIDPrefixError(kind, input string, matches []idCandidate) error {
for _, m := range matches {
parts = append(parts, " "+m.ID)
}
- return fmt.Errorf("ambiguous %s id prefix %q; matches:\n%s\nUse more characters or run the list command with --full-id", kind, input, strings.Join(parts, "\n"))
+ return fmt.Errorf("ambiguous %s id prefix %q; matches:\n%s\nUse more characters or %s", kind, input, strings.Join(parts, "\n"), listHint)
}
// resolveIssueRef accepts only the two canonical issue references: