def runCommand(String command) {
    if (isUnix()) {
        sh(script: command)
    } else {
        bat(script: command)
    }
}

pipeline {
    agent {
        label 'Deb-Agent'  // Имя или метка агента
    }

	stages {
        stage('Remove oint') {
            steps {
                script {
                    // Удаляем oint, если он установлен
                    sh 'dpkg -r oint || echo "oint not installed"'
                }
            }
        }

        stage('Download oint package') {
            steps {
                script {
                    // Скачиваем новый deb-пакет oint
                    sh 'wget -O oint_1.30.0_all_en.deb https://jenkins.openintegrations.dev/job/OpiBuild/job/OpiRelease/lastSuccessfulBuild/artifact/1.30.0/oint_1.30.0_all_en.deb '
                }
            }
        }

        stage('Install oint package') {
            steps {
                script {
                    // Устанавливаем скаченный пакет
                    sh 'dpkg -i oint_1.30.0_all_en.deb'
                }
            }
        }

      	stage('Prepare'){
         		steps{
                    sh 'cd ./src/en/OInt && opm build && opm install oint-1.30.0.ospx && rm oint-1.30.0.ospx'
            		withCredentials([string(credentialsId: 'gpgkey', variable: 'GPGKEY')]) {
              			sh 'gpg --quiet --batch --yes --decrypt --passphrase="$GPGKEY" --output ./data.json ./data.json.gpg'
            		}
         		}
      	}
        stage('Create ReportPortal launch'){
				steps{
					sh 'oscript ./ci/os/rp_start.os "CLI"'
				}
		}

		stage('Testing-Ozon') {
			steps {			
			catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {   
				runCommand('1testrunner -run "./src/en/OInt/tests/Modules/internal/OPI_TestsCLI.os" "OzonAPI_AttributesAndFeatures"')
			}
			catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {   
				runCommand('1testrunner -run "./src/en/OInt/tests/Modules/internal/OPI_TestsCLI.os" "OzonAPI_UploadingAndUpdatingProducts"')
			}
			catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {   
				runCommand('1testrunner -run "./src/en/OInt/tests/Modules/internal/OPI_TestsCLI.os" "OzonAPI_Barcodes"')
			}
			catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {   
				runCommand('1testrunner -run "./src/en/OInt/tests/Modules/internal/OPI_TestsCLI.os" "OzonAPI_PricesAndStocks"')
			}
			catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {   
				runCommand('1testrunner -run "./src/en/OInt/tests/Modules/internal/OPI_TestsCLI.os" "OzonAPI_PromotionsManagement"')
			}
			catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {   
				runCommand('1testrunner -run "./src/en/OInt/tests/Modules/internal/OPI_TestsCLI.os" "OzonAPI_WarehousesManagement"')
			}
			catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {   
				runCommand('1testrunner -run "./src/en/OInt/tests/Modules/internal/OPI_TestsCLI.os" "OzonAPI_FBOScheme"')
			}
			}
		}

	}
	post{
		always{
			script {
                catchError() {   
				    runCommand('oscript ./ci/os/rp_stop.os "en"')
			    }
				withCredentials([string(credentialsId: 'gpgkey', variable: 'GPGKEY')]) {
					sh 'rm "./data.json.gpg"'
					sh 'gpg --batch --symmetric --cipher-algo AES256 --passphrase="$GPGKEY" ./data.json'
					sh 'rm "./data.json"'
				}
				withCredentials([gitUsernamePassword(credentialsId: 'gitmain', gitToolName: 'Default')]) {
					sh "git config user.email vitaly.the.alpaca@gmail.com"
					sh 'git config user.name "Vitaly the Alpaca (bot)"'
					sh "git config --global core.ignorecase true"
					sh "git add ."
					sh 'git commit -m "Test data update (Jenkins)"'
					sh "git push origin HEAD:main"
				}
			}
		}
	}
}