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

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

    environment {
        YQ_TOOL_URL='https://github.com/mikefarah/yq/releases/download/2.4.0/yq_linux_amd64'

        DEVFILE_URL="${WORKSPACE}/happy-path-workspace.yaml"
    }

    parameters {
        string(name: 'RELEASE_BRANCH',
                defaultValue: "7.12.x",
                description: 'Branch with pre-release version to take E2E Happy path test.')

        string(name: 'RELEASE_VERSION',
                defaultValue: "7.12.1",
                description: 'Version of Che-theia')

        string(name: 'CHECTL_PACKAGE_URL',
                defaultValue: "https://github.com/che-incubator/chectl/releases/download/20200501092240/chectl-linux-x64.tar.gz",
                description: 'URL address of chectl package tar ball')
    }

    stages {
        stage('Prepare Happy path devfile') {
            steps {
                withCredentials([string(credentialsId: 'e45af3e6-8061-4d02-b187-b1c3bb133d3a', variable: 'GITHUB_TOKEN')]) {
                    sh """
                      CHE_THEIA_META_YAML_URL='https://raw.githubusercontent.com/eclipse/che-plugin-registry/$RELEASE_BRANCH/v3/plugins/eclipse/che-theia/next/meta.yaml'

                      wget https://raw.githubusercontent.com/eclipse/che/$RELEASE_BRANCH/tests/e2e/files/happy-path/happy-path-workspace.yaml --no-check-certificate -O $DEVFILE_URL
                    
                      PR_CHECK_FILES_DIR=${WORKSPACE}/pr-check-files/che/$RELEASE_VERSION
                      PR_CHECK_FILES_GITHUB_URL="https://raw.githubusercontent.com/chepullreq4/pr-check-files/master/che/$RELEASE_VERSION"
                    
                      git clone https://chepullreq4:${GITHUB_TOKEN}@github.com/chepullreq4/pr-check-files.git
                      mkdir -p \$PR_CHECK_FILES_DIR
                    
                      wget $YQ_TOOL_URL
                      sudo chmod +x yq_linux_amd64
                    
                      wget \$CHE_THEIA_META_YAML_URL -O \$PR_CHECK_FILES_DIR/che_theia_meta.yaml
                      ./yq_linux_amd64 w -i \$PR_CHECK_FILES_DIR/che_theia_meta.yaml spec.containers[0].image quay.io/eclipse/che-theia:$RELEASE_VERSION
                      ./yq_linux_amd64 w -i \$PR_CHECK_FILES_DIR/che_theia_meta.yaml spec.initContainers[0].image quay.io/eclipse/che-theia-endpoint-runtime-binary:$RELEASE_VERSION
                    
                      # patch che/tests/e2e/files/happy-path/happy-path-workspace.yaml
                      sed -i "s|id: eclipse/che-theia/next|alias: che-theia\\n    reference: \$PR_CHECK_FILES_GITHUB_URL/che_theia_meta.yaml|" $DEVFILE_URL
                  
                      cat $DEVFILE_URL
                    
                      cp $DEVFILE_URL \${PR_CHECK_FILES_DIR}
                      cd \${PR_CHECK_FILES_DIR}
                      git add -A
                      git diff-index --quiet HEAD || git commit -m "Che $RELEASE_VERSION files for pre-release testing on Jenkins $JOB_URL"
                      git push
                    """
                }
            }
        }

        stage("Run E2E tests") {
            parallel {
                stage('Run Happy path tests against Eclipse Che release candidate') {
                    steps {
                        build job: 'basic-MultiUser-Che-check-e2e-tests-against-k8s',
                                parameters: [
                                        string(name: 'cheImageRepo',
                                                value: 'quay.io/eclipse/che-server'),

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

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

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

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

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

                                        string(name: 'testWorkspaceDevfileUrl',
                                                value: "https://raw.githubusercontent.com/chepullreq4/pr-check-files/master/che/$RELEASE_VERSION/happy-path-workspace.yaml"),

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

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

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

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

                stage('Run devfile tests against Eclipse Che release candidate') {
                    steps {
                        build job: 'basic-MultiUser-Che-check-e2e-tests-against-k8s',
                                parameters: [
                                        string(name: 'cheImageRepo',
                                                value: 'quay.io/eclipse/che-server'),

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

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

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

                                        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: CHECTL_PACKAGE_URL),

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

                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: 'quay.io/eclipse/che-server'),

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

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

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

                                                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: CHECTL_PACKAGE_URL),

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