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

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

    // build each 6 hours
    triggers { cron("H H/6 * * *") }

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

    stages {
        stage("Run E2E tests against master branch") {
            parallel {
                stage('Run Happy path tests') {
                    steps {
                        build job: 'basic-MultiUser-Che-check-e2e-tests-against-k8s',
                                parameters: [
                                        string(name: 'cheImageRepo',
                                                value: 'maxura/che-server'),

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

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

                                        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')
                                ]
                    }
                }

                stage('Run devfile tests') {
                    steps {
                        build job: 'basic-MultiUser-Che-check-e2e-tests-against-k8s',
                                parameters: [
                                        string(name: 'cheImageRepo',
                                                value: 'maxura/che-server'),

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

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

                                        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')
                                ]
                    }
                }

                stage('Run Git SSH flow tests') {
                    steps {
                        withCredentials([string(credentialsId: 'e45af3e6-8061-4d02-b187-b1c3bb133d3a', variable: 'github_oauth_token')]) {
                            build job: 'basic-MultiUser-Che-check-e2e-tests-against-k8s',
                                    parameters: [
                                            string(name: 'cheImageRepo',
                                                    value: 'maxura/che-server'),

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

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

                                            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 {
        failure {
            script {
                if (emails != "") {
                    mail to: emails,
                        subject: "Jenkins Job $currentBuild.fullDisplayName has failed.",
                        body: "Build $currentBuild.absoluteUrl has failed."
                }
            }
        }
    }
}