pipeline {
    agent { label 'rhel7-8gb' }

    options {
        timestamps()
        timeout(time: 6, unit: 'HOURS')
        buildDiscarder(logRotator(artifactDaysToKeepStr: '',
                artifactNumToKeepStr: '', daysToKeepStr: '60', numToKeepStr: '30'))
    }

    triggers { cron("H 1 * * *") }

    parameters {
        string(name: 'emails',
                defaultValue: 'dnochevn@redhat.com, oshmarai@redhat.com',
                description: 'Comma-separated list of emails to send results')
    }

    stages {
        stage("Run E2E tests") {
            steps {
                script {
                    catchError(buildResult: 'UNSTABLE', stageResult: 'FAILED') {
                        stage('Run Happy path tests') {
                            retry(2) {
                                build job: 'basic-MultiUser-Che-check-e2e-tests-against-k8s',
                                        parameters: [
                                                booleanParam(name: 'cheSingleHost',
                                                        value: true),

                                                string(name: 'cheImageRepo',
                                                        value: 'quay.io/eclipse/che-server'),

                                                string(name: 'cheImageTag',
                                                        value: 'nightly'),

                                                booleanParam(name: 'buildChe',
                                                        value: false),

                                                string(name: 'ghprbSourceBranch',
                                                        value: 'master'),

                                                string(name: 'ghprbPullId',
                                                        value: ''),

                                                string(name: 'e2eTestToRun',
                                                        value: 'test-happy-path'),

                                                string(name: 'testWorkspaceDevfileUrl',
                                                        value: 'https://raw.githubusercontent.com/eclipse/che/master/tests/e2e/files/happy-path/happy-path-workspace.yaml'),

                                                booleanParam(name: 'createTestWorkspace',
                                                        value: true),

                                                string(name: 'e2eTestParameters',
                                                        value: ''),

                                                string(name: 'chectlPackageUrl',
                                                        value: ''),

                                                string(name: 'cheE2eImageTag',
                                                        value: 'nightly')
                                        ]
                            }
                        }
                    }

                    catchError(buildResult: 'UNSTABLE', stageResult: 'FAILED') {
                        stage('Run devfile tests') {
                            retry(2) {
                                build job: 'basic-MultiUser-Che-check-e2e-tests-against-k8s',
                                        parameters: [
                                                booleanParam(name: 'cheSingleHost',
                                                        value: true),

                                                string(name: 'cheImageRepo',
                                                        value: 'quay.io/eclipse/che-server'),

                                                string(name: 'cheImageTag',
                                                        value: 'nightly'),

                                                booleanParam(name: 'buildChe',
                                                        value: false),

                                                string(name: 'ghprbSourceBranch',
                                                        value: 'master'),

                                                string(name: 'ghprbPullId',
                                                        value: ''),

                                                string(name: 'e2eTestToRun',
                                                        value: 'test-all-devfiles'),

                                                string(name: 'testWorkspaceDevfileUrl',
                                                        value: ''),

                                                booleanParam(name: 'createTestWorkspace',
                                                        value: false),

                                                string(name: 'e2eTestParameters',
                                                        value: ''),

                                                string(name: 'chectlPackageUrl',
                                                        value: ''),

                                                string(name: 'cheE2eImageTag',
                                                        value: 'nightly')
                                        ]
                            }
                        }
                    }

                    catchError(buildResult: 'UNSTABLE', stageResult: 'FAILED') {
                        stage('Run Git SSH flow tests') {
                            retry(2) {
                                withCredentials([string(credentialsId: 'e45af3e6-8061-4d02-b187-b1c3bb133d3a', variable: 'github_oauth_token')]) {
                                    build job: 'basic-MultiUser-Che-check-e2e-tests-against-k8s',
                                            parameters: [
                                                    booleanParam(name: 'cheSingleHost',
                                                        value: true),

                                                    string(name: 'cheImageRepo',
                                                            value: 'quay.io/eclipse/che-server'),

                                                    string(name: 'cheImageTag',
                                                            value: 'nightly'),

                                                    booleanParam(name: 'buildChe',
                                                            value: false),

                                                    string(name: 'ghprbSourceBranch',
                                                            value: 'master'),

                                                    string(name: 'ghprbPullId',
                                                            value: ''),

                                                    string(name: 'e2eTestToRun',
                                                            value: 'test-git-ssh'),

                                                    string(name: 'testWorkspaceDevfileUrl',
                                                            value: ''),

                                                    booleanParam(name: 'createTestWorkspace',
                                                            value: false),

                                                    string(name: 'e2eTestParameters',
                                                            value: "-e TS_GITHUB_TEST_REPO_ACCESS_TOKEN=$github_oauth_token  -e TS_GITHUB_TEST_REPO=chepullreq4/Spoon-Knife -e NODE_TLS_REJECT_UNAUTHORIZED=0"),

                                                    string(name: 'chectlPackageUrl',
                                                            value: ''),

                                                    string(name: 'cheE2eImageTag',
                                                            value: 'nightly')
                                            ]
                                }
                            }
                        }
                    }
                }
            }
        }

    }

    post {
        success {
            script {
                if (emails != "") {
                    mail to: emails,
                            subject: "Jenkins Job $currentBuild.fullDisplayName has passed successfully.",
                            body: "Build $currentBuild.absoluteUrl has passed successfully."
                }
            }
        }
    
        failure {
            script {
                if (emails != "") {
                    mail to: emails,
                        subject: "Jenkins Job $currentBuild.fullDisplayName has failed.",
                        body: "Build $currentBuild.absoluteUrl has failed."
                }
            }
        }
    }
}
