String GITHUB_REPOSITORY = 'python-fansifter-common'
String SLACK_BUILDS_CHANNEL = '#fansifter-builds'
String SLACK_ALERTS_CHANNEL = '#fansifter-alerts'

pipeline {
    agent any

    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        timestamps()
    }

    parameters {
        string(
            name: 'SHARED_LIBRARIES_VERSION',
            defaultValue: 'master',
            description: 'The version of the Jenkins shared libraries to use. Can be a branch, tag, Git revision or PR ref (e.g. pull/PR_NUMBER/merge).',
        )
    }

    triggers {
        issueCommentTrigger('.*retest this please.*')
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }

        stage('Compliance Checks') {
            steps {
                script {
                    complianceChecks()
                }
            }
        }

        stage('Unit Tests and Style Checks') {
            steps {
                script {
                    sh "docker compose run --rm --build lint-and-test"
                }
            }
        }

        stage('Static Application Security Tests') {
            steps {
                script {
                    echo "Running Static App Security Tests"
                    sastTests()
                }
            }
        }

        stage('Sonar Scan and Analysis') {
            steps {
                sonarScan project: GITHUB_REPOSITORY, language: 'py'
            }
        }

        stage('Create a release') {
            when {
                branch 'master'
                not {
                    changelog '.*Release version.*'
                }
            }
            steps {
                build job: 'publish-pypi-package-v2', parameters: [
                    [$class: 'StringParameterValue', name: 'GITREPO', value: GITHUB_REPOSITORY],
                    [$class: 'StringParameterValue', name: 'VERSION', value: 'minor'],
                    [$class: 'StringParameterValue', name: 'PYTHON_VERSION', value: '3.12']
                ]
                build job: 'publish-pypi-package-v2', parameters: [
                    [$class: 'StringParameterValue', name: 'GITREPO', value: GITHUB_REPOSITORY],
                    [$class: 'StringParameterValue', name: 'VERSION', value: 'release'],
                    [$class: 'StringParameterValue', name: 'PYTHON_VERSION', value: '3.12']
                ]
            }
        }
    }

    post {
        always {
            script {
                if (env.BRANCH_NAME == 'master') {
                    slackNotify channel: SLACK_BUILDS_CHANNEL
                }
            }
        }

        failure {
            script {
                if (env.BRANCH_NAME == 'master') {
                    slackNotify channel: SLACK_ALERTS_CHANNEL
                }
            }
        }

        fixed {
            script {
                if (env.BRANCH_NAME == 'master') {
                    slackNotify channel: SLACK_ALERTS_CHANNEL
                }
            }
        }

        cleanup {
            cleanWs()
        }
    }
}
