Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions Readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,9 @@ 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 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
Expand Down Expand Up @@ -558,6 +558,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
Expand All @@ -568,6 +585,12 @@ Update" feature), not an edit to the project itself.
$ lc project update Manhattan --body "Shipping ahead of schedule" --health onTrack
----

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

The `bin/` wrapper scripts (ported verbatim from `linear-cli`'s own `exe/scripts/`)
Expand Down
3 changes: 2 additions & 1 deletion app/lib/linear_cli/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
144 changes: 127 additions & 17 deletions app/lib/linear_cli/cli/commands/projects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,77 @@ 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
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
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
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
|> 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)}
error -> error
end
end

Expand All @@ -22,6 +86,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),
Expand Down Expand Up @@ -62,17 +129,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 the active team's projects,
prompting if ambiguous. Team is resolved via `--team`, the active
profile, or an interactive prompt.
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, team} <- resolve_team(options),
{:ok, projects} <- Linear.projects_by_team(team.id, %{search: search}),
{:ok, project} <- resolve_project(projects, search, 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
Display.show(update, %{output: options.output})
Expand All @@ -82,6 +145,51 @@ defmodule LinearCli.CLI.Commands.Projects do
end
end

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, %{search: search})
end
end

defp update_candidates(_options, _search), 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()

Expand Down Expand Up @@ -145,12 +253,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
21 changes: 19 additions & 2 deletions app/lib/linear_cli/cli/display.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(<<date::binary-size(10), _rest::binary>>), 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
Expand Down
1 change: 1 addition & 0 deletions app/lib/linear_cli/linear.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading