defmodule VigilWeb.Api.V0.ProjectJSON do
@moduledoc """
Teams, projects, DSN keys and issues in Sentry's shapes (2019 Sentry's
`api/serializers/models/{team,project,project_key,group}.py`, BSD-3;
GlitchTip's `apps/projects/schema.py`, MIT), with what sentry-cli
deserializes: a project's `id`, `slug`, `name` and optional `team`; an
issue's `id`, `shortId`, `title`, `lastSeen`, `status` and `level`.
Ids follow the rest of `/api/0`: a project's is its DSN number, an
issue's its short id within the project (`WEB-12` is `"12"`, as the web
UI's issue URLs), a key's its public key.
"""
alias Vigil.Accounts.Scope
alias Vigil.Issues.Issue
alias Vigil.Organizations.Team
alias Vigil.Projects
alias Vigil.Projects.{Project, ProjectKey}
@doc "A project; `teams` loaded (or not) on it."
def project(%Project{} = p, org) do
teams = if Ecto.assoc_loaded?(p.teams), do: Enum.sort_by(p.teams, & &1.name), else: []
%{
id: to_string(p.dsn_id),
slug: p.slug,
name: p.name,
platform: p.platform,
dateCreated: p.inserted_at,
firstEvent: p.first_event_at,
status: "active",
isBookmarked: false,
isMember: true,
hasAccess: true,
features: [],
team: teams |> List.first() |> then(&(&1 && team_ref(&1))),
teams: Enum.map(teams, &team_ref/1),
organization: %{id: org.id, slug: org.slug, name: org.name}
}
end
defp team_ref(%Team{} = t), do: %{id: t.id, slug: t.slug, name: t.name}
@doc """
A team, for `scope`'s caller: `isMember` says whether they're on it;
`projects` are those of its projects the caller may see.
"""
def team(%Team{} = t, %Scope{} = scope, visible_ids) do
member? = Enum.any?(t.members, &(&1.id == scope.member.id))
%{
id: t.id,
slug: t.slug,
name: t.name,
dateCreated: t.inserted_at,
isMember: member?,
hasAccess: member? or Scope.global?(scope) or scope.organization.open_membership,
isPending: false,
memberCount: length(t.members),
avatar: %{avatarType: "letter_avatar", avatarUuid: nil},
projects:
t.projects
|> Enum.filter(&(&1.id in visible_ids))
|> Enum.map(&%{id: to_string(&1.dsn_id), slug: &1.slug, name: &1.name})
}
end
@doc """
A DSN key. Vigil keys have no secret half, so `secret` repeats the
public DSN, as GlitchTip's does (sentry-wizard requires the field).
"""
def key(%ProjectKey{} = k, %Project{} = p) do
public = String.replace(k.public_key, "-", "")
base = VigilWeb.base_url()
%{
id: public,
name: k.label,
label: k.label,
public: public,
secret: public,
projectId: p.dsn_id,
isActive: k.active,
rateLimit:
if(k.rate_limit_count,
do: %{window: k.rate_limit_window, count: k.rate_limit_count},
else: nil
),
dsn: %{
public: Projects.dsn(k, p),
secret: Projects.dsn(k, p),
csp: Projects.security_endpoint(k, p),
security: Projects.security_endpoint(k, p),
minidump: "#{base}/api/#{p.dsn_id}/minidump/?sentry_key=#{public}",
unreal: nil,
cdn: nil
},
browserSdkVersion: nil,
dateCreated: k.inserted_at
}
end
@doc """
An issue of `project`, in the shape of Sentry's group list. `user_count`
is how many distinct users it has seen (`Vigil.Issues.user_counts/1`).
"""
def issue(%Issue{} = i, %Project{} = p, org, user_count \\ 0) do
%{
id: to_string(i.short_id),
shortId: "#{String.upcase(p.slug)}-#{i.short_id}",
title: i.title,
culprit: i.culprit,
permalink:
"#{VigilWeb.base_url()}/orgs/#{org.slug}/projects/#{p.slug}/issues/#{i.short_id}",
logger: nil,
level: i.level,
status: to_string(i.status),
statusDetails: status_details(i),
isPublic: i.is_public,
platform: i.platform,
project: %{id: to_string(p.dsn_id), slug: p.slug, name: p.name},
type: to_string(i.type),
metadata: i.metadata,
numComments: 0,
assignedTo: assignee(i),
isBookmarked: false,
isSubscribed: false,
hasSeen: false,
annotations: [],
count: to_string(i.count),
userCount: user_count,
firstSeen: i.first_seen,
lastSeen: i.last_seen
}
end
@doc """
One issue, as Sentry's group details: the list shape plus its releases
and a summary of its tags (`facets`, `Vigil.Issues.tag_facets/1`: the
top values of each key, with their counts, and `totalValues` the events
those top values account for). The issue has `preload_detail/1` done.
"""
def issue_detail(%Issue{} = i, %Project{} = p, org, user_count, facets) do
i
|> issue(p, org, user_count)
|> Map.merge(%{
firstRelease: release_ref(i.first_release),
lastRelease: release_ref(i.last_release),
tags:
facets
|> Enum.sort_by(&elem(&1, 0))
|> Enum.map(fn {key, values} ->
%{
key: key,
name: key,
totalValues: values |> Enum.map(&elem(&1, 1)) |> Enum.sum(),
topValues:
Enum.map(values, fn {value, count} ->
%{key: key, value: value, name: value, count: count}
end)
}
end)
})
end
defp release_ref(%Vigil.Releases.Release{} = r), do: %{version: r.version}
defp release_ref(_), do: nil
defp status_details(%Issue{status: :resolved, resolved_in_next_release: true}),
do: %{inNextRelease: true}
defp status_details(_), do: %{}
defp assignee(%Issue{assignee: %Vigil.Accounts.User{} = u}),
do: %{type: "user", id: u.id, email: u.email, name: Vigil.Accounts.display_name(u)}
defp assignee(_), do: nil
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
5.6 KiB · text
History
3ec21a7