def coverageStatus(String sha, String base) {
              step([
                    $class: 'GitHubCommitStatusSetter',
                    commitShaSource: [$class: 'ManuallyEnteredShaSource', sha: sha],
                    contextSource: [$class: 'ManuallyEnteredCommitContextSource', context: 'coverage/line'],
                    reposSource: [
                        $class: 'ManuallyEnteredRepositorySource',
                        url: 'https://github.yandex-team.ru/mail/logbroker-client-common'
                    ],
                    errorHandlers: [[$class: 'ShallowAnyErrorHandler']],
                    statusResultSource: [
                        $class: 'CoberturaCoverageStatusResultSource',
                        baseJob: base
                    ]
               ])
}

def version() {
    def commit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim().take(6)
    def revname = sh(returnStdout: true, script: "git describe --tags || git symbolic-ref -q --short HEAD || echo '${env.BRANCH_NAME}'").trim()
    def version = "${revname}.${currentBuild.number}"
    echo """
==================================
|         |
| VERSION | ${version}
|         |
==================================
"""
    return version
}

pipeline {
    agent {
        label 'maildev'
    }
    options { timestamps() }
    environment {
        VERSION = version()
    }
    stages {
        stage('prepare') {
            steps {
                script {
                    docker.withRegistry('https://registry.yandex.net', 'robot-gerrit-docker-registry-creds') {
                        sh 'echo "Update docker registry credentials"'
                    }
                }
            }
        }
//         stage('test') {
//             agent {
//                 dockerfile {
//                     filename '.jenkins/Dockerfile'
//                 }
//             }
//             steps {
//                 sh 'pytest --cov=logbroker_processors --cov=logbroker_client_common --cov-report html --cov-report xml --cov-report term --junitxml=junit.xml'
//             }
//
//             post {
//                 always {
//                     cobertura coberturaReportFile: 'coverage.xml', sourceEncoding: 'UTF_8'
//                     junit allowEmptyResults: true, testResults: 'junit.xml'
//                 }
//             }
//         }
        stage('set coverage status') {
            when {
                expression { env.CHANGE_ID != null && !env.CHANGE_ID.isEmpty() }
            }
            steps {
                coverageStatus(env.GIT_COMMIT, "mail/logbroker-client-common/master")
            }
        }
        stage('build and deploy') {
            steps {
                script {
                    docker.withRegistry('https://registry.yandex.net', 'robot-gerrit-docker-registry-creds') {
                        def logconsumer = docker.build("registry.yandex.net/mail/logconsumer:${env.VERSION}", "-f docker/Dockerfile .")
                        logconsumer.push()

                        echo """
========================================
|          |
| IMAGE ID | ${logconsumer.id}
|          |
========================================
                        """
                    }
                }
            }
            post {
                always {
                    deleteDir()
                }
            }
        }
    }
}
