From 2761c52cd65d3bc6bccf469ca1316d8fdd34ad33 Mon Sep 17 00:00:00 2001 From: "Tj (bougyman) Vanderpoel" Date: Mon, 23 Nov 2026 10:34:34 -0500 Subject: [PATCH 1/3] feat(project): search all teams on update, add list --updates, page past 100 `project update` only searched the active profile's team, so projects on other teams (leopard, Sequel Outbox) could not be found. It now searches the whole workspace unless --team is given. `project list --updates` shows each project's most recent status update with its date. Favorites are refetched with their update, since cached snapshots carry none. `project list` silently stopped at 100 projects. Interactive, workspace-wide listings now show 100 at a time and ask before fetching the next page. JSON, piped, --mine and --team listings fetch every project without prompting. Co-Authored-By: Claude Sonnet 5.5 --- Readme.adoc | 27 ++- app/lib/linear_cli/cli.ex | 3 +- app/lib/linear_cli/cli/commands/projects.ex | 96 +++++++-- app/lib/linear_cli/cli/display.ex | 21 +- app/lib/linear_cli/linear.ex | 1 + app/lib/linear_cli/linear/project.ex | 159 ++++++++++---- app/lib/linear_cli/linear/project_update.ex | 4 +- .../cli/favorites_commands_test.exs | 35 +++ app/test/linear_cli/cli_test.exs | 204 +++++++++++++++++- app/test/linear_cli/linear/project_test.exs | 44 ++++ documents/ash-domain-erd.adoc | 25 ++- 11 files changed, 547 insertions(+), 72 deletions(-) diff --git a/Readme.adoc b/Readme.adoc index 2bdbef5..1d67690 100644 --- a/Readme.adoc +++ b/Readme.adoc @@ -522,9 +522,10 @@ An explicit `--team`/`--project` on the command line always overrides the active profile. Profile and favorite mutations return one JSON result when `--output json` is -used. Project favorites and project updates require an exact project name or -ID, and require `--team` or an active profile when more than one team is -available. Text mode keeps its existing selection prompts. +used. Project favorites require an exact project name or ID, and require +`--team` or an active profile when more than one team is available. Project +updates require an exact project name or ID, and search every team unless +`--team` is given. Text mode keeps its existing selection prompts. Issue commands also accept a bare number (`lc issue develop 1234`) in place of a full team-prefixed identifier - it's resolved via the active @@ -558,6 +559,23 @@ $ lc project favorite Manhattan $ lc project list ---- +==== Show the latest project updates + +`project list --updates` also prints each project's most recent status update: +the date, the health (if set), and the full text. Projects with no update show +`(no updates)`. With `--output json`, each project has a `latest_update` field. + +[source,sh] +---- +$ lc project list --updates +$ lc project list --updates --team CRY +---- + +In a terminal, a workspace-wide `project list` shows 100 projects at a time and +asks `Show the next 100 projects?` before fetching more. Press Enter for yes. +With `--output json`, a pipe, `--mine`, `--team`, or an active favorites filter, +it fetches every project with no prompt. + ==== Post a project status update Not in Ruby's `linear-cli` - a status post on a project (Linear's own "Project @@ -568,6 +586,9 @@ Update" feature), not an edit to the project itself. $ lc project update Manhattan --body "Shipping ahead of schedule" --health onTrack ---- +The project is searched across every team. The active profile's team does not +narrow the search. Use `--team` to limit the search to one team. + === Wrapper scripts The `bin/` wrapper scripts (ported verbatim from `linear-cli`'s own `exe/scripts/`) diff --git a/app/lib/linear_cli/cli.ex b/app/lib/linear_cli/cli.ex index b45bbff..095ff63 100644 --- a/app/lib/linear_cli/cli.ex +++ b/app/lib/linear_cli/cli.ex @@ -633,7 +633,8 @@ defmodule LinearCli.CLI do about: "List projects", flags: [ mine: [short: "-m", long: "--mine", help: "Only show my projects"], - all: [long: "--all", help: "Ignore favorites (doesn't affect --mine/--team)"] + all: [long: "--all", help: "Ignore favorites (doesn't affect --mine/--team)"], + updates: [long: "--updates", help: "Show each project's most recent update"] ], options: [ team: [short: "-t", long: "--team", help: "Show projects for only this team"] diff --git a/app/lib/linear_cli/cli/commands/projects.ex b/app/lib/linear_cli/cli/commands/projects.ex index 946f855..5198f2c 100644 --- a/app/lib/linear_cli/cli/commands/projects.ex +++ b/app/lib/linear_cli/cli/commands/projects.ex @@ -9,10 +9,67 @@ defmodule LinearCli.CLI.Commands.Projects do @doc "Ported from commands/project/list.rb. Ruby's `--mine` defaults false." def project_list(%{flags: flags, options: options}) do - with {:ok, cached} <- Favorites.records("project", flags.all), - {:ok, projects} <- listed_projects(cached, flags, options) do - Display.show(projects, %{output: options.output}) - :ok + with {:ok, cached} <- Favorites.records("project", flags.all) do + if cached == nil and paged?(flags, options) do + show_pages(flags, options, nil) + else + with {:ok, projects} <- listed_projects(cached, flags, options), + {:ok, projects} <- refresh_updates(cached, projects, flags) do + show_projects(projects, flags, options) + end + end + end + end + + # Interactive, workspace-wide, unfiltered listings (the caller has already + # ruled out a favorites filter) are shown 100 at a time, asking before each + # further page. Everything else (JSON, pipes, --mine, --team) fetches every + # page. + defp paged?(flags, options) do + interactive?() and not Output.json?(options) and not flags.mine and + not is_binary(options.team) + end + + defp interactive? do + Application.get_env(:linear_cli, :interactive) || + (:prim_tty.isatty(:stdin) == true and :prim_tty.isatty(:stdout) == true) + end + + defp show_pages(flags, options, cursor) do + with {:ok, %{projects: projects, has_next_page: more?, end_cursor: next}} <- + Linear.projects_page(%{after: cursor, with_updates: flags.updates}) do + show_projects(projects, flags, options) + + if more? and Prompt.yes?("Show the next 100 projects?") do + show_pages(flags, options, next) + else + :ok + end + end + end + + defp show_projects(projects, flags, options) do + Display.show(projects, %{output: options.output, updates: flags.updates}) + :ok + end + + # Favorites are served from cached snapshots, which carry no project + # updates. When `--updates` is given, refetch each favorite with its latest + # update. Listings fetched live already include them. + defp refresh_updates(nil, projects, _flags), do: {:ok, projects} + defp refresh_updates(_cached, projects, %{updates: false}), do: {:ok, projects} + + defp refresh_updates(_cached, projects, _flags) do + projects + |> Enum.reduce_while({:ok, []}, fn project, {:ok, acc} -> + case Linear.find_project(project.id, %{with_updates: true}) do + {:ok, refreshed} -> {:cont, {:ok, [refreshed | acc]}} + {:error, reason} -> {:halt, {:error, reason}} + end + end) + |> case do + {:ok, refreshed} -> {:ok, Enum.reverse(refreshed)} + error -> error end end @@ -22,6 +79,9 @@ defmodule LinearCli.CLI.Commands.Projects do prompting if ambiguous. Team is resolved via `--team`, the active profile, or an interactive prompt. Once any project is favorited, `project list` defaults to showing just favorites (`--all` overrides). + `--updates` also shows each project's most recent status update. An + interactive, unscoped listing is paged 100 projects at a time, asking before + each next page; every other listing fetches all projects. """ def project_favorite(%{args: %{project: search}, options: options}) do with {:ok, team} <- resolve_team(options), @@ -65,13 +125,13 @@ defmodule LinearCli.CLI.Commands.Projects do New in this port - Ruby has no equivalent. Posts a status update (Linear's own "Project Update" feature - a journal-style status post, not an edit to the project's own fields) via the projectUpdateCreate - mutation. `PROJECT` is resolved against the active team's projects, - prompting if ambiguous. Team is resolved via `--team`, the active - profile, or an interactive prompt. + mutation. `PROJECT` is resolved against every project in the workspace, + prompting if ambiguous, unless `--team` narrows the search to one team. + The active profile's team is deliberately not used here: a project can + live on a different team than the one the profile defaults to. """ def project_update(%{args: %{project: search}, options: options}) do - with {:ok, team} <- resolve_team(options), - {:ok, projects} <- Linear.projects_by_team(team.id, %{search: search}), + with {:ok, projects} <- update_candidates(options), {:ok, project} <- resolve_project(projects, search, options), {:ok, update} <- Linear.post_project_update(project.id, options.body, %{health: options.health}) do @@ -82,6 +142,14 @@ defmodule LinearCli.CLI.Commands.Projects do end end + defp update_candidates(%{team: team_key} = options) when is_binary(team_key) do + with {:ok, team} <- resolve_team(options) do + Linear.projects_by_team(team.id) + end + end + + defp update_candidates(_options), do: Linear.workspace_projects() + defp resolve_team(options) do key = options.team || Profiles.default_team() @@ -145,12 +213,14 @@ defmodule LinearCli.CLI.Commands.Projects do Enum.any?([team.id, team.key], &(String.downcase(&1 || "") == String.downcase(key))) end - defp projects_for(_flags, %{team: team_key}) when is_binary(team_key) do + defp projects_for(flags, %{team: team_key}) when is_binary(team_key) do with {:ok, team} <- Linear.find_team(team_key) do - Linear.projects_by_team(team.id) + Linear.projects_by_team(team.id, %{with_updates: flags.updates}) end end - defp projects_for(%{mine: true}, _options), do: Linear.my_projects() - defp projects_for(_flags, _options), do: Linear.projects() + defp projects_for(%{mine: true} = flags, _options), + do: Linear.my_projects(%{with_updates: flags.updates}) + + defp projects_for(flags, _options), do: Linear.projects(%{with_updates: flags.updates}) end diff --git a/app/lib/linear_cli/cli/display.ex b/app/lib/linear_cli/cli/display.ex index a8cc1b0..c0ba9df 100644 --- a/app/lib/linear_cli/cli/display.ex +++ b/app/lib/linear_cli/cli/display.ex @@ -60,8 +60,14 @@ defmodule LinearCli.CLI.Display do "#{String.pad_trailing(team.key || "", 6)} #{team.name}" end - defp format(%Project{} = project, _opts) do - "#{String.pad_trailing(project.name || "", 12)} #{project.url}" + defp format(%Project{} = project, opts) do + line = "#{String.pad_trailing(project.name || "", 12)} #{project.url}" + + if Map.get(opts, :updates, false) do + line <> "\n" <> latest_update_block(project.latest_update) + else + line + end end defp format(%ProjectUpdate{} = update, _opts) do @@ -368,6 +374,17 @@ defmodule LinearCli.CLI.Display do Enum.join([header | rows], "\n") end + defp latest_update_block(nil), do: " (no updates)\n" + + defp latest_update_block(%ProjectUpdate{} = update) do + health = if update.health, do: " (#{update.health})", else: "" + body = update.body |> to_string() |> String.trim() |> String.replace("\n", "\n ") + " #{update_date(update.created_at)}#{health}\n #{body}\n" + end + + defp update_date(<>), do: date + defp update_date(_other), do: "(unknown date)" + defp to_plain(list) when is_list(list), do: Enum.map(list, &to_plain/1) defp to_plain(%_struct{} = record) do diff --git a/app/lib/linear_cli/linear.ex b/app/lib/linear_cli/linear.ex index 62a74ec..e29efc2 100644 --- a/app/lib/linear_cli/linear.ex +++ b/app/lib/linear_cli/linear.ex @@ -27,6 +27,7 @@ defmodule LinearCli.Linear do resource LinearCli.Linear.Project do define :projects, action: :all define :find_project, action: :find, args: [:id], get?: true + define :projects_page, action: :page define :workspace_projects, action: :all_for_lookup define :my_projects, action: :mine define :projects_by_team, action: :by_team, args: [:team_id] diff --git a/app/lib/linear_cli/linear/project.ex b/app/lib/linear_cli/linear/project.ex index 5efa73d..bfd9b9d 100644 --- a/app/lib/linear_cli/linear/project.ex +++ b/app/lib/linear_cli/linear/project.ex @@ -8,25 +8,38 @@ defmodule LinearCli.Linear.Project do actions do read :find do argument :id, :string, allow_nil?: false + argument :with_updates, :boolean, default: false get? true manual LinearCli.Linear.Project.Read.Find end + # Every project in the workspace, following all pages. read :all do + argument :with_updates, :boolean, default: false manual LinearCli.Linear.Project.Read.All end + # One page of 100 plus its cursor, for callers that let the user decide + # whether to fetch the next page. + action :page, :map do + argument :after, :string, allow_nil?: true + argument :with_updates, :boolean, default: false + run LinearCli.Linear.Project.Actions.Page + end + read :all_for_lookup do manual LinearCli.Linear.Project.Read.AllForLookup end read :mine do + argument :with_updates, :boolean, default: false manual LinearCli.Linear.Project.Read.Mine end read :by_team do argument :team_id, :string, allow_nil?: false argument :search, :string + argument :with_updates, :boolean, default: false manual LinearCli.Linear.Project.Read.ByTeam end @@ -56,6 +69,8 @@ defmodule LinearCli.Linear.Project do attribute :description, :string, public?: true attribute :url, :string, public?: true attribute :teams, {:array, :term}, public?: true, default: [] + # Only populated when fetched with `list_fields(true)`; nil otherwise. + attribute :latest_update, :term, public?: true end @base_fields "id name content slugId description url createdAt updatedAt" @@ -63,6 +78,20 @@ defmodule LinearCli.Linear.Project do @doc "GraphQL field selection for a project's own fields (Ruby: Project::Base)." def base_fields, do: @base_fields + @doc """ + Field selection for project listings. With `with_updates?`, also selects + each project's most recent project update (Linear orders by `createdAt`, + newest first, by default). + """ + def list_fields(true), do: "#{@base_fields} #{latest_update_selection()}" + + def list_fields(_with_updates?), do: @base_fields + + @doc "GraphQL selection of a project's newest update. See `list_fields/1`." + def latest_update_selection do + "projectUpdates(first: 1) { nodes { #{LinearCli.Linear.ProjectUpdate.base_fields()} } }" + end + @doc "GraphQL field selection including the project's teams. New in Phase 7." def fields_with_teams do "#{@base_fields} teams { nodes { #{LinearCli.Linear.Team.base_fields()} } }" @@ -77,10 +106,16 @@ defmodule LinearCli.Linear.Project do slug_id: map["slugId"], description: map["description"], url: map["url"], - teams: Enum.map(get_in(map, ["teams", "nodes"]) || [], &LinearCli.Linear.Team.from_map/1) + teams: Enum.map(get_in(map, ["teams", "nodes"]) || [], &LinearCli.Linear.Team.from_map/1), + latest_update: latest_update(map) ) end + defp latest_update(%{"projectUpdates" => %{"nodes" => [update | _]}}), + do: LinearCli.Linear.ProjectUpdate.from_map(update) + + defp latest_update(_map), do: nil + @doc """ Ported from Ruby's `Project#slug`: the URL's basename with the trailing `-slugId` suffix stripped (first occurrence only, matching Ruby's `sub`). @@ -165,7 +200,12 @@ defmodule LinearCli.Linear.Project.Read.Find do alias LinearCli.Linear.Project def read(query, _ecto_query, _opts, _context) do - document = "query($id: String!) { project(id: $id) { #{Project.fields_with_teams()} } }" + fields = + if Map.get(query.arguments, :with_updates, false), + do: "#{Project.fields_with_teams()} #{Project.latest_update_selection()}", + else: Project.fields_with_teams() + + document = "query($id: String!) { project(id: $id) { #{fields} } }" case Api.call(document, %{"id" => query.arguments.id}) do {:ok, %{"project" => nil}} -> {:ok, []} @@ -183,23 +223,61 @@ defmodule LinearCli.Linear.Project.Read.All do alias LinearCli.Linear.{Paginate, Project} - @document """ - query($first: Int!, $after: String) { - projects(first: $first, after: $after) { - edges { node { #{Project.base_fields()} } cursor } - pageInfo { hasNextPage endCursor } - } - } - """ - - def read(_query, _ecto_query, _opts, _context) do - Paginate.all( - @document, + def read(query, _ecto_query, _opts, _context) do + Paginate.all_pages( + document(Map.get(query.arguments, :with_updates, false)), "projects", fn after_cursor -> %{"first" => 50, "after" => after_cursor} end, &Project.from_map/1 ) end + + @doc false + def document(with_updates?) do + """ + query($first: Int!, $after: String) { + projects(first: $first, after: $after) { + edges { node { #{Project.list_fields(with_updates?)} } cursor } + pageInfo { hasNextPage endCursor } + } + } + """ + end +end + +defmodule LinearCli.Linear.Project.Actions.Page do + @moduledoc false + use Ash.Resource.Actions.Implementation + + alias LinearCli.Api + alias LinearCli.Linear.Project + + @page_size 100 + + @impl true + def run(input, _opts, _context) do + args = input.arguments + document = Project.Read.All.document(Map.get(args, :with_updates, false)) + + case Api.call(document, %{"first" => @page_size, "after" => Map.get(args, :after)}) do + {:ok, %{"projects" => %{"edges" => edges, "pageInfo" => page_info}}} -> + {:ok, + %{ + projects: Enum.map(edges, &Project.from_map(&1["node"])), + has_next_page: page_info["hasNextPage"] == true, + end_cursor: page_info["endCursor"] + }} + + {:ok, other} -> + {:error, {:unexpected_response, other}} + + {:error, {:http_error, status, _body}} -> + {:error, {:http_error, status}} + + {:error, reason} -> + {:error, reason} + end + end end defmodule LinearCli.Linear.Project.Read.AllForLookup do @@ -237,17 +315,6 @@ defmodule LinearCli.Linear.Project.Read.ByTeam do @uuid ~r/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i @slugged_reference ~r/^(.*)-([[:alnum:]]{12})$/ - @document """ - query($teamId: String!, $after: String) { - team(id: $teamId) { - projects(first: 100, after: $after) { - nodes { #{Project.base_fields()} } - pageInfo { hasNextPage endCursor } - } - } - } - """ - @search_document """ query($teamId: String!, $filter: ProjectFilter!) { team(id: $teamId) { @@ -260,39 +327,53 @@ defmodule LinearCli.Linear.Project.Read.ByTeam do def read(query, _ecto_query, _opts, _context) do team_id = query.arguments.team_id + with_updates? = Map.get(query.arguments, :with_updates, false) case Map.get(query.arguments, :search) do search when is_binary(search) and search not in ["", "-"] -> - search(team_id, search) + search(team_id, search, with_updates?) _ -> - all(team_id) + all(team_id, with_updates?) end end - defp search(team_id, search) do + defp search(team_id, search, with_updates?) do variables = %{"teamId" => team_id, "filter" => project_filter(search)} with {:ok, projects} <- fetch(@search_document, variables) do - if projects == [], do: all(team_id), else: {:ok, projects} + if projects == [], do: all(team_id, with_updates?), else: {:ok, projects} end end - defp all(team_id), do: page(team_id, nil, []) + defp all(team_id, with_updates?), do: page(team_id, with_updates?, nil, []) - defp page(team_id, after_cursor, acc) do + defp document(with_updates?) do + """ + query($teamId: String!, $after: String) { + team(id: $teamId) { + projects(first: 100, after: $after) { + nodes { #{Project.list_fields(with_updates?)} } + pageInfo { hasNextPage endCursor } + } + } + } + """ + end + + defp page(team_id, with_updates?, after_cursor, acc) do variables = if after_cursor, do: %{"teamId" => team_id, "after" => after_cursor}, else: %{"teamId" => team_id} - case Api.call(@document, variables) do + case Api.call(document(with_updates?), variables) do {:ok, %{"team" => %{"projects" => projects}}} -> acc = acc ++ Enum.map(projects["nodes"] || [], &Project.from_map/1) case projects["pageInfo"] do %{"hasNextPage" => true, "endCursor" => cursor} when is_binary(cursor) -> - page(team_id, cursor, acc) + page(team_id, with_updates?, cursor, acc) _ -> {:ok, acc} @@ -436,10 +517,12 @@ defmodule LinearCli.Linear.Project.Read.Mine do # Ruby: Project.mine = User.me.teams.flat_map(&:projects) - fully serial, # one request per team. Each team's projects are an independent GraphQL # call, so fan them out concurrently instead. - def read(_query, _ecto_query, _opts, _context) do + def read(query, _ecto_query, _opts, _context) do + with_updates? = Map.get(query.arguments, :with_updates, false) + with {:ok, user} <- Ash.read_one(Ash.Query.for_read(User, :me)) do user.teams - |> Task.async_stream(&fetch_team_projects/1, timeout: 30_000) + |> Task.async_stream(&fetch_team_projects(&1, with_updates?), timeout: 30_000) |> Enum.reduce_while({:ok, []}, fn {:ok, {:ok, projects}}, {:ok, acc} -> {:cont, {:ok, acc ++ projects}} {:ok, {:error, reason}}, {:ok, _acc} -> {:halt, {:error, reason}} @@ -450,7 +533,9 @@ defmodule LinearCli.Linear.Project.Read.Mine do # Ash.read/1 (not read!/1) - a raise inside an async task would surface as # an unhelpful {:exit, reason} to the caller instead of a clean error tuple. - defp fetch_team_projects(team) do - Ash.read(Ash.Query.for_read(Project, :by_team, %{team_id: team.id})) + defp fetch_team_projects(team, with_updates?) do + Ash.read( + Ash.Query.for_read(Project, :by_team, %{team_id: team.id, with_updates: with_updates?}) + ) end end diff --git a/app/lib/linear_cli/linear/project_update.ex b/app/lib/linear_cli/linear/project_update.ex index 10d431a..629515a 100644 --- a/app/lib/linear_cli/linear/project_update.ex +++ b/app/lib/linear_cli/linear/project_update.ex @@ -22,6 +22,7 @@ defmodule LinearCli.Linear.ProjectUpdate do attribute :body, :string, public?: true attribute :health, :string, public?: true attribute :url, :string, public?: true + attribute :created_at, :string, public?: true end @doc "GraphQL field selection for a project update's own fields." @@ -35,7 +36,8 @@ defmodule LinearCli.Linear.ProjectUpdate do id: map["id"], body: map["body"], health: map["health"], - url: map["url"] + url: map["url"], + created_at: map["createdAt"] ) end end diff --git a/app/test/linear_cli/cli/favorites_commands_test.exs b/app/test/linear_cli/cli/favorites_commands_test.exs index f4469f1..69033f6 100644 --- a/app/test/linear_cli/cli/favorites_commands_test.exs +++ b/app/test/linear_cli/cli/favorites_commands_test.exs @@ -386,6 +386,41 @@ defmodule LinearCli.CLI.FavoritesCommandsTest do refute output =~ "Platform Cleanup" end + test "--updates refetches favorites with their latest update" do + LinearCli.Favorites.add("project", "p1") + {:ok, _} = LinearCli.Favorites.records("project", false) + + Req.Test.stub(LinearCli.Api, fn conn -> + {:ok, body, conn} = Plug.Conn.read_body(conn) + query = Jason.decode!(body)["query"] + + assert query =~ "project(id: $id)" + assert query =~ "projectUpdates(first: 1)" + + Req.Test.json(conn, %{ + "data" => %{ + "project" => %{ + "id" => "p1", + "name" => "Manhattan", + "slugId" => "abc", + "url" => "https://linear.app/x/project/manhattan-abc", + "teams" => %{"nodes" => []}, + "projectUpdates" => %{ + "nodes" => [ + %{"id" => "pu1", "body" => "On pace.", "createdAt" => "2026-11-18T09:00:00Z"} + ] + } + } + } + }) + end) + + output = capture_io(fn -> LinearCli.CLI.main(["project", "list", "--updates"]) end) + assert output =~ "Manhattan" + assert output =~ "2026-11-18" + assert output =~ "On pace." + end + test "--all overrides the favorites filter" do LinearCli.Favorites.add("project", "p1") diff --git a/app/test/linear_cli/cli_test.exs b/app/test/linear_cli/cli_test.exs index cd6a0e5..a987dbb 100644 --- a/app/test/linear_cli/cli_test.exs +++ b/app/test/linear_cli/cli_test.exs @@ -140,20 +140,22 @@ defmodule LinearCli.CLITest do } }) - String.contains?(query, "team(id: $teamId)") -> + String.contains?(query, "projects(first: $first") -> Req.Test.json(conn, %{ "data" => %{ - "team" => %{ - "projects" => %{ - "nodes" => [ - %{ + "projects" => %{ + "edges" => [ + %{ + "node" => %{ "id" => "p1", "name" => "Manhattan", "slugId" => "abc", "url" => "https://linear.app/x/project/manhattan-abc" - } - ] - } + }, + "cursor" => "c1" + } + ], + "pageInfo" => %{"hasNextPage" => false} } } }) @@ -200,6 +202,192 @@ defmodule LinearCli.CLITest do %{"projectId" => "p1", "body" => "Doing great", "health" => "onTrack"}} end + test "project update finds projects on any team when --team is not given" do + # The active profile's team must not narrow the search: a project can live + # on a different team than the profile's default. + {:ok, _} = LinearCli.Profiles.create("other-team", team: "OPS") + :ok = LinearCli.Profiles.activate("other-team") + + Req.Test.stub(LinearCli.Api, fn conn -> + {:ok, body, conn} = Plug.Conn.read_body(conn) + query = Jason.decode!(body)["query"] + + refute query =~ "team(id: $teamId)" + + if query =~ "projectUpdateCreate" do + Req.Test.json(conn, %{ + "data" => %{ + "projectUpdateCreate" => %{ + "projectUpdate" => %{"id" => "pu1", "url" => "https://linear.app/x/update/pu1"} + } + } + }) + else + Req.Test.json(conn, %{ + "data" => %{ + "projects" => %{ + "edges" => [ + %{ + "node" => %{ + "id" => "p9", + "name" => "leopard", + "slugId" => "f14", + "url" => "https://linear.app/x/project/leopard-f14" + }, + "cursor" => "c1" + } + ], + "pageInfo" => %{"hasNextPage" => false} + } + } + }) + end + end) + + output = + capture_io(fn -> + assert :ok = + LinearCli.CLI.main(["project", "update", "leopard", "--body", "Shipping"]) + end) + + assert output =~ "https://linear.app/x/update/pu1" + end + + test "project list --updates shows each project's latest update and its date" do + Req.Test.stub(LinearCli.Api, fn conn -> + {:ok, body, conn} = Plug.Conn.read_body(conn) + query = Jason.decode!(body)["query"] + + assert query =~ "projectUpdates(first: 1)" + + Req.Test.json(conn, %{ + "data" => %{ + "projects" => %{ + "edges" => [ + %{ + "node" => %{ + "id" => "p1", + "name" => "Manhattan", + "slugId" => "abc", + "url" => "https://linear.app/x/project/manhattan-abc", + "projectUpdates" => %{ + "nodes" => [ + %{ + "id" => "pu1", + "body" => "Phase one done.\nPhase two next.", + "health" => "onTrack", + "url" => "https://linear.app/x/update/pu1", + "createdAt" => "2026-11-14T15:30:00.000Z" + } + ] + } + }, + "cursor" => "c1" + }, + %{ + "node" => %{ + "id" => "p2", + "name" => "Quiet", + "slugId" => "def", + "url" => "https://linear.app/x/project/quiet-def", + "projectUpdates" => %{"nodes" => []} + }, + "cursor" => "c2" + } + ], + "pageInfo" => %{"hasNextPage" => false} + } + } + }) + end) + + output = capture_io(fn -> LinearCli.CLI.main(["project", "list", "--updates", "--all"]) end) + + assert output =~ "2026-11-14 (onTrack)" + assert output =~ "Phase one done." + assert output =~ " Phase two next." + assert output =~ "(no updates)" + end + + describe "project list paging" do + setup do + test_pid = self() + + Req.Test.stub(LinearCli.Api, fn conn -> + {:ok, body, conn} = Plug.Conn.read_body(conn) + after_cursor = Jason.decode!(body)["variables"]["after"] + send(test_pid, {:requested, after_cursor}) + + {node, page_info} = + case after_cursor do + nil -> + {%{"id" => "p1", "name" => "FirstPage", "slugId" => "a", "url" => "u1"}, + %{"hasNextPage" => true, "endCursor" => "page-1"}} + + "page-1" -> + {%{"id" => "p2", "name" => "SecondPage", "slugId" => "b", "url" => "u2"}, + %{"hasNextPage" => false, "endCursor" => "page-2"}} + end + + Req.Test.json(conn, %{ + "data" => %{ + "projects" => %{ + "edges" => [%{"node" => node, "cursor" => node["id"]}], + "pageInfo" => page_info + } + } + }) + end) + + Application.put_env(:linear_cli, :interactive, true) + on_exit(fn -> Application.delete_env(:linear_cli, :interactive) end) + end + + test "interactive listing asks before fetching the next page and continues on yes" do + output = + capture_io([input: "y\n"], fn -> + assert :ok = LinearCli.CLI.main(["project", "list", "--all"]) + end) + + assert output =~ "FirstPage" + assert output =~ "Show the next 100 projects?" + assert output =~ "SecondPage" + assert_received {:requested, "page-1"} + end + + test "interactive listing stops after the first page on no" do + output = + capture_io([input: "n\n"], fn -> + assert :ok = LinearCli.CLI.main(["project", "list", "--all"]) + end) + + assert output =~ "FirstPage" + refute output =~ "SecondPage" + refute_received {:requested, "page-1"} + end + + test "JSON output fetches every page without prompting" do + output = + capture_io(fn -> + assert :ok = LinearCli.CLI.main(["project", "list", "--all", "-o", "json"]) + end) + + refute output =~ "Show the next" + assert output =~ "FirstPage" + assert output =~ "SecondPage" + end + end + + test "project list without --updates does not request project updates" do + Req.Test.stub(LinearCli.Api, fn conn -> + {:ok, body, conn} = Plug.Conn.read_body(conn) + refute Jason.decode!(body)["query"] =~ "projectUpdates" + respond(conn, Jason.decode!(body)["query"]) + end) + + assert capture_io(fn -> LinearCli.CLI.main(["project", "list", "--all"]) end) =~ "Manhattan" + end + test "issue list prints a one-line summary per issue" do assert capture_io(fn -> LinearCli.CLI.main(["issue", "list"]) end) =~ "CRY-1" end diff --git a/app/test/linear_cli/linear/project_test.exs b/app/test/linear_cli/linear/project_test.exs index c7b494b..447edae 100644 --- a/app/test/linear_cli/linear/project_test.exs +++ b/app/test/linear_cli/linear/project_test.exs @@ -70,6 +70,50 @@ defmodule LinearCli.Linear.ProjectTest do assert {:ok, [%Linear.Project{id: "p1", name: "Manhattan"}]} = Linear.projects() end + defp stub_two_project_pages do + Req.Test.stub(LinearCli.Api, fn conn -> + {:ok, body, conn} = Plug.Conn.read_body(conn) + %{"variables" => %{"after" => after_cursor}} = Jason.decode!(body) + + {project, page_info} = + case after_cursor do + nil -> + {%{"id" => "p1", "name" => "First"}, + %{"hasNextPage" => true, "endCursor" => "page-1"}} + + "page-1" -> + {%{"id" => "p2", "name" => "Later"}, + %{"hasNextPage" => false, "endCursor" => "page-2"}} + end + + Req.Test.json(conn, %{ + "data" => %{ + "projects" => %{ + "edges" => [%{"node" => project, "cursor" => project["id"]}], + "pageInfo" => page_info + } + } + }) + end) + end + + test "projects/0 follows every page instead of stopping at 100" do + stub_two_project_pages() + + assert {:ok, projects} = Linear.projects() + assert Enum.map(projects, & &1.id) == ["p1", "p2"] + end + + test "projects_page/1 returns one page with its cursor and whether more remain" do + stub_two_project_pages() + + assert {:ok, %{projects: [%{id: "p1"}], has_next_page: true, end_cursor: "page-1"}} = + Linear.projects_page(%{}) + + assert {:ok, %{projects: [%{id: "p2"}], has_next_page: false}} = + Linear.projects_page(%{after: "page-1"}) + end + test "workspace_projects/0 follows every project page for lookup" do Req.Test.stub(LinearCli.Api, fn conn -> {:ok, body, conn} = Plug.Conn.read_body(conn) diff --git a/documents/ash-domain-erd.adoc b/documents/ash-domain-erd.adoc index b8c3a4e..c841a8a 100644 --- a/documents/ash-domain-erd.adoc +++ b/documents/ash-domain-erd.adoc @@ -62,6 +62,7 @@ erDiagram string description string url Team[] teams + ProjectUpdate latest_update } Issue { string id PK @@ -116,6 +117,7 @@ erDiagram string body string health string url + string created_at } Issue }o--o| User : "assignee [nested]" @@ -150,7 +152,7 @@ Nine resources are registered in `LinearCli.Linear` | `LinearCli.Linear.User` | `id` (`:string`) -| `name`, `display_name`, `email`, `teams` (`{:array, :term}`) +| `name`, `display_name`, `email`, `teams` (`{:array, :term}`), `latest_update` (`:term`, a `ProjectUpdate` or `nil`; only set when listed with `with_updates: true`) | `LinearCli.Linear.Team` | `id` (`:string`) @@ -178,7 +180,7 @@ Nine resources are registered in `LinearCli.Linear` | `LinearCli.Linear.ProjectUpdate` | `id` (`:string`) -| `body`, `health`, `url` +| `body`, `health`, `url`, `created_at` (`:string`, ISO-8601) | `LinearCli.Linear.IssueRelation` | `id` (`:string`) @@ -347,14 +349,14 @@ manual-implementation module, and the Linear GraphQL operation it calls. | `:all` | read | `Linear.Project.Read.All` -| `projects(first:, after:)` — paginated with `Project.base_fields/0` +| `projects(first:, after:)` — follows every page (`Paginate.all_pages/4`, no 100-project cap) with `Project.list_fields/1`. The optional `with_updates` argument also selects each project's newest `projectUpdates(first: 1)` | `Project` | `find_project` | `:find` | read (`get?: true`) | `Linear.Project.Read.Find` -| `project(id: $id)` with `Project.fields_with_teams/0` — exact lookup for saving or hydrating favorite snapshots +| `project(id: $id)` with `Project.fields_with_teams/0` — exact lookup for saving or hydrating favorite snapshots. The optional `with_updates` argument also selects the newest update (`Project.latest_update_selection/0`) | `Project` | `workspace_projects` @@ -368,14 +370,14 @@ manual-implementation module, and the Linear GraphQL operation it calls. | `:mine` | read | `Linear.Project.Read.Mine` -| `team(id:) { projects }` per user team, fanned out concurrently +| `team(id:) { projects }` per user team, fanned out concurrently. Accepts `with_updates` and passes it to `by_team` | `Project` | `projects_by_team` | `:by_team` | read | `Linear.Project.Read.ByTeam` -| `team(id: $teamId) { projects(first:, after:, filter:) { ... } }` — paginates the selected team's projects, or applies a server-side name/slug/id filter when `search` is supplied +| `team(id: $teamId) { projects(first:, after:, filter:) { ... } }` — paginates the selected team's projects, or applies a server-side name/slug/id filter when `search` is supplied. The optional `with_updates` argument also selects each project's newest update (not applied to the `search` filter query) | `Project` | `create_project` @@ -405,6 +407,14 @@ manual-implementation module, and the Linear GraphQL operation it calls. unassigned issues for filter-only batch operations; or `issue(id:)` per id (full, fanned concurrently). +| `Project` +| `projects_page` +| `:page` +| generic action (`:map`) +| `Linear.Project.Actions.Page` +| `projects(first: 100, after:)` — one page. Arguments: `after` (cursor), `with_updates`. Returns + `%{projects:, has_next_page:, end_cursor:}`. The action never follows the cursor. + | `Issue` | `issues_first_page` | `:list_first_page` @@ -613,7 +623,8 @@ Used by: * `Team.Read.All` — `teams(first:, after:)` paginated query * `Team.Read.AllForLookup` — complete workspace team lookup -* `Project.Read.All` — `projects(first:, after:)` paginated query +* `Project.Read.All` — every project, via `all_pages/4` +* `Project.Actions.Page` — one page of projects with its end cursor (it calls the API directly rather than `Paginate`) * `Project.Read.AllForLookup` — complete workspace project lookup * `Issue.Read.List` — `issues(filter:, first:, after:)` paginated list (the find-by-ids path fans out individual `issue(id:)` calls instead) From 62002911a6c3aaec5b7560bf32bf7a13acade14c Mon Sep 17 00:00:00 2001 From: bougyman's bot Date: Mon, 23 Nov 2026 12:18:31 -0500 Subject: [PATCH 2/3] fix(project): disambiguate status update targets (EXT-82) Move latest_update to the Project row in the Ash domain ERD. --- Readme.adoc | 12 ++- app/lib/linear_cli/cli/commands/projects.ex | 50 +++++++-- app/test/linear_cli/cli_test.exs | 107 ++++++++++++++++++++ documents/ash-domain-erd.adoc | 4 +- 4 files changed, 158 insertions(+), 15 deletions(-) diff --git a/Readme.adoc b/Readme.adoc index 1d67690..ca32eb6 100644 --- a/Readme.adoc +++ b/Readme.adoc @@ -523,9 +523,8 @@ active profile. Profile and favorite mutations return one JSON result when `--output json` is used. Project favorites require an exact project name or ID, and require -`--team` or an active profile when more than one team is available. Project -updates require an exact project name or ID, and search every team unless -`--team` is given. Text mode keeps its existing selection prompts. +`--team` or an active profile when more than one team is available. +Project updates require a unique exact match in JSON mode. Issue commands also accept a bare number (`lc issue develop 1234`) in place of a full team-prefixed identifier - it's resolved via the active @@ -586,8 +585,11 @@ Update" feature), not an edit to the project itself. $ lc project update Manhattan --body "Shipping ahead of schedule" --health onTrack ---- -The project is searched across every team. The active profile's team does not -narrow the search. Use `--team` to limit the search to one team. +The search covers the whole workspace, regardless of the active profile. +Use `--team` to limit the search to one team, or pass the project ID. +If multiple projects match exactly, text mode lists their names and IDs. +Select a project by number or press Enter to cancel. +JSON mode returns an error for an ambiguous match before it posts an update. === Wrapper scripts diff --git a/app/lib/linear_cli/cli/commands/projects.ex b/app/lib/linear_cli/cli/commands/projects.ex index 5198f2c..e671107 100644 --- a/app/lib/linear_cli/cli/commands/projects.ex +++ b/app/lib/linear_cli/cli/commands/projects.ex @@ -6,6 +6,7 @@ defmodule LinearCli.CLI.Commands.Projects do alias LinearCli.CLI.{Display, Output, Projects, Prompt, WhatFor} alias LinearCli.{Favorites, Linear, Profiles} + alias LinearCli.Linear.Project @doc "Ported from commands/project/list.rb. Ruby's `--mine` defaults false." def project_list(%{flags: flags, options: options}) do @@ -122,17 +123,13 @@ defmodule LinearCli.CLI.Commands.Projects do end @doc """ - New in this port - Ruby has no equivalent. Posts a status update - (Linear's own "Project Update" feature - a journal-style status post, - not an edit to the project's own fields) via the projectUpdateCreate - mutation. `PROJECT` is resolved against every project in the workspace, - prompting if ambiguous, unless `--team` narrows the search to one team. - The active profile's team is deliberately not used here: a project can - live on a different team than the one the profile defaults to. + Posts a status update to a Linear project. The search covers the whole + workspace unless `--team` limits it. If multiple projects match exactly, + text mode prompts for a choice and JSON mode returns an error. """ def project_update(%{args: %{project: search}, options: options}) do with {:ok, projects} <- update_candidates(options), - {:ok, project} <- resolve_project(projects, search, options), + {:ok, project} <- resolve_update_project(projects, search, options), {:ok, update} <- Linear.post_project_update(project.id, options.body, %{health: options.health}) do Display.show(update, %{output: options.output}) @@ -150,6 +147,43 @@ defmodule LinearCli.CLI.Commands.Projects do defp update_candidates(_options), do: Linear.workspace_projects() + defp resolve_update_project(projects, search, options) do + matches = + case Enum.filter(projects, &Project.matches_attributes?(&1, search, [:id, :url])) do + [] -> Enum.filter(projects, &(Project.match_score?(&1, search) == 100)) + references -> references + end + + case Enum.uniq_by(matches, & &1.id) do + [] -> resolve_project(projects, search, options) + [project] -> {:ok, project} + ambiguous -> disambiguate_update_project(ambiguous, search, options) + end + end + + defp disambiguate_update_project(projects, search, options) do + if Output.json?(options) do + {:error, + {:smells_bad, + "Ambiguous project match for #{inspect(search)}. Use a project ID or --team."}} + else + projects + |> Enum.with_index(1) + |> Enum.each(fn {project, number} -> + Prompt.say("#{number}. #{project.name} (#{project.id})") + end) + + with answer when is_binary(answer) <- + Prompt.ask("Choose a project by number (Enter to cancel)"), + {number, ""} when number > 0 <- Integer.parse(answer), + {:ok, project} <- Enum.fetch(projects, number - 1) do + {:ok, project} + else + _ -> {:error, {:smells_bad, "No project selected."}} + end + end + end + defp resolve_team(options) do key = options.team || Profiles.default_team() diff --git a/app/test/linear_cli/cli_test.exs b/app/test/linear_cli/cli_test.exs index a987dbb..baffce3 100644 --- a/app/test/linear_cli/cli_test.exs +++ b/app/test/linear_cli/cli_test.exs @@ -253,6 +253,113 @@ defmodule LinearCli.CLITest do assert output =~ "https://linear.app/x/update/pu1" end + describe "project update with duplicate project names" do + setup do + test_pid = self() + + first = %{ + "id" => "p1", + "name" => "Website", + "slugId" => "first", + "url" => "https://linear.app/x/project/website-first" + } + + second = %{ + "id" => "p2", + "name" => "Website", + "slugId" => "second", + "url" => "https://linear.app/x/project/website-second" + } + + Req.Test.stub(LinearCli.Api, fn conn -> + {:ok, body, conn} = Plug.Conn.read_body(conn) + %{"query" => query, "variables" => variables} = Jason.decode!(body) + + data = + cond do + query =~ "projectUpdateCreate" -> + send(test_pid, {:posted_to, variables["input"]["projectId"]}) + %{"projectUpdateCreate" => %{"projectUpdate" => %{"id" => "pu1"}}} + + query =~ "team(id: $id)" -> + assert variables == %{"id" => "OPS"} + %{"team" => %{"id" => "t2", "key" => "OPS", "name" => "Ops"}} + + query =~ "team(id: $teamId)" -> + assert variables["teamId"] == "t2" + %{"team" => %{"projects" => %{"nodes" => [second]}}} + + query =~ "projects(first: $first" -> + %{ + "projects" => %{ + "edges" => Enum.map([first, second], &%{"node" => &1}), + "pageInfo" => %{"hasNextPage" => false} + } + } + end + + Req.Test.json(conn, %{"data" => data}) + end) + + %{halt: fn code -> send(test_pid, {:halted, code}) end} + end + + test "text mode posts only to the selected project" do + output = + capture_io([input: "2\n"], fn -> + assert :ok = + LinearCli.CLI.main(["project", "update", "Website", "--body", "Shipping"]) + end) + + assert output =~ "1. Website (p1)" + assert output =~ "2. Website (p2)" + assert_received {:posted_to, "p2"} + refute_received {:posted_to, _} + end + + test "text mode sends no update when the user cancels", %{halt: halt} do + error = + capture_io(:stderr, fn -> + capture_io([input: "\n"], fn -> + LinearCli.CLI.main(["project", "update", "Website", "--body", "Shipping"], halt) + end) + end) + + assert error =~ "No project selected" + assert_received {:halted, 22} + refute_received {:posted_to, _} + end + + test "JSON mode rejects ambiguity before posting or prompting", %{halt: halt} do + error = + capture_io(:stderr, fn -> + assert capture_io(fn -> + LinearCli.CLI.main( + ["project", "update", "Website", "--body", "Shipping", "--output", "json"], + halt + ) + end) == "" + end) + + assert error =~ ~s(Ambiguous project match for "Website". Use a project ID or --team.) + assert_received {:halted, 22} + refute_received {:posted_to, _} + end + + test "an explicit ID or team scope selects the intended project in both modes" do + for output <- ["text", "json"], + {search, scope} <- [{"p2", []}, {"Website", ["--team", "OPS"]}] do + args = ["project", "update", search, "--body", "Shipping", "--output", output] ++ scope + + captured = capture_io(fn -> assert :ok = LinearCli.CLI.main(args) end) + + refute captured =~ "Choose a project by number" + assert_received {:posted_to, "p2"} + refute_received {:posted_to, _} + end + end + end + test "project list --updates shows each project's latest update and its date" do Req.Test.stub(LinearCli.Api, fn conn -> {:ok, body, conn} = Plug.Conn.read_body(conn) diff --git a/documents/ash-domain-erd.adoc b/documents/ash-domain-erd.adoc index c841a8a..0dae6e4 100644 --- a/documents/ash-domain-erd.adoc +++ b/documents/ash-domain-erd.adoc @@ -152,7 +152,7 @@ Nine resources are registered in `LinearCli.Linear` | `LinearCli.Linear.User` | `id` (`:string`) -| `name`, `display_name`, `email`, `teams` (`{:array, :term}`), `latest_update` (`:term`, a `ProjectUpdate` or `nil`; only set when listed with `with_updates: true`) +| `name`, `display_name`, `email`, `teams` (`{:array, :term}`) | `LinearCli.Linear.Team` | `id` (`:string`) @@ -160,7 +160,7 @@ Nine resources are registered in `LinearCli.Linear` | `LinearCli.Linear.Project` | `id` (`:string`) -| `name`, `content`, `slug_id`, `description`, `url`, `teams` (`{:array, :term}`) +| `name`, `content`, `slug_id`, `description`, `url`, `teams` (`{:array, :term}`), `latest_update` (`:term`, a `ProjectUpdate` or `nil`, loaded with `with_updates: true`) | `LinearCli.Linear.Issue` | `id` (`:string`) From aacd8f04ccaf579e32db7f13f41fb4c681901302 Mon Sep 17 00:00:00 2001 From: bougyman's bot Date: Mon, 23 Nov 2026 12:42:05 -0500 Subject: [PATCH 3/3] fix(project): address paging and lookup review feedback --- app/lib/linear_cli/cli/commands/projects.ex | 28 +++++---- app/lib/linear_cli/linear/project.ex | 60 ++++++++----------- .../cli/commands/issues/read_test.exs | 2 +- .../cli/favorites_commands_test.exs | 49 ++++++++++----- app/test/linear_cli/cli_test.exs | 17 ++++++ app/test/linear_cli/linear/project_test.exs | 49 +++++++++++++++ documents/ash-domain-erd.adoc | 4 +- 7 files changed, 147 insertions(+), 62 deletions(-) diff --git a/app/lib/linear_cli/cli/commands/projects.ex b/app/lib/linear_cli/cli/commands/projects.ex index e671107..9e6701b 100644 --- a/app/lib/linear_cli/cli/commands/projects.ex +++ b/app/lib/linear_cli/cli/commands/projects.ex @@ -32,8 +32,10 @@ defmodule LinearCli.CLI.Commands.Projects do end defp interactive? do - Application.get_env(:linear_cli, :interactive) || - (:prim_tty.isatty(:stdin) == true and :prim_tty.isatty(:stdout) == true) + case Application.get_env(:linear_cli, :interactive) do + nil -> :prim_tty.isatty(:stdin) == true and :prim_tty.isatty(:stdout) == true + interactive? when is_boolean(interactive?) -> interactive? + end end defp show_pages(flags, options, cursor) do @@ -62,11 +64,15 @@ defmodule LinearCli.CLI.Commands.Projects do defp refresh_updates(_cached, projects, _flags) do projects - |> Enum.reduce_while({:ok, []}, fn project, {:ok, acc} -> - case Linear.find_project(project.id, %{with_updates: true}) do - {:ok, refreshed} -> {:cont, {:ok, [refreshed | acc]}} - {:error, reason} -> {:halt, {:error, reason}} - end + |> Task.async_stream(&Linear.find_project(&1.id, %{with_updates: true}), + max_concurrency: 4, + timeout: 30_000, + on_timeout: :kill_task + ) + |> Enum.reduce_while({:ok, []}, fn + {:ok, {:ok, refreshed}}, {:ok, acc} -> {:cont, {:ok, [refreshed | acc]}} + {:ok, {:error, reason}}, {:ok, _acc} -> {:halt, {:error, reason}} + {:exit, reason}, {:ok, _acc} -> {:halt, {:error, {:task_exit, reason}}} end) |> case do {:ok, refreshed} -> {:ok, Enum.reverse(refreshed)} @@ -128,7 +134,7 @@ defmodule LinearCli.CLI.Commands.Projects do text mode prompts for a choice and JSON mode returns an error. """ def project_update(%{args: %{project: search}, options: options}) do - with {:ok, projects} <- update_candidates(options), + with {:ok, projects} <- update_candidates(options, search), {:ok, project} <- resolve_update_project(projects, search, options), {:ok, update} <- Linear.post_project_update(project.id, options.body, %{health: options.health}) do @@ -139,13 +145,13 @@ defmodule LinearCli.CLI.Commands.Projects do end end - defp update_candidates(%{team: team_key} = options) when is_binary(team_key) do + defp update_candidates(%{team: team_key} = options, search) when is_binary(team_key) do with {:ok, team} <- resolve_team(options) do - Linear.projects_by_team(team.id) + Linear.projects_by_team(team.id, %{search: search}) end end - defp update_candidates(_options), do: Linear.workspace_projects() + defp update_candidates(_options, _search), do: Linear.workspace_projects() defp resolve_update_project(projects, search, options) do matches = diff --git a/app/lib/linear_cli/linear/project.ex b/app/lib/linear_cli/linear/project.ex index bfd9b9d..9cba4e1 100644 --- a/app/lib/linear_cli/linear/project.ex +++ b/app/lib/linear_cli/linear/project.ex @@ -261,11 +261,15 @@ defmodule LinearCli.Linear.Project.Actions.Page do case Api.call(document, %{"first" => @page_size, "after" => Map.get(args, :after)}) do {:ok, %{"projects" => %{"edges" => edges, "pageInfo" => page_info}}} -> + cursor = page_info["endCursor"] + {:ok, %{ projects: Enum.map(edges, &Project.from_map(&1["node"])), - has_next_page: page_info["hasNextPage"] == true, - end_cursor: page_info["endCursor"] + has_next_page: + page_info["hasNextPage"] == true and is_binary(cursor) and cursor != "" and + cursor != Map.get(args, :after), + end_cursor: cursor }} {:ok, other} -> @@ -316,10 +320,11 @@ defmodule LinearCli.Linear.Project.Read.ByTeam do @slugged_reference ~r/^(.*)-([[:alnum:]]{12})$/ @search_document """ - query($teamId: String!, $filter: ProjectFilter!) { + query($teamId: String!, $filter: ProjectFilter!, $after: String) { team(id: $teamId) { - projects(first: 100, filter: $filter) { + projects(first: 100, filter: $filter, after: $after) { nodes { #{Project.base_fields()} } + pageInfo { hasNextPage endCursor } } } } @@ -341,12 +346,15 @@ defmodule LinearCli.Linear.Project.Read.ByTeam do defp search(team_id, search, with_updates?) do variables = %{"teamId" => team_id, "filter" => project_filter(search)} - with {:ok, projects} <- fetch(@search_document, variables) do - if projects == [], do: all(team_id, with_updates?), else: {:ok, projects} + case page(@search_document, variables, [], MapSet.new()) do + {:ok, []} -> all(team_id, with_updates?) + result -> result end end - defp all(team_id, with_updates?), do: page(team_id, with_updates?, nil, []) + defp all(team_id, with_updates?) do + page(document(with_updates?), %{"teamId" => team_id}, [], MapSet.new()) + end defp document(with_updates?) do """ @@ -361,23 +369,11 @@ defmodule LinearCli.Linear.Project.Read.ByTeam do """ end - defp page(team_id, with_updates?, after_cursor, acc) do - variables = - if after_cursor, - do: %{"teamId" => team_id, "after" => after_cursor}, - else: %{"teamId" => team_id} - - case Api.call(document(with_updates?), variables) do + defp page(document, variables, acc, seen_cursors) do + case Api.call(document, variables) do {:ok, %{"team" => %{"projects" => projects}}} -> acc = acc ++ Enum.map(projects["nodes"] || [], &Project.from_map/1) - - case projects["pageInfo"] do - %{"hasNextPage" => true, "endCursor" => cursor} when is_binary(cursor) -> - page(team_id, with_updates?, cursor, acc) - - _ -> - {:ok, acc} - end + continue(projects["pageInfo"], document, variables, acc, seen_cursors) {:ok, other} -> {:error, {:unexpected_response, other}} @@ -390,22 +386,18 @@ defmodule LinearCli.Linear.Project.Read.ByTeam do end end - defp fetch(document, variables) do - case Api.call(document, variables) do - {:ok, %{"team" => %{"projects" => %{"nodes" => nodes}}}} -> - {:ok, Enum.map(nodes, &Project.from_map/1)} - - {:ok, other} -> - {:error, {:unexpected_response, other}} + defp continue(%{"hasNextPage" => true} = info, document, variables, acc, seen_cursors) do + cursor = info["endCursor"] - {:error, {:http_error, status, _body}} -> - {:error, {:http_error, status}} - - {:error, reason} -> - {:error, reason} + if is_binary(cursor) and cursor != "" and not MapSet.member?(seen_cursors, cursor) do + page(document, Map.put(variables, "after", cursor), acc, MapSet.put(seen_cursors, cursor)) + else + {:error, {:non_advancing_cursor, cursor}} end end + defp continue(_info, _document, _variables, acc, _seen_cursors), do: {:ok, acc} + defp project_filter(search) do reference = project_reference(search) {name, slug_id} = reference_terms(reference) diff --git a/app/test/linear_cli/cli/commands/issues/read_test.exs b/app/test/linear_cli/cli/commands/issues/read_test.exs index 512d021..e404f65 100644 --- a/app/test/linear_cli/cli/commands/issues/read_test.exs +++ b/app/test/linear_cli/cli/commands/issues/read_test.exs @@ -196,7 +196,7 @@ defmodule LinearCli.CLI.Commands.Issues.ReadTest do String.contains?(query, "team(id: $id)") -> Req.Test.json(conn, %{"data" => %{"team" => team_map()}}) - String.contains?(query, "projects(first: 100, filter: $filter)") -> + query =~ "team(id: $teamId)" and query =~ "filter: $filter" -> filters = decoded["variables"]["filter"]["or"] assert %{"name" => %{"containsIgnoreCase" => "Wallet Service Extraction"}} in filters diff --git a/app/test/linear_cli/cli/favorites_commands_test.exs b/app/test/linear_cli/cli/favorites_commands_test.exs index 69033f6..5939de2 100644 --- a/app/test/linear_cli/cli/favorites_commands_test.exs +++ b/app/test/linear_cli/cli/favorites_commands_test.exs @@ -386,28 +386,42 @@ defmodule LinearCli.CLI.FavoritesCommandsTest do refute output =~ "Platform Cleanup" end - test "--updates refetches favorites with their latest update" do - LinearCli.Favorites.add("project", "p1") - {:ok, _} = LinearCli.Favorites.records("project", false) + test "--updates refetches favorites concurrently and keeps their order" do + for id <- ["p1", "p2"] do + :ok = LinearCli.Favorites.add("project", id, %{"id" => id, "name" => id}) + end + + coordinator = + Task.async(fn -> + assert_receive {:refresh, "p1", first_worker}, 2_000 + assert_receive {:refresh, "p2", _second_worker}, 2_000 + send(first_worker, :second_refresh_started) + end) Req.Test.stub(LinearCli.Api, fn conn -> {:ok, body, conn} = Plug.Conn.read_body(conn) - query = Jason.decode!(body)["query"] + %{"query" => query, "variables" => %{"id" => id}} = Jason.decode!(body) assert query =~ "project(id: $id)" assert query =~ "projectUpdates(first: 1)" + send(coordinator.pid, {:refresh, id, self()}) + + if id == "p1" do + assert_receive :second_refresh_started, 2_000 + end Req.Test.json(conn, %{ "data" => %{ "project" => %{ - "id" => "p1", - "name" => "Manhattan", - "slugId" => "abc", - "url" => "https://linear.app/x/project/manhattan-abc", - "teams" => %{"nodes" => []}, + "id" => id, + "name" => id, "projectUpdates" => %{ "nodes" => [ - %{"id" => "pu1", "body" => "On pace.", "createdAt" => "2026-11-18T09:00:00Z"} + %{ + "id" => "update-#{id}", + "body" => "Update for #{id}", + "createdAt" => "2026-11-18T09:00:00Z" + } ] } } @@ -415,10 +429,17 @@ defmodule LinearCli.CLI.FavoritesCommandsTest do }) end) - output = capture_io(fn -> LinearCli.CLI.main(["project", "list", "--updates"]) end) - assert output =~ "Manhattan" - assert output =~ "2026-11-18" - assert output =~ "On pace." + output = + capture_io(fn -> + assert :ok = LinearCli.CLI.main(["project", "list", "--updates", "--output", "json"]) + end) + + Task.await(coordinator) + + assert [ + %{"id" => "p1", "latest_update" => %{"body" => "Update for p1"}}, + %{"id" => "p2", "latest_update" => %{"body" => "Update for p2"}} + ] = Jason.decode!(output) end test "--all overrides the favorites filter" do diff --git a/app/test/linear_cli/cli_test.exs b/app/test/linear_cli/cli_test.exs index baffce3..4632b12 100644 --- a/app/test/linear_cli/cli_test.exs +++ b/app/test/linear_cli/cli_test.exs @@ -287,6 +287,12 @@ defmodule LinearCli.CLITest do query =~ "team(id: $teamId)" -> assert variables["teamId"] == "t2" + assert query =~ "filter: $filter" + + assert %{"name" => %{"containsIgnoreCase" => "Website"}} in variables["filter"][ + "or" + ] + %{"team" => %{"projects" => %{"nodes" => [second]}}} query =~ "projects(first: $first" -> @@ -483,6 +489,17 @@ defmodule LinearCli.CLITest do assert output =~ "FirstPage" assert output =~ "SecondPage" end + + test "explicitly disabling interactivity fetches every page without prompting" do + Application.put_env(:linear_cli, :interactive, false) + + output = capture_io(fn -> assert :ok = LinearCli.CLI.main(["project", "list", "--all"]) end) + + refute output =~ "Show the next" + assert output =~ "FirstPage" + assert output =~ "SecondPage" + assert_received {:requested, "page-1"} + end end test "project list without --updates does not request project updates" do diff --git a/app/test/linear_cli/linear/project_test.exs b/app/test/linear_cli/linear/project_test.exs index 447edae..ce771fe 100644 --- a/app/test/linear_cli/linear/project_test.exs +++ b/app/test/linear_cli/linear/project_test.exs @@ -114,6 +114,24 @@ defmodule LinearCli.Linear.ProjectTest do Linear.projects_page(%{after: "page-1"}) end + test "projects_page/1 does not report another page without an advancing cursor" do + for end_cursor <- [nil, "", "page-1"] do + Req.Test.stub(LinearCli.Api, fn conn -> + Req.Test.json(conn, %{ + "data" => %{ + "projects" => %{ + "edges" => [%{"node" => %{"id" => "p1"}}], + "pageInfo" => %{"hasNextPage" => true, "endCursor" => end_cursor} + } + } + }) + end) + + assert {:ok, %{projects: [%{id: "p1"}], has_next_page: false}} = + Linear.projects_page(%{after: "page-1"}) + end + end + test "workspace_projects/0 follows every project page for lookup" do Req.Test.stub(LinearCli.Api, fn conn -> {:ok, body, conn} = Plug.Conn.read_body(conn) @@ -171,6 +189,37 @@ defmodule LinearCli.Linear.ProjectTest do assert Enum.map(projects, & &1.id) == ["p1", "p2"] end + test "projects_by_team/2 follows every page with the server-side search filter" do + Req.Test.stub(LinearCli.Api, fn conn -> + {:ok, body, conn} = Plug.Conn.read_body(conn) + %{"query" => query, "variables" => variables} = Jason.decode!(body) + + assert query =~ "filter: $filter" + assert variables["teamId"] == "team-1" + assert %{"name" => %{"containsIgnoreCase" => "Website"}} in variables["filter"]["or"] + + {project_id, page_info} = + case variables["after"] do + nil -> {"p1", %{"hasNextPage" => true, "endCursor" => "page-1"}} + "page-1" -> {"p2", %{"hasNextPage" => false}} + end + + Req.Test.json(conn, %{ + "data" => %{ + "team" => %{ + "projects" => %{ + "nodes" => [%{"id" => project_id, "name" => "Website"}], + "pageInfo" => page_info + } + } + } + }) + end) + + assert {:ok, [%{id: "p1", name: "Website"}, %{id: "p2", name: "Website"}]} = + Linear.projects_by_team("team-1", %{search: "Website"}) + end + test "my_projects/0 flat-maps each of the viewer's teams' projects (Ruby: Project.mine)" do Req.Test.stub(LinearCli.Api, fn conn -> {:ok, body, conn} = Plug.Conn.read_body(conn) diff --git a/documents/ash-domain-erd.adoc b/documents/ash-domain-erd.adoc index 0dae6e4..31ee6b4 100644 --- a/documents/ash-domain-erd.adoc +++ b/documents/ash-domain-erd.adoc @@ -377,7 +377,7 @@ manual-implementation module, and the Linear GraphQL operation it calls. | `:by_team` | read | `Linear.Project.Read.ByTeam` -| `team(id: $teamId) { projects(first:, after:, filter:) { ... } }` — paginates the selected team's projects, or applies a server-side name/slug/id filter when `search` is supplied. The optional `with_updates` argument also selects each project's newest update (not applied to the `search` filter query) +| `team(id: $teamId) { projects(first:, after:, filter:) { ... } }` — paginates the selected team's projects. The `search` argument applies a server-side name, slug, or ID filter to every page. With `with_updates`, the unfiltered query includes each project's newest update. | `Project` | `create_project` @@ -413,7 +413,7 @@ manual-implementation module, and the Linear GraphQL operation it calls. | generic action (`:map`) | `Linear.Project.Actions.Page` | `projects(first: 100, after:)` — one page. Arguments: `after` (cursor), `with_updates`. Returns - `%{projects:, has_next_page:, end_cursor:}`. The action never follows the cursor. + `%{projects:, has_next_page:, end_cursor:}`. The next page requires a non-empty cursor that differs from `after`. | `Issue` | `issues_first_page`