Skip to content

Commit 532871c

Browse files
committed
feat: add approval groups policy value model
Signed-off-by: Vitor Mattos <[email protected]>
1 parent 4bf376d commit 532871c

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

  • src/views/Settings/PolicyWorkbench/settings/approval-groups
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)