|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import type { EffectivePolicyValue } from '../../../../../types/index' |
| 7 | + |
| 8 | +export const DEFAULT_APPROVAL_GROUPS = ['admin'] |
| 9 | + |
| 10 | +export function resolveApprovalGroups(value: EffectivePolicyValue | string[]): string[] { |
| 11 | + if (Array.isArray(value)) { |
| 12 | + const normalized = normalizeGroupIds(value) |
| 13 | + return normalized.length > 0 ? normalized : [...DEFAULT_APPROVAL_GROUPS] |
| 14 | + } |
| 15 | + |
| 16 | + if (typeof value !== 'string') { |
| 17 | + return [...DEFAULT_APPROVAL_GROUPS] |
| 18 | + } |
| 19 | + |
| 20 | + const trimmed = value.trim() |
| 21 | + if (!trimmed) { |
| 22 | + return [...DEFAULT_APPROVAL_GROUPS] |
| 23 | + } |
| 24 | + |
| 25 | + try { |
| 26 | + const parsed = JSON.parse(trimmed) |
| 27 | + if (Array.isArray(parsed)) { |
| 28 | + const normalized = normalizeGroupIds(parsed) |
| 29 | + return normalized.length > 0 ? normalized : [...DEFAULT_APPROVAL_GROUPS] |
| 30 | + } |
| 31 | + } catch { |
| 32 | + return [...DEFAULT_APPROVAL_GROUPS] |
| 33 | + } |
| 34 | + |
| 35 | + return [...DEFAULT_APPROVAL_GROUPS] |
| 36 | +} |
| 37 | + |
| 38 | +export function serializeApprovalGroups(value: EffectivePolicyValue | string[]): string { |
| 39 | + return JSON.stringify(resolveApprovalGroups(value)) |
| 40 | +} |
| 41 | + |
| 42 | +function normalizeGroupIds(raw: unknown[]): string[] { |
| 43 | + const normalized = raw |
| 44 | + .filter((candidate): candidate is string => typeof candidate === 'string') |
| 45 | + .map((candidate) => candidate.trim()) |
| 46 | + .filter((candidate) => candidate.length > 0) |
| 47 | + |
| 48 | + return [...new Set(normalized)].sort((left, right) => left.localeCompare(right)) |
| 49 | +} |
0 commit comments