// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2019-2021, Intel Corporation */

/* Jenkinsfile - Scripts to create libpmemobj-cpp and libpmemobj-cpp_matrix jobs - to run with initial jenkins job. */

/* define common properties of pipelines: */
description_param = '''
	string {
		name('DESCRIPTION')
		defaultValue('')
		trim(true)
		description('Optional description for this execution')
	}
'''

label_param = '''
	string {
		name('LABEL')
		defaultValue('fedora')
		trim(true)
		description("Name of the node or group to run job on: rhel8_2, openSUSE15_2, fedora32, ubuntu20_04, ubuntu19.10, debian10, etc.")
	}
'''

repo_url_param = '''
	string {
		name('REPO_URL')
		defaultValue('https://github.com/pmem/libpmemobj-cpp.git')
		trim(true)
		description("Git repository address")
	}
'''

branch_param = '''
	string {
		name('BRANCH')
		defaultValue('master')
		trim(true)
		description("Repository's branch")
	}
'''

test_options_param = '''
	string {
		name('TEST_OPTIONS')
		defaultValue('tests_clang_debug_cpp17_no_valgrind tests_clang_release_cpp11_no_valgrind build_gcc_debug_cpp14 tests_gcc_debug_cpp14_no_valgrind tests_gcc_debug_cpp14_valgrind_memcheck_drd tests_gcc_debug_cpp14_valgrind_other tests_gcc_release_cpp17_no_valgrind ')
		trim(true)
		description("Test builds, as defined in run-build.sh")
	}
'''

test_device = '''
	choiceParam('DEVICE_TYPE', ['PMEM', 'DAX', 'NONE'], 'Select tested device type. For PMEM and DAX, capable persistent memory must be present on the server.')
'''

type_param = '''
	choiceParam('TEST_TYPE', ['normal'], 'Type of tests')
'''

check_cpp_style_param = '''
choiceParam('CHECK_CPP_STYLE', ['OFF', 'ON'], 'Toggle CHECK_CPP_STYLE')
'''

tests_long_param = '''
choiceParam('TESTS_LONG', ['ON', 'OFF'], 'Toggle TESTS_LONG')
'''

tbb_param = '''
choiceParam('TESTS_TBB', ['ON', 'OFF'], 'Toggle TESTS_TBB')
'''

pmreorder_param = '''
choiceParam('TESTS_PMREORDER', ['ON', 'OFF'], 'Toggle TESTS_PMREORDER')
'''

timeout_param = '''
	string {
		name('TEST_TIMEOUT')
		defaultValue('28800')
		trim(true)
		description("Set test timeout")
}
'''

coverage_param = '''
	choiceParam('COVERAGE', ['no', 'yes'], 'Enable code coverage')
'''

gitlab_connection = '''
	properties {
		gitLabConnection {
			gitLabConnection('gitlabIT')
		}
	}
'''

email_options_param = '''
	string {
		name('EMAIL_RECIPIENTS')
		defaultValue('')
		trim(true)
		description("Recipients of the e-mail sent after execution, separated by the comma.")
	}
	booleanParam('SEND_RESULTS', true, 'Uncheck to disable sending email with report after execution')
'''

/* Branch with Jenkins libraries to pull: */
dev_utils_kit_branch_source = 'master'
dev_utils_kit_files = 'dev-utils-kit/jenkins'
libpmemobj_cpp_branch = 'master'

/* Loads pipeline scripts from repository */
def remote_definition(current_job_script_path) {
	return """
		definition {
			cpsScm {
				scm {
					git {
						remote {
							url('https://github.com/pmem/libpmemobj-cpp')
							branch('${libpmemobj_cpp_branch}')
							scriptPath('${current_job_script_path}')
						}
					}
				}
			}
		}
	"""
}

environmental_variables = """
	environmentVariables {
		envs(
			api_lib: "${dev_utils_kit_files}/lib/api.groovy",
			lib_path: "${dev_utils_kit_files}/lib/",
			scripts_path: "${dev_utils_kit_files}/scripts/",
			dev_utils_kit_files_repo: "https://github.com/pmem/dev-utils-kit.git",
			dev_utils_kit_branch: "${dev_utils_kit_branch_source}",
			info_addr: "https://github.com/pmem/libpmemobj-cpp.git"
		)
	}
"""

triggers = '''
	triggers {
		gitlabPush {
			buildOnMergeRequestEvents(true)
			buildOnPushEvents(true)
			enableCiSkip(false)
			setBuildDescription(false)
			rebuildOpenMergeRequest('never')
		}
	}
'''

node {
	stage('libpmemobj-cpp_linux'){
		jobDsl scriptText: """pipelineJob("libpmemobj-cpp_linux") {
			parameters {
				${test_device}
				${label_param}
				${branch_param}
				${type_param}
				${test_options_param}
				${repo_url_param}
				${description_param}
				${check_cpp_style_param}
				${tests_long_param}
				${tbb_param}
				${pmreorder_param}
				${timeout_param}
				${coverage_param}
				${email_options_param}
			}
			${gitlab_connection}
			${environmental_variables}
			${triggers}
			${remote_definition 'utils/jenkins/libpmemobj-cpp.jenkins'}
		}"""
	}
	stage('libpmemobj-cpp_linux_matrix'){
		jobDsl scriptText: """matrixJob("libpmemobj-cpp_linux_matrix") {
			parameters {
				matrixCombinationsParam('COMBINATIONS', '', 'choose which combinations to run')
				${branch_param}
				${coverage_param}
				${repo_url_param}
				${test_options_param}
				${description_param}
				${email_options_param}
			}
			axes {
				text('DISTRO', 'ubuntu20_10', 'ubuntu21_04', 'fedora34')
				text('TYPE', 'normal', 'building', 'compatibility')
				label('master', 'master')
			}
			steps {
				downstreamParameterized {
					trigger("libpmemobj-cpp_linux") {
						parameters {
							predefinedProp('COV', '\${COVERAGE}')
							predefinedProp('TEST_TYPE', '\${TYPE}')
							predefinedProp('LABEL', '\${DISTRO}')
							predefinedProp('BRANCH', '\${BRANCH}')
							predefinedProp('TEST_OPTIONS', '\${TEST_OPTIONS}')
							predefinedProp('DEVICE_TYPE', '\${DEVICE_TYPE}')
							predefinedProp('REPO_URL', '\${REPO_URL}')
							predefinedProp('EMAIL_RECIPIENTS', '\${EMAIL_RECIPIENTS}')
							predefinedProp('SEND_EMAIL', '\${SEND_RESULTS}')
							predefinedProp('DESCRIPTION', '\${DESCRIPTION}  #Triggered by libpmemobj-cpp matrixJob #\${BUILD_NUMBER}   ->   \${JENKINS_URL}view/all/job/libpmemobj-cpp_linux_matrix/\${BUILD_NUMBER}')
						}
					block {
						buildStepFailure('FAILURE')
						failure('FAILURE')
						unstable('UNSTABLE')
						}
					}
				}
			}
		}"""
	}
}
