// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! `zfj workflow ...`: a repository's workflows. Starting a run of one is //! all there is: Forgejo's API has no operation listing them. const std = @import("std"); 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 repo_note = porcelain.repo_note; pub const commands = [_]porcelain.Command{ Runner("workflow", Run).command("run", "start a run of a workflow"), }; pub fn main(ctx: *Context, words: []const []const u8) !Exit { return porcelain.dispatch(ctx, "workflow", &commands, words); } const Run = struct { file: []const u8, repo: ?[]const u8 = null, ref: ?[]const u8 = null, field: ?[]const []const u8 = null, json: bool = false, pub const short = .{ .R = "repo", .r = "ref", .f = "field" }; pub const positional: []const []const u8 = &.{"file"}; pub const usage = "run [--ref ] [-f =]... [--json]"; pub const about = "Starts a run of a workflow that is started by workflow_dispatch, and says its run number and where to see it."; pub const notes = [_]args.Note{ .{ .description = "the workflow, named by its file, such as release.yaml", .complete = .workflows }, repo_note, .{ .description = "the branch or tag to run it on (default: the repository's default branch)" }, .{ .description = "an input the workflow declares, as =" }, .{ .description = "write Forgejo's JSON for the run started rather than text for reading" }, }; pub fn run(ctx: *Context, o: Run) !Exit { var inputs: std.json.ObjectMap = .empty; for (o.field orelse &.{}) |field| { const equals = std.mem.findScalar(u8, field, '=') orelse return ctx.usage("-f takes =, not '{s}'", .{field}); if (equals == 0) return ctx.usage("-f takes =, and '{s}' has no key", .{field}); const key = field[0..equals]; if (inputs.contains(key)) return ctx.usage("the input '{s}' is given twice", .{key}); try inputs.put(ctx.arena, key, .{ .string = field[equals + 1 ..] }); } const at = place(ctx, o.repo) catch |e| return Context.exitFor(e); // For the default branch, and for where the run can be seen: what // Forgejo answers with is the run's number and no more. var repository = switch (call.result(ctx, "repoGet", zfj.repoGetResult(at.client, .{ .owner = at.owner, .repo = at.repo }))) { .ok => |ok| ok, .failed => |exit| return exit, }; defer repository.deinit(); const ref = o.ref orelse repository.value().default_branch orelse return ctx.environment("Forgejo gave no default branch for {s}/{s}; name a ref with --ref", .{ at.owner, at.repo }); var started = switch (call.result(ctx, "dispatchWorkflow", zfj.dispatchWorkflowResult(at.client, .{ .owner = at.owner, .repo = at.repo, .workflowfilename = o.file, }, .{ .ref = ref, .inputs = if (inputs.count() == 0) null else .{ .object = inputs }, .return_run_info = true, }))) { .ok => |ok| ok, .failed => |exit| return exit, }; defer started.deinit(); if (o.json) return writeJson(ctx, started.body); const number = started.value().run_number orelse return ctx.environment("Forgejo started {s} but gave no run number for it", .{o.file}); try ctx.stdout.print("Started run {d} of {s} on {s}\n", .{ number, o.file, ref }); if (repository.value().html_url) |html_url| try ctx.stdout.print("{s}/actions/runs/{d}\n", .{ std.mem.trimEnd(u8, html_url, "/"), number }); try ctx.stdout.flush(); return .ok; } }; const testing = std.testing; test "every command compiles" { for (commands) |command| _ = command.run; } test "every command's notes cover its options" { try testing.expectEqual(@typeInfo(Run).@"struct".field_names.len, Run.notes.len); }