╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
199 // event db next to it (synced from the events branch on each page build); a 200 // xit repo reads its own db. 201 pub fn init( 202 comptime repo_kind: rp.RepoKind, 203 comptime repo_opts: rp.RepoOpts(repo_kind), 204 arena: *std.heap.ArenaAllocator, 205 repo: *rp.Repo(repo_kind, repo_opts), 206 io: std.Io, 207 // the admin db's moment, for resolving author emails to user names (null 208 // in local mode, which has no users) 209 admin_moment: ?evt.AdminDB.HashMap(.read_only), 210 identity: []const u8, 211 search: []const u8, 212 selected_id: []const u8, 213 comment_id: []const u8, 214 comments_start: usize, 215 theirs_picks: []const u8, 216 view: ui.RoutablePage.IssuesView, 217 viewer: ?ui.Viewer, 218 ) !Self { 219 var empty = try emptyResult(arena.allocator(), identity, search, selected_id, comment_id, comments_start, theirs_picks, view); 220 // the early returns below still let the viewer write 221 empty.viewer = viewer; 222 223 const aa = arena.allocator(); 224 const DB = evt.EventDB(repo_opts.hash); 225 // an explicitly named issue that doesn't exist is a bad url (NotFound -> 226 // 404); the bare route falls through to an empty listing. 227 const rooted = empty.selected_id.len != 0; 228 229 // a repo with no consumed events has no moment yet. 230 const gpa = arena.child_allocator; 231 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 232 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 233 const haxy_moment = (if (event_db_maybe) |*event_db| 234 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 235 else if (repo_kind == .git) 236 return empty 237 else 238 evt.currentMoment(repo_opts, repo)) catch { 239 if (rooted) return error.NotFound; 240 return empty; 241 }; 242 243 const filters = try srch_thrd.parseFilters(aa, empty.search orelse ""); 244 245 // a name resolves to its user's email. an unknown name or an email no 246 // issue carries lists nothing. 247 var assignee_sets: ?[]const DB.SortedSet(.read_only) = &.{}; 248 if (filters.assignees.len != 0) { 249 const by_assignee: ?DB.HashMap(.read_only) = if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Assignment.assignee_to_issue_id_set_key))) |cursor| try DB.HashMap(.read_only).init(cursor) else null; 250 var sets: std.ArrayList(DB.SortedSet(.read_only)) = .empty; 251 for (filters.assignees) |assignee| { 252 const email = switch (try ui.assigneeEmail(admin_moment, arena, assignee)) { 253 .email => |email| email, 254 .failure => { 255 empty.assignee_unknown = true; 256 continue; 257 }, 258 }; 259 const set_cursor = (if (by_assignee) |map| try map.getCursor(hash.hashInt(repo_opts.hash, email)) else null) orelse continue; 260 try sets.append(aa, try DB.SortedSet(.read_only).init(set_cursor)); 261 } 262 assignee_sets = if (sets.items.len == filters.assignees.len) sets.items else null; 263 } 264 265 // each status's listing intersects every filter's set 266 const open_sets = try thread.statusSets(Self, repo_opts.hash, aa, haxy_moment, filters.labels, assignee_sets, .open); 267 const closed_sets = try thread.statusSets(Self, repo_opts.hash, aa, haxy_moment, filters.labels, assignee_sets, .closed); 268 269 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse { 270 if (rooted) return error.NotFound; 271 return empty; 272 }; 273 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 274 275 // the conflicted issues' container: a set view for membership and 276 // windowing, a map view for the resolve entry, over the same cursor. 277 var conflict_set: ?DB.SortedSet(.read_only) = null; 278 var conflicts_map: ?DB.SortedMap(.read_only) = null; 279 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.conflicts_key))) |cursor| { 280 conflict_set = try DB.SortedSet(.read_only).init(cursor); 281 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 282 } 283 284 // a named issue roots its own status's window at itself (the conflicts 285 // view's window instead); the other windows start at the beginning. 286 var resolved_view = empty.view; 287 var open_root: ?[]const u8 = null; 288 var closed_root: ?[]const u8 = null; 289 var conflicts_root: ?[]const u8 = null; 290 var conflict_data: ?Conflict = null; 291 if (rooted) { 292 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 293 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 294 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 295 const issue_event = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, issue_map); 296 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(issue_event.created_order, &id_bytes)); 297 298 if (view == .conflicts) { 299 // the id roots the conflicts window, so it must be conflicted 300 const set = conflict_set orelse return error.NotFound; 301 if (!try set.contains(order_key)) return error.NotFound; 302 conflicts_root = order_key; 303 } else { 304 // the named issue must be in every set its window intersects (a 305 // filtered url can name an issue the filters leave out). 306 const sets = switch (issue_event.event.status) { 307 .open => open_sets, 308 .closed => closed_sets, 309 }; 310 if (!try thread.inEverySet(DB, sets, order_key)) return error.NotFound; 311 312 switch (issue_event.event.status) { 313 .open => open_root = order_key, 314 .closed => closed_root = order_key, 315 } 316 // form urls keep their view; otherwise the issue's status picks it. 317 if (view != .edit and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (issue_event.event.status) { 318 .open => .open, 319 .closed => .closed, 320 }; 321 322 // a resolve url shows the resolve view only while the issue still 323 // has a conflict entry; otherwise the status list stands. the 324 // wasm client renders from the snapshot, so the diff machinery 325 // this pulls in is gated out of its build. 326 if (comptime !wasm) { 327 if (view == .resolve) { 328 if (conflicts_map) |map| { 329 if (try map.getCursor(order_key)) |conflict_cursor| { 330 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 331 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, issue_event, conflict_entry); 332 resolved_view = .resolve; 333 } 334 } 335 } 336 } 337 } 338 } 339 340 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 341 const open_window = try thread.loadWindow(Self, .issue, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, open_sets, open_root, conflict_set, empty.selected_id, thread_comments_start, filters.words, viewer); 342 const closed_window = try thread.loadWindow(Self, .issue, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, closed_sets, closed_root, conflict_set, empty.selected_id, thread_comments_start, filters.words, viewer); 343 const conflicts_window = try thread.loadWindow(Self, .issue, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, if (conflict_set) |*set| set[0..1] else &.{}, conflicts_root, conflict_set, empty.selected_id, thread_comments_start, "", viewer); 344 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 345 346 const comment_page = if (empty.comment_id.len == 0) 347 null 348 else 349 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start, viewer); 350 351 const labels = try thread.loadLabels(Self, repo_opts.hash, arena, haxy_moment); 352 353 return .{ 354 .identity = empty.identity, 355 .search = empty.search, 356 .assignee_unknown = empty.assignee_unknown, 357 .selected_id = empty.selected_id, 358 .comment_id = empty.comment_id, 359 .comments_start = comments_start, 360 .comment_page = comment_page, 361 .viewer = viewer, 362 .open = open_window, 363 .closed = closed_window, 364 .conflicts = conflicts_window, 365 .conflict = conflict_data, 366 .theirs_picks = empty.theirs_picks, 367 .view = resolved_view, 368 .description_page = empty.description_page, 369 .labels = labels, 370 }; 371 } 372 373 pub const View = thread.View(.issue, Self); 374 pub const Detail = thread.Detail(.issue, Self); 375 pub const detail_widget_name = "repo_issue_detail"; 376 377 pub const Header = thread.Header; 378 379 const open_tab_label = "open"; 380 const closed_tab_label = "closed"; 381 const labels_tab_label = "labels"; 382 const edit_tab_label = "edit"; 383 const reply_tab_label = "reply"; 384 const remove_tab_label = "remove"; 385 const resolve_tab_label = "resolve"; 386 const new_tab_label = "new"; 387 const conflicts_tab_label = "conflicts"; 388 389 // tabs switching between the issues page's views 390 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 391 var header = try Header.init(allocator, session, data.search, if (data.assignee_unknown) " search (assignee not found) " else " search "); 392 errdefer header.deinit(allocator); 393 const selected_index = View.viewIndex(data.view); 394 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_issues; 395 396 // a list tab per status, labeled with its listing's issue count 397 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label }; 398 for ([_]evt.Issue.Status{ .open, .closed }, status_labels, 0..) |status, status_label, index| { 399 const route = try thread.searchRoute(ui.RoutablePage.repoIssuesRoute(data.identity, status, ""), data.search); 400 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 401 try header.addTab(allocator, status_label, try thread.countLabel(session, data.window(status).*), link, index); 402 } 403 404 // labels tab 405 { 406 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.issue, data.identity), data.search); 407 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 408 try header.addTab(allocator, labels_tab_label, "", labels_link, View.viewIndex(.labels)); 409 } 410 411 // new-issue tab; an edit or resolve url shows its tab in this place 412 { 413 const route = switch (data.view) { 414 .edit => ui.RoutablePage.repoThreadEditRoute(.issue, data.identity, data.selected_id) orelse return error.RouteTooLong, 415 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 416 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 417 .remove => ui.RoutablePage.repoThreadRemoveRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 418 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 419 else => ui.RoutablePage.repoThreadNewRoute(.issue, data.identity) orelse return error.RouteTooLong, 420 }; 421 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 422 const label: []const u8 = switch (data.view) { 423 .edit => edit_tab_label, 424 .new_comment => reply_tab_label, 425 .edit_comment => edit_tab_label, 426 .remove => remove_tab_label, 427 .resolve => resolve_tab_label, 428 else => new_tab_label, 429 }; 430 try header.addTab(allocator, label, "", link, View.viewIndex(.new)); 431 } 432 433 // conflicts tab, labeled with the conflict count 434 if (data.conflicts.count > 0) { 435 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 436 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 437 try header.addTab(allocator, conflicts_tab_label, try session.page_arena.allocator().print("({d})", .{data.conflicts.count}), link, View.viewIndex(.conflicts)); 438 } 439 440 header.select(View.viewIndex(data.view)); 441 return header; 442 } 443