╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
101 theirs_picks: []const u8 = "", 102 // the view the page shows initially (a selected issue's status overrides the route's) 103 view: ui.RoutablePage.IssuesView, 104 // the /description page: the detail pane shows the selected issue's whole 105 // description behind a back link. 106 description_page: bool = false, 107 // every label in the repo, in sorted order, for the labels view. 108 labels: []const []const u8, 109 // the on-disk repo this page was read from, for the terminal submit path 110 // (the web posts the new-issue form to the issue route instead). 111 repo_source: ?ui.RepoSource = null, 112 113 const Self = @This(); 114 115 pub const Event = evt.Issue; 116 pub const Status = evt.Issue.Status; 117 pub const ViewKind = ui.RoutablePage.IssuesView; 118 pub const thread_name = "issue"; 119 pub const header_widget_name = "repo_issues_header"; 120 121 pub fn statusKind(event: Event) Status { 122 return event.status; 123 } 124 125 pub fn listRoute(identity: []const u8, status: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 126 return ui.RoutablePage.repoIssuesRoute(identity, status, label, selected); 127 } 128 129 pub fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 130 return ui.RoutablePage.repoIssuesConflictsRoute(identity, selected); 131 } 132 133 pub fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 134 return ui.RoutablePage.repoIssuesResolveRoute(identity, selected, picks); 135 } 136 137 // `status`'s windowed listing. 138 pub fn window(self: *const Self, status: evt.Issue.Status) *const Window { 139 return switch (status) { 140 .open => &self.open, 141 .closed => &self.closed, 142 }; 143 } 144 145 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 146 // theirs: picks. 147 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 148 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 149 while (it.next()) |pick| { 150 if (std.mem.eql(u8, pick, field)) return true; 151 } 152 return false; 153 } 154 155 // the issue `selected_id` names, in whichever window holds it. 156 pub fn selectedThread(self: *const Self) ?*const IssueWithId { 157 if (self.selected_id.len == 0) return null; 158 for ([_]*const Window{ &self.open, &self.closed }) |win| { 159 for (win.items) |*entry| { 160 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 161 } 162 } 163 return null; 164 } 165 166 // an empty listing, for the wasm / no-repo paths. 167 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, label: []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 { 168 return .{ 169 .identity = try aa.dupe(u8, identity), 170 .label = try aa.dupe(u8, label), 171 .search = if (search.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, search)), 172 .selected_id = try aa.dupe(u8, selected_id), 173 .comment_id = try aa.dupe(u8, comment_id), 174 .comments_start = comments_start, 175 .open = .empty, 176 .closed = .empty, 177 .conflicts = .empty, 178 .theirs_picks = try aa.dupe(u8, theirs_picks), 179 // a description url shows a status list, the issue's own status 180 // picking it once the issue is read; conflicts and resolve urls also 181 // start there, upgraded by init when the conflict data exists. 182 .view = switch (view) { 183 .description, .conflicts, .resolve => .open, 184 else => view, 185 }, 186 .description_page = view == .description, 187 .labels = &.{}, 188 }; 189 } 190 191 // read one window per status of an opened repo's issues (filtered to `label` 192 // when set), ordered by creation (newest first). the window of the issue 193 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 194 // event db next to it (synced from the events branch on each page build); a 195 // xit repo reads its own db. 196 pub fn init( 197 comptime repo_kind: rp.RepoKind, 198 comptime repo_opts: rp.RepoOpts(repo_kind), 199 arena: *std.heap.ArenaAllocator, 200 repo: *rp.Repo(repo_kind, repo_opts), 201 io: std.Io, 202 // the admin db's moment, for resolving author emails to user names (null 203 // in local mode, which has no users) 204 admin_moment: ?evt.AdminDB.HashMap(.read_only), 205 identity: []const u8, 206 label: []const u8, 207 search: []const u8, 208 selected_id: []const u8, 209 comment_id: []const u8, 210 comments_start: usize, 211 theirs_picks: []const u8, 212 view: ui.RoutablePage.IssuesView, 213 viewer: ?ui.Viewer, 214 ) !Self { 215 const empty = try emptyResult(arena.allocator(), identity, label, search, selected_id, comment_id, comments_start, theirs_picks, view); 216 217 const aa = arena.allocator(); 218 const DB = evt.EventDB(repo_opts.hash); 219 const rooted = empty.selected_id.len != 0; 220 const labeled = empty.label.len != 0; 221 // an explicitly named issue or label that doesn't exist is a bad url 222 // (NotFound -> 404); the bare route falls through to an empty listing. 223 const strict = rooted or labeled; 224 225 // a repo with no consumed events has no moment yet. 226 const gpa = arena.child_allocator; 227 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; 228 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 229 const haxy_moment = (if (event_db_maybe) |*event_db| 230 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 231 else if (repo_kind == .git) 232 return empty 233 else 234 evt.currentMoment(repo_opts, repo)) catch { 235 if (strict) return error.NotFound; 236 return empty; 237 }; 238 239 // the sorted sets to window, per status: the label's sets when filtered, 240 // else the top-level per-status sets. a missing set is an empty window. 241 var open_set: ?DB.SortedSet(.read_only) = null; 242 var closed_set: ?DB.SortedSet(.read_only) = null; 243 if (labeled) { 244 const label_to_issues_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.label_status_to_id_set_key)) orelse return error.NotFound; 245 const label_to_issues = try DB.SortedMap(.read_only).init(label_to_issues_cursor); 246 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.label)); 247 open_set = try thread.labelStatusSet(Self, DB, label_to_issues, decoded, .open); 248 closed_set = try thread.labelStatusSet(Self, DB, label_to_issues, decoded, .closed); 249 // a label no issue carries is a bad url 250 if (open_set == null and closed_set == null) return error.NotFound; 251 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.status_to_id_set_key))) |status_to_issues_cursor| { 252 const status_to_issues = try DB.SortedMap(.read_only).init(status_to_issues_cursor); 253 open_set = try thread.statusSet(Self, DB, status_to_issues, .open); 254 closed_set = try thread.statusSet(Self, DB, status_to_issues, .closed); 255 } else if (rooted) return error.NotFound; 256 257 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse { 258 if (strict) return error.NotFound; 259 return empty; 260 }; 261 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 262 263 // the conflicted issues' container: a set view for membership and 264 // windowing, a map view for the resolve entry, over the same cursor. 265 var conflict_set: ?DB.SortedSet(.read_only) = null; 266 var conflicts_map: ?DB.SortedMap(.read_only) = null; 267 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.conflicts_key))) |cursor| { 268 conflict_set = try DB.SortedSet(.read_only).init(cursor); 269 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 270 } 271 272 // a named issue roots its own status's window at itself (the conflicts 273 // view's window instead); the other windows start at the beginning. 274 var resolved_view = empty.view; 275 var open_root: ?[]const u8 = null; 276 var closed_root: ?[]const u8 = null; 277 var conflicts_root: ?[]const u8 = null; 278 var conflict_data: ?Conflict = null; 279 if (rooted) { 280 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 281 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 282 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 283 const issue_event = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, issue_map); 284 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(issue_event.created_order, &id_bytes)); 285 286 if (view == .conflicts) { 287 // the id roots the conflicts window, so it must be conflicted 288 const set = conflict_set orelse return error.NotFound; 289 if (!try set.contains(order_key)) return error.NotFound; 290 conflicts_root = order_key; 291 } else { 292 // the named issue must be in its windowed set (a label url can name 293 // an issue that doesn't carry the label). 294 const set = (switch (issue_event.event.status) { 295 .open => open_set, 296 .closed => closed_set, 297 }) orelse return error.NotFound; 298 if (!try set.contains(order_key)) return error.NotFound; 299 300 switch (issue_event.event.status) { 301 .open => open_root = order_key, 302 .closed => closed_root = order_key, 303 } 304 // form urls keep their view; otherwise the issue's status picks it. 305 if (view != .edit and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (issue_event.event.status) { 306 .open => .open, 307 .closed => .closed, 308 }; 309 310 // a resolve url shows the resolve view only while the issue still 311 // has a conflict entry; otherwise the status list stands. the 312 // wasm client renders from the snapshot, so the diff machinery 313 // this pulls in is gated out of its build. 314 if (comptime !wasm) { 315 if (view == .resolve) { 316 if (conflicts_map) |map| { 317 if (try map.getCursor(order_key)) |conflict_cursor| { 318 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 319 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, issue_event, conflict_entry); 320 resolved_view = .resolve; 321 } 322 } 323 } 324 } 325 } 326 } 327 328 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 329 const open_window = try thread.loadWindow(Self, .issue, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, open_set, open_root, conflict_set, empty.selected_id, thread_comments_start, empty.search, viewer); 330 const closed_window = try thread.loadWindow(Self, .issue, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, closed_set, closed_root, conflict_set, empty.selected_id, thread_comments_start, empty.search, viewer); 331 const conflicts_window = try thread.loadWindow(Self, .issue, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, conflict_set, conflicts_root, conflict_set, empty.selected_id, thread_comments_start, null, viewer); 332 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 333 334 const comment_page = if (empty.comment_id.len == 0) 335 null 336 else 337 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start, viewer); 338 339 const labels = try thread.loadLabels(Self, repo_opts.hash, arena, haxy_moment); 340 341 return .{ 342 .identity = empty.identity, 343 .label = empty.label, 344 .search = empty.search, 345 .selected_id = empty.selected_id, 346 .comment_id = empty.comment_id, 347 .comments_start = comments_start, 348 .comment_page = comment_page, 349 .viewer = viewer, 350 .open = open_window, 351 .closed = closed_window, 352 .conflicts = conflicts_window, 353 .conflict = conflict_data, 354 .theirs_picks = empty.theirs_picks, 355 .view = resolved_view, 356 .description_page = empty.description_page, 357 .labels = labels, 358 }; 359 } 360 361 pub const View = thread.View(.issue, Self); 362 pub const Detail = thread.Detail(.issue, Self); 363 pub const detail_widget_name = "repo_issue_detail"; 364 365 pub const Header = thread.Header; 366 367 const open_tab_label = "open"; 368 const closed_tab_label = "closed"; 369 const labels_tab_label = "labels"; 370 const edit_tab_label = "edit"; 371 const reply_tab_label = "reply"; 372 const remove_tab_label = "remove"; 373 const resolve_tab_label = "resolve"; 374 const new_tab_label = "new"; 375 const conflicts_tab_label = "conflicts"; 376 377 // tabs switching between the issues page's views 378 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 379 var header = try Header.init(allocator, session, data.search); 380 errdefer header.deinit(allocator); 381 const aa = session.page_arena.allocator(); 382 const selected_index = View.viewIndex(data.view); 383 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_issues; 384 385 // a list tab per status, labeled with its listing's issue count 386 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label }; 387 for ([_]evt.Issue.Status{ .open, .closed }, status_labels, 0..) |status, status_label, index| { 388 const route = try thread.searchRoute(ui.RoutablePage.repoIssuesRoute(data.identity, status, data.label, ""), data.search); 389 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 390 var label_buf: [64]u8 = undefined; 391 const label = try thread.countLabel(&label_buf, status_label, data.window(status).*); 392 try header.addTab(allocator, label, link, index); 393 } 394 395 // labels tab, showing the active label filter 396 { 397 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.issue, data.identity, data.label), data.search); 398 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 399 const label = if (data.label.len == 0) labels_tab_label else blk: { 400 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.label)); 401 break :blk try std.fmt.allocPrint(aa, labels_tab_label ++ " ({s})", .{decoded}); 402 }; 403 try header.addTab(allocator, label, labels_link, View.viewIndex(.labels)); 404 } 405 406 // new-issue tab; an edit or resolve url shows its tab in this place 407 { 408 const route = switch (data.view) { 409 .edit => ui.RoutablePage.repoThreadEditRoute(.issue, data.identity, data.selected_id) orelse return error.RouteTooLong, 410 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 411 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 412 .remove => ui.RoutablePage.repoThreadRemoveRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 413 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 414 else => ui.RoutablePage.repoThreadNewRoute(.issue, data.identity) orelse return error.RouteTooLong, 415 }; 416 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 417 const label: []const u8 = switch (data.view) { 418 .edit => edit_tab_label, 419 .new_comment => reply_tab_label, 420 .edit_comment => edit_tab_label, 421 .remove => remove_tab_label, 422 .resolve => resolve_tab_label, 423 else => new_tab_label, 424 }; 425 try header.addTab(allocator, label, link, View.viewIndex(.new)); 426 } 427 428 // conflicts tab, labeled with the conflict count 429 if (data.conflicts.count > 0) { 430 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 431 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 432 var label_buf: [64]u8 = undefined; 433 const label = try std.fmt.bufPrint(&label_buf, conflicts_tab_label ++ " ({d})", .{data.conflicts.count}); 434 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 435 } 436 437 header.select(View.viewIndex(data.view)); 438 return header; 439 } 440