Something went wrong. Try again.
Zig bindings for all 506 operations of the Forgejo API, generated from its own description, and zfj, a command line built on them.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! `zfj label ...`: a repository's labels, listed and created by name.//!//! Forgejo lets a repository hold two labels of the same name, which is//! never what a person meant, so `create` looks for the name first: it//! refuses one that exists, and with `--force` brings that one up to date//! instead. That makes a script creating a project's labels safe to run//! again.
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 writeJson = porcelain.writeJson;const Style = porcelain.Style;const repo_note = porcelain.repo_note;const json_note = porcelain.json_note;const oneLine = porcelain.oneLine;
pub const commands = [_]porcelain.Command{ Runner("label", List).command("list", "list a repository's labels"), Runner("label", Create).command("create", "create a label, or with --force update one"),};
pub fn main(ctx: *Context, words: []const []const u8) !Exit { return porcelain.dispatch(ctx, "label", &commands, words);}
const List = struct { repo: ?[]const u8 = null, json: bool = false,
pub const short = .{ .R = "repo" }; pub const usage = "list [--json]"; pub const about = "Lists a repository's labels, every page of them."; pub const notes = [_]args.Note{ repo_note, json_note };
pub fn run(ctx: *Context, o: List) !Exit { const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); var params: zfj.IssueListLabelsParams = .{ .owner = at.owner, .repo = at.repo }; // A repository's labels are few, and a list missing some of them // reads as though they were not there. const joined = switch (try porcelain.collectAll(ctx, "issueListLabels", zfj.issueListLabelsRaw, at.client, ¶ms)) { .ok => |ok| ok, .failed => |exit| return exit, }; if (o.json) return writeJson(ctx, joined);
const parsed = std.json.parseFromSlice([]const zfj.Label, ctx.gpa, joined, .{ .ignore_unknown_fields = true }) catch |err| { _ = ctx.parseFailure("the list", "a list of Label", @errorName(err)); return .response_mismatch; }; defer parsed.deinit(); return writeLabelList(ctx, parsed.value); }};
const Create = struct { name: []const u8, repo: ?[]const u8 = null, color: []const u8, description: ?[]const u8 = null, force: bool = false, json: bool = false,
pub const short = .{ .R = "repo", .c = "color", .d = "description", .f = "force" }; pub const positional: []const []const u8 = &.{"name"}; pub const usage = "create <name> -c <color> [-d <description>] [--force] [--json]"; pub const about = "Creates a label. One of the same name already there is an error, unless --force is given, which changes its color and description instead."; pub const notes = [_]args.Note{ .{ .description = "the label's name" }, repo_note, .{ .description = "its color, as six hex digits, with or without a leading #" }, .{ .description = "what the label means" }, .{ .description = "update the label of that name if there is one" }, json_note, };
pub fn run(ctx: *Context, o: Create) !Exit { const color = normalColor(ctx.arena, o.color) catch return ctx.usage("'{s}' is not a color; give six hex digits, as in 00aabb or #00aabb", .{o.color}); if (std.mem.trim(u8, o.name, " ").len == 0) return ctx.usage("a label needs a name", .{});
const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); var params: zfj.IssueListLabelsParams = .{ .owner = at.owner, .repo = at.repo }; const joined = switch (try porcelain.collectAll(ctx, "issueListLabels", zfj.issueListLabelsRaw, at.client, ¶ms)) { .ok => |ok| ok, .failed => |exit| return exit, }; const parsed = std.json.parseFromSlice([]const zfj.Label, ctx.gpa, joined, .{ .ignore_unknown_fields = true }) catch |err| { _ = ctx.parseFailure("the list", "a list of Label", @errorName(err)); return .response_mismatch; }; defer parsed.deinit();
const existing: ?i64 = for (parsed.value) |label| { if (std.mem.eql(u8, label.name orelse continue, o.name)) break label.id; } else null;
if (existing) |id| { if (!o.force) return ctx.usage("{s} already has a label '{s}'; --force updates it", .{ at.repo, o.name }); var edited = switch (call.result(ctx, "issueEditLabel", zfj.issueEditLabelResult(at.client, .{ .owner = at.owner, .repo = at.repo, .id = id }, .{ .color = color, .description = o.description, }))) { .ok => |ok| ok, .failed => |exit| return exit, }; defer edited.deinit(); if (o.json) return writeJson(ctx, edited.body); try ctx.stdout.print("Updated label '{s}'\n", .{o.name}); return .ok; }
var created = switch (call.result(ctx, "issueCreateLabel", zfj.issueCreateLabelResult(at.client, .{ .owner = at.owner, .repo = at.repo }, .{ .name = o.name, .color = color, .description = o.description, }))) { .ok => |ok| ok, .failed => |exit| return exit, }; defer created.deinit(); if (o.json) return writeJson(ctx, created.body); try ctx.stdout.print("Created label '{s}'\n", .{o.name}); return .ok; }};
/// `#rrggbb`, lowercase, from six hex digits given with or without the `#`.fn normalColor(arena: std.mem.Allocator, given: []const u8) ![]const u8 { const digits = if (std.mem.startsWith(u8, given, "#")) given[1..] else given; if (digits.len != 6) return error.NotAColor; for (digits) |c| if (!std.ascii.isHex(c)) return error.NotAColor; const color = try arena.alloc(u8, 7); color[0] = '#'; _ = std.ascii.lowerString(color[1..], digits); return color;}
/// Columns on a terminal -- name, color, description -- and otherwise one/// label to a line with those separated by tabs.fn writeLabelList(ctx: *Context, labels: []const zfj.Label) !Exit { var out: Io.Writer.Allocating = .init(ctx.arena); if (!ctx.stdoutIsTty()) { for (labels) |label| { try out.writer.print("{s}\t#{s}\t{s}\n", .{ label.name orelse "", std.mem.trimStart(u8, label.color orelse "", "#"), oneLine(label.description orelse ""), }); } try ctx.stdout.writeAll(out.written()); try ctx.stdout.flush(); return .ok; }
if (labels.len == 0) { try ctx.stdout.writeAll("No labels.\n"); try ctx.stdout.flush(); return .ok; } const style = Style.of(ctx); var name_width: usize = 0; for (labels) |label| name_width = @max(name_width, @as([]const u8, label.name orelse "").len); for (labels) |label| { const name = label.name orelse ""; try out.writer.print("{s}{s}{s}", .{ style.bold, name, style.reset }); try out.writer.splatByteAll(' ', name_width - name.len); try out.writer.print(" #{s: <6}", .{std.mem.trimStart(u8, label.color orelse "", "#")}); const description = oneLine(label.description orelse ""); if (description.len != 0) try out.writer.print(" {s}{s}{s}", .{ style.dim, description, style.reset }); try out.writer.writeByte('\n'); } try ctx.page(out.written()); return .ok;}
const testing = std.testing;
test "a color is six hex digits, the # optional, written back lowercase with it" { var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); try testing.expectEqualStrings("#00aabb", try normalColor(arena.allocator(), "00AABB")); try testing.expectEqualStrings("#00aabb", try normalColor(arena.allocator(), "#00aabb")); try testing.expectError(error.NotAColor, normalColor(arena.allocator(), "abc")); try testing.expectError(error.NotAColor, normalColor(arena.allocator(), "#00aabg")); try testing.expectError(error.NotAColor, normalColor(arena.allocator(), "##00aab"));}