|
import java.io.FileInputStream |
|
import java.net.HttpURLConnection |
|
import java.net.URI |
|
import java.util.Properties |
|
|
|
plugins { |
|
id("com.gradle.develocity") version "4.3.1" |
|
id("com.gradle.common-custom-user-data-gradle-plugin") version "2.4.0" |
|
} |
|
|
|
develocity { |
|
server = "https://develocity.example.com" |
|
// ... |
|
} |
|
|
|
fun getAccessKeyForHost(filePath: String, host: String): String? { |
|
return try { |
|
val properties = Properties() |
|
FileInputStream(filePath).use { |
|
properties.load(it) |
|
} |
|
properties.getProperty(host) |
|
} catch (e: Exception) { |
|
throw IllegalStateException( |
|
"Error reading file or parsing access key for server $host. " + |
|
"Please, run `./gradlew provisionDevelocityAccessKey` and try again.", e) |
|
} |
|
} |
|
|
|
fun validateAccessKey() { |
|
val server = develocity.server.get() |
|
val hostname = URI(server).host.replaceFirst("www.", "") |
|
val accessKey = System.getenv("DEVELOCITY_ACCESS_KEY") ?: getAccessKeyForHost( |
|
System.getProperty("user.home") + "/.gradle/develocity/keys.properties", |
|
hostname |
|
) ?: throw IllegalStateException( |
|
"No Develocity access key found for server $hostname. " + |
|
"Please, run `./gradlew provisionDevelocityAccessKey` and try again." |
|
) |
|
|
|
val connection = URI("$server/api/version").toURL().openConnection() as HttpURLConnection |
|
try { |
|
connection.requestMethod = "GET" |
|
connection.setRequestProperty("Authorization", "Bearer $accessKey") |
|
if (connection.responseCode == HttpURLConnection.HTTP_FORBIDDEN || |
|
connection.responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) { |
|
throw IllegalStateException( |
|
"Develocity Unauthorized error, response code ${connection.responseCode}. " + |
|
"Please, run `./gradlew provisionDevelocityAccessKey` and try again." |
|
) |
|
} |
|
} finally { |
|
connection.disconnect() |
|
} |
|
} |
|
|
|
if (gradle.startParameter.taskNames.none { it == "pDAK" || it == "provisionDevelocityAccessKey" }) { |
|
validateAccessKey() |
|
} |