pipeline {
    agent none

     stages {
        stage('Obtener código y análisis estático') {
            parallel {  
                stage ('Get code'){
                agent {label 'agente1'}       
                    steps {
                    // Obtener código del repo
                    git 'https://github.com/josealm22/p1a.git'
                    bat'''
                    echo %WORKSPACE%
                    whoami
                    hostname
                    '''
                    }
                }
        
                stage('Static') {
                agent {label 'agente2'}
                    steps {
                        bat '''
                        C:\\Python312\\Scripts\\flake8 --exit-zero --format=pylint app > flake8.out
                        echo %WORKSPACE%
                        whoami
                        hostname
                        '''
                        recordIssues tools: [flake8(name: 'Flake8', pattern: 'flake8.out')], qualityGates: [[threshold: 8, type: 'TOTAL', unstable: true], [threshold: 10, type: 'TOTAL', unstable: false]]
                    }
                }
            }
        }
        stage('Pruebas unitarias, rest y cobertura'){    
            parallel {
                stage('Unit') {
                agent {label 'agente1'}
                    steps {
                        catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE'){
                        bat '''
                        set PYTHONPATH=%WORKSPACE%
                        pytest --junitxml=result-unit.xml test\\unit
                        echo %WORKSPACE%
                        whoami
                        hostname
                        '''
                        stash name: 'unit-test-results', includes: 'result-unit.xml'
                        }
                    }
                }
                stage('Rest') {
                agent {label 'agente2'}
                    steps {
                            catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE'){
                            bat '''
                            set FLASK_APP=app\\api.py
                            set FLASK_ENV=development
                            start flask run
                            start java -jar test\\wiremock\\wiremock.jar --port 9090 --root-dir test\\wiremock
                            set PYTHONPATH=%WORKSPACE%
                            pytest --junitxml=result-rest.xml test\\rest
                            echo %WORKSPACE%
                            whoami
                            hostname
                            '''
                            unstash 'unit-test-results'
                            junit 'result*.xml'
                            }
                        }
                }
    
                stage('cobertura') {
                agent {label 'agente3'}
                    steps {
                            bat '''
                            coverage run --branch --source=app --omit=app\\__init__.py,app\\api.py -m pytest test\\unit
                            coverage xml
                            coverage report -m > coverage.txt
                            type coverage.txt
                            echo %WORKSPACE%
                            whoami
                            hostname
                            '''
                            cobertura autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: 'coverage.xml', conditionalCoverageTargets: '90, 80, 0', failUnhealthy: false, failUnstable: false, lineCoverageTargets: '95, 85, 0', maxNumberOfBuilds: 0, onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false
                    }
                }
            }
        }
        stage('Pruebas de seguridad y rendimiento'){
            parallel{
                stage('Security') {
                agent {label 'agente1'}
                    steps {
                        bat'''
                        bandit --exit-zero -r . -f custom -o bandit.out --severity-level medium --msg-template "{abspath}:{line}: [{test_id}] {msg}"
                        echo %WORKSPACE%
                        whoami
                        hostname
                        '''
                        recordIssues tools: [pyLint(name: 'Bandit', pattern: 'bandit.out')], qualityGates: [[threshold: 2, type: 'TOTAL', unstable: true], [threshold: 4, type: 'TOTAL', unstable: false]]
                    }
                }

                stage ('Performance'){
                agent {label 'agente2'}
                    steps {
                        bat'''
                            set FLASK_APP=app\\api.py
                            set FLASK_ENV=development
                            start flask run
                            E:\\CursoAvanzadoDevops\\Actividad2\\Jmeter\\apache-jmeter-5.6.2\\bin\\jmeter -n -t test\\jmeter\\flask.jmx -f -l flask.jtl
                            echo %WORKSPACE%
                            whoami
                            hostname
                        '''
                        perfReport sourceDataFiles: 'flask.jtl'
                    }
                }
            }    
        }    
    }   
}      
            