@Library('infraLib') _

def independentProjects = [
  "delphi-monorepos/delphi-slz/libs/slz_config",
  "delphi-monorepos/delphi-slz/libs/slz_storage",
  "delphi-monorepos/delphi-slz/libs/slz_notification",
  "delphi-monorepos/delphi-slz/lambdas/slz_dbx_job_runner"
]

def dependOnSlzLibs = [
  "delphi-monorepos/delphi-slz/tasks/slz_hasher",
  "delphi-monorepos/delphi-slz/lambdas/slz_check_complete",
  "delphi-monorepos/delphi-slz/lambdas/slz_uow_reset",
  "delphi-monorepos/delphi-slz/lambdas/slz_job_manager",
  "delphi-monorepos/delphi-slz/lambdas/slz_job_executor",
  "delphi-monorepos/delphi-slz/lambdas/slz_house_keeper",
  "delphi-monorepos/delphi-slz/lambdas/slz_meta_manager",
  "delphi-monorepos/delphi-slz/lambdas/slz_meta_status_reset",
  "delphi-monorepos/delphi-slz/lambdas/slz_uow_monitoring",
  "delphi-monorepos/delphi-slz/lambdas/slz_prepare_configuration"
]

def slzDownloader = [
  "delphi-monorepos/delphi-slz/tasks/slz_downloader"
]

def dependOnSlzDownloader = [
  "delphi-monorepos/delphi-slz/lambdas/slz_availability_check",
  "delphi-monorepos/delphi-slz/lambdas/slz_clean_up",
  "delphi-monorepos/delphi-slz/lambdas/slz_meta_retrigger"
]

def stepFunctions = [
  "delphi-monorepos/delphi-slz/stepfunctions/slz_step_function",
  "delphi-monorepos/delphi-slz/stepfunctions/slz_meta_step_function"
]

def globalBuildStages = [
  independentProjects,
  dependOnSlzLibs,
  slzDownloader,
  dependOnSlzDownloader,
  stepFunctions,
]

def buildJobsFullList = []
globalBuildStages.each { buildJobsFullList += it}

def checkBoxParamProjects = utility.checkBox(
  "PROJECTS", // name
  buildJobsFullList.join(','), // values
  "", //default value
)

def buildJobList = []
def branchId = env.JOB_NAME.split('/')[-1]



properties(
  [parameters([
    choice(
      choices: [
        'Build latest changes',
        'Select manually',
        'Build all'
      ],
      description: 'Choose what module to build',
      name: 'PROJECT_CHOICE'
    ),
    checkBoxParamProjects,
    choice(
      choices: [
        'no',
        'yes'
      ],
      description: "Build Python Wheels and push to PyPI.",
      name: "PYTHON_DEPS_BUILD_UPLOAD_MODE"
    ),
    choice(
      choices: [
        'default',
        'yes',
        'no',
      ],
      description: 'Whether to trigger testing for all components. By default it\'s only triggered for develop, release* and master branches.',
      name: 'TEST_ALL'
    ),
    choice(
      choices: [
        'default',
        'yes',
        'no',
      ],
      description: 'Whether to build python package. By default it\'s only build for develop, release* and master branches.',
      name: 'BUILD_PYTHON_PACKAGE'
    ),
    choice(
      choices: [
        'default',
        'yes',
        'no',
      ],
      description: '''Whether to trigger deployment pipeline(build deployment artifact, create octopus release, deploy it).
      By default it\'s only triggered for develop, release* and master branches.
      ''',
      name: 'TRIGGER_DEPLOY'
    ),
    choice(
      choices: [
        'default',
        'dev',
        'qa',
        'stage',
        'skip_deployment'
      ],
      description: 'Set environment for deployment. Skipped by default master/release*, dev by default for all other branches',
      name: 'DEPLOY_ENV_CHOICE'
    ),
    choice(
      choices: [
        'error',
        'overwrite',
        'skip_build'
      ],
      description: "Docker image upload mode - ovewrite if the image with such tag already exists, raise error if exists, or skip build part if exists.",
      name: 'IMAGE_UPLOAD_MODE'
    ),
    choice(
      choices: [
        'error',
        'overwrite',
        'ignore'
      ],
      description: 'Defines whether to raise error, overwrite or ignore if python package of specified version already exists in Pypi.',
      name: 'PYTHON_PACKAGE_UPLOAD_MODE'
    ),
    choice(
      choices: [
        'error',
        'overwrite',
        'ignore'
      ],
      description: 'Defines whether to raise error, overwrite or ignore if zipPackage of specified version already exists in Octopus.',
      name: 'ZIP_PACKAGE_UPLOAD_MODE'
    ),
    choice(
      choices: [
        'yes',
        'no'
      ],
      description: "Don't raise errors in case release already exists",
      name: 'FAIL_IF_RELEASE_EXISTS'
    ),
    choice(
        choices: [
          'yes',
          'no',
        ],
        description: "Run integration tests if possible",
        name: 'RUN_INTEGRATION_TESTS'
    ),
    choice(
			choices: [
				'yes',
				'no'
			],
			description: "Run linter test before build",
			name: 'RUN_LINTER'
    )
  ])]
)


pipeline {
  agent {
    label 'linux-agents'
  }
  options { disableConcurrentBuilds() }
  environment {
    AWS_PROFILE             = "gdb-delphi-dev"
    PROJECT_GROUP           = "delphi"
    NOTIFICATIONS_CHANNEL   = "delphi-slz-cicd-alerts"
  }
  stages {
    //get parameters map
    stage('Define build elements') {
      steps {
        script {
          customProjectChoice = params.PROJECTS != null ? params.PROJECTS.split(',') : []
          buildJobList = pythonPackage_v2.defineBuildElements(buildJobsFullList, params.PROJECT_CHOICE, customProjectChoice)
          buildJobList.each {
            println "${it}"
          }
        }
      }
    }
    stage("Build stage 1 - independentProjects") {
      steps {
        script {
          stagesList = pythonPackage_v2.preparePythonPackageBuildStages(params, independentProjects, buildJobList, branchId)
          parallel(stagesList)
        }
      }
    }
    stage("Build stage 2 - dependOnSlzLibs") {
      steps {
        script {
          stagesList = pythonPackage_v2.preparePythonPackageBuildStages(params, dependOnSlzLibs, buildJobList, branchId)
          parallel(stagesList)
        }
      }
    }
    stage("Build stage 3 - slzDownloader") {
      steps {
        script {
          stagesList = pythonPackage_v2.preparePythonPackageBuildStages(params, slzDownloader, buildJobList, branchId)
          parallel(stagesList)
        }
      }
    }
    stage("Build stage 4 - dependOnSlzDownloader") {
      steps {
        script {
          stagesList = pythonPackage_v2.preparePythonPackageBuildStages(params, dependOnSlzDownloader, buildJobList, branchId)
          parallel(stagesList)
        }
      }
    }
    stage("Build stage 5 - stepFunctions") {
      steps {
        script {
          stagesList = zipPackage.prepareStepFunctionBuildStages(params, stepFunctions, buildJobList, branchId)
          parallel(stagesList)
        }
      }
    }
    stage("Run autotests") {
      when {
        branch 'develop'
        //branch 'release'
      }
      steps {
        script {
          build job: "delphi-repos/delphi-qa-automation-DEV", wait: false
        }
      }
    }
  }
  post {
    always {
      sh "sudo chown -R ec2-user:ec2-user ${env.WORKSPACE}"
    }
    success {
      script {
        if (currentBuild.getPreviousBuild() &&
            currentBuild.getPreviousBuild().getResult().toString() != "SUCCESS") {
          utility.slackNotifyMainBranches(currentBuild.currentResult, env.NOTIFICATIONS_CHANNEL)
        }
      }
    }
    failure {
      script {
        utility.slackNotifyMainBranches(currentBuild.currentResult, env.NOTIFICATIONS_CHANNEL)
      }
    }
    cleanup {
        deleteDir()
    }
  }
}
