Created
November 12, 2021 18:52
-
-
Save anthonydahanne/e7933925a396fa24aac6c55483541304 to your computer and use it in GitHub Desktop.
Find all Jenkins Jobs Git origin urls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env kotlin | |
@file:DependsOn("com.github.kittinunf.fuel:fuel:2.3.1") | |
@file:DependsOn("com.google.code.gson:gson:2.8.6") | |
@file:DependsOn("com.beust:klaxon:5.5") | |
import com.beust.klaxon.JsonObject | |
import com.beust.klaxon.Klaxon | |
import com.beust.klaxon.Parser | |
import com.beust.klaxon.PathMatcher | |
import com.github.kittinunf.fuel.core.extensions.authentication | |
import com.github.kittinunf.fuel.httpGet | |
import java.io.StringReader | |
import java.lang.RuntimeException | |
import java.util.regex.Pattern | |
import kotlin.system.exitProcess | |
// Find all Jenkins Jobs Git origin urls , by @anthonydahanne | |
// Info on how to retrieve the Jenkins token: https://www.jenkins.io/doc/book/system-administration/authenticating-scripted-clients/ | |
// basically go to $baseUrl/me/configure | |
if (args.size < 3) { | |
println("Please provide the mandatory arguments: baseUrl, username and token") | |
println("Example: kotlin jenkins-jobs.main.kts https://myjenkins myuser mytoken") | |
exitProcess(2) | |
} | |
val jenkinsBaseUrl = args[0] | |
val username = args[1] | |
val jenkinsApiToken = args[2] | |
fun displayJobsForFolder(folder: String): String { | |
val jenkinsBaseJobApiUrl = | |
jenkinsBaseUrl + folder + "api/json?tree=jobs%5Bname,scm%5BuserRemoteConfigs%5Burl%5D%5D%5D" | |
println("Here are the jobs for the folder named $folder, \nfetched from the URL $jenkinsBaseJobApiUrl") | |
val (_, jenkinsBaseJobApiResponse, result) = jenkinsBaseJobApiUrl | |
.httpGet() | |
.authentication() | |
.basic(username, jenkinsApiToken) | |
.responseString() | |
if (jenkinsBaseJobApiResponse.statusCode == 200) { | |
val pathMatcherUrls = object : PathMatcher { | |
override fun pathMatches(path: String) = Pattern.matches(".*jobs.*scm.*userRemoteConfigs.*url", path) | |
override fun onMatch(path: String, value: Any) { | |
println("$value") | |
} | |
} | |
val jsonResult = result.get() | |
val reader = StringReader(jsonResult) | |
Klaxon() | |
.pathMatcher(pathMatcherUrls) | |
.parseJsonObject(reader) | |
return jsonResult | |
} else { | |
throw RuntimeException( | |
"Something went wrong when connecting to Jenkins...\n" + | |
"Here's the status code: ${jenkinsBaseJobApiResponse.statusCode} \n" + | |
"and the message: \n ${jenkinsBaseJobApiResponse.responseMessage}" | |
) | |
} | |
} | |
val jsonResult = displayJobsForFolder("/") | |
val parser: Parser = Parser.default() | |
val json: JsonObject = parser.parse(StringReader(jsonResult)) as JsonObject | |
@Suppress("UNCHECKED_CAST") | |
val folderNames = | |
(json.map["jobs"] as List<Map<String, String>>).filter { it["_class"] == "com.cloudbees.hudson.plugins.folder.Folder" } | |
.map { | |
it["name"] | |
} | |
folderNames.forEach { | |
displayJobsForFolder("/job/$it/") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment