pipeline {
    agent {label 'linux-spsdk'}
    parameters {
        string(name: 'JIRA_ID', defaultValue: '', description: 'The release jira ticket')
        string(name: 'SPSDK_BRANCH', defaultValue: 'master', description: 'Branch of SPSDK')
    }
    environment {
        JIRA_TOKEN = credentials('spsdk-jira-token')
        GITHUB_TOKEN = credentials('spsdk-github-token')
    }
    stages {
        stage('Checkout') {
            steps {
                // checkout SPSDK
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: env.SPSDK_BRANCH]],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [
                        [$class: 'CloneOption', noTags: false, reference: '', shallow: false],
                        [$class: 'LocalBranch', localBranch: "**"],
                        [$class: 'RelativeTargetDirectory', relativeTargetDir: 'spsdk']
                    ],
                    submoduleCfg: [],
                    userRemoteConfigs: [[credentialsId: 'git_credentials', url: 'ssh://git@bitbucket.sw.nxp.com/spsdk/spsdk.git']]
                ])
                // checkout release tools
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: 'master']],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [
                        [$class: 'RelativeTargetDirectory', relativeTargetDir: 'release_tools']
                    ],
                    submoduleCfg: [],
                    userRemoteConfigs: [[credentialsId: 'git_credentials', url: 'ssh://git@bitbucket.sw.nxp.com/spsdk/release-tools.git']]
                ])
                // checkout SPSDK Plugins
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: "master"]],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'spsdk_plugins']],
                    submoduleCfg: [],
                    userRemoteConfigs: [[credentialsId: 'git_credentials', url: 'ssh://git@bitbucket.sw.nxp.com/spsdk/spsdk_plugins.git']]
                ])
            }
        }
        stage("Setup") {
            steps {
                withPythonEnv('python3.12') {
                    sh 'python -m pip install --upgrade pip'
                    sh 'pip install --upgrade ./spsdk[all]'
                    sh 'pip install --upgrade ./release_tools'
                    sh 'pip install --upgrade ./spsdk_plugins/codecheck'
                }
            }
        }
        stage("CLR") {
            steps {
                withPythonEnv('python3.12') {
                    sh "release_tools clr run --root-dir spsdk --report-output spsdk/clr_out/clr.txt"
                    sh "cd spsdk && dependencies --github_token $GITHUB_TOKEN --output clr_out/dep_check.txt check --strict"
                    sh "cd spsdk && dependencies --github_token $GITHUB_TOKEN --output clr_out/dependencies.txt print"
                }
            }
        }
        stage("Upload Jira") {
            when {
                expression {
                    return env.JIRA_ID != '';
                }
            }
            steps {
                withPythonEnv('python3.12') {
                    sh "release_tools clr push-jira --jira-token $JIRA_TOKEN --parent-jira-issue $JIRA_ID --report-dir spsdk/clr_out"
                }
            }
        }
    }
    post {
        always {
            archiveArtifacts artifacts: 'spsdk/clr_out/**', fingerprint: true
        }
    }
}