[Build, IGS] Add support for checking global consent
This commit is contained in:
committed by
Space Team
parent
a8cec11772
commit
c7b2240c8e
+10
-5
@@ -36,14 +36,19 @@ internal fun String.formatWithLink(link: String) = replace(linkPlaceholder, link
|
|||||||
|
|
||||||
internal class ConsentManager(
|
internal class ConsentManager(
|
||||||
private val modifier: LocalPropertiesModifier,
|
private val modifier: LocalPropertiesModifier,
|
||||||
|
private val globalConsentGiven: Boolean? = null,
|
||||||
private val input: BufferedReader = System.`in`.bufferedReader(),
|
private val input: BufferedReader = System.`in`.bufferedReader(),
|
||||||
private val output: PrintStream = System.out,
|
private val output: PrintStream = System.out,
|
||||||
) {
|
) {
|
||||||
fun getUserDecision() = when {
|
fun getUserDecision(): Boolean? {
|
||||||
modifier.initiallyContains(USER_REFUSAL_MARKER) -> false
|
return when {
|
||||||
modifier.initiallyContains(USER_CONSENT_MARKER) -> true
|
modifier.initiallyContains(USER_REFUSAL_MARKER) -> false
|
||||||
isInIdeaSync -> false
|
modifier.initiallyContains(USER_CONSENT_MARKER) -> true
|
||||||
else -> null
|
globalConsentGiven == false -> false
|
||||||
|
globalConsentGiven == true -> true
|
||||||
|
isInIdeaSync -> false
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun askForConsent(consentDetailsLink: String? = null): Boolean {
|
fun askForConsent(consentDetailsLink: String? = null): Boolean {
|
||||||
|
|||||||
+2
-1
@@ -18,6 +18,7 @@ private const val DOMAIN_NAME = "kotlin-build-properties.labs.jb.gg"
|
|||||||
private const val SETUP_JSON_URL = "https://$DOMAIN_NAME/setup.json"
|
private const val SETUP_JSON_URL = "https://$DOMAIN_NAME/setup.json"
|
||||||
|
|
||||||
private const val PLUGIN_SWITCH_PROPERTY = "kotlin.build.internal.gradle.setup"
|
private const val PLUGIN_SWITCH_PROPERTY = "kotlin.build.internal.gradle.setup"
|
||||||
|
private const val CONSENT_GRADLE_PROPERTY = "kotlin.build.internal.gradle.setup.consent"
|
||||||
|
|
||||||
abstract class InternalGradleSetupSettingsPlugin : Plugin<Settings> {
|
abstract class InternalGradleSetupSettingsPlugin : Plugin<Settings> {
|
||||||
private val log = Logging.getLogger(javaClass)
|
private val log = Logging.getLogger(javaClass)
|
||||||
@@ -33,7 +34,7 @@ abstract class InternalGradleSetupSettingsPlugin : Plugin<Settings> {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
val modifier = LocalPropertiesModifier(target.rootDir.resolve("local.properties"))
|
val modifier = LocalPropertiesModifier(target.rootDir.resolve("local.properties"))
|
||||||
val consentManager = ConsentManager(modifier)
|
val consentManager = ConsentManager(modifier, target.providers.gradleProperty(CONSENT_GRADLE_PROPERTY).orNull?.toBoolean())
|
||||||
val initialDecision = consentManager.getUserDecision()
|
val initialDecision = consentManager.getUserDecision()
|
||||||
if (initialDecision == false) {
|
if (initialDecision == false) {
|
||||||
log.debug("Skipping automatic local.properties configuration as you've opted out")
|
log.debug("Skipping automatic local.properties configuration as you've opted out")
|
||||||
|
|||||||
+27
-1
@@ -49,6 +49,32 @@ class ConsentManagerTest {
|
|||||||
assertEquals(false, ConsentManager(modifier).getUserDecision())
|
assertEquals(false, ConsentManager(modifier).getUserDecision())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("if there's no local consent, but the global one is given")
|
||||||
|
fun testGlobalConsentGiven() {
|
||||||
|
assertEquals(true, ConsentManager(modifier, true).getUserDecision())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("if there's no local consent, but the global one is refused")
|
||||||
|
fun testGlobalConsentRefusal() {
|
||||||
|
assertEquals(false, ConsentManager(modifier, false).getUserDecision())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("local given consent takes priority over the global one")
|
||||||
|
fun testLocalGivenConsentTakesPriority() {
|
||||||
|
localPropertiesFile.toFile().writeText(USER_CONSENT_MARKER)
|
||||||
|
assertEquals(true, ConsentManager(modifier, false).getUserDecision())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("local refused consent takes priority over the global one")
|
||||||
|
fun testLocalRefusedConsentTakesPriority() {
|
||||||
|
localPropertiesFile.toFile().writeText(USER_REFUSAL_MARKER)
|
||||||
|
assertEquals(false, ConsentManager(modifier, true).getUserDecision())
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testConsentRequestAgree() = testUserDecision("yes", true, USER_CONSENT_MARKER)
|
fun testConsentRequestAgree() = testUserDecision("yes", true, USER_CONSENT_MARKER)
|
||||||
|
|
||||||
@@ -76,7 +102,7 @@ class ConsentManagerTest {
|
|||||||
StringInputStream(prompt).bufferedReader().use { input ->
|
StringInputStream(prompt).bufferedReader().use { input ->
|
||||||
val outputStream = ByteArrayOutputStream(255)
|
val outputStream = ByteArrayOutputStream(255)
|
||||||
PrintStream(outputStream).use { printStream ->
|
PrintStream(outputStream).use { printStream ->
|
||||||
val consentManager = ConsentManager(modifier, input, printStream)
|
val consentManager = ConsentManager(modifier, null, input, printStream)
|
||||||
val userDecision = consentManager.askForConsent(consentDetailsLink)
|
val userDecision = consentManager.askForConsent(consentDetailsLink)
|
||||||
assertEquals(expectedDecision, userDecision)
|
assertEquals(expectedDecision, userDecision)
|
||||||
val content = Files.readAllLines(localPropertiesFile)
|
val content = Files.readAllLines(localPropertiesFile)
|
||||||
|
|||||||
Reference in New Issue
Block a user