* fix(labels): normalize labels on write, as read paths already do (#5812) bd applies utils.NormalizeLabels (trim, drop empty, dedupe) to every label FILTER path -- list, search, ready, orphans, count and the workapi equivalents -- but to no label WRITE path. That asymmetry stores labels no filter can match: bd create "T" --labels 'theme:a, theme:b' # stores "theme:a", " theme:b" bd list --label theme:b # filter trims -- no match --labels 'a, b' is the comma-space form most people type, and pflag's CSV split leaves the leading space on the second element, so the split looks like it worked. A filtered list that is silently short is indistinguishable from a complete one. Two more everyday inputs corrupt just as quietly: --labels 'a,,b' stores an empty label, and --labels 'a,a' stores the same label twice. Apply the existing helper on the write paths so write and read agree: create (--labels and its --label alias, normalized after merging the two so dedupe spans both), quick, and update (--add-label, --remove-label, --set-labels). --remove-label matters as much as the additive flags: an untrimmed ' theme:a' fails to match the stored label and silently removes nothing. The --set-labels '' clear-all signal is preserved -- it is gated on pointer identity, not slice length. Also add a warn-only 'Label Whitespace' doctor check, since the write-path fix does nothing for labels already stored and nothing else surfaces them. It reports leading/trailing and blank labels only. A label whose trimmed form still contains whitespace is deliberately not reported: that turns on what a space between labels should mean, which this change does not settle, and "good first issue" is a legitimate multi-word label. Not auto-fixable by design -- trimming a damaged label can collide with a correct label already on the same issue, which needs a human decision. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * feat(labels): warn when a stored label contains a space (#5812) A label containing a space is almost always a comma that was meant and missed. That is the failure behind #5812's evidence: 150 corrupt label rows across 111 issues, found months later only because theme-by-theme counts failed to reconcile. The issue proposed splitting labels on whitespace as well as commas. This deliberately does NOT do that, and the reasoning is worth recording. A shell has exactly one mechanism for "is this one word or two", and it is already unambiguous. All three of these send ONE argument: bd create T --labels 'one two' bd create T --labels "one two" bd create T --labels one\ two They are byte-identical by the time bd sees them: quoting and backslash- escaping are consumed by the shell, which reports only the resulting word boundaries. It is the same mechanism that makes 'cd my\ dir' work. Splitting on whitespace would therefore override a boundary the caller has already explicitly set, making labels the one place on the command line where quoting does not mean what it means everywhere else. It is also undoable: with splitting there is no way left to ask for a label containing a space except a second, nested layer of quotes. And it cannot deliver what it appears to promise, since 'one two' and "one two" are indistinguishable by the time any code could act on them. So honor the shell's boundaries, which bd already does, and warn instead: ⚠ Stored "theme:a theme:b" as ONE label — it contains a space. If you meant several labels, separate them with commas (a,b) or repeat the flag. The 150 rows would have produced 150 warnings at the keystroke, while --labels 'good first issue' still does exactly what it says. A warning rather than an error, because a space-containing label is a legitimate thing to ask for and the shell has just been used to ask for it; --quiet silences it. No command changes meaning. Applied to the paths that write labels: create, q, --add-label and --set-labels. Deliberately not --remove-label, because removing a space-containing label is how the damage gets repaired and warning on the repair is noise. The Label Whitespace doctor check gains a matching third class so an existing database can find the ones already in it. It is reported separately from the untrimmed/blank classes and is not called corruption: bd cannot tell "good first issue" from a missed comma, so it surfaces them and a human decides. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(labels): close the last raw label write, bd tag (#5812) Review feedback on #5813. `bd tag` describes itself as "Shorthand for 'bd update <id> --add-label <label>'" and was not: update normalizes and warns, tag passed the positional straight to AddLabel, and the proxied route passed it to ApplyLabelPatch, which drops "" but does not trim. Verified on a scratch DB against binaries built either side of this commit: before: bd tag t-fap ' theme:spaced' -> stored " theme:spaced" bd list --label theme:spaced -> 0 rows after: bd tag t-wjz ' theme:spaced' -> stored "theme:spaced" bd list --label theme:spaced -> 1 row That is the unfilterable class this PR set out to close, written by the command whose help text promises equivalence with the one that already closed it. Normalization happens in tag.go BEFORE the route split, so the direct and proxied paths cannot diverge on it; runTagProxiedServer now takes the resolved id and label rather than raw args, which removes the second entry point. A whitespace-only label is refused (exit 1) rather than dropped. The plural flags can discard an empty element and still honor the rest of the request; bd tag has exactly one label to add, so dropping it would report success having written nothing. Also here, both from the same review: - `bd label add` now warns too. splitLabelArg already trimmed, so it never stored the untrimmed class, but it could not tell a caller that `bd label add bd-1 'theme:a theme:b'` is ONE label. Verified firing. - docs/core-concepts/labels.md: says plainly that import and batch ingest are deliberately NOT normalized (round-trip fidelity; the doctor check is what finds legacy damage), so the next reader does not "fix" it. Also notes that bare `bd doctor` is not yet supported in embedded mode (GH#3794), so the new Label Whitespace check reaches classic and server-mode databases only -- "run bd doctor" was overselling to embedded users. - CHANGELOG entries under Fixed and Added. Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Steve Yegge <steve.yegge@gmail.com>
161 lines
5.1 KiB
Go
161 lines
5.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// Write paths must normalize labels the same way read paths already do.
|
|
//
|
|
// bd applies utils.NormalizeLabels (trim, drop empty, dedupe) to every label
|
|
// FILTER path (list, search, ready, orphans, count, workapi) but to no label
|
|
// WRITE path. The asymmetry means `--labels 'a, b'` stores " b" with a leading
|
|
// space, while `--label ' b'` on a filter is trimmed to "b" — so the stored
|
|
// label can never match its own filter and a filtered list is silently short.
|
|
func TestGatherCreateInputNormalizesLabels(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
want []string
|
|
}{
|
|
{
|
|
// The comma-space form most people type. pflag's CSV split keeps
|
|
// the leading space, and the split looks like it worked.
|
|
name: "comma space separated",
|
|
args: []string{"--labels", "theme:a, theme:b"},
|
|
want: []string{"theme:a", "theme:b"},
|
|
},
|
|
{
|
|
name: "surrounding whitespace trimmed",
|
|
args: []string{"--labels", " theme:a "},
|
|
want: []string{"theme:a"},
|
|
},
|
|
{
|
|
name: "empty elements dropped",
|
|
args: []string{"--labels", "theme:a,,theme:b"},
|
|
want: []string{"theme:a", "theme:b"},
|
|
},
|
|
{
|
|
name: "duplicates collapsed",
|
|
args: []string{"--labels", "theme:a,theme:a"},
|
|
want: []string{"theme:a"},
|
|
},
|
|
{
|
|
// --label is documented as an alias for --labels, so it must
|
|
// normalize identically rather than bypass the cleanup.
|
|
name: "label alias normalized",
|
|
args: []string{"--label", " theme:a , theme:b"},
|
|
want: []string{"theme:a", "theme:b"},
|
|
},
|
|
{
|
|
// The alias appends to --labels; dedupe must span both flags.
|
|
name: "alias and labels deduped together",
|
|
args: []string{"--labels", "theme:a", "--label", " theme:a"},
|
|
want: []string{"theme:a"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
args := append([]string{"--title", "Title"}, tt.args...)
|
|
cmd := newCreateFlagsCommand(t, args...)
|
|
|
|
in, err := gatherCreateInput(cmd, nil)
|
|
if err != nil {
|
|
t.Fatalf("gatherCreateInput: %v", err)
|
|
}
|
|
|
|
assertLabels(t, in.labels, tt.want)
|
|
})
|
|
}
|
|
}
|
|
|
|
// The update write paths carry the same gap on --add-label, --remove-label and
|
|
// --set-labels.
|
|
func TestGatherUpdateInputNormalizesLabels(t *testing.T) {
|
|
// gatherUpdateInput reads flags through Flags().Changed, which reports
|
|
// false for anything unregistered, so the three label flags are all this
|
|
// case needs. ctx is consulted only for --status validation.
|
|
cmd := &cobra.Command{Use: "update"}
|
|
cmd.Flags().StringSlice("add-label", nil, "Add labels (repeatable)")
|
|
cmd.Flags().StringSlice("remove-label", nil, "Remove labels (repeatable)")
|
|
cmd.Flags().StringSlice("set-labels", nil, "Set labels, replacing all existing (repeatable)")
|
|
|
|
args := []string{
|
|
"--add-label", "theme:a, theme:b",
|
|
"--remove-label", " theme:c ",
|
|
"--set-labels", "theme:d,,theme:d",
|
|
}
|
|
if err := cmd.ParseFlags(args); err != nil {
|
|
t.Fatalf("parse update flags: %v", err)
|
|
}
|
|
|
|
in, err := gatherUpdateInput(context.Background(), cmd)
|
|
if err != nil {
|
|
t.Fatalf("gatherUpdateInput: %v", err)
|
|
}
|
|
|
|
assertLabels(t, in.addLabels, []string{"theme:a", "theme:b"})
|
|
assertLabels(t, in.removeLabels, []string{"theme:c"})
|
|
if in.setLabels == nil {
|
|
t.Fatal("expected --set-labels to be captured")
|
|
}
|
|
assertLabels(t, *in.setLabels, []string{"theme:d"})
|
|
}
|
|
|
|
// `bd tag` calls itself "Shorthand for 'bd update <id> --add-label <label>'",
|
|
// so it must normalize identically. It took the positional verbatim, which made
|
|
// it the one CLI label write that could still store an unfilterable label.
|
|
//
|
|
// normalizeLabelForTag is called before the direct/proxied route split, so one
|
|
// test covers both routes.
|
|
func TestNormalizeLabelForTag(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
raw string
|
|
want string
|
|
}{
|
|
{"leading space trimmed", " theme:a", "theme:a"},
|
|
{"surrounding whitespace trimmed", " theme:a\t", "theme:a"},
|
|
{"already clean is untouched", "theme:a", "theme:a"},
|
|
// A label containing a space is legitimate and is stored as asked;
|
|
// warnLabelsContainingWhitespace is what flags it.
|
|
{"internal space preserved", "good first issue", "good first issue"},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := normalizeLabelForTag(tt.raw)
|
|
if err != nil {
|
|
t.Fatalf("normalizeLabelForTag(%q): %v", tt.raw, err)
|
|
}
|
|
if got != tt.want {
|
|
t.Fatalf("normalizeLabelForTag(%q) = %q, want %q", tt.raw, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
|
|
// The plural flags can drop an empty element and still honor the rest of
|
|
// the request. `bd tag` has one label to add, so dropping it would report
|
|
// success having written nothing.
|
|
for _, raw := range []string{"", " ", "\t"} {
|
|
t.Run("rejects empty "+fmt.Sprintf("%q", raw), func(t *testing.T) {
|
|
if _, err := normalizeLabelForTag(raw); err == nil {
|
|
t.Fatalf("normalizeLabelForTag(%q) = nil error, want a refusal", raw)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func assertLabels(t *testing.T, got, want []string) {
|
|
t.Helper()
|
|
if len(got) != len(want) {
|
|
t.Fatalf("labels = %q, want %q", got, want)
|
|
}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Fatalf("labels = %q, want %q", got, want)
|
|
}
|
|
}
|
|
}
|