|
| 1 | +import { |
| 2 | + assert, |
| 3 | + normalizeColor, |
| 4 | + normalizeDescription, |
| 5 | + normalizeName, |
| 6 | + normalizeRepositoryRef, |
| 7 | +} from "./config-utils.mjs"; |
| 8 | + |
| 9 | +function isFullRepositoryName(value) { |
| 10 | + return /^[^/\s]+\/[^/\s]+$/.test(value); |
| 11 | +} |
| 12 | + |
| 13 | +function isRepositoryName(value) { |
| 14 | + return /^[^/\s]+$/.test(value); |
| 15 | +} |
| 16 | + |
| 17 | +export function validateProperties(properties, options = {}) { |
| 18 | + const { |
| 19 | + requireOrganization = false, |
| 20 | + requireLabelSyncTokenSecretName = false, |
| 21 | + includeSourceRepository = false, |
| 22 | + defaultSourceRepository = "", |
| 23 | + includeDeleteMissingByDefault = false, |
| 24 | + } = options; |
| 25 | + |
| 26 | + assert(properties && typeof properties === "object" && !Array.isArray(properties), "config/properties.jsonc must contain an object."); |
| 27 | + |
| 28 | + const validated = {}; |
| 29 | + |
| 30 | + if (requireOrganization) { |
| 31 | + assert( |
| 32 | + typeof properties.organization === "string" && properties.organization.trim(), |
| 33 | + "properties.organization must be a non-empty string.", |
| 34 | + ); |
| 35 | + validated.organization = properties.organization.trim(); |
| 36 | + } |
| 37 | + |
| 38 | + if (requireLabelSyncTokenSecretName) { |
| 39 | + assert( |
| 40 | + typeof properties.labelSyncTokenSecretName === "string" && /^[A-Z_][A-Z0-9_]*$/.test(properties.labelSyncTokenSecretName), |
| 41 | + "properties.labelSyncTokenSecretName must look like a GitHub secret name.", |
| 42 | + ); |
| 43 | + validated.labelSyncTokenSecretName = properties.labelSyncTokenSecretName.trim(); |
| 44 | + } |
| 45 | + |
| 46 | + if (properties.sourceRepository !== undefined) { |
| 47 | + assert( |
| 48 | + typeof properties.sourceRepository === "string" && /^[^/\s]+\/[^/\s]+$/.test(properties.sourceRepository.trim()), |
| 49 | + "properties.sourceRepository must match owner/repo when provided.", |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + if (includeSourceRepository) { |
| 54 | + validated.sourceRepository = (properties.sourceRepository ?? defaultSourceRepository).trim(); |
| 55 | + } |
| 56 | + |
| 57 | + if (properties.deleteMissingByDefault !== undefined) { |
| 58 | + assert(typeof properties.deleteMissingByDefault === "boolean", "properties.deleteMissingByDefault must be a boolean."); |
| 59 | + } |
| 60 | + |
| 61 | + if (includeDeleteMissingByDefault) { |
| 62 | + validated.deleteMissingByDefault = properties.deleteMissingByDefault ?? false; |
| 63 | + } |
| 64 | + |
| 65 | + return validated; |
| 66 | +} |
| 67 | + |
| 68 | +export function validateLabels(labels) { |
| 69 | + assert(Array.isArray(labels), "config/labels.jsonc must contain an array."); |
| 70 | + |
| 71 | + const seen = new Set(); |
| 72 | + |
| 73 | + return labels.map((label, index) => { |
| 74 | + assert(label && typeof label === "object" && !Array.isArray(label), `Label at index ${index} must be an object.`); |
| 75 | + assert(typeof label.name === "string" && label.name.trim(), `Label at index ${index} is missing a valid name.`); |
| 76 | + assert(typeof label.color === "string" && /^[0-9a-fA-F]{6}$/.test(normalizeColor(label.color)), `Label "${label.name}" must have a 6-character hex color.`); |
| 77 | + |
| 78 | + const key = normalizeName(label.name); |
| 79 | + assert(!seen.has(key), `Duplicate label name detected: "${label.name}".`); |
| 80 | + seen.add(key); |
| 81 | + |
| 82 | + if (label.description !== undefined) { |
| 83 | + assert(typeof label.description === "string", `Label "${label.name}" has a non-string description.`); |
| 84 | + } |
| 85 | + |
| 86 | + return { |
| 87 | + name: label.name.trim(), |
| 88 | + color: normalizeColor(label.color), |
| 89 | + description: normalizeDescription(label.description), |
| 90 | + }; |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +export function validateDeleteLabels(deleteLabels) { |
| 95 | + assert(Array.isArray(deleteLabels), "config/auto-pruned-labels.jsonc must contain an array."); |
| 96 | + |
| 97 | + const seen = new Set(); |
| 98 | + |
| 99 | + return deleteLabels.map((entry, index) => { |
| 100 | + assert(typeof entry === "string" && entry.trim(), `Delete label at index ${index} must be a non-empty string.`); |
| 101 | + |
| 102 | + const name = entry.trim(); |
| 103 | + const key = normalizeName(name); |
| 104 | + assert(!seen.has(key), `Duplicate delete label detected: "${name}".`); |
| 105 | + seen.add(key); |
| 106 | + return name; |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +export function assertNoLabelOverlap(desiredLabels, deleteLabels) { |
| 111 | + const desiredKeys = new Set(desiredLabels.map((label) => normalizeName(label.name))); |
| 112 | + const overlaps = deleteLabels.filter((label) => desiredKeys.has(normalizeName(label))); |
| 113 | + |
| 114 | + assert( |
| 115 | + overlaps.length === 0, |
| 116 | + `Labels cannot exist in both config/labels.jsonc and config/auto-pruned-labels.jsonc: ${overlaps.join(", ")}.`, |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +function validateRepositoryEntries(entries, configKey) { |
| 121 | + assert(Array.isArray(entries), `config/repository-filter.jsonc field "${configKey}" must contain an array.`); |
| 122 | + |
| 123 | + const seen = new Set(); |
| 124 | + |
| 125 | + return new Set(entries.map((entry, index) => { |
| 126 | + assert(typeof entry === "string" && entry.trim(), `"${configKey}" entry at index ${index} must be a non-empty string.`); |
| 127 | + |
| 128 | + const name = entry.trim(); |
| 129 | + assert( |
| 130 | + isRepositoryName(name) || isFullRepositoryName(name), |
| 131 | + `"${configKey}" entry "${name}" must be either "repo-name" or "owner/repo-name".`, |
| 132 | + ); |
| 133 | + |
| 134 | + const key = normalizeRepositoryRef(name); |
| 135 | + assert(!seen.has(key), `Duplicate "${configKey}" entry detected: "${name}".`); |
| 136 | + seen.add(key); |
| 137 | + return key; |
| 138 | + })); |
| 139 | +} |
| 140 | + |
| 141 | +export function validateRepositoryFilter(repositoryFilter) { |
| 142 | + assert( |
| 143 | + repositoryFilter && typeof repositoryFilter === "object" && !Array.isArray(repositoryFilter), |
| 144 | + "config/repository-filter.jsonc must contain an object.", |
| 145 | + ); |
| 146 | + |
| 147 | + if (repositoryFilter.useWhitelist !== undefined) { |
| 148 | + assert(typeof repositoryFilter.useWhitelist === "boolean", 'config/repository-filter.jsonc field "useWhitelist" must be a boolean.'); |
| 149 | + } |
| 150 | + |
| 151 | + return { |
| 152 | + useWhitelist: repositoryFilter.useWhitelist ?? false, |
| 153 | + whitelist: validateRepositoryEntries(repositoryFilter.whitelist ?? [], "whitelist"), |
| 154 | + blacklist: validateRepositoryEntries(repositoryFilter.blacklist ?? [], "blacklist"), |
| 155 | + }; |
| 156 | +} |
0 commit comments