Skip to content

Commit d03b838

Browse files
fix(cli): keep bulk JSON output parseable (#293)
Co-authored-by: bougyman's bot <ruby-automation@users.noreply.github.com>
1 parent d5ee641 commit d03b838

8 files changed

Lines changed: 705 additions & 39 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Do not apply both to the same text.
2525
- Phase 10 Plan: documents/phase-10-plan.adoc
2626
- Phase 11 Plan: documents/phase-11-plan.adoc
2727
- Phase 12 Plan: documents/phase-12-plan.adoc
28+
- Phase 12 JSON output decision: documents/phase-12-json-output-decision.adoc
2829
- Phase 12 confirmation decision: documents/phase-12-confirmation-decision.adoc
2930
- Phase 13 Plan: documents/phase-13-plan.adoc
3031
- Phase 14 Plan: documents/phase-14-plan.adoc

‎Readme.adoc‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ unassigned. In JSON mode, the warning goes to stderr so stdout stays valid
323323
JSON. The existing `--unassigned` issue-list filter is not a selector for this
324324
command because unassigning already-unassigned issues has no effect.
325325

326+
NOTE: In JSON mode, filtered unassign requires `--yes` or `--dry-run`. Partial
327+
project or assignee matches return a diagnostic on stderr instead of prompting.
328+
Successful commands write one JSON value to stdout.
329+
326330
==== Create an issue
327331

328332
[source,sh]
@@ -460,6 +464,12 @@ $ lc issue move --from Retired --to Active --dry-run <4>
460464
<3> Move by project UUID — the way to target a project in another team
461465
<4> Preview without mutating
462466

467+
NOTE: In JSON mode, ID and bulk moves require `--yes` or `--dry-run`. Project
468+
names must match exactly. A name-based bulk move must provide `--team` or an
469+
active profile; UUID-only moves do not need either. Partial matches and other
470+
prompt-required inputs return a diagnostic on stderr. A successful dry-run
471+
writes the selected issues as one JSON value.
472+
463473
==== Default team/project (profiles)
464474

465475
Not in Ruby's `linear-cli` - save a named team/project bundle once, then

‎app/lib/linear_cli/cli/commands/issues/filter.ex‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ defmodule LinearCli.CLI.Commands.Issues.Filter do
8989
end
9090
end
9191

92+
defp resolve_project_match(projects, search, :strict_non_interactive) do
93+
case Projects.project_for_strict(projects, search) do
94+
nil ->
95+
{:error,
96+
{:smells_bad, "JSON output requires an exact project match for #{inspect(search)}"}}
97+
98+
project ->
99+
{:ok, project.id}
100+
end
101+
end
102+
92103
defp resolve_assignee_id(nil, _team_key, _opts), do: {:ok, nil}
93104

94105
defp resolve_assignee_id(assignee, _team_key, _opts)
@@ -98,7 +109,12 @@ defmodule LinearCli.CLI.Commands.Issues.Filter do
98109
defp resolve_assignee_id(assignee, team_key, opts) do
99110
if Keyword.get(opts, :resolve_assignee, false) do
100111
with {:ok, members} <- assignee_members(team_key),
101-
{:ok, member} <- resolve_assignee_member(members, assignee) do
112+
{:ok, member} <-
113+
resolve_assignee_member(
114+
members,
115+
assignee,
116+
Keyword.get(opts, :non_interactive, false)
117+
) do
102118
{:ok, member.id}
103119
end
104120
else
@@ -136,7 +152,7 @@ defmodule LinearCli.CLI.Commands.Issues.Filter do
136152
defp members_from_result({:ok, members_by_id}), do: {:ok, Map.values(members_by_id)}
137153
defp members_from_result(error), do: error
138154

139-
defp resolve_assignee_member(members, search) do
155+
defp resolve_assignee_member(members, search, non_interactive) do
140156
normalized_search = String.downcase(search)
141157
exact = Enum.filter(members, &assignee_exact_match?(&1, normalized_search))
142158

@@ -151,8 +167,15 @@ defmodule LinearCli.CLI.Commands.Issues.Filter do
151167
partial = Enum.filter(members, &assignee_partial_match?(&1, normalized_search))
152168

153169
case partial do
154-
[] -> {:error, unknown_assignee_error(members, search)}
155-
matches -> {:ok, Prompt.select("Assignee:", assignee_choices(matches))}
170+
[] ->
171+
{:error, unknown_assignee_error(members, search)}
172+
173+
_matches when non_interactive ->
174+
{:error,
175+
{:smells_bad, "JSON output requires an exact assignee match for #{inspect(search)}"}}
176+
177+
matches ->
178+
{:ok, Prompt.select("Assignee:", assignee_choices(matches))}
156179
end
157180
end
158181
end

‎app/lib/linear_cli/cli/commands/issues/move.ex‎

Lines changed: 110 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,47 @@ defmodule LinearCli.CLI.Commands.Issues.Move do
5252
defp resolve_move_project(issues, options) do
5353
with {:ok, tid} <- resolve_move_team_id(options.team || Profiles.default_team(), issues),
5454
{:ok, projects} <- Linear.projects_by_team(tid, %{search: options.project}) do
55-
project_result(Projects.project_for(projects, options.project), options.project)
55+
project =
56+
if options.output == "json" do
57+
Projects.project_for_strict(projects, options.project)
58+
else
59+
Projects.project_for(projects, options.project)
60+
end
61+
62+
project_result(project, options.project, options.output)
5663
end
5764
end
5865

59-
defp project_result(nil, search),
66+
defp project_result(nil, search, "json"),
67+
do:
68+
{:error,
69+
{:smells_bad, "JSON output requires an exact project match for #{inspect(search)}"}}
70+
71+
defp project_result(nil, search, _output),
6072
do: {:error, {:smells_bad, "No project found matching #{inspect(search)}"}}
6173

62-
defp project_result(project, _search), do: {:ok, project}
74+
defp project_result(project, _search, _output), do: {:ok, project}
6375

6476
defp resolve_move_team_id(nil, issues), do: {:ok, hd(issues).team.id}
6577

6678
defp resolve_move_team_id(key, _issues) do
6779
with {:ok, team} <- Linear.find_team(key), do: {:ok, team.id}
6880
end
6981

82+
defp execute_moves_if_confirmed(issues, _project, %{dry_run: true}, "json") do
83+
Display.show(one_or_many(issues), %{output: "json"})
84+
:ok
85+
end
86+
7087
defp execute_moves_if_confirmed(_issues, _project, %{dry_run: true}, _output), do: :ok
7188

7289
defp execute_moves_if_confirmed(issues, project, %{yes: true}, output),
7390
do: apply_moves(issues, project, output)
7491

92+
defp execute_moves_if_confirmed(_issues, _project, _flags, "json") do
93+
{:error, {:smells_bad, "JSON output requires --yes or --dry-run for issue move"}}
94+
end
95+
7596
defp execute_moves_if_confirmed(issues, project, _flags, output) do
7697
if Prompt.confirm_destructive?("Proceed with move?"),
7798
do: apply_moves(issues, project, output),
@@ -127,58 +148,114 @@ defmodule LinearCli.CLI.Commands.Issues.Move do
127148
defp validate_issue_ids(_issue_ids), do: :ok
128149

129150
defp move_issues_by_project(options, flags) do
130-
team_fn = fn -> WhatFor.team_for(options.team || Profiles.default_team()) end
151+
team_key = options.team || Profiles.default_team()
152+
team_fn = fn -> WhatFor.team_for(team_key) end
131153

132-
with {:ok, source} <- resolve_bulk_project(options.from, team_fn),
133-
{:ok, target} <- resolve_bulk_project(options.to, team_fn),
154+
with :ok <- validate_bulk_team(team_key, options),
155+
{:ok, source} <- resolve_bulk_project(options.from, team_fn, options.output),
156+
{:ok, target} <- resolve_bulk_project(options.to, team_fn, options.output),
134157
:ok <- guard_different_projects(source, target),
135158
{:ok, issues} <- Linear.issues(%{project_id: source.id, mine: false, all: flags.all}) do
136-
cond do
137-
issues == [] ->
138-
label = if flags.all, do: "issues", else: "open issues"
139-
Prompt.ok("No #{label} in #{source.name} to move")
140-
:ok
141-
142-
flags.dry_run ->
143-
Display.show(one_or_many(issues), %{output: options.output})
144-
Prompt.ok("Would move #{length(issues)} issue(s) from #{source.name} to #{target.name}")
145-
:ok
146-
147-
not flags.yes and
148-
not Prompt.confirm_destructive?(
149-
"Move #{length(issues)} issue(s) from #{source.name} to #{target.name}?"
150-
) ->
151-
Prompt.warn("Move cancelled")
152-
153-
true ->
154-
with {:ok, pairs} <- apply_project_moves(issues, target) do
155-
show_move_results(pairs, source, target, options.output)
156-
end
157-
end
159+
handle_bulk_move(issues, source, target, flags, options)
160+
end
161+
end
162+
163+
defp handle_bulk_move([], _source, _target, _flags, %{output: "json"}) do
164+
Display.show([], %{output: "json"})
165+
:ok
166+
end
167+
168+
defp handle_bulk_move([], source, _target, flags, %{output: _output}) do
169+
label = if flags.all, do: "issues", else: "open issues"
170+
Prompt.ok("No #{label} in #{source.name} to move")
171+
:ok
172+
end
173+
174+
defp handle_bulk_move(issues, source, target, %{dry_run: true}, options) do
175+
Display.show(one_or_many(issues), %{output: options.output})
176+
print_bulk_dry_run(issues, source, target, options)
177+
:ok
178+
end
179+
180+
defp handle_bulk_move(_issues, _source, _target, %{yes: false}, %{output: "json"}) do
181+
{:error, {:smells_bad, "JSON output requires --yes or --dry-run for issue move"}}
182+
end
183+
184+
defp handle_bulk_move(issues, source, target, %{yes: false}, options) do
185+
if Prompt.confirm_destructive?(
186+
"Move #{length(issues)} issue(s) from #{source.name} to #{target.name}?"
187+
),
188+
do: apply_project_moves_and_show(issues, source, target, options.output),
189+
else: Prompt.warn("Move cancelled")
190+
end
191+
192+
defp handle_bulk_move(issues, source, target, %{yes: true}, options),
193+
do: apply_project_moves_and_show(issues, source, target, options.output)
194+
195+
defp print_bulk_dry_run(_issues, _source, _target, %{output: "json"}), do: :ok
196+
197+
defp print_bulk_dry_run(issues, source, target, %{output: output}) when output != "json" do
198+
Prompt.ok("Would move #{length(issues)} issue(s) from #{source.name} to #{target.name}")
199+
end
200+
201+
defp apply_project_moves_and_show(issues, source, target, output) do
202+
with {:ok, pairs} <- apply_project_moves(issues, target) do
203+
show_move_results(pairs, source, target, output)
158204
end
159205
end
160206

161207
# UUID by structure: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12, dashes at fixed positions)
162208
defp resolve_bulk_project(
163209
<<_::8*8, ?-, _::4*8, ?-, _::4*8, ?-, _::4*8, ?-, _::12*8>> = uuid,
164-
_team_fn
210+
_team_fn,
211+
_output
165212
) do
166213
short_name = String.slice(uuid, 0, 8) <> "…"
167214
{:ok, struct(LinearCli.Linear.Project, %{id: uuid, name: short_name})}
168215
end
169216

170-
defp resolve_bulk_project(value, team_fn) do
217+
defp resolve_bulk_project(value, team_fn, output) do
171218
team = team_fn.()
172219

173220
with {:ok, projects} <- Linear.projects_by_team(team.id, %{search: value}),
174-
project when not is_nil(project) <- Projects.project_for(projects, value) do
221+
project when not is_nil(project) <- project_for_bulk(projects, value, output) do
175222
{:ok, project}
176223
else
177-
nil -> {:error, {:smells_bad, "No project found matching #{value}"}}
178-
{:error, reason} -> {:error, reason}
224+
nil ->
225+
if output == "json" do
226+
{:error,
227+
{:smells_bad, "JSON output requires an exact project match for #{inspect(value)}"}}
228+
else
229+
{:error, {:smells_bad, "No project found matching #{value}"}}
230+
end
231+
232+
{:error, reason} ->
233+
{:error, reason}
179234
end
180235
end
181236

237+
defp project_for_bulk(projects, value, "json"),
238+
do: Projects.project_for_strict(projects, value)
239+
240+
defp project_for_bulk(projects, value, _output),
241+
do: Projects.project_for(projects, value)
242+
243+
defp validate_bulk_team(nil, %{output: "json", from: from, to: to}) do
244+
if uuid?(from) and uuid?(to) do
245+
:ok
246+
else
247+
{:error,
248+
{:smells_bad, "JSON output requires --team or an active profile for bulk issue move"}}
249+
end
250+
end
251+
252+
defp validate_bulk_team(_team_key, _options), do: :ok
253+
254+
defp uuid?(<<_::8*8, ?-, _::4*8, ?-, _::4*8, ?-, _::4*8, ?-, _::12*8>>),
255+
do: true
256+
257+
defp uuid?(_value), do: false
258+
182259
defp guard_different_projects(%{id: id}, %{id: id}),
183260
do: {:error, {:smells_bad, "source and target are the same project"}}
184261

‎app/lib/linear_cli/cli/commands/issues/mutations.ex‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ defmodule LinearCli.CLI.Commands.Issues.Mutations do
186186
defp issue_unassign_by_filter(flags, options) do
187187
with {:ok, input} <-
188188
Filter.build_input(flags, options, [],
189-
project_resolution: :strict,
189+
project_resolution: project_resolution(options),
190190
include_labels: false,
191191
resolve_assignee: true,
192-
assigned_only: true
192+
assigned_only: true,
193+
non_interactive: json_output?(options)
193194
),
194195
{:ok, %{issues: issues, has_next_page: has_next_page}} <- Linear.issues_first_page(input) do
195196
unassign_filtered_issues(issues, has_next_page, flags, options)
@@ -211,6 +212,10 @@ defmodule LinearCli.CLI.Commands.Issues.Mutations do
211212
unassign_and_show(issues, options)
212213
end
213214

215+
defp unassign_filtered_issues(_issues, _flags, %{output: "json"}) do
216+
{:error, {:smells_bad, "JSON output requires --yes or --dry-run for issue unassign"}}
217+
end
218+
214219
defp unassign_filtered_issues(issues, _flags, options) do
215220
if Prompt.confirm_destructive?("Unassign #{length(issues)} issue(s)?") do
216221
unassign_and_show(issues, options)
@@ -301,6 +306,12 @@ defmodule LinearCli.CLI.Commands.Issues.Mutations do
301306
Enum.any?([:state, :status, :labels], &present_list?(Map.get(options, &1)))
302307
end
303308

309+
defp project_resolution(options) do
310+
if json_output?(options), do: :strict_non_interactive, else: :strict
311+
end
312+
313+
defp json_output?(options), do: Map.get(options, :output, "text") == "json"
314+
304315
defp filter_qualifier?(flags) do
305316
Map.get(flags, :no_mine, false) or
306317
Map.get(flags, :no_profile, false) or

0 commit comments

Comments
 (0)