// [skip ci] and [ci skip] have no effect here.
if (utils.scm_checkout(['skip_disable':true])) return

def PipInject(String reqs) {
    def result = []
    if (reqs.trim().isEmpty()) {
        return result
    }
    for (req in reqs.split('\n')) {
        result += "pip install $req"
    }
    return result
}

def MultiLineToArray(String reqs) {
    def result = []
    if (reqs.trim().isEmpty()) {
        return result
    }
    for (req in reqs.split('\n')) {
        result += req
    }
    return result
}

artifactoryenv = "dev"
if (env.ARTIFACTORY_ENV) {
    artifactoryenv = env.ARTIFACTORY_ENV
}

pytest_args = ""
if (env.PYTEST_ARGS) {
    pytest_args = env.PYTEST_ARGS
}

withCredentials([string(
    credentialsId: 'romancal-codecov',
    variable: 'codecov_token')]) {

jobconfig = new JobConfig()
jobconfig.enable_env_publication = true
jobconfig.publish_env_on_success_only = true
jobconfig.publish_env_filter = "spacetelescope/main"

// Define python version for conda
python_version = "3.11"

// pip related setup
def pip_index = "https://bytesalad.stsci.edu/artifactory/api/pypi/datb-pypi-virtual/simple"
def pip_install_args = "--index-url ${pip_index} --progress-bar=off"

// Define environment variables needed for the regression tests
env_vars = [
    "TEST_BIGDATA=https://bytesalad.stsci.edu/artifactory",
    "CRDS_CONTEXT=roman_0058.pmap",
    "CRDS_SERVER_URL=https://roman-serverless",
    'CRDS_PATH=/grp/crds/roman/test/',
    'DD_ENV=ci',
    'DD_SERVICE=romancal',
    'WEBBPSF_PATH=/grp/jwst/ote/webbpsf-data',
    'PYSYN_CDBS=/grp/hst/cdbs',
]
if (env.ENV_VARS) {
    env_vars.addAll(MultiLineToArray(env.ENV_VARS))
}

// Set pytest basetemp output directory
pytest_basetemp = "test_outputs"

// Configure artifactory ingest
data_config = new DataConfig()
data_config.server_id = 'bytesalad'
data_config.root = 'clone/${pytest_basetemp}'
data_config.match_prefix = '(.*)_result' // .json is appended automatically


// Build and test with dependencies specified in requirements-sdp.txt
bc0 = new BuildConfig()
bc0.nodetype = 'romancal'
bc0.name = 'stable-deps'
bc0.env_vars = env_vars
bc0.conda_packages = [
    "python=${python_version}",
    "freetds",
]
bc0.pip_reqs_files = ['requirements-sdp.txt']
bc0.build_cmds = [
    "pip install -e .[test]",
    "pip install pytest-xdist ddtrace",
    'echo "CRDS_CONTEXT = $(crds list --contexts $CRDS_CONTEXT --mappings | grep pmap)"',
]
bc0.build_cmds = bc0.build_cmds + PipInject(env.OVERRIDE_REQUIREMENTS)
bc0.build_cmds = bc0.build_cmds + [
    "pip list"
]
bc0.test_cmds = [
    "pytest -r sxf -n auto --bigdata --slow --webbpsf \
    --cov --cov-report=xml:coverage.xml \
    --ddtrace \
    --color=no \
    --basetemp=${pytest_basetemp} --junit-xml=results.xml --dist=loadscope \
    --env=${artifactoryenv} ${pytest_args}",
    "curl -Os https://uploader.codecov.io/latest/linux/codecov",
    "chmod +x codecov",
    './codecov --token=${codecov_token} -F nightly -f coverage.xml',
]
bc0.test_configs = [data_config]
bc0.failedFailureThresh = 0

// macos-specific buildconfig to cause the creation of counterparts to the linux
// environment dumps. Packages in a mininmal conda environment differ by OS
// which is why this is needed.
bc1 = utils.copy(bc0)
bc1.nodetype = 'macos'
bc1.name = 'macos-stable-deps'
bc1.build_cmds = ["pip install -e ."]
bc1.build_cmds = bc1.build_cmds + PipInject(env.OVERRIDE_REQUIREMENTS)
bc1.test_cmds = []
bc1.test_configs = []

bc2 = utils.copy(bc0)
bc2.pip_reqs_files = ['requirements-sdp.txt', 'requirements-dev-st.txt']

utils.run([jobconfig, bc0, bc1, bc2])
}  // withCredentials
