Skip to content

Commit

Permalink
Create JenkinsAPI#isFreestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
gmitch215 committed Jul 31, 2024
1 parent f2b124b commit fd7c1f0
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/main/kotlin/io/codemc/api/jenkins/jenkins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
package io.codemc.api.jenkins

import com.cdancy.jenkins.rest.JenkinsClient
import io.codemc.api.JOB_FREESTYLE
import io.codemc.api.JOB_MAVEN
import io.codemc.api.RESOURCE_CACHE
import io.codemc.api.USER_CONFIG
import io.codemc.api.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import org.jetbrains.annotations.VisibleForTesting

var jenkinsConfig: JenkinsConfig = JenkinsConfig("", "", "")
Expand Down Expand Up @@ -62,4 +64,32 @@ fun createJenkinsJob(username: String, jobName: String, repoLink: String, isFree
internal fun getJenkinsJob(username: String, jobName: String): String {
val job = client.api().jobsApi().config("/", "$username/job/$jobName")
return job ?: ""
}

private val freestyleMappings = mapOf(
"pom.xml" to false,

"gradlew" to true,
"gradlew.bat" to true,
"build.gradle" to true,
"build.gradle.kts" to true,
"settings.gradle" to true,
"settings.gradle.kts" to true,
)

suspend fun isFreestyle(username: String, jobName: String): Boolean = withContext(Dispatchers.IO) {
val github = json.parseToJsonElement(github(username, jobName).body()).jsonObject
val defaultBranch = github["default_branch"]?.jsonPrimitive?.contentOrNull ?: "master"

for ((file, freestyle) in freestyleMappings) {
val response = req("https://raw.githubusercontent.com/$username/$jobName/$defaultBranch/$file")

when (response.statusCode()) {
200 -> return@withContext freestyle
404 -> continue
else -> return@withContext false
}
}

return@withContext true
}

0 comments on commit fd7c1f0

Please sign in to comment.