╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
25 id: []const u8, 26 record: evt.Issue.Record, 27 // the creation commit's author 28 author: ui.Author = .unknown, 29 // whether the issue has an unresolved merge conflict 30 conflicted: bool = false, 31 comments: Comment.Window = .empty, 32 attachments: []const Attachment.WithId = &.{}, 33 assignees: []const ui.Assignee = &.{}, 34 // whether the viewer may change the thread, computed here so the web ui has it 35 can_modify: bool = false, 36 // the viewer's notify button, null when hidden 37 notify_action: ?ui.NotifyAction = null, 38 }; 39 40 pub const Entry = IssueWithId; 41 42 // one side of a conflicted field: its value and who set it 43 pub const Side = struct { 44 text: []const u8, 45 author: ui.Author = .unknown, 46 }; 47 48 pub const FieldConflict = struct { 49 ours: Side, 50 theirs: Side, 51 }; 52 53 // the selected issue's conflict, read for the resolve view. ours is the live 54 // record; the chunks split the description against the merge base. 55 pub const Conflict = struct { 56 title: ?FieldConflict = null, 57 labels: ?FieldConflict = null, 58 description: ?struct { 59 chunks: []const diff3.Chunk, 60 ours_author: ui.Author = .unknown, 61 theirs_author: ui.Author = .unknown, 62 } = null, 63 }; 64 65 // one status's windowed listing. 66 pub const Window = struct { 67 items: []const IssueWithId, 68 // the id of the previous window's first issue, or null when this window 69 // is already the first. 70 prev_id: ?[]const u8, 71 // the id of the next window's first issue, or null when this is the last 72 // window. 73 next_id: ?[]const u8, 74 // how many issues the listing holds across all windows. 75 count: usize, 76 // whether a search stopped counting at its cap, so `count` is a floor. 77 more: bool = false, 78 79 pub const empty: Window = .{ .items = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 80 }; 81 82 // "owner:name", so the view can build /owner:name/issues/... links. 83 identity: []const u8, 84 // the decoded query the lists are filtered to, or null when unfiltered. 85 search: ?[]const u8 = null, 86 // whether an assignee term names no user, which lists nothing 87 assignee_unknown: bool = false, 88 // the hex event id of the issue its status's window is rooted at ("" = the 89 // first window), mirrored into the url. 90 selected_id: []const u8, 91 // the comment shown in the selected issue's detail pane. 92 comment_id: []const u8, 93 // the selected issue's comment window (0 = the first window). 94 comments_start: usize, 95 // the selected comment and its immediate replies. 96 comment_page: ?Comment.Permalink = null, 97 // who the page is built for, null when logged out 98 viewer: ?ui.Viewer = null, 99 open: Window, 100 closed: Window, 101 // the conflicted issues' listing; its count also gates the conflicts tab. 102 conflicts: Window, 103 // the selected issue's conflict, set when the page shows the resolve view. 104 conflict: ?Conflict = null, 105 // the resolve view's comma-separated fields prefilled from their side, 106 // mirrored from the url. 107 theirs_picks: []const u8 = "", 108 // the view the page shows initially (a selected issue's status overrides the route's) 109 view: ui.RoutablePage.IssuesView, 110 // the /description page: the detail pane shows the selected issue's whole 111 // description behind a back link. 112 description_page: bool = false, 113 // every label in the repo, in sorted order, for the labels view. 114 labels: []const []const u8, 115 // the on-disk repo this page was read from, for the terminal submit path 116 // (the web posts the new-issue form to the issue route instead). 117 repo_source: ?ui.RepoSource = null, 118 119 const Self = @This(); 120 121 pub const Event = evt.Issue; 122 pub const Status = evt.Issue.Status; 123 pub const ViewKind = ui.RoutablePage.IssuesView; 124 pub const thread_name = "issue"; 125 pub const header_widget_name = "repo_issues_header"; 126 127 pub fn statusKind(event: Event) Status { 128 return event.status; 129 } 130 131 pub fn listRoute(identity: []const u8, status: Status, selected: []const u8) ?ui.RoutablePage { 132 return ui.RoutablePage.repoIssuesRoute(identity, status, selected); 133 } 134 135 pub fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 136 return ui.RoutablePage.repoIssuesConflictsRoute(identity, selected); 137 } 138 139 pub fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 140 return ui.RoutablePage.repoIssuesResolveRoute(identity, selected, picks); 141 } 142 143 // `status`'s windowed listing. 144 pub fn window(self: *const Self, status: evt.Issue.Status) *const Window { 145 return switch (status) { 146 .open => &self.open, 147 .closed => &self.closed, 148 }; 149 } 150 151 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 152 // theirs: picks. 153 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 154 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 155 while (it.next()) |pick| { 156 if (std.mem.eql(u8, pick, field)) return true; 157 } 158 return false; 159 } 160 161 // the issue `selected_id` names, in whichever window holds it. 162 pub fn selectedThread(self: *const Self) ?*const IssueWithId { 163 if (self.selected_id.len == 0) return null; 164 for ([_]*const Window{ &self.open, &self.closed }) |win| { 165 for (win.items) |*entry| { 166 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 167 } 168 } 169 return null; 170 } 171 172 // an empty listing, for the wasm / no-repo paths. 173 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, search: []const u8, selected_id: []const u8, comment_id: []const u8, comments_start: usize, theirs_picks: []const u8, view: ui.RoutablePage.IssuesView) !Self { 174 return .{ 175 .identity = try aa.dupe(u8, identity), 176 .search = if (search.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, search)), 177 .selected_id = try aa.dupe(u8, selected_id), 178 .comment_id = try aa.dupe(u8, comment_id), 179 .comments_start = comments_start, 180 .open = .empty, 181 .closed = .empty, 182 .conflicts = .empty, 183 .theirs_picks = try aa.dupe(u8, theirs_picks), 184 // a description url shows a status list, the issue's own status 185 // picking it once the issue is read; conflicts and resolve urls also 186 // start there, upgraded by init when the conflict data exists. 187 .view = switch (view) { 188 .description, .conflicts, .resolve => .open, 189 else => view, 190 }, 191 .description_page = view == .description, 192 .labels = &.{}, 193 }; 194 } 195 196 // read one window per status of an opened repo's issues (filtered to the 197 // search when set), ordered by creation (newest first). the window of 198 // the issue `selected_id` names starts at it ("" = the beginning). a git repo reads the 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