dvPlanPreviewSecrets function

DVPreviewSecretPlan dvPlanPreviewSecrets({
  1. required Map<String, Set<String>> required,
  2. required String? previewValue(
    1. String name
    ),
  3. String? productionValue(
    1. String name
    )?,
})

Plans a preview's secrets from the declaration.

required is each declared secret's required: list. A secret required in production or in preview must have a preview value, or the plan carries DV-PREVIEW-002: a production requirement means the application cannot run without it, and the preview is that application.

previewValue is the preview's own source. productionValue, when given, is consulted only to compare digests: a preview value identical to production's is refused the same way as a missing one, because it is the production credential under another variable name, and that is how a PREVIEW_STRIPE_KEY=$STRIPE_KEY line in a workflow takes real payments.

Implementation

DVPreviewSecretPlan dvPlanPreviewSecrets({
  required Map<String, Set<String>> required,
  required String? Function(String name) previewValue,
  String? Function(String name)? productionValue,
}) {
  final Map<String, String> values = <String, String>{};
  final List<DVPreviewFinding> findings = <DVPreviewFinding>[];
  final List<String> names = required.keys.toList()..sort();
  for (final String name in names) {
    final Set<String> environments = required[name]!;
    final bool needed = environments.contains('production') ||
        environments.contains(dvPreviewEnvironment);

    final String? value = _nonEmpty(previewValue(name));
    final String? production =
        productionValue == null ? null : _nonEmpty(productionValue(name));
    final bool isProductions =
        value != null && production != null && _same(value, production);

    if (value != null && !isProductions) {
      values[name] = value;
      continue;
    }
    if (isProductions) {
      findings.add(DVPreviewFinding(
        'DV-PREVIEW-002',
        '"$name" has a preview value identical to production\'s. Production '
        'values are never resolved for a preview; give the preview its own '
        '(a test-mode key, a sandbox account) or the preview is not deployed.',
      ));
      continue;
    }
    if (needed) {
      findings.add(DVPreviewFinding(
        'DV-PREVIEW-002',
        '"$name" is required in ${(environments.toList()..sort()).join(', ')} '
        'and has no preview value, so the preview was not deployed. Set a '
        'preview value for it; production\'s is never used.',
      ));
    }
  }
  return DVPreviewSecretPlan._(
    Map<String, String>.unmodifiable(values),
    List<DVPreviewFinding>.unmodifiable(findings),
  );
}