pipeline {
    agent any

     stages {
        stage('Get Code') {
            
            steps {
                // Obtener código del repo
                git 'https://github.com/josealm22/p1a.git'
                
                bat 'whoami'
                bat 'hostname'
                
            }
        }
        
        stage('Static') {
            steps {
                bat '''
                C:\\Python312\\Scripts\\flake8 --exit-zero --format=pylint app > flake8.out
                '''
                recordIssues tools: [flake8(name: 'Flake8', pattern: 'flake8.out')], qualityGates: [[threshold: 8, type: 'TOTAL', unstable: true], [threshold: 10, type: 'TOTAL', unstable: false]]
            }
        }
        
            stage('Unit') {
            
            steps {
                catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE'){
                
                bat '''
                    whoami
                    hostname
                    set PYTHONPATH=%WORKSPACE%
                    echo %WORKSPACE%
                    pytest --junitxml=result-unit.xml test\\unit
                    whoami
                    hostname
                    '''
                    
                
                    
                    
                    }
                }
            }

            stage('Rest') {
            
            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
                    '''
                    
                    junit 'result*.xml'
                        }
                    }    
                
                
                   
                  
            }
            stage('cobertura') {
            
            steps {
                
                bat '''
                coverage run --branch --source=app --omit=app\\__init__.py,app\\api.py -m pytest test\\unit 
                coverage xml
                '''
                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('Security') {
                steps {
                    bat'''
                        bandit --exit-zero -r . -f custom -o bandit.out --severity-level medium --msg-template "{abspath}:{line}: [{test_id}] {msg}"
                    '''
                    recordIssues tools: [pyLint(name: 'Bandit', pattern: 'bandit.out')], qualityGates: [[threshold: 2, type: 'TOTAL', unstable: true], [threshold: 4, type: 'TOTAL', unstable: false]]
                }
            }

            stage ('Performance'){
                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
                    '''
                    perfReport sourceDataFiles: 'flask.jtl'
                }
            }
        }    
}    

          
            