properties([
  parameters([
    string(
      name: 'TEST_MARK',
      defaultValue: 'All',
      description: 'To filter test cases, default is All'
    )
  ])
])

node('ccis') {
  stage('Setup Environment') {
    cleanWs()
    checkout scm

    def image = docker.build('pytest-test', '.')
    env.IMAGE_ID = image.id
  }

  stage('Test') {
    docker.image(env.IMAGE_ID).inside("--user jenkins --shm-size=1g") {
      sh 'mkdir -p Report test-reports || true'
      sh 'if [ -f Report_Http.zip ]; then rm Report_Http.zip; fi'

      withEnv(["TEST_MARK=${params.TEST_MARK}"]) {
        sh 'python3 exec.py'
      }

      sh '7z a -tzip Report_Http.zip Report'
      archiveArtifacts artifacts: 'Report_Http.zip', followSymlinks: false
      archiveArtifacts artifacts: 'test-reports/**', followSymlinks: false
    }
  }

  stage('Prepare Report') {
    junit testResults: 'test-reports/junit-report.xml', allowEmptyResults: true
  }

  stage('Notify to Discord') {
    withCredentials([string(credentialsId: '725eb3cc-b38a-412a-b83c-cd5a5017f8b8', variable: 'DISCORD_WEBHOOK_URL')]) {
        script {
            def status = currentBuild.currentResult
            def desc = """
            **Job:** ${env.JOB_NAME}
            **Build #:** ${env.BUILD_NUMBER}
            **Status:** ${status}
            **URL:** ${env.BUILD_URL}
            """.stripIndent()

            discordSend(
                webhookURL: DISCORD_WEBHOOK_URL,
                title: "Jenkins Build - ${status}",
                description: desc,
                result: status
            )
        }
    }
  }
}