Open History to see the last commit
1/**
2 * Which source control host settings fields hold secrets, read from each built-in host's client
3 * definition. `serverSettings` keeps those values in the secret store and redacts them for
4 * clients, the same way for every host.
5 *
6 * A secret field holds a string, or a string per server host such as GitHub's `tokens`.
7 *
8 * @module sourceControl/sourceControlHostSecrets
9 */
10import * as AzureDevOps from "@t3tools/source-control-azure-devops/client/definition";
11import * as Bitbucket from "@t3tools/source-control-bitbucket/client/definition";
12import {
13 secretSourceControlHostSettingsFields,
14 type SourceControlClientDefinition,
15} from "@t3tools/source-control-core/client/definition";
16import * as Forgejo from "@t3tools/source-control-forgejo/client/definition";
17import * as GitCafe from "@t3tools/source-control-gitcafe/client/definition";
18import * as GitHub from "@t3tools/source-control-github/client/definition";
19import * as GitLab from "@t3tools/source-control-gitlab/client/definition";
20
21const definitions: ReadonlyArray<SourceControlClientDefinition> = [
22 GitHub.definition,
23 GitLab.definition,
24 AzureDevOps.definition,
25 Bitbucket.definition,
26 Forgejo.definition,
27 GitCafe.definition,
28];
29
30/** Secret field names per host kind; a host with none is absent. */
31export const SOURCE_CONTROL_HOST_SECRET_FIELDS: ReadonlyMap<
32 string,
33 ReadonlyArray<string>
34> = new Map(
35 definitions.flatMap((definition) => {
36 const fields = definition.settings
37 ? secretSourceControlHostSettingsFields(definition.settings)
38 : [];
39 return fields.length > 0 ? [[definition.kind, fields] as const] : [];
40 }),
41);
42
43/** One stored secret: a host's field, and the server host it is for when the field is per host. */
44export interface SourceControlHostSecretSlot {
45 readonly kind: string;
46 readonly field: string;
47 readonly serverHost: string | null;
48}
49
50/** The secret store entry for one slot; names stay stable across releases. */
51export function sourceControlHostSecretName(slot: SourceControlHostSecretSlot): string {
52 const encode = (value: string) => Buffer.from(value, "utf8").toString("base64url");
53 return slot.serverHost === null
54 ? `source-control-${encode(slot.kind)}-${encode(slot.field)}`
55 : `source-control-${encode(slot.kind)}-${encode(slot.field)}-${encode(slot.serverHost.trim().toLowerCase())}`;
56}
57
58/** The string values a secret field holds, keyed by server host, or `null` for a plain string. */
59export function readSecretFieldValues(
60 value: unknown,
61): ReadonlyArray<readonly [serverHost: string | null, value: string]> {
62 if (typeof value === "string") return [[null, value]];
63 if (value === null || typeof value !== "object" || Array.isArray(value)) return [];
64 return Object.entries(value).flatMap(([serverHost, entry]) =>
65 typeof entry === "string" ? [[serverHost.trim().toLowerCase(), entry] as const] : [],
66 );
67}
68
69/** A secret field's value rebuilt from per-slot values, in the shape it was read from. */
70export function writeSecretFieldValues(
71 original: unknown,
72 values: ReadonlyArray<readonly [serverHost: string | null, value: string]>,
73): unknown {
74 if (typeof original === "string") return values[0]?.[1] ?? "";
75 return Object.fromEntries(values.map(([serverHost, value]) => [serverHost ?? "", value]));
76}