feat(serve): name the blockers on a live-blocker not_closable (#6893)
* feat(issueops): type the blocked-close refusal as BlockedError
Every close-policy blocker refusal was fmt.Errorf("%w: %s is blocked by
%v", ErrCloseBlocked, ...), so the blocker list existed only as prose. A
caller reporting the refusal elsewhere (the HTTP surface) had to parse it
or drop it.
issueops.BlockedError carries IssueID, the typed []Blocker (id plus the
blocking edge type when known) and the wrapped sentinel. Its message is
byte-identical to the historical sentence, so the direct and proxied CLI
lines and failed[].error do not move, and errors.Is(ErrCloseBlocked) still
matches. The sentinel is a field rather than fixed so other "X is blocked
by Y" refusals (e.g. a blocked claim) can reuse the type.
The in-transaction guard keeps the edges typed (isBlockedByInTx, with
IsBlockedInTx formatting through Blocker.String), and the external guard
builds the error from IsBlocked's []string via ParseBlocker, the inverse
of that spelling.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(httpapi): name the blockers on a live-blocker not_closable
The served close refusal said only "issue is blocked", so an HTTP client
could not tell the user what held the close, while bd close on the direct
and proxied routes prints "cannot close blocked issue: X is blocked by
[Y]".
A live-blocker not_closable now carries a `blockers` extension member,
[{id, kind: local|external, type?}], read from *issueops.BlockedError's
typed list, and a detail that opens with the direct route's sentence
built from the same list. It covers issues/{id}:close, per-item
issues:batchClose outcomes, PATCH into a done status and
issues:batchApply. The code, status and open_children discriminator are
unchanged; an error that only mentions blockers in prose keeps the old
generic detail and no member.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(issueops): test the external: prefix before the type split in ParseBlocker
Maintainer review fixup for #6893.
ParseBlocker tried the " (" / ")" split before the external: prefix
branch, so it won for any external reference whose capability ends in
")". validateExternalRef (cmd/bd/dep.go) constrains a capability to
non-empty only, so `external:remote:pay (beta)` is a legal reference
created by the sanctioned `bd dep add` path -- and it parsed to a
truncated ID of "external:remote:pay" plus Type "beta", a value outside
the documented blocks|waits-for|conditional-blocks vocabulary that
falsifies the spec's own promise that an external blocker carries no
type.
Only the structured half was wrong: the mis-split round-trips through
String(), so the prose detail and the CLI sentence stayed byte-identical
and the failure was silent on exactly the new `blockers` member this
change exists to make trustworthy.
Hoisting the prefix test is loss-free -- Blocker.Type's doc comment
already states an external reference never carries a reported type.
Pin: the ParseBlocker/String table in
TestBlockedErrorKeepsTheHistoricalSentence gains the paren-suffixed
external row; every external case tabled there was paren-free, which is
why this got through. Restoring the old ordering reds the new row.
The DeepEqual failure now prints %#v, because Blocker implements
String() and the default verbs render got and want identically for
precisely this defect.
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
This commit is contained in:
parent
7a21446e6c
commit
4caeeffeec
24 changed files with 665 additions and 39 deletions
14
CHANGELOG.md
14
CHANGELOG.md
|
|
@ -133,6 +133,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
first. They now accept an optional leading `v`; versions without it are
|
||||
unchanged ([#6152](https://github.com/gastownhall/beads/issues/6152)).
|
||||
|
||||
- **`bd serve`'s blocked-close refusal names the blockers.** A `409
|
||||
not_closable` for a live blocker — on `issues/{id}:close`, `issues:batchClose`
|
||||
per-item outcomes, `PATCH issues/{id}` into a done status, and
|
||||
`issues:batchApply` — said only "issue is blocked", so an HTTP client could
|
||||
not tell the user what held the close, while `bd close` on the direct and
|
||||
proxied routes prints `cannot close blocked issue: X is blocked by [Y]`. The
|
||||
refusal now carries a `blockers` extension member (`[{id, kind, type}]`,
|
||||
`kind` `local` or `external`, `type` the blocking edge type when known) and a
|
||||
`detail` that opens with the direct route's sentence. The code, the status
|
||||
and the `open_children` discriminator are unchanged. The list travels typed
|
||||
end to end: close refusals are now an `issueops.BlockedError` (still matching
|
||||
`ErrCloseBlocked`, with a byte-identical message), so the server never parses
|
||||
prose to build the member and a client can rebuild the same typed error.
|
||||
|
||||
- **The #6716 fan-in stall is fixed on the proxied-server route and the
|
||||
remaining store routes.** Two blockers of one dependent taken away at the
|
||||
same time (parallel workers closing both, or a close racing a
|
||||
|
|
|
|||
|
|
@ -192,6 +192,37 @@ func TestProxiedServerServeBatchClose(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
// The per-item twin of the single close's blocker-naming proof: the refused
|
||||
// item carries its blockers, and its detail opens with the sentence `bd
|
||||
// close` records for the same refusal.
|
||||
t.Run("a blocked item names its blockers as the CLI does", func(t *testing.T) {
|
||||
blocker := bdProxiedCreate(t, bd, p.dir, "a batch blocker", "-p", "1")
|
||||
blocked := bdProxiedCreate(t, bd, p.dir, "a blocked batch item", "-p", "1")
|
||||
if out, err := bdProxiedRun(t, bd, p.dir, "dep", "add", blocked.ID, blocker.ID); err != nil {
|
||||
t.Fatalf("bd dep add: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
status, body := sp.batchClose(t, `{"actor":"http-agent","items":[{"id":"`+blocked.ID+`"}]}`)
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200: %v", status, body)
|
||||
}
|
||||
outcome := batchCloseOutcomes(t, body)[0]
|
||||
if outcome["code"] != "not_closable" {
|
||||
t.Fatalf("code = %v, want not_closable: %v", outcome["code"], outcome)
|
||||
}
|
||||
got, err := json.Marshal(outcome["blockers"])
|
||||
if err != nil {
|
||||
t.Fatalf("re-encode blockers: %v", err)
|
||||
}
|
||||
if want := `[{"id":"` + blocker.ID + `","kind":"local","type":"blocks"}]`; string(got) != want {
|
||||
t.Errorf("blockers = %s, want %s", got, want)
|
||||
}
|
||||
cli := bdProxiedCloseFailedError(t, bd, p.dir, blocked.ID)
|
||||
if want := cli + "; clear the blocker, or send `force`"; outcome["detail"] != want {
|
||||
t.Errorf("detail = %q, want the CLI's failed[].error %q plus the HTTP hint", outcome["detail"], cli)
|
||||
}
|
||||
})
|
||||
|
||||
// A refused BODY means the batch never ran, which is the other half of the
|
||||
// contract: nothing may be written by a request that earned a problem
|
||||
// document.
|
||||
|
|
|
|||
|
|
@ -471,9 +471,80 @@ func TestProxiedServerServeClose(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
// THE BLOCKER-NAMING PROOF, against the CLI's own sentence. A live blocker
|
||||
// refuses with a `blockers` member read from the refusing check's typed
|
||||
// list, and `detail` opens with exactly what `bd close` records in
|
||||
// failed[].error for the same refusal — so an HTTP client can render the
|
||||
// direct route's message instead of an anonymous "blocked".
|
||||
t.Run("a live blocker refusal names its blockers as the CLI does", func(t *testing.T) {
|
||||
blocker := bdProxiedCreate(t, bd, p.dir, "the blocker", "-p", "1")
|
||||
local := bdProxiedCreate(t, bd, p.dir, "blocked locally", "-p", "1")
|
||||
if out, err := bdProxiedRun(t, bd, p.dir, "dep", "add", local.ID, blocker.ID); err != nil {
|
||||
t.Fatalf("bd dep add: %v\n%s", err, out)
|
||||
}
|
||||
external := bdProxiedCreate(t, bd, p.dir, "blocked externally", "-p", "1")
|
||||
const ref = "external:remote:payments"
|
||||
if out, err := bdProxiedRun(t, bd, p.dir, "dep", "add", external.ID, ref); err != nil {
|
||||
t.Fatalf("bd dep add external: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
id string
|
||||
want string
|
||||
}{
|
||||
{local.ID, `[{"id":"` + blocker.ID + `","kind":"local","type":"blocks"}]`},
|
||||
{external.ID, `[{"id":"` + ref + `","kind":"external"}]`},
|
||||
} {
|
||||
status, body := sp.closeIssue(t, tc.id, `{"actor":"http-agent"}`)
|
||||
if status != http.StatusConflict || body["code"] != "not_closable" {
|
||||
t.Fatalf("close %s: status = %d code = %v, want 409 not_closable: %v", tc.id, status, body["code"], body)
|
||||
}
|
||||
if _, present := body["open_children"]; present {
|
||||
t.Errorf("close %s: open_children on the blocker refusal: %v", tc.id, body)
|
||||
}
|
||||
got, err := json.Marshal(body["blockers"])
|
||||
if err != nil {
|
||||
t.Fatalf("re-encode blockers: %v", err)
|
||||
}
|
||||
if string(got) != tc.want {
|
||||
t.Errorf("close %s: blockers = %s, want %s", tc.id, got, tc.want)
|
||||
}
|
||||
|
||||
cli := bdProxiedCloseFailedError(t, bd, p.dir, tc.id)
|
||||
if want := cli + "; clear the blocker or close with force"; body["detail"] != want {
|
||||
t.Errorf("close %s: detail = %q, want the CLI's failed[].error %q plus the HTTP hint", tc.id, body["detail"], cli)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
sp.shutdown(t)
|
||||
}
|
||||
|
||||
// bdProxiedCloseFailedError runs an unforced `bd close --json` of id that must
|
||||
// be refused and returns the failed[].error it reports for id — the
|
||||
// route-independent spelling of the refusal (see assertCloseFailedErrorIsTyped).
|
||||
// A closable companion rides along because the failure report is the
|
||||
// partial-batch shape: a lone refused id prints no failed[] array.
|
||||
func bdProxiedCloseFailedError(t *testing.T, bd, dir, id string) string {
|
||||
t.Helper()
|
||||
companion := bdProxiedCreate(t, bd, dir, "closable companion", "-p", "2")
|
||||
stdout, stderr, err := bdProxiedRunBuffers(t, bd, dir, "close", "--json", companion.ID, id)
|
||||
if err == nil {
|
||||
t.Fatalf("bd close %s succeeded; it must be refused\nstdout:\n%s", id, stdout)
|
||||
}
|
||||
var report struct {
|
||||
Failed []struct {
|
||||
ID string `json:"id"`
|
||||
Error string `json:"error"`
|
||||
} `json:"failed"`
|
||||
}
|
||||
line := lastJSONObjectLine(stderr)
|
||||
if jsonErr := json.Unmarshal([]byte(line), &report); jsonErr != nil || len(report.Failed) != 1 || report.Failed[0].ID != id {
|
||||
t.Fatalf("bd close %s failure report = %+v (%v)\nstderr:\n%s", id, report, jsonErr, stderr)
|
||||
}
|
||||
return report.Failed[0].Error
|
||||
}
|
||||
|
||||
// bdProxiedCloseOneRaw runs `bd close --json` and decodes the one item it
|
||||
// reports as a generic object rather than into types.Issue: the parity oracle
|
||||
// compares the JSON both surfaces actually emit, and decoding through the
|
||||
|
|
|
|||
|
|
@ -815,6 +815,18 @@ type BatchCreateResponse struct {
|
|||
Items []Issue `json:"items"`
|
||||
}
|
||||
|
||||
// Blocker One live blocker named by a blocked-issue refusal.
|
||||
type Blocker struct {
|
||||
// Id The blocker: a local issue id, or the full `external:<project>:<capability>` reference.
|
||||
Id string `json:"id"`
|
||||
|
||||
// Kind `local` for an issue this database holds, which clears when it closes; `external` for an `external:` reference, which clears when the named project ships the capability. The set may grow; default-branch on unknown values.
|
||||
Kind string `json:"kind"`
|
||||
|
||||
// Type The blocking edge's dependency type (`blocks`, `waits-for`, `conditional-blocks`). ABSENT when the refusal did not report it, which is the case for an `external:` blocker.
|
||||
Type *string `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// BlockingAnnotations The blocking decoration of the named issues. It is NOT a page: this operation has no limit and no cursor, because the number of issues asked about is what bounds it.
|
||||
type BlockingAnnotations struct {
|
||||
// Items One entry per DISTINCT requested id, in the order the request first named it — so a client can zip this against the ids it sent. Empty array (never null) when the request named none.
|
||||
|
|
@ -917,6 +929,9 @@ type CloseOutcome struct {
|
|||
// A BATCH WHOSE ITEMS ARE ALL `true` LANDED NOTHING, and records no history entry: a per-item success that changed nothing is not work the caller did.
|
||||
AlreadyClosed *bool `json:"already_closed,omitempty"`
|
||||
|
||||
// Blockers With `not_closable` on the live-blocker refusal: the blockers that refused THIS item, exactly as `Problem.blockers` carries them for the single close, and optional for the same reason. Absent on a successful item and on every other refusal.
|
||||
Blockers *[]Blocker `json:"blockers,omitempty"`
|
||||
|
||||
// Code This item's refusal, from `Problem.code`'s vocabulary and restricted to `not_found` (the id names no row in either plane) and `not_closable` (close policy refused it: open children, or a live blocker — see `open_children`). ABSENT means the item succeeded.
|
||||
//
|
||||
// It is the problem vocabulary rather than a second one because an item refusal and a request refusal are the same question asked at two scopes, and a client that had to learn two vocabularies to classify one condition would be classifying the SCOPE rather than the condition.
|
||||
|
|
@ -1480,6 +1495,13 @@ type Problem struct {
|
|||
// BlockerIsAncestor With `dependency_cycle`, hierarchy refusal only: true when `blocker_id` is an ANCESTOR of `issue_id` (which cannot close until its descendants finish, so the gate would never clear), false when it is a DESCENDANT (blocked status cascades, so it would inherit the block and never close). Both polarities are reported; this member is never omitted to mean false. See `issue_id`.
|
||||
BlockerIsAncestor *bool `json:"blocker_is_ancestor,omitempty"`
|
||||
|
||||
// Blockers With `not_closable`, and ONLY on the live-blocker refusal: the live blockers that refused the close, in the order the refusing check reported them, read from the refusal's typed list rather than parsed out of any message. Never present together with `open_children`.
|
||||
//
|
||||
// A client renders the direct CLI's sentence from it: `cannot close blocked issue: <id> is blocked by [<b1> <b2> …]`, each blocker spelled `id` when its `type` is absent or `blocks` and `id (type)` otherwise — the sentence `detail` already begins with when this member is present.
|
||||
//
|
||||
// IT IS OPTIONAL. A refusal that could not name its blockers omits it and keeps the generic `detail`; absence means "this refusal did not name them", never "nothing blocks it". Re-read the issue's dependencies then.
|
||||
Blockers *[]Blocker `json:"blockers,omitempty"`
|
||||
|
||||
// Code The stable machine-readable reason, and the ONLY member a client may dispatch on. v0's vocabulary: `invalid_argument` (400, also emitted by the Host-header middleware on any route), `invalid_cursor` (400), `unauthenticated` (401, only on a server configured with a token file), `not_found` (404), `already_claimed` (409), `not_claimable` (409), `not_closable` (409), `not_releasable` (409), `dependency_cycle` (409), `dependency_exists` (409), `already_exists` (409), `precondition_failed` (409), `events_journal_disabled` (409), `events_journal_truncated` (410), `busy` (503), `db_unavailable` (503), `events_watch_saturated` (503), `internal` (500). Renaming or removing a status+code pair is a breaking change; ADDING one is not, so clients MUST default-branch on unknown values and fall back to the status class (unknown 4xx → client bug, fail loud; unknown 503 → retry per `Retry-After`; other unknown 5xx → server fault).
|
||||
Code string `json:"code"`
|
||||
|
||||
|
|
@ -1539,7 +1561,7 @@ type Problem struct {
|
|||
|
||||
// OpenChildren With `not_closable`: how many open children the transaction that refused the close observed, read inside that transaction rather than parsed out of `detail`.
|
||||
//
|
||||
// PRESENT ONLY for the open-children refusal. The other `not_closable` refusal is a live blocker and carries no such member, so member presence — not prose — is how a client tells the two apart. Both are bypassed by `force`.
|
||||
// PRESENT ONLY for the open-children refusal. The other `not_closable` refusal is a live blocker and carries `blockers` instead, so member presence — not prose — is how a client tells the two apart. Both are bypassed by `force`.
|
||||
OpenChildren *int `json:"open_children,omitempty"`
|
||||
|
||||
// Param With `invalid_argument`: the offending query parameter, body member or header name. Present on every 400 except a body that fails to parse at all.
|
||||
|
|
|
|||
|
|
@ -974,8 +974,8 @@ func (s *Server) failApplyBatch(w http.ResponseWriter, r *http.Request, request
|
|||
s.fail(w, r, res)
|
||||
|
||||
case errors.Is(err, issueops.ErrCloseBlocked):
|
||||
s.fail(w, r, at(newResult(CodeNotClosable,
|
||||
"an item closes a blocked issue; clear the blocker, or send the item's force flag"), ""))
|
||||
s.fail(w, r, at(closeBlockedResult(err,
|
||||
"an item closes a blocked issue", "clear the blocker, or send the item's force flag"), ""))
|
||||
|
||||
case errors.Is(err, storage.ErrAlreadyClaimed):
|
||||
res := at(newResult(CodeAlreadyClaimed,
|
||||
|
|
|
|||
|
|
@ -893,6 +893,31 @@ func TestApplyBatchMapsTheRolesTypedRefusalsOntoTheDocumentedCodes(t *testing.T)
|
|||
// branch is driven rather than assumed.
|
||||
var errUnmapped = fmt.Errorf("a failure this mapping has never seen")
|
||||
|
||||
// TestApplyBatchNamesTheBlockersOfARefusedClose: the all-or-nothing plan has no
|
||||
// per-item result array to carry the refusal in, so the problem document names
|
||||
// the offending item AND its blockers, from the same typed list the single
|
||||
// close publishes.
|
||||
func TestApplyBatchNamesTheBlockersOfARefusedClose(t *testing.T) {
|
||||
applier := &roleBatchApplier{err: itemErr(0, issueops.ItemClose, "", "bd-1", blockedCloseFixture())}
|
||||
ts := newApplyBatchServer(t, applier)
|
||||
|
||||
resp := ts.claim(t, batchApplyPath, `{"actor":"alice","items":[{"kind":"create","create":{"title":"one"}}]}`)
|
||||
if resp.StatusCode != http.StatusConflict {
|
||||
t.Fatalf("status = %d, want 409: %s", resp.StatusCode, readAll(t, resp))
|
||||
}
|
||||
body := decodeBody(t, resp)
|
||||
if body["code"] != string(CodeNotClosable) {
|
||||
t.Errorf("code = %v, want %s", body["code"], CodeNotClosable)
|
||||
}
|
||||
if body["item_index"] != float64(0) {
|
||||
t.Errorf("item_index = %v, want 0", body["item_index"])
|
||||
}
|
||||
assertBlockersMember(t, "problem", body["blockers"])
|
||||
if want := blockedCloseSentence + "; clear the blocker, or send the item's force flag"; body["detail"] != want {
|
||||
t.Errorf("detail = %q, want %q", body["detail"], want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplyBatchCarriesTheHierarchyMembersOnlyOnTheHierarchyRefusal pins the
|
||||
// discriminator inside `dependency_cycle`: member PRESENCE tells a plain
|
||||
// scheduling cycle from the hierarchy case, exactly as it does on
|
||||
|
|
|
|||
|
|
@ -94,7 +94,9 @@ func (s *Server) handleBatchClose(w http.ResponseWriter, r *http.Request) {
|
|||
// on this surface follows, applied one scope down. `open_children` comes from
|
||||
// *issueops.CloseOpenChildrenError's own field, filled inside the transaction
|
||||
// that refused, and its ABSENCE is what tells a client the other not_closable
|
||||
// refusal (a live blocker) apart from this one.
|
||||
// refusal (a live blocker) apart from this one. That refusal's `blockers` come
|
||||
// from *issueops.BlockedError's typed list, the same member the single close
|
||||
// carries.
|
||||
//
|
||||
// AN UNRECOGNIZED ITEM ERROR IS NOT SILENTLY DROPPED. The role documents three
|
||||
// refusals and this maps three; anything else becomes a `not_closable` carrying
|
||||
|
|
@ -128,8 +130,10 @@ func closeOutcome(outcome issueops.CloseOutcome) apigen.CloseOutcome {
|
|||
|
||||
case errors.Is(outcome.Err, issueops.ErrCloseBlocked):
|
||||
// No `open_children`, and its ABSENCE is what tells a client which of
|
||||
// the two close-policy refusals this item got.
|
||||
code, detail = CodeNotClosable, "this issue is blocked; clear the blocker, or send `force`"
|
||||
// the two close-policy refusals this item got. The blockers travel as
|
||||
// the single close's do, so a client names them per item.
|
||||
code = CodeNotClosable
|
||||
detail, wire.Blockers = closeBlocked(outcome.Err, "this issue is blocked", "clear the blocker, or send `force`")
|
||||
|
||||
case errors.Is(outcome.Err, storage.ErrNotFound):
|
||||
code, detail = CodeNotFound, "no issue with this id in either plane"
|
||||
|
|
|
|||
|
|
@ -189,6 +189,50 @@ func TestBatchCloseReportsItemRefusalsInsideTheBody(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestBatchCloseNamesEachRefusedItemsBlockers is the per-item twin of
|
||||
// TestCloseBlockedNamesItsBlockers: `bd close a b c` over HTTP must be able to
|
||||
// say which issues hold EACH refused item, exactly as the single close does,
|
||||
// and only the item whose refusal carried the typed list gets the member.
|
||||
func TestBatchCloseNamesEachRefusedItemsBlockers(t *testing.T) {
|
||||
closer := &roleBatchCloser{result: issueops.CloseBatchResult{Outcomes: []issueops.CloseOutcome{
|
||||
{IssueID: "bd-0", Issue: closedIssue("bd-0"), Changed: true},
|
||||
{IssueID: "bd-1", Err: fmt.Errorf("close bd-1: %w", blockedCloseFixture())},
|
||||
{IssueID: "bd-untyped", Err: fmt.Errorf("%w: bd-untyped is blocked by [bd-2]", issueops.ErrCloseBlocked)},
|
||||
{IssueID: "bd-parent", Err: &issueops.CloseOpenChildrenError{IssueID: "bd-parent", OpenChildren: 2}},
|
||||
}}}
|
||||
ts := newBatchCloseServer(t, closer)
|
||||
|
||||
resp := ts.batchClose(t, `{"actor":"alice","items":[{"id":"bd-0"},{"id":"bd-1"},{"id":"bd-untyped"},{"id":"bd-parent"}]}`)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
||||
}
|
||||
outcomes := outcomesOf(t, resp)
|
||||
if len(outcomes) != 4 {
|
||||
t.Fatalf("outcomes = %v, want four", outcomes)
|
||||
}
|
||||
|
||||
blocked := outcomes[1]
|
||||
if blocked["code"] != string(CodeNotClosable) {
|
||||
t.Errorf("outcomes[1].code = %v, want %s", blocked["code"], CodeNotClosable)
|
||||
}
|
||||
assertBlockersMember(t, "outcomes[1]", blocked["blockers"])
|
||||
if want := blockedCloseSentence + "; clear the blocker, or send `force`"; blocked["detail"] != want {
|
||||
t.Errorf("outcomes[1].detail = %q, want %q", blocked["detail"], want)
|
||||
}
|
||||
if _, present := blocked["open_children"]; present {
|
||||
t.Errorf("outcomes[1] carries open_children on the blocker refusal: %v", blocked)
|
||||
}
|
||||
|
||||
for _, index := range []int{0, 2, 3} {
|
||||
if got, present := outcomes[index]["blockers"]; present {
|
||||
t.Errorf("outcomes[%d] carries blockers=%v; only a typed live-blocker refusal names them", index, got)
|
||||
}
|
||||
}
|
||||
if want := "this issue is blocked; clear the blocker, or send `force`"; outcomes[2]["detail"] != want {
|
||||
t.Errorf("outcomes[2].detail = %q, want the generic %q", outcomes[2]["detail"], want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBatchCloseDoesNotReportAnUnknownItemErrorAsSuccess is the projection's
|
||||
// fail-closed arm. The role documents three item refusals; anything else must
|
||||
// still arrive as a REFUSAL, because the alternative shape — a success carrying
|
||||
|
|
|
|||
|
|
@ -236,8 +236,9 @@ func (s *Server) booleanMember(w http.ResponseWriter, r *http.Request, members m
|
|||
// classification is what a client adopting this endpoint gets to delete, and it
|
||||
// can only delete it if the server never does it either.
|
||||
//
|
||||
// The live-blocker refusal shares the code and carries NO member, which is what
|
||||
// makes member presence the discriminator the document promises.
|
||||
// The live-blocker refusal shares the code and carries no `open_children`,
|
||||
// which is what makes member presence the discriminator the document promises.
|
||||
// It takes ClassifyError's arm, which attaches the typed `blockers` list.
|
||||
func (s *Server) failClose(w http.ResponseWriter, r *http.Request, request issueops.CloseRequest, err error) {
|
||||
if errors.Is(err, issueops.ErrVersionMismatch) {
|
||||
s.fail(w, r, versionPreconditionResult(request.ExpectedVersion))
|
||||
|
|
|
|||
|
|
@ -170,6 +170,92 @@ func TestCloseBlockedIsTheSameCodeWithoutTheCount(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// blockedCloseFixture is the typed refusal a store raises for bd-1 when a local
|
||||
// `blocks` edge, a local `waits-for` edge and an unsatisfied `external:`
|
||||
// reference all hold it — built by the same constructor the external guard
|
||||
// uses, over the []string spelling IsBlocked reports.
|
||||
func blockedCloseFixture() *issueops.BlockedError {
|
||||
return issueops.NewCloseBlockedError("bd-1", []string{"bd-2", "bd-3 (waits-for)", "external:remote:payments"})
|
||||
}
|
||||
|
||||
// blockedCloseSentence is what the direct route prints for that refusal before
|
||||
// its --force hint (closeDirectRefusal), and what `failed[].error` carries on
|
||||
// both CLI routes. It is transcribed rather than derived so a change to either
|
||||
// side's spelling fails here.
|
||||
const blockedCloseSentence = "cannot close blocked issue: bd-1 is blocked by [bd-2 bd-3 (waits-for) external:remote:payments]"
|
||||
|
||||
// blockedCloseWire is the `blockers` member the fixture must travel as: every
|
||||
// blocker in the check's order, local ones with their edge type, and the
|
||||
// external one with `kind` external and no `type`, because the guard that
|
||||
// named it did not report one.
|
||||
const blockedCloseWire = `[{"id":"bd-2","kind":"local","type":"blocks"},` +
|
||||
`{"id":"bd-3","kind":"local","type":"waits-for"},` +
|
||||
`{"id":"external:remote:payments","kind":"external"}]`
|
||||
|
||||
// assertBlockersMember checks a decoded `blockers` member against
|
||||
// blockedCloseWire, comparing re-encoded JSON so member order inside each
|
||||
// object does not matter and a missing or extra member does.
|
||||
func assertBlockersMember(t *testing.T, where string, member any) {
|
||||
t.Helper()
|
||||
if member == nil {
|
||||
t.Fatalf("%s carries no blockers member; the typed refusal named three", where)
|
||||
}
|
||||
got, err := json.Marshal(member)
|
||||
if err != nil {
|
||||
t.Fatalf("re-encode %s blockers: %v", where, err)
|
||||
}
|
||||
if string(got) != blockedCloseWire {
|
||||
t.Errorf("%s blockers = %s, want %s", where, got, blockedCloseWire)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCloseBlockedNamesItsBlockers is the reason the live-blocker refusal is no
|
||||
// longer anonymous: a client must be able to say WHICH issues hold the close,
|
||||
// as the direct CLI does, without parsing prose. The list is the typed one the
|
||||
// refusing check filled, and `detail` opens with the direct route's sentence
|
||||
// built from it.
|
||||
func TestCloseBlockedNamesItsBlockers(t *testing.T) {
|
||||
lifecycle := &roleLifecycle{closeErr: fmt.Errorf("close bd-1: %w", blockedCloseFixture())}
|
||||
ts := newCloseServer(t, lifecycle)
|
||||
|
||||
resp := ts.closeIssue(t, closePath, `{"actor":"alice"}`)
|
||||
if resp.StatusCode != http.StatusConflict {
|
||||
t.Fatalf("status = %d, want 409: %s", resp.StatusCode, readAll(t, resp))
|
||||
}
|
||||
body := decodeBody(t, resp)
|
||||
if body["code"] != string(CodeNotClosable) {
|
||||
t.Errorf("code = %v, want %s", body["code"], CodeNotClosable)
|
||||
}
|
||||
if _, present := body["open_children"]; present {
|
||||
t.Errorf("the blocker refusal reported open_children: %v", body)
|
||||
}
|
||||
assertBlockersMember(t, "problem", body["blockers"])
|
||||
if want := blockedCloseSentence + "; clear the blocker or close with force"; body["detail"] != want {
|
||||
t.Errorf("detail = %q, want %q", body["detail"], want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCloseBlockedWithoutTheTypedListNamesNobody is the other side of the rule:
|
||||
// an error that only MENTIONS blockers in its message carries no member and the
|
||||
// generic detail, because the member is read from the typed list and never
|
||||
// scraped from prose.
|
||||
func TestCloseBlockedWithoutTheTypedListNamesNobody(t *testing.T) {
|
||||
lifecycle := &roleLifecycle{closeErr: fmt.Errorf("%w: bd-1 is blocked by [bd-2]", issueops.ErrCloseBlocked)}
|
||||
ts := newCloseServer(t, lifecycle)
|
||||
|
||||
resp := ts.closeIssue(t, closePath, `{"actor":"alice"}`)
|
||||
if resp.StatusCode != http.StatusConflict {
|
||||
t.Fatalf("status = %d, want 409: %s", resp.StatusCode, readAll(t, resp))
|
||||
}
|
||||
body := decodeBody(t, resp)
|
||||
if got, present := body["blockers"]; present {
|
||||
t.Errorf("blockers = %v; it was scraped from the message rather than read from the typed list", got)
|
||||
}
|
||||
if want := "issue is blocked; clear the blocker or close with force"; body["detail"] != want {
|
||||
t.Errorf("detail = %q, want the generic %q", body["detail"], want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCloseUnknownIDIs404 keeps the miss on the shape the document already
|
||||
// describes rather than inventing one for this operation.
|
||||
func TestCloseUnknownIDIs404(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -117,7 +117,9 @@ const (
|
|||
// children, or a live blocker. The open-children refusal carries the count
|
||||
// in the `open_children` extension member, read inside the refusing
|
||||
// transaction — never parsed out of the sentinel's message text — and its
|
||||
// PRESENCE is how a client tells the two refusals apart without prose.
|
||||
// PRESENCE is how a client tells the two refusals apart without prose. The
|
||||
// live-blocker refusal names its blockers in the `blockers` member, from
|
||||
// *issueops.BlockedError's typed list (see closeBlocked).
|
||||
//
|
||||
// A 409 rather than the delete precedent's 400: this is a statement about
|
||||
// the current state of one named resource, so the same request succeeds or
|
||||
|
|
@ -1056,6 +1058,69 @@ func (r Result) WithOpenChildren(n int) Result {
|
|||
return r
|
||||
}
|
||||
|
||||
// Blocker kinds on the wire, the `kind` member of apigen.Blocker.
|
||||
const (
|
||||
blockerKindLocal = "local"
|
||||
blockerKindExternal = "external"
|
||||
)
|
||||
|
||||
// closeBlocked builds the live-blocker `not_closable` for one refusal site.
|
||||
//
|
||||
// subject and hint are the site's own words ("… closes a blocked issue",
|
||||
// "clear the blocker, or send …"). When err carries the typed
|
||||
// *issueops.BlockedError the refusing check filled, the subject is replaced by
|
||||
// that error's own sentence — "cannot close blocked issue: <id> is blocked by
|
||||
// [<blockers>]", byte-identical to what the direct CLI prints before its
|
||||
// --force hint — and the list travels typed in the `blockers` member. Both are
|
||||
// built from the typed fields, never parsed out of a message: an error that
|
||||
// only MENTIONS blockers in prose keeps the generic detail and no member.
|
||||
func closeBlocked(err error, subject, hint string) (string, *[]apigen.Blocker) {
|
||||
blocked := closeBlockedError(err)
|
||||
if blocked == nil {
|
||||
return subject + "; " + hint, nil
|
||||
}
|
||||
return blocked.Error() + "; " + hint, wireBlockers(blocked.Blockers)
|
||||
}
|
||||
|
||||
// closeBlockedError returns the typed close-policy refusal err carries, or nil
|
||||
// when it carries none or names no blockers. The wrapped sentinel is checked
|
||||
// too, so a BlockedError raised for some other refusal is never reported as a
|
||||
// close's blockers.
|
||||
func closeBlockedError(err error) *issueops.BlockedError {
|
||||
var blocked *issueops.BlockedError
|
||||
if !errors.As(err, &blocked) || !errors.Is(blocked.Err, issueops.ErrCloseBlocked) || len(blocked.Blockers) == 0 {
|
||||
return nil
|
||||
}
|
||||
return blocked
|
||||
}
|
||||
|
||||
// wireBlockers projects typed blockers onto the `blockers` member, in the
|
||||
// order the refusing check reported them. `type` is omitted, not sent empty,
|
||||
// when the refusal did not report the edge type.
|
||||
func wireBlockers(blockers []issueops.Blocker) *[]apigen.Blocker {
|
||||
out := make([]apigen.Blocker, 0, len(blockers))
|
||||
for _, blocker := range blockers {
|
||||
wire := apigen.Blocker{Id: blocker.ID, Kind: blockerKindLocal}
|
||||
if blocker.External() {
|
||||
wire.Kind = blockerKindExternal
|
||||
}
|
||||
if blocker.Type != "" {
|
||||
wire.Type = ptrTo(string(blocker.Type))
|
||||
}
|
||||
out = append(out, wire)
|
||||
}
|
||||
return &out
|
||||
}
|
||||
|
||||
// closeBlockedResult is closeBlocked as a problem document: the live-blocker
|
||||
// `not_closable` with its `blockers` member when the refusal named them.
|
||||
func closeBlockedResult(err error, subject, hint string) Result {
|
||||
detail, blockers := closeBlocked(err, subject, hint)
|
||||
res := newResult(CodeNotClosable, detail)
|
||||
res.Problem.Blockers = blockers
|
||||
return res
|
||||
}
|
||||
|
||||
// WithDependencyTypeConflict attaches the two `dependency_exists` extension
|
||||
// members: the type the pair already carries and the type the request asked
|
||||
// for. Populate them from *issueops.DependencyTypeConflictError's fields — the
|
||||
|
|
@ -1395,7 +1460,7 @@ func ClassifyError(err error) Result {
|
|||
return newResult(CodeNotClosable, "issue has open children; close them first or close with force")
|
||||
|
||||
case errors.Is(err, issueops.ErrCloseBlocked):
|
||||
return newResult(CodeNotClosable, "issue is blocked; clear the blocker or close with force")
|
||||
return closeBlockedResult(err, "issue is blocked", "clear the blocker or close with force")
|
||||
|
||||
case errors.Is(err, ErrBusy):
|
||||
res := newResult(CodeBusy, "")
|
||||
|
|
|
|||
|
|
@ -2388,8 +2388,8 @@ paths:
|
|||
`not_closable` is close policy refusing a `patch.status` that
|
||||
crosses into the workspace's done category: open children, or a live
|
||||
blocker. `open_children` is attached for the first and withheld for
|
||||
the second, so member presence tells them apart.
|
||||
`force_close_policy` is the bypass.
|
||||
the second, so member presence tells them apart; the second names
|
||||
its blockers in `blockers`. `force_close_policy` is the bypass.
|
||||
|
||||
|
||||
`already_claimed` with `param: "patch.assignee"` is the assignee
|
||||
|
|
@ -3049,9 +3049,10 @@ paths:
|
|||
|
||||
An unforced close is refused with `409` / `not_closable` when the issue
|
||||
has open children (the refusal carries `open_children`, the count the
|
||||
refusing transaction observed) or a live blocker (no such member). The
|
||||
MEMBER'S PRESENCE is the discriminator, so telling the two apart never
|
||||
requires reading `detail`. Both refusals are the ROLE's, so `force`
|
||||
refusing transaction observed) or a live blocker (no `open_children`;
|
||||
the blockers themselves travel in `blockers`). The MEMBER'S PRESENCE is
|
||||
the discriminator, so telling the two apart never requires reading
|
||||
`detail`. Both refusals are the ROLE's, so `force`
|
||||
bypasses those two and nothing else — this endpoint cannot skip a guard
|
||||
by forgetting one exists.
|
||||
|
||||
|
|
@ -3142,7 +3143,8 @@ paths:
|
|||
|
||||
`not_closable`: close policy refused an unforced close — the issue
|
||||
has open children, carrying the `open_children` extension member, or
|
||||
a live blocker, which carries none. Resend with `force: true` to
|
||||
a live blocker, which carries no count and names the blockers in
|
||||
`blockers`. Resend with `force: true` to
|
||||
bypass both. `force` does NOT bypass the guard above.
|
||||
|
||||
|
||||
|
|
@ -3991,9 +3993,9 @@ paths:
|
|||
|
||||
`not_closable` is close policy refusing a `close` item, or an
|
||||
`update` whose `patch.status` crosses into the workspace's done
|
||||
category: open children, or a live blocker. `close.force` and
|
||||
`update.force_close_policy` are the per-item bypasses, and a forced
|
||||
item never earns this.
|
||||
category: open children (`open_children`), or a live blocker
|
||||
(`blockers`). `close.force` and `update.force_close_policy` are the
|
||||
per-item bypasses, and a forced item never earns this.
|
||||
|
||||
|
||||
`already_claimed` is the assignee fence: an `update` whose
|
||||
|
|
@ -10025,6 +10027,15 @@ components:
|
|||
PRESENCE is the discriminator between the two `not_closable`
|
||||
refusals, exactly as it is on a problem document: present means open
|
||||
children, absent means a live blocker.
|
||||
blockers:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Blocker'
|
||||
description: >-
|
||||
With `not_closable` on the live-blocker refusal: the blockers that
|
||||
refused THIS item, exactly as `Problem.blockers` carries them for
|
||||
the single close, and optional for the same reason. Absent on a
|
||||
successful item and on every other refusal.
|
||||
code:
|
||||
type: string
|
||||
description: >-
|
||||
|
|
@ -10046,6 +10057,31 @@ components:
|
|||
`code`. It reflects the request and this server's own words rather
|
||||
than the role's message, for the reason `Problem.detail` gives.
|
||||
|
||||
Blocker:
|
||||
type: object
|
||||
required: [id, kind]
|
||||
description: >-
|
||||
One live blocker named by a blocked-issue refusal.
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
description: >-
|
||||
The blocker: a local issue id, or the full
|
||||
`external:<project>:<capability>` reference.
|
||||
kind:
|
||||
type: string
|
||||
description: >-
|
||||
`local` for an issue this database holds, which clears when it
|
||||
closes; `external` for an `external:` reference, which clears when
|
||||
the named project ships the capability. The set may grow;
|
||||
default-branch on unknown values.
|
||||
type:
|
||||
type: string
|
||||
description: >-
|
||||
The blocking edge's dependency type (`blocks`, `waits-for`,
|
||||
`conditional-blocks`). ABSENT when the refusal did not report it,
|
||||
which is the case for an `external:` blocker.
|
||||
|
||||
CloseIssueRequest:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
|
|
@ -11076,9 +11112,32 @@ components:
|
|||
|
||||
|
||||
PRESENT ONLY for the open-children refusal. The other
|
||||
`not_closable` refusal is a live blocker and carries no such
|
||||
member, so member presence — not prose — is how a client tells the
|
||||
`not_closable` refusal is a live blocker and carries `blockers`
|
||||
instead, so member presence — not prose — is how a client tells the
|
||||
two apart. Both are bypassed by `force`.
|
||||
blockers:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Blocker'
|
||||
description: >-
|
||||
With `not_closable`, and ONLY on the live-blocker refusal: the live
|
||||
blockers that refused the close, in the order the refusing check
|
||||
reported them, read from the refusal's typed list rather than
|
||||
parsed out of any message. Never present together with
|
||||
`open_children`.
|
||||
|
||||
|
||||
A client renders the direct CLI's sentence from it: `cannot close
|
||||
blocked issue: <id> is blocked by [<b1> <b2> …]`, each blocker
|
||||
spelled `id` when its `type` is absent or `blocks` and `id (type)`
|
||||
otherwise — the sentence `detail` already begins with when this
|
||||
member is present.
|
||||
|
||||
|
||||
IT IS OPTIONAL. A refusal that could not name its blockers omits
|
||||
it and keeps the generic `detail`; absence means "this refusal did
|
||||
not name them", never "nothing blocks it". Re-read the issue's
|
||||
dependencies then.
|
||||
existing_type:
|
||||
type: string
|
||||
description: >-
|
||||
|
|
|
|||
|
|
@ -572,8 +572,8 @@ func (s *Server) failUpdate(w http.ResponseWriter, r *http.Request, request issu
|
|||
case errors.Is(err, issueops.ErrCloseBlocked):
|
||||
// No `open_children` member, and its ABSENCE is what tells a client
|
||||
// which of the two close-policy refusals it got.
|
||||
s.fail(w, r, named(newResult(CodeNotClosable,
|
||||
"`patch.status` closes a blocked issue; clear the blocker, or send `force_close_policy`"),
|
||||
s.fail(w, r, named(closeBlockedResult(err,
|
||||
"`patch.status` closes a blocked issue", "clear the blocker, or send `force_close_policy`"),
|
||||
patchParam("status")))
|
||||
|
||||
case errors.Is(err, storage.ErrAlreadyClaimed):
|
||||
|
|
|
|||
|
|
@ -895,6 +895,23 @@ func TestUpdateAnswersThePolicyRefusalsItsMembersEarned(t *testing.T) {
|
|||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "status crossing with a typed live blocker names it",
|
||||
body: `{"actor":"alice","patch":{"status":"closed"}}`,
|
||||
err: fmt.Errorf("close: %w", blockedCloseFixture()),
|
||||
wantStatus: http.StatusConflict,
|
||||
wantCode: CodeNotClosable,
|
||||
wantParam: "patch.status",
|
||||
check: func(t *testing.T, body map[string]any) {
|
||||
assertBlockersMember(t, "problem", body["blockers"])
|
||||
if want := blockedCloseSentence + "; clear the blocker, or send `force_close_policy`"; body["detail"] != want {
|
||||
t.Errorf("detail = %q, want %q", body["detail"], want)
|
||||
}
|
||||
if _, present := body["open_children"]; present {
|
||||
t.Error("open_children is present on the blocker refusal")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "assignee transfer off a live owner",
|
||||
body: `{"actor":"alice","patch":{"assignee":"carol"}}`,
|
||||
|
|
|
|||
|
|
@ -116,6 +116,7 @@ go_test(
|
|||
"//internal/storage/sqlbuild",
|
||||
"//internal/testutil",
|
||||
"//internal/types",
|
||||
"//issueops",
|
||||
"@com_github_data_dog_go_sqlmock//:go-sqlmock",
|
||||
"@com_github_go_sql_driver_mysql//:mysql",
|
||||
"@com_github_google_uuid//:uuid",
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/domain"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
publicops "github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
func (s *testSuite) TestIssueUseCase_CloseIssueChecked() {
|
||||
|
|
@ -82,6 +83,13 @@ func (s *testSuite) uccCloseCheckedDirectBlockerRefuses() {
|
|||
s.True(errors.Is(err, storage.ErrCloseBlocked), "refusal must carry the shared sentinel")
|
||||
s.ErrorContains(err, "is blocked by")
|
||||
s.ErrorContains(err, "bd-ucc-clc-tgt", "message must name the live blocker")
|
||||
// The list is typed as well as printed, so the HTTP surface can publish it
|
||||
// without parsing the sentence, and the sentence is unchanged.
|
||||
var blocked *publicops.BlockedError
|
||||
s.Require().True(errors.As(err, &blocked), "refusal must carry *BlockedError: %v", err)
|
||||
s.Equal("bd-ucc-clc-src", blocked.IssueID)
|
||||
s.Equal([]publicops.Blocker{{ID: "bd-ucc-clc-tgt", Type: types.DepBlocks}}, blocked.Blockers)
|
||||
s.Equal("cannot close blocked issue: bd-ucc-clc-src is blocked by [bd-ucc-clc-tgt]", blocked.Error())
|
||||
s.False(res.Closed)
|
||||
|
||||
// The refused issue stays open.
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ func (s *Store) guardExternalClose(ctx context.Context, id string, force bool) e
|
|||
return err
|
||||
}
|
||||
if blockers := state.refsByIssue[id]; len(blockers) > 0 {
|
||||
return fmt.Errorf("%w: %s is blocked by %v", storage.ErrCloseBlocked, id, blockers)
|
||||
return publicops.NewCloseBlockedError(id, blockers)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -471,7 +471,7 @@ func (s *Store) CloseIssueChecked(ctx context.Context, issueID, actor string, op
|
|||
return storage.CloseIssueResult{}, err
|
||||
}
|
||||
if blocked && len(blockers) > 0 {
|
||||
return storage.CloseIssueResult{}, fmt.Errorf("%w: %s is blocked by %v", storage.ErrCloseBlocked, issueID, blockers)
|
||||
return storage.CloseIssueResult{}, publicops.NewCloseBlockedError(issueID, blockers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -499,9 +499,11 @@ func TestIssueLifecycleRefusesExternalCloseAndDoneUpdate(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("IssueLifecycle: %v", err)
|
||||
}
|
||||
if _, err := ops.Close(t.Context(), publicops.CloseRequest{IssueID: issue.ID}); !errors.Is(err, storage.ErrCloseBlocked) {
|
||||
_, err = ops.Close(t.Context(), publicops.CloseRequest{IssueID: issue.ID})
|
||||
if !errors.Is(err, storage.ErrCloseBlocked) {
|
||||
t.Fatalf("Close error = %v, want ErrCloseBlocked", err)
|
||||
}
|
||||
assertExternalCloseBlockers(t, err, issue.ID, "external:remote:payments")
|
||||
if _, err := ops.Update(t.Context(), publicops.UpdateRequest{IssueID: issue.ID, Patch: publicops.IssuePatch{
|
||||
Status: publicops.Field[types.Status]{Set: true, Value: types.StatusClosed},
|
||||
}}); !errors.Is(err, storage.ErrCloseBlocked) {
|
||||
|
|
@ -592,9 +594,11 @@ func TestCloseIssueCheckedRefusesUnsatisfiedExternalRef(t *testing.T) {
|
|||
}}
|
||||
store := testStore(raw, &fakeStore{}, true)
|
||||
|
||||
if _, err := store.CloseIssueChecked(t.Context(), a.ID, "tester", storage.CloseIssueOptions{}); !errors.Is(err, storage.ErrCloseBlocked) {
|
||||
_, err := store.CloseIssueChecked(t.Context(), a.ID, "tester", storage.CloseIssueOptions{})
|
||||
if !errors.Is(err, storage.ErrCloseBlocked) {
|
||||
t.Fatalf("CloseIssueChecked error = %v, want ErrCloseBlocked", err)
|
||||
}
|
||||
assertExternalCloseBlockers(t, err, a.ID, ref)
|
||||
if len(raw.closed) != 0 {
|
||||
t.Fatalf("raw close calls = %v, want none", raw.closed)
|
||||
}
|
||||
|
|
@ -675,3 +679,21 @@ func issueIDs(issues []*types.Issue) []string {
|
|||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// assertExternalCloseBlockers checks that an external close refusal carries the
|
||||
// typed blocker list — the unsatisfied reference, as an external blocker with no
|
||||
// reported edge type — and still spells the historical sentence.
|
||||
func assertExternalCloseBlockers(t *testing.T, err error, issueID, ref string) {
|
||||
t.Helper()
|
||||
var blocked *publicops.BlockedError
|
||||
if !errors.As(err, &blocked) {
|
||||
t.Fatalf("close refusal %v carries no *BlockedError", err)
|
||||
}
|
||||
want := []publicops.Blocker{{ID: ref}}
|
||||
if blocked.IssueID != issueID || !slices.Equal(blocked.Blockers, want) || !blocked.Blockers[0].External() {
|
||||
t.Errorf("BlockedError = %+v, want issue %s blocked by %+v (external)", blocked, issueID, want)
|
||||
}
|
||||
if wantText := "cannot close blocked issue: " + issueID + " is blocked by [" + ref + "]"; err.Error() != wantText {
|
||||
t.Errorf("refusal = %q, want %q", err.Error(), wantText)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -379,7 +379,7 @@ func (u *issueUseCase) guardExternalClose(ctx context.Context, id string, force
|
|||
return fmt.Errorf("external dependencies: %w", err)
|
||||
}
|
||||
if blockers := state.refsByIssue[id]; len(blockers) > 0 {
|
||||
return fmt.Errorf("%w: %s is blocked by %v", storage.ErrCloseBlocked, id, blockers)
|
||||
return publicops.NewCloseBlockedError(id, blockers)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -211,9 +211,11 @@ func TestWrapUOWProviderRefusesProxiedCheckedClose(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("NewUOW: %v", err)
|
||||
}
|
||||
if _, err := uw.IssueUseCase().CloseIssueChecked(t.Context(), blocked.ID, domain.CloseIssueParams{}, "tester", false); !errors.Is(err, storage.ErrCloseBlocked) {
|
||||
_, err = uw.IssueUseCase().CloseIssueChecked(t.Context(), blocked.ID, domain.CloseIssueParams{}, "tester", false)
|
||||
if !errors.Is(err, storage.ErrCloseBlocked) {
|
||||
t.Fatalf("CloseIssueChecked error = %v, want ErrCloseBlocked", err)
|
||||
}
|
||||
assertExternalCloseBlockers(t, err, blocked.ID, "external:remote:payments")
|
||||
if len(issues.closed) != 0 {
|
||||
t.Fatalf("inner close calls = %v, want none", issues.closed)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
publicops "github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
const closeCheckedSavepointPrefix = "issueops_close_checked_"
|
||||
|
|
@ -151,12 +152,12 @@ func enforceClosePolicyForTargetInTx(ctx context.Context, tx DBTX, id, targetCol
|
|||
return 0, &storage.CloseOpenChildrenError{IssueID: id, OpenChildren: openChildren}
|
||||
}
|
||||
if !force && !closed {
|
||||
blocked, blockers, err := IsBlockedInTx(ctx, tx, id)
|
||||
blocked, blockers, err := isBlockedByInTx(ctx, tx, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if blocked && len(blockers) > 0 {
|
||||
return 0, fmt.Errorf("%w: %s is blocked by %v", storage.ErrCloseBlocked, id, blockers)
|
||||
return 0, &publicops.BlockedError{IssueID: id, Blockers: blockers, Err: storage.ErrCloseBlocked}
|
||||
}
|
||||
}
|
||||
return openChildren, nil
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
publicops "github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// GetAllDependencyRecordsInTx returns all dependency records from permanent and
|
||||
|
|
@ -943,10 +944,25 @@ func GetNewlyUnblockedByCloseInTx(ctx context.Context, tx DBTX, closedIssueID st
|
|||
|
||||
// IsBlockedInTx checks if an issue is blocked by active dependencies within
|
||||
// an existing transaction. Returns whether the issue is blocked and, if so,
|
||||
// a list of blocker descriptions for display.
|
||||
// a list of blocker descriptions for display, each spelled by
|
||||
// publicops.Blocker.String.
|
||||
func IsBlockedInTx(ctx context.Context, tx DBTX, issueID string) (bool, []string, error) {
|
||||
blocked, blockers, err := isBlockedByInTx(ctx, tx, issueID)
|
||||
if err != nil || len(blockers) == 0 {
|
||||
return blocked, nil, err
|
||||
}
|
||||
names := make([]string, 0, len(blockers))
|
||||
for _, blocker := range blockers {
|
||||
names = append(names, blocker.String())
|
||||
}
|
||||
return blocked, names, nil
|
||||
}
|
||||
|
||||
// isBlockedByInTx is IsBlockedInTx with the live direct blockers kept typed, so
|
||||
// a refusal can carry them without re-reading its own display strings.
|
||||
//
|
||||
//nolint:gosec // G201: table names are hardcoded constants.
|
||||
func IsBlockedInTx(ctx context.Context, tx DBTX, issueID string) (bool, []string, error) {
|
||||
func isBlockedByInTx(ctx context.Context, tx DBTX, issueID string) (bool, []publicops.Blocker, error) {
|
||||
var blocked bool
|
||||
found := false
|
||||
for _, table := range []string{"issues", "wisps"} {
|
||||
|
|
@ -1010,7 +1026,7 @@ func IsBlockedInTx(ctx context.Context, tx DBTX, issueID string) (bool, []string
|
|||
if err != nil {
|
||||
return false, nil, fmt.Errorf("check blocker status: %w", err)
|
||||
}
|
||||
var blockers []string
|
||||
var blockers []publicops.Blocker
|
||||
for _, e := range edges {
|
||||
status, ok := statusByID[e.dependsOnID]
|
||||
if !ok {
|
||||
|
|
@ -1019,11 +1035,7 @@ func IsBlockedInTx(ctx context.Context, tx DBTX, issueID string) (bool, []string
|
|||
if status == types.StatusClosed || status == types.StatusPinned {
|
||||
continue
|
||||
}
|
||||
if e.depType != "blocks" {
|
||||
blockers = append(blockers, e.dependsOnID+" ("+e.depType+")")
|
||||
} else {
|
||||
blockers = append(blockers, e.dependsOnID)
|
||||
}
|
||||
blockers = append(blockers, publicops.Blocker{ID: e.dependsOnID, Type: types.DependencyType(e.depType)})
|
||||
}
|
||||
|
||||
return true, blockers, nil
|
||||
|
|
|
|||
|
|
@ -3,8 +3,10 @@ package issueops
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/beadserrors"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// ErrAlreadyClaimed is returned when attempting to claim an issue that is already
|
||||
|
|
@ -103,6 +105,97 @@ var ErrPrefixMismatch = errors.New("prefix mismatch")
|
|||
// or an open blocking gate). Bypass with CloseIssueOptions.Force.
|
||||
var ErrCloseBlocked = errors.New("cannot close blocked issue")
|
||||
|
||||
// Blocker is one live blocker named by a BlockedError: a local issue id, or an
|
||||
// `external:<project>:<capability>` reference, with the blocking edge's type
|
||||
// when the refusal knew it.
|
||||
type Blocker struct {
|
||||
// ID is the blocker's issue id or its full `external:` reference.
|
||||
ID string
|
||||
// Type is the blocking edge's dependency type. It is empty when the
|
||||
// refusal did not report it, which is the case for an `external:`
|
||||
// reference named by the external-dependency guard.
|
||||
Type types.DependencyType
|
||||
}
|
||||
|
||||
// externalBlockerPrefix marks a blocker that lives in another project and is
|
||||
// satisfied by a shipped capability rather than by a local close.
|
||||
const externalBlockerPrefix = "external:"
|
||||
|
||||
// External reports whether the blocker is an `external:` reference rather than
|
||||
// an issue this database holds.
|
||||
func (b Blocker) External() bool { return strings.HasPrefix(b.ID, externalBlockerPrefix) }
|
||||
|
||||
// String spells the blocker the way a blocked-issue refusal always has: the
|
||||
// bare id for a `blocks` edge or an unreported type, and "id (type)" for the
|
||||
// other blocking edges. ParseBlocker is its inverse.
|
||||
func (b Blocker) String() string {
|
||||
if b.Type == "" || b.Type == types.DepBlocks {
|
||||
return b.ID
|
||||
}
|
||||
return b.ID + " (" + string(b.Type) + ")"
|
||||
}
|
||||
|
||||
// ParseBlocker reads one entry of the []string blocker list a store's IsBlocked
|
||||
// reports — the spelling Blocker.String writes — back into its typed form. It
|
||||
// exists for producers that hold only that string contract; a producer with the
|
||||
// edges in hand builds Blocker values directly. A bare local id is a `blocks`
|
||||
// edge, and an `external:` reference keeps an unreported type.
|
||||
//
|
||||
// The `external:` test comes first because a capability may itself end in a
|
||||
// parenthesized suffix: `external:remote:pay (beta)` is a legal reference, and
|
||||
// splitting it would report a truncated ID and a type the external guard never
|
||||
// names.
|
||||
func ParseBlocker(s string) Blocker {
|
||||
if strings.HasPrefix(s, externalBlockerPrefix) {
|
||||
return Blocker{ID: s}
|
||||
}
|
||||
if i := strings.LastIndex(s, " ("); i > 0 && strings.HasSuffix(s, ")") {
|
||||
return Blocker{ID: s[:i], Type: types.DependencyType(s[i+2 : len(s)-1])}
|
||||
}
|
||||
return Blocker{ID: s, Type: types.DepBlocks}
|
||||
}
|
||||
|
||||
// BlockedError reports the live blockers that refused an operation on a blocked
|
||||
// issue, read by the refusing check itself. It wraps the refusal rather than
|
||||
// replacing it — the ClaimConflictError arrangement — so the sentinel still
|
||||
// matches and its message is byte-identical to the historical
|
||||
// "<sentinel>: <id> is blocked by [<blockers>]", while a caller that reports the
|
||||
// refusal elsewhere (the HTTP surface's `blockers` member) reads the typed list
|
||||
// instead of parsing that prose.
|
||||
//
|
||||
// Err is ErrCloseBlocked for a close-policy refusal. The type is not
|
||||
// close-specific: any refusal whose subject is "this issue is blocked by these"
|
||||
// can carry its own sentinel.
|
||||
type BlockedError struct {
|
||||
// IssueID names the issue that was refused.
|
||||
IssueID string
|
||||
// Blockers are the live blockers, in the order the check reported them.
|
||||
Blockers []Blocker
|
||||
// Err is the wrapped refusal sentinel, e.g. ErrCloseBlocked.
|
||||
Err error
|
||||
}
|
||||
|
||||
// NewCloseBlockedError builds the close-policy refusal for issueID from the
|
||||
// []string blocker list a store's IsBlocked reports (see ParseBlocker).
|
||||
func NewCloseBlockedError(issueID string, blockers []string) *BlockedError {
|
||||
parsed := make([]Blocker, 0, len(blockers))
|
||||
for _, blocker := range blockers {
|
||||
parsed = append(parsed, ParseBlocker(blocker))
|
||||
}
|
||||
return &BlockedError{IssueID: issueID, Blockers: parsed, Err: ErrCloseBlocked}
|
||||
}
|
||||
|
||||
func (e *BlockedError) Error() string {
|
||||
names := make([]string, 0, len(e.Blockers))
|
||||
for _, blocker := range e.Blockers {
|
||||
names = append(names, blocker.String())
|
||||
}
|
||||
return fmt.Sprintf("%v: %s is blocked by %v", e.Err, e.IssueID, names)
|
||||
}
|
||||
|
||||
// Unwrap makes BlockedError match the refusal it carries.
|
||||
func (e *BlockedError) Unwrap() error { return e.Err }
|
||||
|
||||
// ErrCloseOpenChildren is returned when an unforced close finds open
|
||||
// parent-child dependents.
|
||||
var ErrCloseOpenChildren = errors.New("cannot close issue with open children")
|
||||
|
|
|
|||
|
|
@ -200,6 +200,54 @@ func TestOpenChildrenCloseContractShape(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestBlockedErrorKeepsTheHistoricalSentence pins the compatibility half of the
|
||||
// typed refusal: its message is byte-identical to the fmt.Errorf every producer
|
||||
// used before it, so the direct and proxied CLI lines and `failed[].error` do
|
||||
// not move, while the list it prints is now typed.
|
||||
func TestBlockedErrorKeepsTheHistoricalSentence(t *testing.T) {
|
||||
blockers := []string{"bd-2", "bd-3 (waits-for)", "bd-4 (conditional-blocks)", "external:remote:payments", "external:remote:pay (beta)"}
|
||||
err := issueops.NewCloseBlockedError("bd-1", blockers)
|
||||
|
||||
legacy := fmt.Errorf("%w: %s is blocked by %v", issueops.ErrCloseBlocked, "bd-1", blockers)
|
||||
if err.Error() != legacy.Error() {
|
||||
t.Errorf("BlockedError = %q, want the historical %q", err.Error(), legacy.Error())
|
||||
}
|
||||
if !errors.Is(err, issueops.ErrCloseBlocked) {
|
||||
t.Errorf("BlockedError does not match ErrCloseBlocked: %v", err)
|
||||
}
|
||||
var typed *issueops.BlockedError
|
||||
if !errors.As(fmt.Errorf("close bd-1: %w", err), &typed) || typed.IssueID != "bd-1" {
|
||||
t.Fatalf("errors.As through a wrap = %+v", typed)
|
||||
}
|
||||
|
||||
want := []issueops.Blocker{
|
||||
{ID: "bd-2", Type: "blocks"},
|
||||
{ID: "bd-3", Type: "waits-for"},
|
||||
{ID: "bd-4", Type: "conditional-blocks"},
|
||||
{ID: "external:remote:payments"},
|
||||
// A capability may end in a parenthesized suffix. The whole
|
||||
// reference is the ID, and the external guard reports no type.
|
||||
{ID: "external:remote:pay (beta)"},
|
||||
}
|
||||
// %#v, not %+v: Blocker implements String(), so the default verbs print
|
||||
// the reassembled spelling — which round-trips even when the parse split
|
||||
// an external reference wrongly, making got and want look identical.
|
||||
if !reflect.DeepEqual(typed.Blockers, want) {
|
||||
t.Errorf("Blockers = %#v, want %#v", typed.Blockers, want)
|
||||
}
|
||||
for i, blocker := range typed.Blockers {
|
||||
if got := blocker.String(); got != blockers[i] {
|
||||
t.Errorf("Blockers[%d].String() = %q, want the IsBlocked spelling %q", i, got, blockers[i])
|
||||
}
|
||||
if parsed := issueops.ParseBlocker(blocker.String()); parsed != blocker {
|
||||
t.Errorf("ParseBlocker(String(%+v)) = %+v; the two must be inverses", blocker, parsed)
|
||||
}
|
||||
if got, wantExternal := blocker.External(), i >= 3; got != wantExternal {
|
||||
t.Errorf("Blockers[%d].External() = %v, want %v", i, got, wantExternal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicDependencyConflictTypesRemainCanonical(t *testing.T) {
|
||||
var typeConflict error = &issueops.DependencyTypeConflictError{}
|
||||
var hierarchyConflict error = &issueops.DependencyHierarchyConflictError{}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue