defmodule VigilWeb.Api.V0.IssueController do
@moduledoc """
A project's issues, as sentry-cli's `issues list/resolve/mute/unresolve`
use them (2019 Sentry's `project_group_index.py`, BSD-3):
* `GET /projects/{org}/{project}/issues/` (`event:read` and up):
`query` in the issue search's syntax (`is:resolved`, `level:error`,
free text; `is:unresolved` when absent), Sentry's Link-header cursors
(`cursor`, `per_page`). `statsPeriod` and the like are ignored.
* `PUT /projects/{org}/{project}/issues/` (`event:write`): one change
(`{"status": "resolved" | "resolvedInNextRelease" | "unresolved" |
"ignored" | "muted"}`) to the issues named by `?id=` (repeated; a
short id such as `12`, or the issue's UUID), else to every issue with
`?status=`, else to every issue matching `query` (as the list), at
most 1000. 200 with the change; 204 when nothing matched.
For the Android app (read-only), with the list's shape and cursors:
* `GET /organizations/{org}/issues/` (`event:read` and up): the issues
of every project the token can see, or of those named by `?project=`
(repeated; a project's numeric id, as Sentry's), newest activity
first.
* `GET /projects/{org}/{project}/issues/{id}/`: one issue, as Sentry's
group details (its releases and a tag summary).
* `GET /projects/{org}/{project}/issues/{id}/events/latest/`: its
newest event (`VigilWeb.Api.V0.EventJSON`). Sentry has these under
`/issues/{id}/`, with global ids; here an issue is found through its
project, as everywhere else in `/api/0`.
An issue's `id` is its short id within the project, as the web UI's
issue URLs (`WEB-12` is `"12"`), so sentry-cli's numeric `--id` works.
"""
use VigilWeb, :controller
alias Vigil.{Issues, Projects}
alias VigilWeb.Api.{Auth, Pagination}
alias VigilWeb.Api.V0.{EventJSON, ProjectController, ProjectJSON, ReleaseController}
@read ~w(event:read event:write event:admin)
@bulk_limit 1000
@actions %{
"resolved" => "resolve",
"resolvedInNextRelease" => :next_release,
"unresolved" => "unresolve",
"ignored" => "ignore",
"muted" => "ignore"
}
def index(conn, params) do
with {:ok, conn, project} <- ProjectController.resolve(conn, params, @read) do
scope = conn.assigns.current_scope
offset = Pagination.offset(params["cursor"])
per_page = Pagination.per_page(params["per_page"])
filters =
params["query"]
|> query_filters()
|> Map.merge(%{limit: per_page, offset: offset})
%{issues: issues, more?: more?} = Issues.search(scope, project.id, filters)
org = scope.organization
users = issues |> Enum.map(& &1.id) |> Issues.user_counts()
conn
|> Pagination.put_link(offset, per_page, more?)
|> json(Enum.map(issues, &ProjectJSON.issue(&1, project, org, Map.get(users, &1.id, 0))))
end
|> ReleaseController.done()
end
def org_index(conn, %{"org" => org} = params) do
with {:ok, conn} <- Auth.put_org(conn, org),
{:ok, conn} <- Auth.require_any_scope(conn, @read) do
scope = conn.assigns.current_scope
projects = scope |> Projects.list_visible() |> only_projects(conn)
by_id = Map.new(projects, &{&1.id, &1})
offset = Pagination.offset(params["cursor"])
per_page = Pagination.per_page(params["per_page"])
filters =
params["query"]
|> query_filters()
|> Map.merge(%{limit: per_page, offset: offset})
%{issues: issues, more?: more?} = Issues.search(scope, Map.keys(by_id), filters)
users = issues |> Enum.map(& &1.id) |> Issues.user_counts()
conn
|> Pagination.put_link(offset, per_page, more?)
|> json(
Enum.map(issues, fn i ->
ProjectJSON.issue(i, by_id[i.project_id], scope.organization, Map.get(users, i.id, 0))
end)
)
end
|> ReleaseController.done()
end
# `?project=` may repeat; none (or Sentry's `-1`, "all") means every one.
defp only_projects(projects, conn) do
wanted =
for {"project", id} <- URI.query_decoder(conn.query_string), id != "-1", do: id
if wanted == [],
do: projects,
else: Enum.filter(projects, &(to_string(&1.dsn_id) in wanted))
end
def show(conn, %{"id" => id} = params) do
with {:ok, conn, project} <- ProjectController.resolve(conn, params, @read),
{:ok, issue} <- fetch_issue(conn, project, id) do
issue = Issues.preload_detail(issue)
users = Issues.user_counts([issue.id])
json(
conn,
ProjectJSON.issue_detail(
issue,
project,
conn.assigns.current_scope.organization,
Map.get(users, issue.id, 0),
Issues.tag_facets(issue)
)
)
end
|> ReleaseController.done()
end
def latest_event(conn, %{"id" => id} = params) do
with {:ok, conn, project} <- ProjectController.resolve(conn, params, @read),
{:ok, issue} <- fetch_issue(conn, project, id) do
case Issues.latest_event(issue) do
nil -> Auth.not_found(conn)
event -> json(conn, EventJSON.event(event, issue, project))
end
end
|> ReleaseController.done()
end
defp fetch_issue(conn, project, id) do
case find_issue(project.id, id) do
nil -> {:error, Auth.not_found(conn)}
issue -> {:ok, issue}
end
end
def bulk_update(conn, params) do
with {:ok, conn, project} <- ProjectController.resolve(conn, params, ["event:write"]),
{:ok, status, action} <- action(conn) do
scope = conn.assigns.current_scope
case target_issues(conn, scope, project) do
[] ->
send_resp(conn, 204, "")
issues ->
apply_action(scope, project, issues, action)
json(conn, %{status: status, statusDetails: %{}})
end
end
|> ReleaseController.done()
end
defp action(conn) do
status = conn.body_params["status"]
case @actions[status] do
nil ->
{:error, conn |> put_status(400) |> json(%{status: ["Unsupported status"]}) |> halt()}
action ->
{:ok, status, action}
end
end
defp apply_action(scope, _project, issues, :next_release),
do: Enum.each(issues, &Issues.resolve_in_next_release(scope, &1))
defp apply_action(scope, project, issues, action),
do: Issues.bulk(scope, project.id, Enum.map(issues, & &1.id), action)
# `?id=` may repeat (`id=1&id=2`), which Plug's parser collapses to the
# last; read them from the raw query string.
defp target_issues(conn, scope, project) do
query = URI.query_decoder(conn.query_string) |> Enum.to_list()
case for({"id", id} <- query, do: id) do
[] ->
filters =
case List.keyfind(query, "status", 0) do
{"status", status} -> query_filters("is:#{status}")
nil -> query_filters(conn.query_params["query"])
end
Issues.search(scope, project.id, Map.put(filters, :limit, @bulk_limit)).issues
ids ->
ids |> Enum.map(&find_issue(project.id, &1)) |> Enum.reject(&is_nil/1)
end
end
defp find_issue(project_id, id) do
case Integer.parse(id) do
{_, ""} -> Issues.get_by_short_id(project_id, id)
_ -> Issues.get_issue(project_id, id)
end
end
defp query_filters(query) when query in [nil, ""], do: %{status: :unresolved}
defp query_filters(query), do: Issues.parse_query(query)
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
7.3 KiB · text
History
3ec21a7