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
23 changes: 20 additions & 3 deletions app/lib/linear_cli/linear/issue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,31 @@ defmodule LinearCli.Linear.Issue.Update.Close do
@moduledoc false
use Ash.Resource.ManualUpdate

alias LinearCli.Api
alias LinearCli.Linear.Issue

def update(changeset, _opts, _context) do
args = changeset.arguments
input = %{"stateId" => args.state_id}
input = if args.trash, do: Map.put(input, "trashed", true), else: input

Issue.Update.run(changeset.data.identifier, input)
with {:ok, issue} <-
Issue.Update.run(changeset.data.identifier, %{"stateId" => args.state_id}),
:ok <- maybe_trash(changeset.data.id, args.trash) do
{:ok, issue}
end
end

defp maybe_trash(_id, false), do: :ok

defp maybe_trash(id, true) do
case Api.call(trash_document(), %{"id" => id}) do
{:ok, %{"issueArchive" => %{"success" => true}}} -> :ok
{:ok, other} -> {:error, {:unexpected_response, other}}
{:error, reason} -> {:error, reason}
end
end

defp trash_document do
"mutation($id: String!) { issueArchive(id: $id, trash: true) { success } }"
end
end

Expand Down
46 changes: 29 additions & 17 deletions app/test/linear_cli/linear/issue_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -371,34 +371,46 @@ defmodule LinearCli.Linear.IssueTest do
assert updated.state.name == "Done"
end

test "sends trashed: true when given via opts" do
test "transitions the issue before trashing it with issueArchive" do
issue = struct!(LinearCli.Linear.Issue, id: "i1", identifier: "CRY-1")
test_pid = self()

Req.Test.stub(LinearCli.Api, fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)
%{"variables" => %{"input" => input}} = Jason.decode!(body)
%{"query" => query, "variables" => variables} = Jason.decode!(body)

assert input == %{"stateId" => "s1", "trashed" => true}
if String.contains?(query, "issueUpdate") do
assert variables["input"] == %{"stateId" => "s1"}
send(test_pid, :transitioned)

Req.Test.json(conn, %{
"data" => %{
"issueUpdate" => %{
"issue" => %{
"id" => "i1",
"identifier" => "CRY-1",
"title" => "Fix it",
"branchName" => "cry-1-fix-it",
"description" => nil,
"assignee" => nil,
"team" => %{"id" => "t1", "key" => "ENG", "name" => "Engineering"},
"comments" => %{"nodes" => []}
Req.Test.json(conn, %{
"data" => %{
"issueUpdate" => %{
"issue" => %{
"id" => "i1",
"identifier" => "CRY-1",
"title" => "Fix it",
"branchName" => "cry-1-fix-it",
"description" => nil,
"assignee" => nil,
"team" => %{"id" => "t1", "key" => "ENG", "name" => "Engineering"},
"comments" => %{"nodes" => []}
}
}
}
}
})
})
else
assert query =~ "issueArchive(id: $id, trash: true)"
assert variables == %{"id" => "i1"}
send(test_pid, :trashed)

Req.Test.json(conn, %{"data" => %{"issueArchive" => %{"success" => true}}})
end
end)

assert {:ok, _updated} = Linear.close_issue(issue, "s1", %{trash: true})
assert_receive :transitioned
assert_receive :trashed
end

test "surfaces a GraphQL error" do
Expand Down
3 changes: 2 additions & 1 deletion documents/ash-domain-erd.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ manual-implementation module, and the Linear GraphQL operation it calls.
| `:close`
| update
| `Linear.Issue.Update.Close`
| `issueUpdate(id:, input: { stateId, trashed? })` via `Issue.Update.run/2`
| `issueUpdate(id:, input: { stateId })` via `Issue.Update.run/2`, then
`issueArchive(id:, trash: true)` when requested

| `Issue`
| `set_issue_status`
Expand Down