// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! `zfj issue ...`: porcelain commands for working with issues, each a task //! made of one operation or a few. //! //! Their flags are spelled as `fj`'s are, so that instructions written for //! one read the same for the other. `-R /` names the repository; //! without it, the clone's git remote does. //! //! On a terminal, output is for reading: columns for a list, the issue and //! its body as Markdown for `view`, through a pager. Elsewhere a list is one //! issue to a line, tab-separated. `--json` writes Forgejo's own JSON, //! adding nothing to it but a `comments` key to `view --comments`. const std = @import("std"); const Io = std.Io; const zfj = @import("zfj"); const args = @import("args.zig"); const call = @import("call.zig"); const Context = @import("Context.zig"); const Exit = Context.Exit; const porcelain = @import("porcelain.zig"); const Runner = porcelain.Runner; const place = porcelain.place; const collect = porcelain.collect; const writeJson = porcelain.writeJson; const Style = porcelain.Style; const repo_note = porcelain.repo_note; const json_note = porcelain.json_note; const oneLine = porcelain.oneLine; const splitCommas = porcelain.splitCommas; const writeLabels = porcelain.writeLabels; const login = porcelain.login; const day = porcelain.day; const text = @import("text.zig"); pub const commands = [_]porcelain.Command{ Runner("issue", Create).command("create", "open an issue"), Runner("issue", View).command("view", "show an issue, and with --comments its thread"), Runner("issue", List).command("list", "list a repository's issues"), Runner("issue", Comment).command("comment", "comment on an issue"), Runner("issue", Edit).command("edit", "change an issue's title, body, labels or assignees"), Runner("issue", Close).command("close", "close an issue, optionally with a comment saying why"), Runner("issue", Reopen).command("reopen", "reopen a closed issue, optionally with a comment"), Runner("issue", Blockers).command("blockers", "list the issues blocking an issue"), Runner("issue", Block).command("block", "record that one issue blocks another"), Runner("issue", Unblock).command("unblock", "remove that record"), }; pub fn main(ctx: *Context, words: []const []const u8) !Exit { return porcelain.dispatch(ctx, "issue", &commands, words); } const body_note: args.Note = .{ .description = "the text, or - to read it from stdin" }; const body_file_note: args.Note = .{ .description = "read the text from a file, or - for stdin", .complete = .files }; fn labelValues(ctx: *Context, names: []const []const u8) ![]const std.json.Value { const values = try ctx.arena.alloc(std.json.Value, names.len); for (names, values) |name, *value| value.* = .{ .string = name }; return values; } const Create = struct { repo: ?[]const u8 = null, title: []const u8, body: ?[]const u8 = null, @"body-file": ?[]const u8 = null, label: ?[]const []const u8 = null, assignee: ?[]const []const u8 = null, json: bool = false, pub const short = .{ .R = "repo", .t = "title", .b = "body", .F = "body-file", .l = "label", .a = "assignee" }; pub const usage = "create -t [-b <text> | -F <file>] [-l <label>]... [-a <user>]..."; pub const about = "Opens an issue. Without -b or -F, and on a terminal, $EDITOR is opened for the body."; pub const notes = [_]args.Note{ repo_note, .{ .description = "the issue's title" }, body_note, body_file_note, .{ .description = "a label to give it, by name; commas separate several" }, .{ .description = "a user to assign it to; commas separate several" }, json_note, }; pub fn run(ctx: *Context, o: Create) !Exit { const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); const body = text.body(ctx, .{ .body = o.body, .body_file = o.@"body-file" }, .optional, "issue body") catch |e| switch (e) { error.OutOfMemory => return e, else => |f| return Context.exitFor(f), }; const labels = try splitCommas(ctx, o.label); const assignees = try splitCommas(ctx, o.assignee); var created = switch (call.result(ctx, "issueCreateIssue", zfj.issueCreateIssueResult(at.client, .{ .owner = at.owner, .repo = at.repo }, .{ .title = o.title, .body = body, .assignees = if (assignees.len == 0) null else assignees, }))) { .ok => |ok| ok, .failed => |exit| return exit, }; defer created.deinit(); const number = created.value().number orelse 0; // Labels go on by name, which needs an operation of its own: the // one that creates an issue takes them only by id. if (labels.len != 0) { var labelled = switch (call.result(ctx, "issueAddLabel", zfj.issueAddLabelResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = number }, .{ .labels = try labelValues(ctx, labels), }))) { .ok => |ok| ok, .failed => |exit| { ctx.stderr.print("zfj: #{d} was opened, without its labels\n", .{number}) catch {}; ctx.stderr.flush() catch {}; return exit; }, }; labelled.deinit(); } if (o.json) { if (labels.len == 0) return writeJson(ctx, created.body); var fresh = switch (call.result(ctx, "issueGetIssue", zfj.issueGetIssueResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = number }))) { .ok => |ok| ok, .failed => |exit| return exit, }; defer fresh.deinit(); return writeJson(ctx, fresh.body); } try ctx.stdout.print("{s}\n", .{created.value().html_url orelse ""}); return .ok; } }; const View = struct { number: i64, repo: ?[]const u8 = null, comments: bool = false, json: bool = false, web: bool = false, pub const short = .{ .R = "repo", .w = "web" }; pub const positional: []const []const u8 = &.{"number"}; pub const usage = "view <number> [--comments] [--json | --web]"; pub const about = "Shows an issue, and with --comments every comment on it."; pub const notes = [_]args.Note{ .{ .description = "the issue's number" }, repo_note, .{ .description = "show the comment thread too" }, .{ .description = "write Forgejo's JSON for the issue, with its comments as a \"comments\" list when --comments is given" }, .{ .description = "open the issue in the browser instead: $BROWSER, else the system's" }, }; pub fn run(ctx: *Context, o: View) !Exit { if (o.web and (o.json or o.comments)) return ctx.usage("--web opens the issue in the browser, and takes neither --json nor --comments", .{}); const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); var issue = switch (call.result(ctx, "issueGetIssue", zfj.issueGetIssueResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = o.number }))) { .ok => |ok| ok, .failed => |exit| return exit, }; defer issue.deinit(); if (o.web) { const url = issue.value().html_url orelse return ctx.environment("Forgejo gave no web address for #{d}", .{o.number}); return porcelain.openInBrowser(ctx, url); } var comments: ?zfj.Owned([]const zfj.Comment) = null; defer if (comments) |*c| c.deinit(); if (o.comments) { comments = switch (call.result(ctx, "issueGetComments", zfj.issueGetCommentsResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = o.number }))) { .ok => |ok| ok, .failed => |exit| return exit, }; } if (o.json) return porcelain.writeJsonWithComments(ctx, issue.body, if (comments) |c| c.body else null); var out: Io.Writer.Allocating = .init(ctx.arena); const style = Style.of(ctx); try renderIssue(&out.writer, style, issue.value().*); if (comments) |*c| try porcelain.renderComments(&out.writer, style, c.value().*); try ctx.page(out.written()); return .ok; } }; const List = struct { repo: ?[]const u8 = null, state: []const u8 = "open", label: ?[]const []const u8 = null, assignee: ?[]const u8 = null, author: ?[]const u8 = null, mentioned: ?[]const u8 = null, milestone: ?[]const u8 = null, search: ?[]const u8 = null, limit: i64 = 30, page: ?i64 = null, json: bool = false, pub const short = .{ .R = "repo", .s = "state", .l = "label", .a = "assignee", .A = "author", .S = "search", .L = "limit" }; pub const usage = "list [--state open|closed|all] [-l <label>]... [-a <user>] [-S <text>] [--json]"; pub const about = "Lists a repository's issues, newest first; pull requests are left out. The global --paginate fetches every page."; pub const notes = [_]args.Note{ repo_note, .{ .description = "which issues: open, closed or all", .allowed = &.{ "open", "closed", "all" } }, .{ .description = "only issues with this label; given more than once, issues with any of them" }, .{ .description = "only issues assigned to this user" }, .{ .description = "only issues this user opened" }, .{ .description = "only issues mentioning this user" }, .{ .description = "only issues in this milestone, by name or id; commas separate several" }, .{ .description = "only issues matching this text" }, .{ .description = "how many to a page (the instance may give fewer)" }, .{ .description = "which page" }, json_note, }; pub fn run(ctx: *Context, o: List) !Exit { const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); const labels = try splitCommas(ctx, o.label); var params: zfj.IssueListIssuesParams = .{ .owner = at.owner, .repo = at.repo, .state = o.state, .type = "issues", .labels = if (labels.len == 0) null else try std.mem.join(ctx.arena, ",", labels), .q = o.search, .assigned_by = o.assignee, .created_by = o.author, .mentioned_by = o.mentioned, .milestones = o.milestone, .limit = o.limit, .page = o.page, }; const joined = switch (try collect(ctx, "issueListIssues", zfj.issueListIssuesRaw, at.client, ¶ms)) { .ok => |ok| ok, .failed => |exit| return exit, }; if (o.json) return writeJson(ctx, joined); return writeIssueList(ctx, joined); } }; const Comment = struct { number: i64, repo: ?[]const u8 = null, body: ?[]const u8 = null, @"body-file": ?[]const u8 = null, json: bool = false, pub const short = .{ .R = "repo", .b = "body", .F = "body-file" }; pub const positional: []const []const u8 = &.{"number"}; pub const usage = "comment <number> (-b <text> | -F <file>)"; pub const about = "Comments on an issue. Without -b or -F, and on a terminal, $EDITOR is opened for the comment."; pub const notes = [_]args.Note{ .{ .description = "the issue's number" }, repo_note, body_note, body_file_note, json_note }; pub fn run(ctx: *Context, o: Comment) !Exit { const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); const body = (text.body(ctx, .{ .body = o.body, .body_file = o.@"body-file" }, .required, "comment") catch |e| switch (e) { error.OutOfMemory => return e, else => |f| return Context.exitFor(f), }).?; var comment = switch (call.result(ctx, "issueCreateComment", zfj.issueCreateCommentResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = o.number }, .{ .body = body }))) { .ok => |ok| ok, .failed => |exit| return exit, }; defer comment.deinit(); if (o.json) return writeJson(ctx, comment.body); try ctx.stdout.print("{s}\n", .{comment.value().html_url orelse ""}); return .ok; } }; const Edit = struct { number: i64, repo: ?[]const u8 = null, title: ?[]const u8 = null, body: ?[]const u8 = null, @"body-file": ?[]const u8 = null, @"add-label": ?[]const []const u8 = null, @"remove-label": ?[]const []const u8 = null, @"add-assignee": ?[]const []const u8 = null, @"remove-assignee": ?[]const []const u8 = null, pub const short = .{ .R = "repo", .t = "title", .b = "body", .F = "body-file" }; pub const positional: []const []const u8 = &.{"number"}; pub const usage = "edit <number> [-t <title>] [-b <text> | -F <file>] [--add-label <label>]... [--remove-label <label>]... [--add-assignee <user>]... [--remove-assignee <user>]..."; pub const about = "Changes an issue. Labels and users may be given more than once, or separated by commas."; pub const notes = [_]args.Note{ .{ .description = "the issue's number" }, repo_note, .{ .description = "a new title" }, .{ .description = "a new body, or - to read it from stdin" }, .{ .description = "read a new body from a file, or - for stdin", .complete = .files }, .{ .description = "a label to add, by name" }, .{ .description = "a label to take off, by name" }, .{ .description = "a user to assign" }, .{ .description = "a user to unassign" }, }; pub fn run(ctx: *Context, o: Edit) !Exit { const add_labels = try splitCommas(ctx, o.@"add-label"); const remove_labels = try splitCommas(ctx, o.@"remove-label"); const add_assignees = try splitCommas(ctx, o.@"add-assignee"); const remove_assignees = try splitCommas(ctx, o.@"remove-assignee"); const changes_text = o.title != null or o.body != null or o.@"body-file" != null; const changes_assignees = add_assignees.len != 0 or remove_assignees.len != 0; if (!changes_text and !changes_assignees and add_labels.len == 0 and remove_labels.len == 0) return ctx.usage("nothing to change; `zfj issue edit --help` lists what can be", .{}); const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); const index = o.number; const body = if (o.body != null or o.@"body-file" != null) text.body(ctx, .{ .body = o.body, .body_file = o.@"body-file" }, .optional, "issue body") catch |e| switch (e) { error.OutOfMemory => return e, else => |f| return Context.exitFor(f), } else null; var html_url: []const u8 = ""; if (changes_text or changes_assignees) { // An issue's assignees are set as a whole, so changing some of // them starts from the ones it has. var assignees: ?[]const []const u8 = null; if (changes_assignees) { var current = switch (call.result(ctx, "issueGetIssue", zfj.issueGetIssueResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = index }))) { .ok => |ok| ok, .failed => |exit| return exit, }; defer current.deinit(); var set: std.ArrayList([]const u8) = .empty; for (current.value().assignees orelse &.{}) |user| { const name = user.login orelse continue; if (contains(remove_assignees, name)) continue; try set.append(ctx.arena, try ctx.arena.dupe(u8, name)); } for (add_assignees) |name| { if (!contains(set.items, name)) try set.append(ctx.arena, name); } assignees = set.items; } var edited = switch (call.result(ctx, "issueEditIssue", zfj.issueEditIssueResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = index }, .{ .title = o.title, .body = body, .assignees = assignees, }))) { .ok => |ok| ok, .failed => |exit| return exit, }; defer edited.deinit(); html_url = try ctx.arena.dupe(u8, edited.value().html_url orelse ""); } if (add_labels.len != 0) { var added = switch (call.result(ctx, "issueAddLabel", zfj.issueAddLabelResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = index }, .{ .labels = try labelValues(ctx, add_labels), }))) { .ok => |ok| ok, .failed => |exit| return exit, }; added.deinit(); } for (remove_labels) |name| { if (call.done(ctx, "issueRemoveLabel", zfj.issueRemoveLabelRaw(at.client, .{ .owner = at.owner, .repo = at.repo, .index = index, .identifier = name }, .{}))) |exit| return exit; } try ctx.stdout.print("Edited #{d}{s}{s}\n", .{ index, if (html_url.len != 0) " " else "", html_url }); return .ok; } }; const Close = struct { number: i64, repo: ?[]const u8 = null, comment: ?[]const u8 = null, @"comment-file": ?[]const u8 = null, pub const short = .{ .R = "repo", .c = "comment", .F = "comment-file" }; pub const positional: []const []const u8 = &.{"number"}; pub const usage = "close <number> [-c <text> | -F <file>]"; pub const about = "Closes an issue, first commenting on it with -c or -F to say why."; pub const notes = stateNotes("closing"); pub fn run(ctx: *Context, o: Close) !Exit { return changeState(ctx, o, "closed", "Closed"); } }; const Reopen = struct { number: i64, repo: ?[]const u8 = null, comment: ?[]const u8 = null, @"comment-file": ?[]const u8 = null, pub const short = .{ .R = "repo", .c = "comment", .F = "comment-file" }; pub const positional: []const []const u8 = &.{"number"}; pub const usage = "reopen <number> [-c <text> | -F <file>]"; pub const about = "Reopens a closed issue, first commenting on it with -c or -F to say why."; pub const notes = stateNotes("reopening"); pub fn run(ctx: *Context, o: Reopen) !Exit { return changeState(ctx, o, "open", "Reopened"); } }; fn stateNotes(comptime doing: []const u8) [4]args.Note { return .{ .{ .description = "the issue's number" }, repo_note, .{ .description = "a comment to leave before " ++ doing ++ ", or - to read it from stdin" }, .{ .description = "read the comment from a file, or - for stdin", .complete = .files }, }; } /// `close` and `reopen`: comment if asked to, then set the issue's state. fn changeState(ctx: *Context, o: anytype, state: []const u8, done: []const u8) !Exit { // Read before anything is sent, so that a comment that cannot be read // leaves the issue as it was. No editor is opened: without -c or -F the // state changes with no comment. const comment = if (o.comment != null or o.@"comment-file" != null) (text.body(ctx, .{ .body = o.comment, .body_file = o.@"comment-file" }, .required, "comment") catch |e| switch (e) { error.OutOfMemory => return e, else => |f| return Context.exitFor(f), }).? else null; const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); // The comment goes first. Should the change then fail, the reason is // already on the issue; the other way round could leave an issue closed // or reopened with nothing to say why. if (comment) |body| { var commented = switch (call.result(ctx, "issueCreateComment", zfj.issueCreateCommentResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = o.number }, .{ .body = body }))) { .ok => |ok| ok, .failed => |exit| return exit, }; commented.deinit(); } var changed = switch (call.result(ctx, "issueEditIssue", zfj.issueEditIssueResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = o.number }, .{ .state = state }))) { .ok => |ok| ok, .failed => |exit| { if (comment != null) { ctx.stderr.print("zfj: #{d} was commented on, but its state was not changed\n", .{o.number}) catch {}; ctx.stderr.flush() catch {}; } return exit; }, }; defer changed.deinit(); try ctx.stdout.print("{s} #{d} {s}\n", .{ done, o.number, changed.value().html_url orelse "" }); return .ok; } const Blockers = struct { number: i64, repo: ?[]const u8 = null, json: bool = false, pub const short = .{ .R = "repo" }; pub const positional: []const []const u8 = &.{"number"}; pub const usage = "blockers <number> [--json]"; pub const about = "Lists the issues that block an issue: those that must be closed before it can be worked on."; pub const notes = [_]args.Note{ .{ .description = "the issue's number" }, repo_note, json_note }; pub fn run(ctx: *Context, o: Blockers) !Exit { const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); var params: zfj.IssueListIssueDependenciesParams = .{ .owner = at.owner, .repo = at.repo, .index = o.number }; const joined = switch (try collect(ctx, "issueListIssueDependencies", zfj.issueListIssueDependenciesRaw, at.client, ¶ms)) { .ok => |ok| ok, .failed => |exit| return exit, }; if (o.json) return writeJson(ctx, joined); return writeIssueList(ctx, joined); } }; /// `block` and `unblock`, which differ only in the operation. fn Dependency(comptime blocking: bool) type { return struct { number: i64, by: []const u8, repo: ?[]const u8 = null, pub const short = .{ .R = "repo" }; pub const positional: []const []const u8 = &.{"number"}; pub const usage = if (blocking) "block <number> --by <blocker>" else "unblock <number> --by <blocker>"; pub const about = if (blocking) "Records that the issue --by names blocks this one, so that this one waits until it is closed." else "Removes the record that the issue --by names blocks this one."; pub const notes = [_]args.Note{ .{ .description = "the issue that is blocked" }, .{ .description = "the issue that blocks it: a number in the same repository, or <owner>/<repo>#<number>" }, repo_note, }; pub fn run(ctx: *Context, o: @This()) !Exit { const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); const blocker = parseReference(o.by, at.owner, at.repo) orelse return ctx.usage("--by takes an issue number, or <owner>/<repo>#<number>, not '{s}'", .{o.by}); const meta: zfj.IssueMeta = .{ .owner = blocker.owner, .repo = blocker.repo, .index = blocker.number }; const name = if (blocking) "issueCreateIssueDependencies" else "issueRemoveIssueDependencies"; const attempt = if (blocking) zfj.issueCreateIssueDependenciesResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = o.number }, meta) else zfj.issueRemoveIssueDependenciesResult(at.client, .{ .owner = at.owner, .repo = at.repo, .index = o.number }, meta); var outcome = switch (call.result(ctx, name, attempt)) { .ok => |ok| ok, .failed => |exit| return exit, }; outcome.deinit(); if (blocking) { try ctx.stdout.print("#{d} is blocked by {s}\n", .{ o.number, o.by }); } else { try ctx.stdout.print("#{d} is no longer blocked by {s}\n", .{ o.number, o.by }); } return .ok; } }; } const Block = Dependency(true); const Unblock = Dependency(false); const Reference = struct { owner: []const u8, repo: []const u8, number: i64 }; /// `12`, `#12`, or `owner/repo#12`. fn parseReference(reference: []const u8, owner: []const u8, repo: []const u8) ?Reference { const hash = std.mem.findScalar(u8, reference, '#'); const number_text = if (hash) |h| reference[h + 1 ..] else reference; const number = std.fmt.parseInt(i64, number_text, 10) catch return null; if (number <= 0) return null; const prefix = if (hash) |h| reference[0..h] else ""; if (prefix.len == 0) return .{ .owner = owner, .repo = repo, .number = number }; const slash = std.mem.findScalar(u8, prefix, '/') orelse return null; if (slash == 0 or slash == prefix.len - 1) return null; return .{ .owner = prefix[0..slash], .repo = prefix[slash + 1 ..], .number = number }; } /// A list of issues: columns on a terminal, and otherwise one issue to a /// line with its number, state, title and labels separated by tabs. fn writeIssueList(ctx: *Context, json: []const u8) !Exit { const parsed = std.json.parseFromSlice([]const zfj.Issue, ctx.gpa, json, .{ .ignore_unknown_fields = true }) catch |err| { _ = ctx.parseFailure("the list", "a list of Issue", @errorName(err)); return .response_mismatch; }; defer parsed.deinit(); const issues = parsed.value; var out: Io.Writer.Allocating = .init(ctx.arena); if (!ctx.stdoutIsTty()) { for (issues) |issue| { try out.writer.print("{d}\t{s}\t{s}\t", .{ issue.number orelse 0, issue.state orelse "", oneLine(issue.title orelse "") }); try writeLabels(&out.writer, issue.labels, ","); try out.writer.writeByte('\n'); } try ctx.stdout.writeAll(out.written()); try ctx.stdout.flush(); return .ok; } if (issues.len == 0) { try ctx.stdout.writeAll("No issues.\n"); try ctx.stdout.flush(); return .ok; } const style = Style.of(ctx); var number_width: usize = 0; for (issues) |issue| number_width = @max(number_width, std.fmt.count("{d}", .{issue.number orelse 0})); for (issues) |issue| { const number = issue.number orelse 0; try out.writer.splatByteAll(' ', number_width - std.fmt.count("{d}", .{number})); const state = issue.state orelse ""; try out.writer.print("{s}#{d}{s} {s}{s: <6}{s} {s}", .{ style.bold, number, style.reset, style.stateColor(state), state, style.reset, oneLine(issue.title orelse ""), }); if (issue.labels) |labels| if (labels.len != 0) { try out.writer.print(" {s}", .{style.dim}); try writeLabels(&out.writer, labels, ", "); try out.writer.writeAll(style.reset); }; try out.writer.writeByte('\n'); } try ctx.page(out.written()); return .ok; } fn renderIssue(writer: *Io.Writer, style: Style, issue: zfj.Issue) !void { const state = issue.state orelse ""; try writer.print("{s}{s}{s} #{d}\n", .{ style.bold, issue.title orelse "", style.reset, issue.number orelse 0 }); try writer.print("{s}{s}{s} · opened by {s} on {s}", .{ style.stateColor(state), state, style.reset, login(issue.user), day(issue.created_at), }); if (issue.comments) |count| try writer.print(" · {d} comment{s}", .{ count, if (count == 1) "" else "s" }); try writer.writeByte('\n'); if (issue.labels) |labels| if (labels.len != 0) { try writer.writeAll("Labels: "); try writeLabels(writer, labels, ", "); try writer.writeByte('\n'); }; if (issue.assignees) |users| if (users.len != 0) { try writer.writeAll("Assignees: "); for (users, 0..) |user, i| try writer.print("{s}{s}", .{ if (i == 0) "" else ", ", user.login orelse "" }); try writer.writeByte('\n'); }; if (issue.milestone) |milestone| try writer.print("Milestone: {s}\n", .{milestone.title orelse ""}); if (issue.html_url) |url| try writer.print("{s}{s}{s}\n", .{ style.dim, url, style.reset }); const body = std.mem.trim(u8, issue.body orelse "", " \t\r\n"); if (body.len != 0) try writer.print("\n{s}\n", .{body}); } fn contains(list: []const []const u8, item: []const u8) bool { for (list) |entry| if (std.mem.eql(u8, entry, item)) return true; return false; } const testing = std.testing; test "an issue is named by number, #number, or owner/repo#number" { const plain = parseReference("12", "jeff", "zfj").?; try testing.expectEqualStrings("jeff", plain.owner); try testing.expectEqual(@as(i64, 12), plain.number); try testing.expectEqual(@as(i64, 3), parseReference("#3", "jeff", "zfj").?.number); const elsewhere = parseReference("forgejo/forgejo#42", "jeff", "zfj").?; try testing.expectEqualStrings("forgejo", elsewhere.owner); try testing.expectEqualStrings("forgejo", elsewhere.repo); try testing.expectEqual(@as(i64, 42), elsewhere.number); try testing.expect(parseReference("x", "jeff", "zfj") == null); try testing.expect(parseReference("/repo#1", "jeff", "zfj") == null); try testing.expect(parseReference("0", "jeff", "zfj") == null); } test "every command compiles" { // They are reached only through `commands`' function pointers, which a // test build would otherwise leave unanalyzed. for (commands) |command| _ = command.run; } test "every command's notes cover its options" { inline for (.{ Create, View, List, Comment, Edit, Close, Blockers, Block, Unblock }) |Options| { try testing.expectEqual(@typeInfo(Options).@"struct".field_names.len, Options.notes.len); } }