58 lines
2.1 KiB
TypeScript
58 lines
2.1 KiB
TypeScript
/** The operations the runtime can perform through a connection. */
|
|
export const OPERATIONS = ["clone", "push", "pr"] as const;
|
|
|
|
/** One organisation-owned forge connection, as the document writes it. */
|
|
export interface Connection {
|
|
id: string;
|
|
name: string;
|
|
type: string;
|
|
host: string;
|
|
account: string;
|
|
token: { engine: string; key: string };
|
|
commit_name?: string;
|
|
commit_email?: string;
|
|
/** OpenSSH private key that signs commits made through the connection. */
|
|
signing_key?: { engine: string; key: string };
|
|
/** Permitted operations per execution identity ID. */
|
|
permits?: Record<string, string[]>;
|
|
}
|
|
|
|
/** permits renders the document's map as rows the page can edit. */
|
|
export const permits = (c: Connection): [string, string[]][] =>
|
|
Object.entries(c.permits ?? {});
|
|
|
|
// Each edit returns a new connection: the one passed in belongs to a settings
|
|
// form, and a write in place never reaches it.
|
|
|
|
export function addPermit(c: Connection): Connection {
|
|
return { ...c, permits: { ...c.permits, "": ["clone"] } };
|
|
}
|
|
|
|
export function removePermit(c: Connection, id: string): Connection {
|
|
const { [id]: _, ...rest } = c.permits ?? {};
|
|
return { ...c, permits: rest };
|
|
}
|
|
|
|
// Renaming rebuilds the entry: the identity reference is the map key.
|
|
export function renamePermit(c: Connection, from: string, to: string): Connection {
|
|
if (!c.permits || from === to) return c;
|
|
const { [from]: operations = [], ...rest } = c.permits;
|
|
return { ...c, permits: to ? { ...rest, [to]: operations } : rest };
|
|
}
|
|
|
|
export function toggleOperation(
|
|
c: Connection,
|
|
id: string,
|
|
operation: string,
|
|
on: boolean,
|
|
): Connection {
|
|
const current = new Set(c.permits?.[id] ?? []);
|
|
if (on) current.add(operation);
|
|
else current.delete(operation);
|
|
return { ...c, permits: { ...c.permits, [id]: OPERATIONS.filter((o) => current.has(o)) } };
|
|
}
|
|
|
|
/** Identities a permit row may pick: any not already permitted on another row. */
|
|
export const choosableIdentities = <T extends { id: string }>(c: Connection, identities: T[], current: string): T[] =>
|
|
identities.filter((a) => a.id === current || !(a.id in (c.permits ?? {})));
|