plugins {
    id "groovy"
}

test {
    jvmArgs = ['--add-opens', 'java.base/java.util=ALL-UNNAMED']
}

// Select java version based on availability
def javaVersion = System.getProperty("java.version")

java {
    toolchain {
        if (javaVersion.startsWith("11")) {
            languageVersion.set(JavaLanguageVersion.of(11))
        } else if (javaVersion.startsWith("17")) {
            languageVersion.set(JavaLanguageVersion.of(17))
        } else if (javaVersion.startsWith("21")) {
            languageVersion.set(JavaLanguageVersion.of(21))
        }
    }
}

group = "com.sonymusic"
version = "0.1.0"

ext {
    jobDslVersion = '3654.vdf58f53e2d15'
    jenkinsVersion = '2.555.3'
}

repositories {
    jcenter()
    maven {
        url 'https://repo.jenkins-ci.org/public/'
    }
}

configurations {
    testPlugins {}

    // see JENKINS-45512
    testCompile {
        exclude group: 'xalan'
        exclude group: 'xerces'
    }
}

dependencies {
    implementation "org.codehaus.groovy:groovy-all:3.0.22"
    testImplementation(platform('org.junit:junit-bom:5.9.2'))
    testImplementation('org.junit.jupiter:junit-jupiter')

    // Jenkins test harness dependencies
    testImplementation 'org.jenkins-ci.main:jenkins-test-harness:2508.v45936ef31cd7'
    testImplementation "org.jenkins-ci.main:jenkins-war:${jenkinsVersion}"

    // Job DSL plugin including plugin dependencies
    testImplementation "org.jenkins-ci.plugins:job-dsl:${jobDslVersion}"
    testImplementation "org.jenkins-ci.plugins:job-dsl:${jobDslVersion}@jar"
    testImplementation 'org.jenkins-ci.plugins:structs:362.va_b_695ef4fdf9@jar'
    testImplementation 'org.jenkins-ci.plugins:script-security:1399.ve6a_66547f6e1@jar'

    // Plugins to install in Jenkins test harness. Any plugins required by Job DSL scripts should be added here.
    testPlugins 'io.jenkins.plugins:github-checks:634.v371dc6d978a_3'
    testPlugins 'org.jenkins-ci.plugins:branch-api:2.1280.v0d4e5b_b_460ef'
    testPlugins 'org.jenkins-ci.plugins:git:5.10.0'
    testPlugins 'org.jenkins-ci.plugins:github-branch-source:1967.vdea_d580c1a_b_a_'
    testPlugins 'org.jenkins-ci.plugins:github-scm-trait-notification-context:45.v8ef831829589'
    testPlugins 'org.jenkins-ci.plugins:token-macro:477.vd4f0dc3cb_cf1'
    testPlugins 'org.jenkins-ci.plugins.workflow:workflow-api:1398.v67030756d3fb_'
    testPlugins 'org.jenkins-ci.plugins.workflow:workflow-job:1571.vb_423c255d6d9'
    testPlugins 'org.jenkins-ci.plugins.workflow:workflow-multibranch:821.vc3b_4ea_780798'
}

task resolveTestPlugins(type: Copy) {
    from configurations.testPlugins
    into new File(sourceSets.test.output.resourcesDir, 'test-dependencies')
    include '*.hpi'
    include '*.jpi'
    def mapping = [:]

    doFirst {
        configurations.testPlugins.resolvedConfiguration.resolvedArtifacts.each {
            mapping[it.file.name] = "${it.name}.${it.extension}"
        }
    }
    rename { mapping[it] }

    doLast {
        List<String> baseNames = source*.name.collect { mapping[it] }.collect { it[0..it.lastIndexOf('.') - 1] }
        new File(destinationDir, 'index').setText(baseNames.join('\n'), 'UTF-8')
    }
}

test {
    dependsOn tasks.resolveTestPlugins

    // set build directory for Jenkins test harness, JENKINS-26331
    systemProperty 'buildDirectory', project.buildDir.absolutePath

    ignoreFailures = false

    useJUnitPlatform()

    testLogging {
        events "passed", "skipped", "failed"
    }
    testLogging.showStandardStreams = true

    reports {
        junitXml {
            outputPerTestCase = true
            mergeReruns = true
        }
    }
}
