#!/usr/bin/env groovy

pipeline {
    agent {
        label 'slave-group-release'
    }

    parameters {
        string(name: 'version', defaultValue: '0.0.0.Qualifier', description: 'Release version')
        string(name: 'nextVersion', defaultValue: '', description: 'Next release (blank to stay on current SNAPSHOT)')
        gitParameter(name: 'branch', defaultValue: 'origin/master', branchFilter: 'origin/(.*)', type: 'PT_BRANCH', description: 'Branch to release from', sortMode: 'DESCENDING_SMART')
    }

    options {
        timeout(time: 3, unit: 'HOURS')
        timestamps()
        buildDiscarder(logRotator(numToKeepStr: '100', daysToKeepStr: '61'))
    }

    stages {
        stage('Prepare') {
            steps {
                script {
                    env.MAVEN_HOME = tool('Maven')
                    env.MAVEN_OPTS = "-Xmx1500m -XX:+HeapDumpOnOutOfMemoryError"
                    env.JAVA_HOME = tool('JDK 17')
                }

                sh returnStdout: true, script: 'cleanup.sh'
            }
        }

        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Commit Proto.lock Files') {
            steps {
                script {
                    if ("${version}".endsWith('Final')) {
                        sh "$MAVEN_HOME/bin/mvn clean install -B -V -e -DskipTests -DcommitProtoLockChanges=true"
                        sh "git add '**/proto.lock'"
                        sh "git diff-index --quiet HEAD || git commit -m 'Committing proto.lock files for ${version}'"
                    }
                }
            }
        }

        stage('Commit version for documentation') {
            steps {
                script {
                    sh "sed -i \"s/^:ispn_version:.*\$/:ispn_version: ${version}/\" documentation/src/main/asciidoc/topics/attributes/community-attributes.adoc"
                    sh "git add 'documentation/src/main/asciidoc/topics/attributes/community-attributes.adoc'"
                    sh "git diff-index --quiet HEAD || git commit -m 'Committing ${version} to doc attribute file'"
                }
            }
        }

        stage('Version') {
            steps {
                sh "$MAVEN_HOME/bin/mvn -B -V versions:set -DnewVersion=${version} -DprocessAllModules=true"
                sh "$MAVEN_HOME/bin/mvn -B -V versions:set-property -Dproperty=version.infinispan -DnewVersion=${version}"
            }
        }

        stage('Deploy') {
            steps {
                sh "$MAVEN_HOME/bin/mvn -B -V -Pcommunity-release -Pdistribution -DskipTests clean deploy -Dinfinispan.brand.version=${version}"
            }
        }

        stage('Tag') {
            steps {
                // Commit and tag once everything is good
                sh "$MAVEN_HOME/bin/mvn -B -V scm:checkin -Dmessage=\"Releasing version ${version}\" -DpushChanges=false"
                sh "$MAVEN_HOME/bin/mvn -B -V scm:tag -Dtag=${version}"
            }
        }

        stage('Next version') {
            when {
                expression { params.nextVersion != '' }
            }
            steps {
                sh "$MAVEN_HOME/bin/mvn -B -V versions:set -DnewVersion=${nextVersion} -DprocessAllModules=true"
                sh "$MAVEN_HOME/bin/mvn -B -V versions:set-property -Dproperty=version.infinispan -DnewVersion=${nextVersion}"
                sh "$MAVEN_HOME/bin/mvn -B -V -Dmessage='next version ${nextVersion}' -DscmVersion=${branch} -DscmVersionType=branch scm:checkin"
            }
        }
    }

    post {
        always {
            // Clean
            sh 'git clean -fdx -e "*.hprof" || echo "git clean failed, exit code $?"'
        }
        failure {
            echo "post build status: failure"
            emailext to: '${DEFAULT_RECIPIENTS}', subject: '${DEFAULT_SUBJECT}', body: '${DEFAULT_CONTENT}'
        }

        success {
            echo "post build status: success"
            emailext to: '${DEFAULT_RECIPIENTS}', subject: '${DEFAULT_SUBJECT}', body: '${DEFAULT_CONTENT}'
        }
    }
}
