async function getRulesets()

in packages/azure-openapi-validator/autorest/src/spectral-plugin-func.ts [68:107]


async function getRulesets(
  initiator: IAutoRestPluginInitiator,
  resolvedOpenapiType: OpenApiTypes,
  isStagingRun: boolean,
): Promise<{
  rulesetPayload: SpectralRulesetPayload
  rulesetForManualSpecs: Ruleset
  rulesetForTypeSpecGeneratedSpecs: Ruleset
}> {
  const rulesetPayload: SpectralRulesetPayload = await getRulesetPayload(initiator, resolvedOpenapiType)
  const namesOfRulesInStagingOnly: string[] = getNamesOfRulesInPayloadWithPropertySetToTrue(rulesetPayload, "stagingOnly")
  const namesOfRulesDisabledForTypespecDataPlane: string[] =
    resolvedOpenapiType === OpenApiTypes.dataplane
      ? getNamesOfRulesInPayloadWithPropertySetToTrue(rulesetPayload, "disableForTypeSpecDataPlane")
      : []

  // We need two of rulesetPayloads:
  // - The original, to prepare it as argument for spectral Rulesets. See deletePropertiesNotValidForSpectralRules for more.
  // - A copy, with all properties on it, so we can access the rpcGuidelineCode property of each rule when post-processing
  //   Spectral run results.
  // Note: the original, not copy, must be used as input to Ruleset constructor, as the way we
  // obtain the copy here makes it not suitable for use as input to Ruleset constructor.
  const rulesetPayloadCopy: SpectralRulesetPayload = JSON.parse(JSON.stringify(rulesetPayload))
  deleteRulesPropertiesInPayloadNotValidForSpectralRules(rulesetPayload)

  const rulesetForManualSpecs = new Ruleset(rulesetPayload, { severity: "recommended" })
  ifNotStagingRunDisableRulesInStagingOnly(initiator, namesOfRulesInStagingOnly, isStagingRun, rulesetForManualSpecs)
  printRuleNames(initiator, rulesetForManualSpecs, resolvedOpenapiType, "manually written OpenAPI specs")

  const rulesetForTypeSpecGeneratedSpecs = new Ruleset(rulesetPayload, { severity: "recommended" })
  ifNotStagingRunDisableRulesInStagingOnly(initiator, namesOfRulesInStagingOnly, isStagingRun, rulesetForTypeSpecGeneratedSpecs)
  disableRulesInRuleset(rulesetForTypeSpecGeneratedSpecs, namesOfRulesDisabledForTypespecDataPlane)
  printRuleNames(initiator, rulesetForTypeSpecGeneratedSpecs, resolvedOpenapiType, "TypeSpec-generated OpenAPI specs")

  return {
    rulesetPayload: rulesetPayloadCopy,
    rulesetForManualSpecs: rulesetForManualSpecs,
    rulesetForTypeSpecGeneratedSpecs,
  }
}