Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(properties): leave issues in hidden private projects out of usage counts (#11023)
pseudoyuc332d07720bc
diff --git a/server/internal/handler/private_project_inbox_relations_test.go b/server/internal/handler/private_project_inbox_relations_test.goindex d689383cbc..05ec97769e 100644--- a/server/internal/handler/private_project_inbox_relations_test.go+++ b/server/internal/handler/private_project_inbox_relations_test.go@@ -178,6 +178,62 @@ func TestPrivateProjectLabelUsageCountHidden(t *testing.T) {}}+func TestPrivateProjectPropertyUsageCountHidden(t *testing.T) {+ f := newPrivateProjectFixture(t)+ // Two definitions so both usage query shapes (one key, many keys) run.+ propA := createTestProperty(t, map[string]any{"name": f.marker + " prop A", "type": "text"})+ propB := createTestProperty(t, map[string]any{"name": f.marker + " prop B", "type": "text"})+ for _, issueID := range []string{f.secretIssue, f.publicIssue} {+ for _, propID := range []string{propA.ID, propB.ID} {+ if w := setIssuePropertyRaw(t, issueID, propID, "v"); w.Code != http.StatusOK {+ t.Fatalf("SetIssueProperty: expected 200, got %d: %s", w.Code, w.Body.String())+ }+ }+ }++ usage := func(userID, query string) map[string]int64 {+ t.Helper()+ var out struct {+ Properties []PropertyResponse `json:"properties"`+ }+ req := asUser(newRequest(http.MethodGet, "/api/properties"+query, nil), userID)+ testutil.Call(t, testHandler.ListProperties, req).Want(http.StatusOK).JSON(&out)+ counts := make(map[string]int64)+ for _, p := range out.Properties {+ counts[p.ID] = p.UsageCount+ }+ return counts+ }+ for _, tc := range []struct {+ name string+ userID string+ want int64+ }{+ {"workspace owner in the member list", testUserID, 2},+ {"project member", f.insider, 2},+ {"workspace member outside the project", f.outsider, 1},+ {"workspace admin outside the project", f.outsideAdmin, 1},+ } {+ counts := usage(tc.userID, "")+ for _, id := range []string{propA.ID, propB.ID} {+ if counts[id] != tc.want {+ t.Errorf("%s: usage_count for %s = %d, want %d", tc.name, id, counts[id], tc.want)+ }+ }+ }++ // A single definition takes the one-key query shape.+ dbfx.Exec(t, `UPDATE issue_property SET archived_at = now() WHERE id = $1`, propB.ID)+ if got := usage(f.outsider, "")[propA.ID]; got != 1 {+ t.Errorf("one definition, member outside the project: usage_count = %d, want 1", got)+ }++ removeProjectMemberAsOwner(t, f, f.insider)+ if got := usage(f.insider, "?include_archived=true")[propA.ID]; got != 1 {+ t.Errorf("revoked project member: usage_count = %d, want 1", got)+ }+}+// A project-scoped view preference on a project the caller cannot see answers// exactly like one on a project that does not exist, for reads and writes.func TestPrivateProjectViewPreferenceScopeHidden(t *testing.T) {diff --git a/server/internal/handler/property.go b/server/internal/handler/property.goindex 00f7076608..fe0bf0b572 100644--- a/server/internal/handler/property.go+++ b/server/internal/handler/property.go@@ -21,6 +21,7 @@ import ("github.com/jackc/pgx/v5/pgtype""github.com/multica-ai/multica/server/internal/issueproperty""github.com/multica-ai/multica/server/internal/logger"+ "github.com/multica-ai/multica/server/internal/projectaccess""github.com/multica-ai/multica/server/internal/util"db "github.com/multica-ai/multica/server/pkg/db/generated""github.com/multica-ai/multica/server/pkg/protocol"@@ -408,7 +409,13 @@ func (h *Handler) ListProperties(w http.ResponseWriter, r *http.Request) {// Anything else keeps the counts, which is what older clients expect.var usage map[string]int64if r.URL.Query().Get("include_usage") != "false" {- if usage, err = h.countPropertyUsage(r.Context(), wsUUID, listed); err != nil {+ // Issues in private projects the caller cannot see are not counted+ // (MUL-8472), as in label usage counts.+ hidden, ok := h.requestHiddenProjects(w, r, workspaceID)+ if !ok {+ return+ }+ if usage, err = h.countPropertyUsage(r.Context(), wsUUID, hidden, listed); err != nil {slog.Warn("count property usage failed", append(logger.RequestAttrs(r), "error", err)...)writeError(w, http.StatusInternalServerError, "failed to list properties")return@@ -426,12 +433,13 @@ func (h *Handler) ListProperties(w http.ResponseWriter, r *http.Request) {const propertyUsageKeysPerScan = 1000// countPropertyUsage returns, keyed by property id, how many of the-// workspace's issues carry a value for each of the given definitions.-func (h *Handler) countPropertyUsage(ctx context.Context, workspaceID pgtype.UUID, props []db.IssueProperty) (map[string]int64, error) {+// workspace's issues outside hidden projects carry a value for each of the+// given definitions.+func (h *Handler) countPropertyUsage(ctx context.Context, workspaceID pgtype.UUID, hidden projectaccess.Hidden, props []db.IssueProperty) (map[string]int64, error) {usage := make(map[string]int64, len(props))for start := 0; start < len(props); start += propertyUsageKeysPerScan {chunk := props[start:min(start+propertyUsageKeysPerScan, len(props))]- query, args := propertyUsageQuery(workspaceID, chunk)+ query, args := propertyUsageQuery(workspaceID, hidden, chunk)counts := make([]int64, len(chunk))dest := make([]any, len(chunk))for i := range counts {@@ -458,8 +466,9 @@ func (h *Handler) countPropertyUsage(ctx context.Context, workspaceID pgtype.UUI// - No row per (issue, key), as jsonb_object_keys or an unnest join would// produce: when most issues carry the counted keys, building and grouping// those rows costs more than the per-definition scans did.-// - The scan filters on workspace_id alone, inside an OFFSET 0 subquery,-// and every JSON check runs above it. A JSON condition in the same scan+// - The scan filters on workspace_id (and, for a caller with hidden+// projects, project_id) inside an OFFSET 0 subquery, and every JSON+// check runs above it. A JSON condition in the same scan// can be checked before workspace_id (PostgreSQL appends the equality it// derives for workspace_id after the other filters). A sequential scan// then reads every other workspace's bags too, which costs most when@@ -468,18 +477,24 @@ func (h *Handler) countPropertyUsage(ctx context.Context, workspaceID pgtype.UUI// Bags past the TOAST threshold would otherwise be fetched and// decompressed again for every `?`. A single definition skips it: its// one `?` reads the bag once already.-func propertyUsageQuery(workspaceID pgtype.UUID, props []db.IssueProperty) (string, []any) {+func propertyUsageQuery(workspaceID pgtype.UUID, hidden projectaccess.Hidden, props []db.IssueProperty) (string, []any) {+ args := make([]any, 0, len(props)+2)+ args = append(args, workspaceID)+ scope := "i.workspace_id = $1::uuid"+ if ids := hidden.Param(); ids != nil {+ args = append(args, ids)+ scope += " AND " + hiddenProjectPredicate("i.project_id", fmt.Sprintf("$%d", len(args)))+ }if len(props) == 1 {- return `SELECT COUNT(*) FILTER (WHERE s.properties ? $2::text)+ args = append(args, uuidToString(props[0].ID))+ return fmt.Sprintf(`SELECT COUNT(*) FILTER (WHERE s.properties ? $%d::text)FROM (SELECT i.propertiesFROM issue i- WHERE i.workspace_id = $1::uuid+ WHERE %sOFFSET 0-) s`, []any{workspaceID, uuidToString(props[0].ID)}+) s`, len(args), scope), args}- args := make([]any, 0, len(props)+1)- args = append(args, workspaceID)columns := make([]string, len(props))for i, p := range props {args = append(args, uuidToString(p.ID))@@ -489,10 +504,10 @@ FROM (FROM (SELECT i.properties || '{}'::jsonb AS propertiesFROM issue i- WHERE i.workspace_id = $1::uuid+ WHERE %sOFFSET 0) s-WHERE s.properties <> '{}'::jsonb`, strings.Join(columns, ", ")), args+WHERE s.properties <> '{}'::jsonb`, strings.Join(columns, ", "), scope), args}func (h *Handler) GetProperty(w http.ResponseWriter, r *http.Request) {diff --git a/server/internal/handler/property_test.go b/server/internal/handler/property_test.goindex 024029b716..4f17c1465e 100644--- a/server/internal/handler/property_test.go+++ b/server/internal/handler/property_test.go@@ -20,6 +20,7 @@ import ("github.com/jackc/pgx/v5""github.com/multica-ai/multica/server/internal/issueproperty"+ "github.com/multica-ai/multica/server/internal/projectaccess""github.com/multica-ai/multica/server/internal/testutil"db "github.com/multica-ai/multica/server/pkg/db/generated")@@ -264,7 +265,7 @@ func TestListPropertiesUsageCount(t *testing.T) {defs = append(defs, db.IssueProperty{ID: parseUUID(uuid.NewString())})}defs = append(defs, db.IssueProperty{ID: parseUUID(propB.ID)}, db.IssueProperty{ID: parseUUID(unused.ID)})- usage, err := testHandler.countPropertyUsage(context.Background(), parseUUID(testWorkspaceID), defs)+ usage, err := testHandler.countPropertyUsage(context.Background(), parseUUID(testWorkspaceID), projectaccess.Hidden{}, defs)if err != nil {t.Fatalf("countPropertyUsage across scans: %v", err)}@@ -544,7 +545,7 @@ func TestPropertyUsageReadsBagsNoMoreThanPerDefinitionScans(t *testing.T) {for i, key := range keys {defs[i] = db.IssueProperty{ID: parseUUID(key)}}- query, args := propertyUsageQuery(parseUUID(ws), defs)+ query, args := propertyUsageQuery(parseUUID(ws), projectaccess.Hidden{}, defs)counts := make([]int64, len(keys))dest := make([]any, len(keys))for i := range counts {