refusal

tcmts.cli.sync.refusal · #ybwlle3d · term · runs anywhere

Why a server refuses a caller this route, or undefined if it doesn't. sourceOf says which branch a contribution proposes: a contributor closes and reopens only their own.

import { CONTRIBUTOR_ROUTES, READ_ROUTES } from "#codebase/tcmts/cli/sync";
import type { Caller } from "#codebase/tcmts/cli/sync";

export function refusal(
  caller: Caller,
  route: string,
  body: Record<string, unknown>,
  sourceOf: (id: number) => string | undefined,
): string | undefined {
  if (caller.role === "maintainer" || READ_ROUTES.has(route)) return undefined;
  if (caller.role === "reader")
    return `${route} needs a token: anyone can pull from this server, but writing to it takes one (ask its maintainer for yours: \`tcm user add\`)`;
  const mine = (branch: unknown) =>
    typeof branch === "string" &&
    branch.startsWith(`@${caller.handle}/`) &&
    branch.length > caller.handle.length + 2;
  const not = `@${caller.handle} can't`;
  if (!CONTRIBUTOR_ROUTES.has(route)) return `${not} ${route}: only the server's maintainer can`;
  if (route === "set-branch" && !mine(body.name))
    return `${not} push ${String(body.name)}: contributors push branches under @${caller.handle}/ (\`tcm push\` and \`tcm contribute\` name them so)`;
  if (route === "open-contribution" && !mine(body.source))
    return `${not} propose ${String(body.source)}: contributors propose branches under @${caller.handle}/`;
  if (
    (route === "close-contribution" || route === "reopen-contribution") &&
    !mine(sourceOf(Number(body.id)))
  )
    return `${not} ${route.split("-")[0]} #${String(body.id)}: it isn't theirs`;
  return undefined;
}

uses

used by