pr_01m47d24b0e6n91zwymwxg0vpx/apps/web/app/lib/secrets.server.ts

40 lines1,793 bytesCodeBlame
1import type { Setting, SettingKind, SettingsOwner, User } from "@g1t/contracts";
2
3import { actions } from "./services.server";
4
5export type SecretsData = {
6 secrets: Setting[];
7 variables: Setting[];
8 error: string | null;
9};
10
11/** A repository's or a workspace's secrets and variables. */
12export async function loadSecrets(owner: SettingsOwner, actor: User): Promise<SecretsData> {
13 const [secrets, variables] = await Promise.all([
14 actions.settings(actor, owner, "secret"),
15 actions.settings(actor, owner, "variable"),
16 ]);
17 return {
18 secrets: secrets.ok ? secrets.value : [],
19 variables: variables.ok ? variables.value : [],
20 error: !secrets.ok ? secrets.error.message : !variables.ok ? variables.error.message : null,
21 };
22}
23
24export type SecretsAction = { done?: string; error?: string; kind?: SettingKind };
25
26export async function actOnSecrets(owner: SettingsOwner, actor: User, form: FormData): Promise<SecretsAction> {
27 const intent = String(form.get("intent") ?? "");
28 const kind: SettingKind = form.get("kind") === "variable" ? "variable" : "secret";
29 const name = String(form.get("name") ?? "").trim();
30 const what = kind === "secret" ? "Secret" : "Variable";
31 if (intent === "delete") {
32 const removed = await actions.deleteSetting(actor, owner, kind, name);
33 return removed.ok ? { done: `${what} ${name} removed.`, kind } : { error: removed.error.message, kind };
34 }
35 const value = String(form.get("value") ?? "");
36 if (!name) return { error: "Give it a name.", kind };
37 if (kind === "secret" && !value) return { error: "Give the secret a value.", kind };
38 const saved = await actions.setSetting(actor, owner, kind, name, value);
39 return saved.ok ? { done: `${what} ${saved.value.name} saved.`, kind } : { error: saved.error.message, kind };
40}