diff --git a/cmd/bd/list.go b/cmd/bd/list.go index 5d509172b..e1796b37f 100644 --- a/cmd/bd/list.go +++ b/cmd/bd/list.go @@ -368,6 +368,33 @@ type skipLabelsIssueView struct { Labels []string `json:"labels"` } +type skipLabelsListJSONResponse struct { + Issues []skipLabelsIssueView `json:"issues"` + Meta skipLabelsListMeta `json:"meta"` +} + +type skipLabelsListMeta struct { + SkipLabels bool `json:"skip_labels"` + Count int `json:"count"` +} + +func newSkipLabelsListJSONResponse(issues []*types.IssueWithCounts) skipLabelsListJSONResponse { + views := make([]skipLabelsIssueView, len(issues)) + for i, issue := range issues { + views[i] = skipLabelsIssueView{ + IssueWithCounts: issue, + Labels: []string{}, + } + } + return skipLabelsListJSONResponse{ + Issues: views, + Meta: skipLabelsListMeta{ + SkipLabels: true, + Count: len(views), + }, + } +} + // skipLabelsConflicts returns the names of label-filter flags that conflict // with --skip-labels. Empty result means no conflict. AD-02 Wireframe 5. func skipLabelsConflicts(labels, labelsAny []string, labelPattern, labelRegex string, excludeLabels []string, noLabels bool) []string { @@ -602,7 +629,7 @@ var listCmd = &cobra.Command{ excludeLabels = utils.NormalizeLabels(excludeLabels) // Apply directory-aware label scoping if no labels explicitly provided (GH#541) - if len(labels) == 0 && len(labelsAny) == 0 { + if !skipLabels && len(labels) == 0 && len(labelsAny) == 0 { if dirLabels := config.GetDirectoryLabels(); len(dirLabels) > 0 { labelsAny = dirLabels } @@ -1043,6 +1070,11 @@ var listCmd = &cobra.Command{ if iwc == nil { iwc = []*types.IssueWithCounts{} } + if skipLabels { + outputJSON(newSkipLabelsListJSONResponse(iwc)) + printTruncationHint(truncated, effectiveLimit) + return + } outputJSON(iwc) printTruncationHint(truncated, effectiveLimit) return diff --git a/cmd/bd/list_embedded_test.go b/cmd/bd/list_embedded_test.go index 0e0464594..94311ef5c 100644 --- a/cmd/bd/list_embedded_test.go +++ b/cmd/bd/list_embedded_test.go @@ -57,6 +57,35 @@ func bdListJSON(t *testing.T, bd, dir string, args ...string) []*types.IssueWith return issues } +type bdListSkipLabelsJSON struct { + SchemaVersion int `json:"schema_version"` + Issues []struct { + ID string `json:"id"` + Labels []string `json:"labels"` + } `json:"issues"` + Meta struct { + SkipLabels bool `json:"skip_labels"` + Count int `json:"count"` + } `json:"meta"` +} + +func bdListSkipLabelsJSONOutput(t *testing.T, bd, dir string, args ...string) bdListSkipLabelsJSON { + t.Helper() + fullArgs := append([]string{"list", "--json", "--skip-labels"}, args...) + cmd := exec.Command(bd, fullArgs...) + cmd.Dir = dir + cmd.Env = bdEnv(dir) + stdout, stderr, err := runCommandBuffers(t, cmd) + if err != nil { + t.Fatalf("bd list --json --skip-labels %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String()) + } + var out bdListSkipLabelsJSON + if err := json.Unmarshal(stdout.Bytes(), &out); err != nil { + t.Fatalf("failed to parse skip-labels JSON output: %v\nraw: %s", err, stdout.String()) + } + return out +} + // bdListCapture runs "bd list" and returns (stdout, stderr) separately. func bdListCapture(t *testing.T, bd, dir string, args ...string) (string, string) { t.Helper() @@ -285,6 +314,25 @@ func TestEmbeddedList(t *testing.T) { } }) + t.Run("skip_labels_json_suppresses_labeled_issue", func(t *testing.T) { + out := bdListSkipLabelsJSONOutput(t, bd, dir, "--id", seed.openBug) + if !out.Meta.SkipLabels { + t.Fatal("expected meta.skip_labels=true") + } + if out.Meta.Count != 1 || len(out.Issues) != 1 { + t.Fatalf("expected one issue and matching count, got count=%d issues=%d", out.Meta.Count, len(out.Issues)) + } + if out.Issues[0].ID != seed.openBug { + t.Fatalf("expected issue %s, got %s", seed.openBug, out.Issues[0].ID) + } + if out.Issues[0].Labels == nil { + t.Fatal("expected labels field to be present as an empty array, got nil") + } + if len(out.Issues[0].Labels) != 0 { + t.Fatalf("--skip-labels JSON leaked labels: %v", out.Issues[0].Labels) + } + }) + // --- C. Status/special filtering --- // Note: --ready, --pinned, --status closed/deferred/in_progress tests are // skipped because bd update and bd close are not yet implemented on diff --git a/cmd/bd/list_skip_labels_test.go b/cmd/bd/list_skip_labels_test.go index 8c4921aff..ac2e1358a 100644 --- a/cmd/bd/list_skip_labels_test.go +++ b/cmd/bd/list_skip_labels_test.go @@ -63,7 +63,7 @@ func TestSkipLabelsIssueView_AlwaysEmitsLabelsArray(t *testing.T) { view := skipLabelsIssueView{ IssueWithCounts: &types.IssueWithCounts{ - Issue: &types.Issue{ID: "be-1", Title: "x"}, + Issue: &types.Issue{ID: "be-1", Title: "x", Labels: []string{"actual"}}, }, Labels: []string{}, } @@ -75,11 +75,41 @@ func TestSkipLabelsIssueView_AlwaysEmitsLabelsArray(t *testing.T) { if !strings.Contains(got, `"labels":[]`) { t.Errorf("expected explicit labels:[] in JSON, got: %s", got) } + if strings.Contains(got, "actual") { + t.Errorf("expected wrapper to suppress embedded issue labels, got: %s", got) + } if !strings.Contains(got, `"id":"be-1"`) { t.Errorf("expected id pass-through from embedded IssueWithCounts, got: %s", got) } } +func TestNewSkipLabelsListJSONResponse(t *testing.T) { + t.Parallel() + + resp := newSkipLabelsListJSONResponse([]*types.IssueWithCounts{ + {Issue: &types.Issue{ID: "be-1", Labels: []string{"backend"}}}, + {Issue: &types.Issue{ID: "be-2"}}, + }) + + if !resp.Meta.SkipLabels { + t.Fatal("expected skip_labels metadata") + } + if resp.Meta.Count != 2 { + t.Fatalf("count = %d, want 2", resp.Meta.Count) + } + out, err := json.Marshal(resp) + if err != nil { + t.Fatalf("marshal failed: %v", err) + } + got := string(out) + if strings.Contains(got, "backend") { + t.Fatalf("response leaked labels despite --skip-labels: %s", got) + } + if strings.Count(got, `"labels":[]`) != 2 { + t.Fatalf("expected explicit empty labels for both issues, got: %s", got) + } +} + // TestFormatSkipLabelsConflictError covers Wireframe 5: the error must echo // the conflicting flags via "got: --skip-labels ", state the reason, // and offer two distinct remediation paths. diff --git a/internal/storage/issueops/ready_work_counts.go b/internal/storage/issueops/ready_work_counts.go index 7eac15267..e7b70aca4 100644 --- a/internal/storage/issueops/ready_work_counts.go +++ b/internal/storage/issueops/ready_work_counts.go @@ -21,7 +21,7 @@ func GetReadyWorkWithCountsInTx(ctx context.Context, tx *sql.Tx, filter types.Wo if err != nil { return nil, err } - out, err := runSearchQueryInTx(ctx, tx, IssuesFilterTables, issuePreds.whereSQL, issuePreds.orderBySQL, issuePreds.limitSQL, issuePreds.args, wispDepsExist) + out, err := runSearchQueryInTx(ctx, tx, IssuesFilterTables, issuePreds.whereSQL, issuePreds.orderBySQL, issuePreds.limitSQL, issuePreds.args, wispDepsExist, false) if err != nil { return nil, err } @@ -41,7 +41,7 @@ func GetReadyWorkWithCountsInTx(ctx context.Context, tx *sql.Tx, filter types.Wo if err != nil { return nil, err } - wisps, err := runSearchQueryInTx(ctx, tx, WispsFilterTables, wispPreds.whereSQL, wispPreds.orderBySQL, wispPreds.limitSQL, wispPreds.args, true) + wisps, err := runSearchQueryInTx(ctx, tx, WispsFilterTables, wispPreds.whereSQL, wispPreds.orderBySQL, wispPreds.limitSQL, wispPreds.args, true, false) if err != nil { if isTableNotExistError(err) { return out, nil diff --git a/internal/storage/issueops/search_counts.go b/internal/storage/issueops/search_counts.go index 1484068d8..bece8aa3c 100644 --- a/internal/storage/issueops/search_counts.go +++ b/internal/storage/issueops/search_counts.go @@ -91,11 +91,11 @@ func runFilterSearchQueryInTx(ctx context.Context, tx *sql.Tx, query string, fil limitSQL = fmt.Sprintf("LIMIT %d", filter.Limit) } const orderBy = "ORDER BY i.priority ASC, i.created_at DESC, i.id ASC" - return runSearchQueryInTx(ctx, tx, tables, whereSQL, orderBy, limitSQL, args, includeWispReverseDeps) + return runSearchQueryInTx(ctx, tx, tables, whereSQL, orderBy, limitSQL, args, includeWispReverseDeps, filter.SkipLabels) } //nolint:gosec // G201: SQL fragments are caller-built from hardcoded shapes -func runSearchQueryInTx(ctx context.Context, tx *sql.Tx, tables FilterTables, whereSQL, orderBySQL, limitSQL string, args []interface{}, includeWispReverseDeps bool) ([]*types.IssueWithCounts, error) { +func runSearchQueryInTx(ctx context.Context, tx *sql.Tx, tables FilterTables, whereSQL, orderBySQL, limitSQL string, args []interface{}, includeWispReverseDeps bool, skipLabels bool) ([]*types.IssueWithCounts, error) { reverseBlockerSelect := ` SELECT COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) AS dep_id FROM dependencies WHERE type = 'blocks' @@ -108,20 +108,28 @@ func runSearchQueryInTx(ctx context.Context, tx *sql.Tx, tables FilterTables, wh ` } + labelsSelect := "l.labels_json AS labels_json" + labelsJoin := fmt.Sprintf(` + LEFT JOIN ( + SELECT issue_id, JSON_ARRAYAGG(label) AS labels_json + FROM %s + GROUP BY issue_id + ) l ON l.issue_id = i.id`, tables.Labels) + if skipLabels { + labelsSelect = "NULL AS labels_json" + labelsJoin = "" + } + searchSQL := fmt.Sprintf(` SELECT %s, - l.labels_json AS labels_json, + %s, COALESCE(dc.cnt, 0) AS dep_count, COALESCE(rc.cnt, 0) AS rdep_count, COALESCE(cc.cnt, 0) AS comment_count, pc.parent_id AS parent_id, d.deps_json AS deps_json FROM %s i - LEFT JOIN ( - SELECT issue_id, JSON_ARRAYAGG(label) AS labels_json - FROM %s - GROUP BY issue_id - ) l ON l.issue_id = i.id + %s LEFT JOIN ( SELECT issue_id, COUNT(*) AS cnt FROM %s @@ -155,8 +163,9 @@ func runSearchQueryInTx(ctx context.Context, tx *sql.Tx, tables FilterTables, wh %s `, readyWorkIssueColumns, + labelsSelect, tables.Main, - tables.Labels, + labelsJoin, tables.Dependencies, reverseBlockerSelect, tables.Comments,