neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
Log in to mark this repository.
Slice 3 groundwork: issue search, counts, navigation, triage API; routes
e677fc5 · gmorell · 2026-12-01 22:27
Message
{commit_body(@commit)}
Files changed
modified
lib/vigil/issues.ex
+435
−21
| 1 | 1 | defmodule Vigil.Issues do |
| 2 | 2 | @moduledoc """ |
| 3 | 3 | Issues and their events, read and triaged. The writes that make them are |
| 4 | − `Vigil.Events.Processor`'s; here are the reads the UI and API need, | |
| 5 | − status changes, and the grouping preview. | |
| 4 | + `Vigil.Events.Processor`'s; here are the reads the UI and API need | |
| 5 | + (search, sparklines, event navigation, tag facets), triage (status, | |
| 6 | + assignee, comments, public sharing, bulk actions) and the grouping | |
| 7 | + preview. | |
| 6 | 8 | |
| 7 | − Status changes need `event:write` in the issue's organization | |
| 8 | − (`Vigil.Accounts.Scope`). | |
| 9 | + Changes need `event:write` in the issue's organization | |
| 10 | + (`Vigil.Accounts.Scope`); deleting needs `event:admin`. Every change is | |
| 11 | + broadcast on `project:<id>:issues` (and `issue:<id>`), as new events are. | |
| 9 | 12 | """ |
| 10 | 13 | |
| 11 | 14 | import Ecto.Query |
| 12 | 15 | |
| 13 | − alias Vigil.Accounts.Scope | |
| 16 | + alias Vigil.Accounts.{Scope, User} | |
| 14 | 17 | alias Vigil.Grouping |
| 15 | − alias Vigil.Issues.{Issue, IssueEvent} | |
| 18 | + alias Vigil.Issues.{Comment, Issue, IssueEvent, UserReport} | |
| 16 | 19 | alias Vigil.Organizations |
| 20 | + alias Vigil.Organizations.Member | |
| 21 | + alias Vigil.Releases.Release | |
| 17 | 22 | alias Vigil.Repo |
| 18 | 23 | |
| 24 | + ## Lookups | |
| 25 | + | |
| 19 | 26 | @doc "An issue of a project, or nil." |
| 20 | 27 | def get_issue(project_id, issue_id) do |
| 21 | 28 | with {:ok, id} <- Ecto.UUID.cast(issue_id) do |
| 36 | 43 | ) |
| 37 | 44 | end |
| 38 | 45 | |
| 46 | + def get_by_short_id(project_id, short_id) when is_binary(short_id) do | |
| 47 | + case Integer.parse(short_id) do | |
| 48 | + {n, ""} -> get_by_short_id(project_id, n) | |
| 49 | + _ -> nil | |
| 50 | + end | |
| 51 | + end | |
| 52 | + | |
| 53 | + @doc "A public issue by id (for its share page), or nil." | |
| 54 | + def get_public_issue(issue_id) do | |
| 55 | + with {:ok, id} <- Ecto.UUID.cast(issue_id) do | |
| 56 | + Repo.one( | |
| 57 | + from i in Issue, | |
| 58 | + where: i.id == ^id and i.is_public and is_nil(i.deleted_at), | |
| 59 | + preload: [:project] | |
| 60 | + ) | |
| 61 | + else | |
| 62 | + _ -> nil | |
| 63 | + end | |
| 64 | + end | |
| 65 | + | |
| 66 | + @doc "The issue with its releases and assignee loaded, for the detail page." | |
| 67 | + def preload_detail(%Issue{} = issue), | |
| 68 | + do: Repo.preload(issue, [:first_release, :last_release, :resolved_in_release, :assignee]) | |
| 69 | + | |
| 39 | 70 | @doc """ |
| 40 | 71 | A project's issues, newest activity first. Options: `:status` (atom, or |
| 41 | − `:all`), `:limit`. | |
| 72 | + `:all`), `:limit`. (The issue list uses `search/2`.) | |
| 42 | 73 | """ |
| 43 | 74 | def list_issues(project_id, opts \\ []) do |
| 44 | 75 | status = Keyword.get(opts, :status, :unresolved) |
| 52 | 83 | |> Repo.all() |
| 53 | 84 | end |
| 54 | 85 | |
| 86 | + ## Search | |
| 87 | + | |
| 88 | + @type filters :: %{ | |
| 89 | + optional(:status) => :unresolved | :resolved | :ignored | :all, | |
| 90 | + optional(:level) => String.t() | nil, | |
| 91 | + optional(:environment) => String.t() | nil, | |
| 92 | + optional(:release) => String.t() | nil, | |
| 93 | + optional(:assigned) => :me | :none | nil, | |
| 94 | + optional(:period) => String.t() | nil, | |
| 95 | + optional(:text) => String.t() | nil, | |
| 96 | + optional(:tags) => [{String.t(), String.t()}], | |
| 97 | + optional(:sort) => :last_seen | :first_seen | :count, | |
| 98 | + optional(:limit) => pos_integer(), | |
| 99 | + optional(:offset) => non_neg_integer() | |
| 100 | + } | |
| 101 | + | |
| 102 | + @periods %{"1h" => 1, "24h" => 24, "7d" => 24 * 7, "14d" => 24 * 14, "30d" => 24 * 30} | |
| 103 | + | |
| 104 | + @doc "The time ranges the issue list offers, as `{key, label}`." | |
| 105 | + def periods, | |
| 106 | + do: [ | |
| 107 | + {"1h", "Last hour"}, | |
| 108 | + {"24h", "Last 24 hours"}, | |
| 109 | + {"7d", "Last 7 days"}, | |
| 110 | + {"14d", "Last 14 days"}, | |
| 111 | + {"30d", "Last 30 days"} | |
| 112 | + ] | |
| 113 | + | |
| 114 | + @doc """ | |
| 115 | + A search box's text as filters, Sentry style: `is:resolved`, | |
| 116 | + `is:ignored`, `is:unresolved`, `is:all`, `level:error`, | |
| 117 | + `environment:prod` (or `env:`), `release:1.2`, `assigned:me`, | |
| 118 | + `assigned:none`, any other `key:value` as a tag (`browser.name:Chrome`, | |
| 119 | + quotes allowed: `url:"https://x/y"`), and the rest as free text. | |
| 120 | + """ | |
| 121 | + @spec parse_query(String.t() | nil) :: filters() | |
| 122 | + def parse_query(nil), do: %{} | |
| 123 | + | |
| 124 | + def parse_query(query) do | |
| 125 | + ~r/(\w[\w.\-]*):("[^"]*"|\S+)|"([^"]*)"|(\S+)/ | |
| 126 | + |> Regex.scan(query) | |
| 127 | + |> Enum.reduce(%{tags: [], text: []}, fn | |
| 128 | + [_, key, value | _], acc when key != "" -> put_token(acc, key, unquote_value(value)) | |
| 129 | + [_, "", "", phrase], acc -> Map.update!(acc, :text, &[phrase | &1]) | |
| 130 | + [_, "", "", "", word], acc -> Map.update!(acc, :text, &[word | &1]) | |
| 131 | + _, acc -> acc | |
| 132 | + end) | |
| 133 | + |> then(fn acc -> | |
| 134 | + text = acc.text |> Enum.reverse() |> Enum.join(" ") | |
| 135 | + | |
| 136 | + acc | |
| 137 | + |> Map.put(:text, if(text == "", do: nil, else: text)) | |
| 138 | + |> Map.update!(:tags, &Enum.reverse/1) | |
| 139 | + end) | |
| 140 | + end | |
| 141 | + | |
| 142 | + defp unquote_value(<<?", rest::binary>>), do: String.trim_trailing(rest, "\"") | |
| 143 | + defp unquote_value(v), do: v | |
| 144 | + | |
| 145 | + defp put_token(acc, "is", v) when v in ~w(resolved ignored unresolved all), | |
| 146 | + do: Map.put(acc, :status, String.to_existing_atom(v)) | |
| 147 | + | |
| 148 | + defp put_token(acc, "level", v), do: Map.put(acc, :level, v) | |
| 149 | + defp put_token(acc, k, v) when k in ~w(environment env), do: Map.put(acc, :environment, v) | |
| 150 | + defp put_token(acc, "release", v), do: Map.put(acc, :release, v) | |
| 151 | + defp put_token(acc, "assigned", "me"), do: Map.put(acc, :assigned, :me) | |
| 152 | + defp put_token(acc, "assigned", v) when v in ~w(none nobody), do: Map.put(acc, :assigned, :none) | |
| 153 | + defp put_token(acc, key, v), do: Map.update!(acc, :tags, &[{key, v} | &1]) | |
| 154 | + | |
| 155 | + @doc """ | |
| 156 | + A project's issues matching `filters` (`t:filters/0`; status defaults to | |
| 157 | + unresolved, sort to last seen). `scope` names "me" for `assigned:me`. | |
| 158 | + Returns `%{issues: [...], more?: boolean}`, a page of `:limit` (50). | |
| 159 | + """ | |
| 160 | + def search(%Scope{} = scope, project_id, filters \\ %{}) do | |
| 161 | + limit = filters[:limit] || 50 | |
| 162 | + offset = filters[:offset] || 0 | |
| 163 | + | |
| 164 | + issues = | |
| 165 | + from(i in Issue, where: i.project_id == ^project_id and is_nil(i.deleted_at)) | |
| 166 | + |> filter_status(filters[:status] || :unresolved) | |
| 167 | + |> filter_eq(:level, filters[:level]) | |
| 168 | + |> filter_period(filters[:period]) | |
| 169 | + |> filter_assigned(filters[:assigned], scope) | |
| 170 | + |> filter_release(filters[:release]) | |
| 171 | + |> filter_event_tag("environment", filters[:environment]) | |
| 172 | + |> filter_tags(filters[:tags] || []) | |
| 173 | + |> filter_text(filters[:text]) | |
| 174 | + |> sort(filters[:sort] || :last_seen) | |
| 175 | + |> limit(^(limit + 1)) | |
| 176 | + |> offset(^offset) | |
| 177 | + |> preload(:assignee) | |
| 178 | + |> Repo.all() | |
| 179 | + | |
| 180 | + %{issues: Enum.take(issues, limit), more?: length(issues) > limit} | |
| 181 | + end | |
| 182 | + | |
| 183 | + defp filter_status(q, :all), do: q | |
| 184 | + defp filter_status(q, status), do: where(q, [i], i.status == ^status) | |
| 185 | + | |
| 186 | + defp filter_eq(q, _field, v) when v in [nil, ""], do: q | |
| 187 | + defp filter_eq(q, field, v), do: where(q, [i], field(i, ^field) == ^v) | |
| 188 | + | |
| 189 | + defp filter_period(q, p) do | |
| 190 | + case @periods[p] do | |
| 191 | + nil -> q | |
| 192 | + hours -> where(q, [i], i.last_seen >= ^DateTime.add(DateTime.utc_now(), -hours, :hour)) | |
| 193 | + end | |
| 194 | + end | |
| 195 | + | |
| 196 | + defp filter_assigned(q, :me, scope), do: where(q, [i], i.assignee_id == ^scope.user.id) | |
| 197 | + defp filter_assigned(q, :none, _scope), do: where(q, [i], is_nil(i.assignee_id)) | |
| 198 | + defp filter_assigned(q, _, _scope), do: q | |
| 199 | + | |
| 200 | + defp filter_release(q, v) when v in [nil, ""], do: q | |
| 201 | + defp filter_release(q, v), do: filter_event_tag(q, "release", v) | |
| 202 | + | |
| 203 | + defp filter_event_tag(q, _key, v) when v in [nil, ""], do: q | |
| 204 | + | |
| 205 | + defp filter_event_tag(q, key, value) do | |
| 206 | + where( | |
| 207 | + q, | |
| 208 | + [i], | |
| 209 | + fragment( | |
| 210 | + "EXISTS (SELECT 1 FROM issue_tags t WHERE t.issue_id = ? AND t.key = ? AND t.value = ?)", | |
| 211 | + i.id, | |
| 212 | + ^key, | |
| 213 | + ^value | |
| 214 | + ) | |
| 215 | + ) | |
| 216 | + end | |
| 217 | + | |
| 218 | + defp filter_tags(q, tags), | |
| 219 | + do: Enum.reduce(tags, q, fn {k, v}, q -> filter_event_tag(q, k, v) end) | |
| 220 | + | |
| 221 | + defp filter_text(q, nil), do: q | |
| 222 | + | |
| 223 | + defp filter_text(q, text) do | |
| 224 | + where( | |
| 225 | + q, | |
| 226 | + [i], | |
| 227 | + fragment("? @@ plainto_tsquery('simple', ?)", i.search_vector, ^text) or | |
| 228 | + ilike(i.title, ^"%#{escape_like(text)}%") or | |
| 229 | + ilike(i.culprit, ^"%#{escape_like(text)}%") | |
| 230 | + ) | |
| 231 | + end | |
| 232 | + | |
| 233 | + defp escape_like(text), do: String.replace(text, ~r/([\\%_])/, "\\\\\\1") | |
| 234 | + | |
| 235 | + defp sort(q, :first_seen), do: order_by(q, [i], desc: i.first_seen, desc: i.id) | |
| 236 | + defp sort(q, :count), do: order_by(q, [i], desc: i.count, desc: i.last_seen) | |
| 237 | + defp sort(q, _), do: order_by(q, [i], desc: i.last_seen, desc: i.id) | |
| 238 | + | |
| 239 | + ## Counts | |
| 240 | + | |
| 241 | + @doc """ | |
| 242 | + Events per hour for each issue over the last `hours` (24), oldest hour | |
| 243 | + first: `%{issue_id => [n, ...]}`, for sparklines. | |
| 244 | + """ | |
| 245 | + def sparklines(issue_ids, hours \\ 24) | |
| 246 | + def sparklines([], _hours), do: %{} | |
| 247 | + | |
| 248 | + def sparklines(issue_ids, hours) do | |
| 249 | + buckets = hour_buckets(hours) | |
| 250 | + since = hd(buckets) | |
| 251 | + | |
| 252 | + counts = | |
| 253 | + from(c in "issue_counts", | |
| 254 | + where: c.issue_id in type(^issue_ids, {:array, :binary_id}) and c.hour >= ^since, | |
| 255 | + select: {type(c.issue_id, :binary_id), c.hour, c.count} | |
| 256 | + ) | |
| 257 | + |> Repo.all() | |
| 258 | + |> Enum.group_by(&elem(&1, 0), fn {_, hour, n} -> | |
| 259 | + {NaiveDateTime.truncate(hour, :second), n} | |
| 260 | + end) | |
| 261 | + | |
| 262 | + Map.new(issue_ids, fn id -> | |
| 263 | + by_hour = Map.new(counts[id] || []) | |
| 264 | + {id, Enum.map(buckets, &Map.get(by_hour, &1, 0))} | |
| 265 | + end) | |
| 266 | + end | |
| 267 | + | |
| 268 | + @doc """ | |
| 269 | + Events per hour over the last `hours` (24) for projects, oldest first: | |
| 270 | + `[{hour, n}]` summed across them. For the organization page's chart. | |
| 271 | + """ | |
| 272 | + def project_hourly(project_ids, hours \\ 24) | |
| 273 | + def project_hourly([], hours), do: Enum.map(hour_buckets(hours), &{&1, 0}) | |
| 274 | + | |
| 275 | + def project_hourly(project_ids, hours) do | |
| 276 | + buckets = hour_buckets(hours) | |
| 277 | + | |
| 278 | + by_hour = | |
| 279 | + from(c in "project_counts", | |
| 280 | + where: | |
| 281 | + c.project_id in type(^project_ids, {:array, :binary_id}) and c.hour >= ^hd(buckets), | |
| 282 | + group_by: c.hour, | |
| 283 | + select: {c.hour, type(sum(c.count), :integer)} | |
| 284 | + ) | |
| 285 | + |> Repo.all() | |
| 286 | + |> Map.new(fn {hour, n} -> {NaiveDateTime.truncate(hour, :second), n} end) | |
| 287 | + | |
| 288 | + Enum.map(buckets, &{&1, Map.get(by_hour, &1, 0)}) | |
| 289 | + end | |
| 290 | + | |
| 291 | + @doc "Events in the last 24 hours per project: `%{project_id => n}`." | |
| 292 | + def project_counts_24h(project_ids) do | |
| 293 | + since = hd(hour_buckets(24)) | |
| 294 | + | |
| 295 | + from(c in "project_counts", | |
| 296 | + where: c.project_id in type(^project_ids, {:array, :binary_id}) and c.hour >= ^since, | |
| 297 | + group_by: c.project_id, | |
| 298 | + select: {type(c.project_id, :binary_id), type(sum(c.count), :integer)} | |
| 299 | + ) | |
| 300 | + |> Repo.all() | |
| 301 | + |> Map.new() | |
| 302 | + end | |
| 303 | + | |
| 304 | + # The start of each of the last `hours` hours (naive UTC, as stored). | |
| 305 | + defp hour_buckets(hours) do | |
| 306 | + now = NaiveDateTime.utc_now() | |
| 307 | + this_hour = %{NaiveDateTime.truncate(now, :second) | minute: 0, second: 0} | |
| 308 | + for h <- (hours - 1)..0//-1, do: NaiveDateTime.add(this_hour, -h, :hour) | |
| 309 | + end | |
| 310 | + | |
| 311 | + @doc "The latest issues across projects, for the organization page." | |
| 312 | + def recent_across(project_ids, limit \\ 10) do | |
| 313 | + from(i in Issue, | |
| 314 | + where: i.project_id in ^project_ids and is_nil(i.deleted_at) and i.status == :unresolved, | |
| 315 | + order_by: [desc: i.last_seen], | |
| 316 | + limit: ^limit, | |
| 317 | + preload: :project | |
| 318 | + ) | |
| 319 | + |> Repo.all() | |
| 320 | + end | |
| 321 | + | |
| 322 | + ## Events | |
| 323 | + | |
| 55 | 324 | @doc "The latest event of an issue, or nil." |
| 56 | 325 | def latest_event(%Issue{id: issue_id}) do |
| 57 | 326 | Repo.one( |
| 59 | 328 | ) |
| 60 | 329 | end |
| 61 | 330 | |
| 331 | + @doc "An issue's event by its (UUIDv7) id, or nil." | |
| 332 | + def get_event(%Issue{id: issue_id}, event_id) do | |
| 333 | + with {:ok, id} <- Ecto.UUID.cast(event_id) do | |
| 334 | + Repo.one(from e in IssueEvent, where: e.issue_id == ^issue_id and e.id == ^id) | |
| 335 | + else | |
| 336 | + _ -> nil | |
| 337 | + end | |
| 338 | + end | |
| 339 | + | |
| 62 | 340 | @doc "An issue's events, newest first." |
| 63 | 341 | def list_events(%Issue{id: issue_id}, limit \\ 50) do |
| 64 | 342 | Repo.all( |
| 66 | 344 | ) |
| 67 | 345 | end |
| 68 | 346 | |
| 347 | + @doc """ | |
| 348 | + Where an event sits among its issue's events: the ids of the `newest`, | |
| 349 | + `oldest`, `newer` and `older` ones (nil at the ends), and its position | |
| 350 | + `index` (1 = newest) of `total`. | |
| 351 | + """ | |
| 352 | + def event_nav(%Issue{id: issue_id}, %IssueEvent{id: id}) do | |
| 353 | + base = from(e in IssueEvent, where: e.issue_id == ^issue_id, select: e.id) | |
| 354 | + | |
| 355 | + %{ | |
| 356 | + newest: Repo.one(base |> order_by(desc: :id) |> limit(1)), | |
| 357 | + oldest: Repo.one(base |> order_by(asc: :id) |> limit(1)), | |
| 358 | + newer: Repo.one(base |> where([e], e.id > ^id) |> order_by(asc: :id) |> limit(1)), | |
| 359 | + older: Repo.one(base |> where([e], e.id < ^id) |> order_by(desc: :id) |> limit(1)), | |
| 360 | + index: | |
| 361 | + Repo.aggregate( | |
| 362 | + from(e in IssueEvent, where: e.issue_id == ^issue_id and e.id >= ^id), | |
| 363 | + :count | |
| 364 | + ), | |
| 365 | + total: Repo.aggregate(from(e in IssueEvent, where: e.issue_id == ^issue_id), :count) | |
| 366 | + } | |
| 367 | + end | |
| 368 | + | |
| 69 | 369 | @doc "Tag counts for an issue over its stored days: `%{key => [{value, count}]}`, top 10 each." |
| 70 | 370 | def tag_facets(%Issue{id: issue_id}) do |
| 71 | 371 | from(t in "issue_tags", |
| 78 | 378 | |> Map.new(fn {k, vs} -> {k, vs |> Enum.sort_by(&elem(&1, 1), :desc) |> Enum.take(10)} end) |
| 79 | 379 | end |
| 80 | 380 | |
| 381 | + @doc "Feedback left about an issue's events, newest first." | |
| 382 | + def list_user_reports(%Issue{id: issue_id}) do | |
| 383 | + Repo.all(from r in UserReport, where: r.issue_id == ^issue_id, order_by: [desc: r.id]) | |
| 384 | + end | |
| 385 | + | |
| 81 | 386 | ## Triage |
| 82 | 387 | |
| 83 | 388 | @doc """ |
| 85 | 390 | from any other release reopens it; without, any new event does. |
| 86 | 391 | """ |
| 87 | 392 | def resolve(%Scope{} = scope, %Issue{} = issue, release \\ nil) do |
| 88 | − set_status(scope, issue, | |
| 393 | + set(scope, issue, "event:write", | |
| 89 | 394 | status: :resolved, |
| 90 | 395 | resolved_at: DateTime.utc_now(:second), |
| 91 | 396 | resolved_in_release_id: release && release.id |
| 92 | 397 | ) |
| 93 | 398 | end |
| 94 | 399 | |
| 400 | + @doc "Resolved in the issue's latest release (the one its last event came from), if it has one." | |
| 401 | + def resolve_in_latest_release(%Scope{} = scope, %Issue{} = issue) do | |
| 402 | + release = issue.last_release_id && Repo.get(Release, issue.last_release_id) | |
| 403 | + resolve(scope, issue, release) | |
| 404 | + end | |
| 405 | + | |
| 95 | 406 | @doc "Ignores an issue: its events still count, but it never reopens." |
| 96 | 407 | def ignore(%Scope{} = scope, %Issue{} = issue), |
| 97 | − do: set_status(scope, issue, status: :ignored, resolved_at: nil, resolved_in_release_id: nil) | |
| 408 | + do: | |
| 409 | + set(scope, issue, "event:write", | |
| 410 | + status: :ignored, | |
| 411 | + resolved_at: nil, | |
| 412 | + resolved_in_release_id: nil | |
| 413 | + ) | |
| 98 | 414 | |
| 99 | 415 | @doc "Back to unresolved." |
| 100 | 416 | def unresolve(%Scope{} = scope, %Issue{} = issue), |
| 101 | 417 | do: |
| 102 | − set_status(scope, issue, status: :unresolved, resolved_at: nil, resolved_in_release_id: nil) | |
| 418 | + set(scope, issue, "event:write", | |
| 419 | + status: :unresolved, | |
| 420 | + resolved_at: nil, | |
| 421 | + resolved_in_release_id: nil | |
| 422 | + ) | |
| 103 | 423 | |
| 104 | − defp set_status(scope, issue, changes) do | |
| 105 | − with :ok <- Organizations.authorize(scope, "event:write"), | |
| 424 | + @doc "Assigns an issue to a member of its organization, or to nobody (`nil`)." | |
| 425 | + def assign(%Scope{} = scope, %Issue{} = issue, nil), | |
| 426 | + do: set(scope, issue, "event:write", assignee_id: nil) | |
| 427 | + | |
| 428 | + def assign(%Scope{} = scope, %Issue{} = issue, user_id) do | |
| 429 | + if Repo.exists?( | |
| 430 | + from m in Member, | |
| 431 | + where: m.organization_id == ^issue.organization_id and m.user_id == ^user_id | |
| 432 | + ), | |
| 433 | + do: set(scope, issue, "event:write", assignee_id: user_id), | |
| 434 | + else: {:error, :not_a_member} | |
| 435 | + end | |
| 436 | + | |
| 437 | + @doc "Makes an issue's share page public, or private again." | |
| 438 | + def set_public(%Scope{} = scope, %Issue{} = issue, public?) when is_boolean(public?), | |
| 439 | + do: set(scope, issue, "event:write", is_public: public?) | |
| 440 | + | |
| 441 | + @doc "Removes an issue (soft; its events are swept with retention). Needs `event:admin`." | |
| 442 | + def delete(%Scope{} = scope, %Issue{} = issue), | |
| 443 | + do: set(scope, issue, "event:admin", deleted_at: DateTime.utc_now(:second)) | |
| 444 | + | |
| 445 | + @doc """ | |
| 446 | + One action on many issues of a project: `"resolve"`, `"ignore"`, | |
| 447 | + `"unresolve"`, `"delete"`, or `{"assign", user_id | nil}`. Returns how | |
| 448 | + many changed. | |
| 449 | + """ | |
| 450 | + def bulk(%Scope{} = scope, project_id, issue_ids, action) do | |
| 451 | + issues = | |
| 452 | + Repo.all( | |
| 453 | + from i in Issue, | |
| 454 | + where: i.project_id == ^project_id and i.id in ^issue_ids and is_nil(i.deleted_at) | |
| 455 | + ) | |
| 456 | + | |
| 457 | + Enum.count(issues, fn issue -> | |
| 458 | + result = | |
| 459 | + case action do | |
| 460 | + "resolve" -> resolve(scope, issue) | |
| 461 | + "ignore" -> ignore(scope, issue) | |
| 462 | + "unresolve" -> unresolve(scope, issue) | |
| 463 | + "delete" -> delete(scope, issue) | |
| 464 | + {"assign", user_id} -> assign(scope, issue, user_id) | |
| 465 | + _ -> {:error, :unknown_action} | |
| 466 | + end | |
| 467 | + | |
| 468 | + match?({:ok, _}, result) | |
| 469 | + end) | |
| 470 | + end | |
| 471 | + | |
| 472 | + defp set(scope, issue, api_scope, changes) do | |
| 473 | + with :ok <- Organizations.authorize(scope, api_scope), | |
| 106 | 474 | true <- issue.organization_id == scope.organization.id || {:error, :not_found} do |
| 107 | 475 | issue |
| 108 | 476 | |> Ecto.Changeset.change(changes) |
| 109 | 477 | |> Repo.update() |
| 110 | 478 | |> tap(fn |
| 111 | − {:ok, issue} -> | |
| 112 | − Phoenix.PubSub.broadcast( | |
| 113 | − Vigil.PubSub, | |
| 114 | − "project:#{issue.project_id}:issues", | |
| 115 | − {:issue_status, %{issue_id: issue.id, status: issue.status}} | |
| 116 | − ) | |
| 117 | − | |
| 118 | − _ -> | |
| 119 | − :ok | |
| 479 | + {:ok, issue} -> broadcast(issue, {:issue_updated, %{issue_id: issue.id}}) | |
| 480 | + _ -> :ok | |
| 120 | 481 | end) |
| 121 | 482 | end |
| 122 | 483 | end |
| 123 | 484 | |
| 485 | + defp broadcast(issue, message) do | |
| 486 | + Phoenix.PubSub.broadcast(Vigil.PubSub, "project:#{issue.project_id}:issues", message) | |
| 487 | + Phoenix.PubSub.broadcast(Vigil.PubSub, "issue:#{issue.id}", message) | |
| 488 | + end | |
| 489 | + | |
| 490 | + @doc "Members an issue can be assigned to: `[%User{}]`, by email." | |
| 491 | + def assignable_users(organization_id) do | |
| 492 | + Repo.all( | |
| 493 | + from u in User, | |
| 494 | + join: m in Member, | |
| 495 | + on: m.user_id == u.id, | |
| 496 | + where: m.organization_id == ^organization_id, | |
| 497 | + order_by: u.email | |
| 498 | + ) | |
| 499 | + end | |
| 500 | + | |
| 501 | + ## Comments | |
| 502 | + | |
| 503 | + def list_comments(%Issue{id: issue_id}) do | |
| 504 | + Repo.all(from c in Comment, where: c.issue_id == ^issue_id, order_by: c.id, preload: :user) | |
| 505 | + end | |
| 506 | + | |
| 507 | + def change_comment(attrs \\ %{}), do: Comment.changeset(%Comment{}, attrs) | |
| 508 | + | |
| 509 | + @doc "Needs `event:write`." | |
| 510 | + def add_comment(%Scope{} = scope, %Issue{} = issue, attrs) do | |
| 511 | + with :ok <- Organizations.authorize(scope, "event:write") do | |
| 512 | + %Comment{issue_id: issue.id, user_id: scope.user.id} | |
| 513 | + |> Comment.changeset(attrs) | |
| 514 | + |> Repo.insert() | |
| 515 | + |> tap(fn | |
| 516 | + {:ok, c} -> broadcast(issue, {:issue_comment, %{issue_id: issue.id, comment_id: c.id}}) | |
| 517 | + _ -> :ok | |
| 518 | + end) | |
| 519 | + end | |
| 520 | + end | |
| 521 | + | |
| 522 | + @doc """ | |
| 523 | + Its author may delete a comment, and so may admins and above | |
| 524 | + (`project:admin`; members hold `event:admin` in GlitchTip's scopes, so | |
| 525 | + that one would let any member delete anyone's). | |
| 526 | + """ | |
| 527 | + def delete_comment(%Scope{} = scope, %Issue{} = issue, comment_id) do | |
| 528 | + with {:ok, id} <- Ecto.UUID.cast(comment_id), | |
| 529 | + %Comment{} = comment <- Repo.get_by(Comment, id: id, issue_id: issue.id), | |
| 530 | + true <- comment.user_id == scope.user.id or Scope.can?(scope, "project:admin") do | |
| 531 | + Repo.delete(comment) | |
| 532 | + else | |
| 533 | + false -> {:error, :forbidden} | |
| 534 | + _ -> {:error, :not_found} | |
| 535 | + end | |
| 536 | + end | |
| 537 | + | |
| 124 | 538 | ## Grouping preview |
| 125 | 539 | |
| 126 | 540 | @doc """ |
added
lib/vigil/issues/comment.ex
+23
−0
| 1 | +defmodule Vigil.Issues.Comment do | |
| 2 | + @moduledoc "A note on an issue, by a member of its organization." | |
| 3 | + use Vigil.Schema | |
| 4 | + import Ecto.Changeset | |
| 5 | + | |
| 6 | + schema "issue_comments" do | |
| 7 | + field :text, :string | |
| 8 | + | |
| 9 | + belongs_to :issue, Vigil.Issues.Issue | |
| 10 | + belongs_to :user, Vigil.Accounts.User | |
| 11 | + | |
| 12 | + timestamps(type: :utc_datetime) | |
| 13 | + end | |
| 14 | + | |
| 15 | + @doc false | |
| 16 | + def changeset(comment, attrs) do | |
| 17 | + comment | |
| 18 | + |> cast(attrs, [:text]) | |
| 19 | + |> update_change(:text, &String.trim/1) | |
| 20 | + |> validate_required([:text]) | |
| 21 | + |> validate_length(:text, max: 10_000) | |
| 22 | + end | |
| 23 | +end |
modified
lib/vigil/issues/issue.ex
+1
−0
| 28 | 28 | belongs_to :resolved_in_release, Vigil.Releases.Release |
| 29 | 29 | belongs_to :first_release, Vigil.Releases.Release |
| 30 | 30 | belongs_to :last_release, Vigil.Releases.Release |
| 31 | + belongs_to :assignee, Vigil.Accounts.User | |
| 31 | 32 | has_many :hashes, Vigil.Issues.IssueHash |
| 32 | 33 | |
| 33 | 34 | timestamps(type: :utc_datetime) |
added
lib/vigil_web/live/issue_live/index.ex
+16
−0
| 1 | +defmodule VigilWeb.IssueLive.Index do | |
| 2 | + @moduledoc "Placeholder; replaced in slice 3." | |
| 3 | + use VigilWeb, :live_view | |
| 4 | + | |
| 5 | + @impl true | |
| 6 | + def mount(_params, _session, socket), do: {:ok, socket} | |
| 7 | + | |
| 8 | + @impl true | |
| 9 | + def render(assigns) do | |
| 10 | + ~H""" | |
| 11 | + <Layouts.app flash={@flash} current_scope={@current_scope}> | |
| 12 | + <p id="placeholder">Coming in slice 3.</p> | |
| 13 | + </Layouts.app> | |
| 14 | + """ | |
| 15 | + end | |
| 16 | +end |
added
lib/vigil_web/live/issue_live/show.ex
+16
−0
| 1 | +defmodule VigilWeb.IssueLive.Show do | |
| 2 | + @moduledoc "Placeholder; replaced in slice 3." | |
| 3 | + use VigilWeb, :live_view | |
| 4 | + | |
| 5 | + @impl true | |
| 6 | + def mount(_params, _session, socket), do: {:ok, socket} | |
| 7 | + | |
| 8 | + @impl true | |
| 9 | + def render(assigns) do | |
| 10 | + ~H""" | |
| 11 | + <Layouts.app flash={@flash} current_scope={@current_scope}> | |
| 12 | + <p id="placeholder">Coming in slice 3.</p> | |
| 13 | + </Layouts.app> | |
| 14 | + """ | |
| 15 | + end | |
| 16 | +end |
modified
lib/vigil_web/live/project_live/new.ex
+1
−1
Click to load diff…
added
lib/vigil_web/live/share_live/issue.ex
+16
−0
Click to load diff…
modified
lib/vigil_web/router.ex
+11
−1
Click to load diff…
added
priv/repo/migrations/20261201222444_add_issue_triage.exs
+23
−0
Click to load diff…
added
test/vigil/issues_test.exs
+152
−0
Click to load diff…
modified
test/vigil_web/live/org_live_test.exs
+1
−1
Click to load diff…
Parents: 1a4b866