pipeline {
    agent none
    environment {
        PJINTEG_REVIEW_CREDS = credentials("PJINTEG_GERRIT_REVIEW_CREDENTIALS")
        PJINTEG_ORANGEPI_CREDS = credentials("PJINTEG_ORANGEPI_CREDENTIALS")
        ORANGEPI_PASS = "${PJINTEG_ORANGEPI_CREDS_PSW}"
    }
    options {
        timestamps()
    }

    stages {
        stage ('Build agent stages') {
            agent {
                node {
                    label 'PJINTEG_BUILD_AGENT'
                }
            }
            stages {
                stage('Setup'){
                    steps {
                        cleanWs()
                    }
                }
                stage('Code Criteria Check') {
                    steps {
                        script {
                            withCredentials([usernamePassword(credentialsId: 'pfm_ci_http', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
                                sh """
                                    git clone --depth=1 https://www.tool.sony.biz/tv-gerrit/sony/android/sony-tools/MIB_StabilityTest --single-branch -b sony/external/CriteriaCheckScripts CriteriaCheckScripts
                                    cd CriteriaCheckScripts
                                    ./run_checkers.sh -r checkpatch.pl -o "--ignore SPDX_LICENSE_TAG,DEVICE_ATTR_FUNCTIONS,DEVICE_ATTR_RO,DEVICE_ATTR_RW,DEVICE_ATTR_WO,DEVICE_ATTR_PERMS" -i "libs/cJSON|gpt/cmd/"
                                    cd ../
                                    rm -rf CriteriaCheckScripts
                                """
                            }
                        }
                    }
                }
            }
        }

    }
    post {
        success {
            node('master') {
                dir ('build-on-commit') {
                    sh """
                        curl -L -u \${PJINTEG_REVIEW_CREDS_USR}:\${PJINTEG_REVIEW_CREDS_PSW} "https://www.tool.sony.biz/tv-gerrit/a/projects/sony%2Fandroid%2Fscript%2Fbuild-on-commit/branches/${PJINTEG_BOC_SCRIPT_BRANCH}/files/gerrit_review/content" | base64 --decode > gerrit_review
                        chmod +x gerrit_review
                        ./gerrit_review -m \"Pipeline build succeeded: ${RUN_DISPLAY_URL}\" -n ${params.GERRIT_CHANGE_NUMBER} -p ${params.GERRIT_PATCHSET_NUMBER}  -c 1
                    """
                }
            }
        }
        failure {
            node('master') {
                dir ('build-on-commit') {
                    sh """
                        curl -L -u \${PJINTEG_REVIEW_CREDS_USR}:\${PJINTEG_REVIEW_CREDS_PSW} "https://www.tool.sony.biz/tv-gerrit/a/projects/sony%2Fandroid%2Fscript%2Fbuild-on-commit/branches/${PJINTEG_BOC_SCRIPT_BRANCH}/files/gerrit_review/content" | base64 --decode > gerrit_review
                        chmod +x gerrit_review
                        ./gerrit_review -m \"Pipeline build failed: ${RUN_DISPLAY_URL}\" -n ${params.GERRIT_CHANGE_NUMBER} -p ${params.GERRIT_PATCHSET_NUMBER}  -c -1
                    """
                }
            }
        }
    }
}
