import Bluebird from "bluebird" ; import slash from "slash" ; import { relative , resolve } from "path" ; import { uniq } from "lodash" ; import { createGardenPlugin } from "@garden-io/sdk" ; import { ConftestProvider } from "@garden-io/garden-conftest" ; import { dedent } from "@garden-io/core/build/src/util/string" ; import { getModuleTypeUrl , getGitHubUrl } from "@garden-io/core/build/src/docs/common" ; import { collectTemplateReferences } from "@garden-io/core/build/src/template-string/template-string" ; const moduleTypeUrl = getModuleTypeUrl ( "conftest" ) ; const gitHubUrl = getGitHubUrl ( "examples/conftest" ) ; /**
 * Auto-generates a conftest module for each helm and kubernetes module in your project
 */ export const gardenPlugin = ( ) => createGardenPlugin ( { name : "conftest-kubernetes" , base : "conftest" , dependencies : [ { name : "kubernetes" } ] , docs : dedent `
    This provider automatically generates [conftest modules](${ moduleTypeUrl }) for \`kubernetes\` and
    \`helm\` modules in your project. A \`conftest\` module is created for each of those module types.

    Simply add this provider to your project configuration, and configure your policies. Check out the below
    reference for how to configure default policies, default namespaces, and test failure thresholds for the generated
    modules.

    See the [conftest example project](${ gitHubUrl }) for a simple
    usage example.
  ` , handlers : { augmentGraph : async ( { ctx , modules } ) => { const provider = ctx . provider as ConftestProvider ; const allModuleNames = new Set ( modules . map ( ( m ) => m . name ) ) ; return { addModules : await Bluebird . filter ( modules , async ( module ) => { return ( module . compatibleTypes . includes ( "helm" ) || module . compatibleTypes . includes ( "kubernetes" ) ) ; } ) . map ( ( module ) => { const baseName = "conftest-" + module . name ; let name = baseName ; let i = 2 ; while ( allModuleNames . has ( name ) ) { name = `${ baseName }-${ i ++ }` ; } allModuleNames . add ( name ) ; const policyPath = slash ( relative ( module . path , resolve ( ctx . projectRoot , provider . config . policyPath ) ) ) ; const isHelmModule = module . compatibleTypes . includes ( "helm" ) ; if ( isHelmModule ) { const runtimeDependencies = uniq ( collectTemplateReferences ( module . spec ) . filter ( ( ref ) => ref [ 0 ] === "runtime" && ref [ 2 ] ) . map ( ( ref ) => ref [ 2 ] ) ) ; return { kind : "Module" , type : "conftest-helm" , name , description : `conftest test for module '${ module . name }' (auto-generated by conftest-kubernetes)` , path : module . path , sourceModule : module . name , policyPath , namespace : provider . config . namespace , combine : false , runtimeDependencies , } ; } else { return { kind : "Module" , type : "conftest" , name , description : `conftest test for module '${ module . name }' (auto-generated by conftest-kubernetes)` , path : module . path , sourceModule : module . name , policyPath , namespace : provider . config . namespace , combine : false , files : module . include || [ "*.yaml" , "**/*.yaml" , "*.yml" , "**/*.yml" ] , } ; } } ) , } ; } , } , } ) ;