pipeline {
  agent none
  parameters {
    string(name: 'RELEASE', description: 'Release numeric name (e.g. 2022.10.01)', trim: true)
    string(name: 'PACKAGER_LIST', description: 'List of projects with debian package(s) (packagers) to build for this release separated with , (e.g. xivo,xivo-confgend,xivo-sysconfd)', trim: true)
  }
  stages {
    stage('welcome') {
      agent any
      input {
        message "Ready to start building XiVO ${RELEASE}?"
        ok "Yes, go go go !"
      }
      steps {
        echo "Welcome to this new RC build for XiVO ${RELEASE}!"
      }
    }
    stage('bump-xivo') {
      agent any
      steps {
        echo "Bumping xivo project if necessary"
        sh 'set -xe; ci-bump-xivo ${RELEASE}'
      }
    }
    stage('simulate-tag') {
      agent any
      steps {
        echo "Simulating the package tagging automation"
        sh 'set -xe; xivo-multi-tag rc ${RELEASE} --dry-run ${PACKAGER_LIST}'
        echo "Simulation done !"
      }
    }
    stage('apply-tag') {
      agent any
      input {
        message "Tag ${PACKAGER_LIST} like the simulation ?"
        ok "Ready"
      }
      steps {
        echo "Tagging for real"
        sh 'set -xe; xivo-multi-tag rc ${RELEASE} ${PACKAGER_LIST}'
        echo "Tagging done !"
      }
    }
    stage('create-local-rc') {
      agent any
      input {
        message "Click \"Create local repo\" to continue."
        ok "Create local repo"
      }
      steps {
        echo "Creating local repo xivo-${RELEASE}"
        build(
            job: 'create-local-repository',
            parameters: [
                string(name: 'REPOSITORY_NAME', value: "xivo-${RELEASE}"),
                string(name: 'PUBLISH', value: "yes"),
                string(name: 'REPOSITORY', value: "archive")],
                wait: true)
        echo "Local repo xivo-${RELEASE} was created."
      }
    }
    stage('build-packages') {
      agent any
      input {
        message "Click \"Build packages\" to continue."
        ok "Build packages"
      }
      steps {
        echo "Now build job will be ran for each package: ${PACKAGER_LIST}."
        echo "And they will be launched with these parameters:"
        echo "- TAG_OR_BRANCH=${RELEASE}-rc"
        echo "- BUILD_TYPE=rc"

        script {
          def jobNames = "$PACKAGER_LIST".split(",")

          jobNames.each { jobName ->
            def job_exec_status = build job: jobName,
            propagate: false,
            parameters: [
              string(name: 'BRANCH_OR_TAG', value: "$RELEASE-rc" ),
              string(name: 'BUILD_TYPE', value: 'rc')
            ]
            if(job_exec_status.result == 'SUCCESS' || job_exec_status.result == 'UNSTABLE'){
              echo "Job for ${jobName} was successful."
            } else {
              def apiRequest = "http://localhost:8080/job/${jobName}/${job_exec_status.number}/consoleText"
              def buildLog = sh(script: "curl -s -X GET ${apiRequest}", returnStdout: true)
              if (buildLog.contains("${params.RELEASE} is already tagged, cannot continue.")){
                echo "Build job for ${jobName} has been already ran."
              } else {
                error "Build job for ${jobName} has failed. Exiting pipeline."
              }
            }
          }
        }
        echo "All packages are built."
      }
    }
    stage('create-release-rc') {
      agent any
      input {
        message "Click \"Release RC\" to continue."
        ok "Release RC"
      }
      steps {
        echo "Launching job create-rc-for-release with"
        echo "- NAME=xivo-${RELEASE}"
        echo "- SOURCE=${SOURCE}"
        echo "- BUILT_PACKAGES=${PACKAGER_LIST}"
        build(
            job: 'create-rc-for-release',
            parameters: [
                string(name: 'NAME', value: "xivo-${RELEASE}"),
                string(name: 'SOURCE', value: "${SOURCE}"),
                string(name: 'BUILT_PACKAGES', value: "${PACKAGER_LIST}")],
            wait: true)
        echo "Created rc for release from xivo-${RELEASE}."
      }
    }
  }
}
