// // Re-running and cancelling workflow runs. // const nodes = require("../views/nodes.js"); const selection = require("./selection.js"); const ui = require("../ui.js"); function contextFrom(argument) { const node = selection.selectedNode(argument); const repo = selection.repoOf(node); const run = selection.runFor(node); const job = node && node.data ? node.data.job : null; return { node, repo, run, job }; } /** Runs `action`, reports the outcome, and refreshes so the new state shows. */ async function perform(store, refresh, repo, label, action) { try { await action(); ui.info(`${label} requested for ${repo.fullName}.`); } catch (error) { const hint = error.status === 403 ? " The token needs Actions write access." : ""; ui.error(`${label} failed: ${error.message || error}${hint}`); return; } await refresh(); } function register(store, refresh) { nova.commands.register("gitea.rerunRun", async (argument) => { const { repo, run } = contextFrom(argument); if (!repo || !run) { ui.warn("Select a workflow run first."); return; } await perform(store, refresh, repo, `Re-run of ${nodes.runTitle(run)}`, () => store.apiFor(repo).rerunRun(repo, run.id), ); }); nova.commands.register("gitea.rerunFailedJobs", async (argument) => { const { repo, run } = contextFrom(argument); if (!repo || !run) { ui.warn("Select a workflow run first."); return; } await perform(store, refresh, repo, `Re-run of failed jobs in ${nodes.runTitle(run)}`, () => store.apiFor(repo).rerunFailedJobs(repo, run.id), ); }); nova.commands.register("gitea.rerunJob", async (argument) => { const { repo, run, job } = contextFrom(argument); if (!repo || !run || !job) { ui.warn("Select a job first."); return; } await perform(store, refresh, repo, `Re-run of ${job.name || job.id}`, () => store.apiFor(repo).rerunJob(repo, run.id, job.id), ); }); nova.commands.register("gitea.cancelRun", async (argument) => { const { repo, run } = contextFrom(argument); if (!repo || !run) { ui.warn("Select a workflow run first."); return; } if (!(await ui.confirm(`Cancel ${nodes.runTitle(run)}?`, "Cancel Run"))) return; await perform(store, refresh, repo, `Cancellation of ${nodes.runTitle(run)}`, () => store.apiFor(repo).cancelRun(repo, run.id), ); }); } exports.register = register;