// Pipeline de scan de projet avec SonarQube
def call() {
    pipeline {
        agent {
            label 'docker'
        }
        
        environment {
            projectDir = "${env.project_name}_${env.BUILD_ID}" 
        }

        stages {
            stage("Package project") {
                when {
                    not {
                        triggeredBy 'TimerTrigger'
                    }
                }
                steps {
                    script {
                        stage("Clone repository") {
                            checkout scm: 
                                [
                                    $class: 'GitSCM', 
                                    userRemoteConfigs: [[url: env.repository_url, credentialsId: 'jenkins-forge-ssh']], 
                                    branches: [[name: env.ref]],
                                    extensions: [
                                        [$class: 'RelativeTargetDirectory', relativeTargetDir: env.projectDir ],
                                        [$class: 'CloneOption', noTags: false, shallow: false, depth: 0, reference: ''],
                                        [$class: 'WipeWorkspace' ]
                                    ]
                                ], 
                                changelog: false, 
                                poll: false
                        }
                    
                        stage("Scan project") {
                            dir(env.projectDir) {
                                withCredentials([
                                    string(credentialsId: 'SONARQUBE_URL', variable: 'SONARQUBE_URL'),
                                    string(credentialsId: 'SONARQUBE_TOKEN', variable: 'SONARQUBE_TOKEN'),
                                ]) {
                                    sh """
                                    docker run \
                                        --rm \
                                        -e SONAR_HOST_URL="${env.SONARQUBE_URL}" \
                                        -e SONAR_LOGIN="${env.SONARQUBE_TOKEN}" \
                                        -v "${env.WORKSPACE}/${env.projectDir}/:/usr/src" \
                                        sonarsource/sonar-scanner-cli \
                                        -Dsonar.projectKey=${env.sonarqubeProjectKey} \
                                        -Dsonar.projectVersion=${env.ref}
                                    """
                                }

                                // On notifie le canal Rocket.Chat du scan
                                // rocketSend (
                                //     avatar: 'https://jenkins.cadol.es/static/b5f67753/images/headshot.png',
                                //     message: """
                                //     Le projet ${env.project_name} a été scanné par SonarQube.
                                    
                                //     - [Voir les résultats](${env.SONARQUBE_URL}/dashboard?id=${env.sonarqubeProjectKey})
                                //     - [Visualiser le job](${env.RUN_DISPLAY_URL})
                                    
                                //     @${env.sender_login}
                                //     """.stripIndent(),
                                //     rawMessage: true,
                                // )                          
                            }
                        }
                    }
                }
                post {
                    always {
                        sh "rm -rf '${env.projectDir}'"
                    }
                }
            }
        }
    }
}