From a4864031c328d1930f9c8a22e15b1073f996d8d4 Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Mon, 18 Mar 2024 19:10:30 +0100 Subject: [PATCH] Enable to skip update checks from pom properties --- .../jib/gradle/GradleProjectProperties.java | 6 +++++ .../cloud/tools/jib/gradle/TaskCommon.java | 1 + .../jib/maven/MavenProjectProperties.java | 6 +++++ .../cloud/tools/jib/maven/MojoCommon.java | 1 + .../jib/maven/MavenProjectPropertiesTest.java | 23 +++++++++++++++++++ .../cloud/tools/jib/maven/MojoCommonTest.java | 19 +++++++++++++++ .../jib/plugins/common/ProjectProperties.java | 2 ++ 7 files changed, 58 insertions(+) diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java index 6a1978fe19..cef93aa1f8 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java @@ -454,6 +454,12 @@ public boolean isOffline() { return project.getGradle().getStartParameter().isOffline(); } + @Override + public boolean isDisableUpdateCheck() { + return Boolean.parseBoolean( + String.valueOf(project.findProperty(PropertyNames.DISABLE_UPDATE_CHECKS))); + } + @Override public JibContainerBuilder runPluginExtensions( List extensionConfigs, diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/TaskCommon.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/TaskCommon.java index ca7fc761eb..230dbe3469 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/TaskCommon.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/TaskCommon.java @@ -50,6 +50,7 @@ class TaskCommon { static Future> newUpdateChecker( ProjectProperties projectProperties, GlobalConfig globalConfig, Logger logger) { if (projectProperties.isOffline() + || projectProperties.isDisableUpdateCheck() || !logger.isLifecycleEnabled() || globalConfig.isDisableUpdateCheck()) { return Futures.immediateFuture(Optional.empty()); diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenProjectProperties.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenProjectProperties.java index 7b457951c6..b1126bb357 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenProjectProperties.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenProjectProperties.java @@ -482,6 +482,12 @@ public boolean isOffline() { return session.isOffline(); } + @Override + public boolean isDisableUpdateCheck() { + return Boolean.parseBoolean( + project.getProperties().getProperty(PropertyNames.DISABLE_UPDATE_CHECKS)); + } + @VisibleForTesting Path getWarArtifact() { Build build = project.getBuild(); diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MojoCommon.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MojoCommon.java index 6857f89a40..d1ec8c72b8 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MojoCommon.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MojoCommon.java @@ -53,6 +53,7 @@ public class MojoCommon { static Future> newUpdateChecker( ProjectProperties projectProperties, GlobalConfig globalConfig, Log logger) { if (projectProperties.isOffline() + || projectProperties.isDisableUpdateCheck() || !logger.isInfoEnabled() || globalConfig.isDisableUpdateCheck()) { return Futures.immediateFuture(Optional.empty()); diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenProjectPropertiesTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenProjectPropertiesTest.java index 5bbceaf3b9..e85a82289f 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenProjectPropertiesTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenProjectPropertiesTest.java @@ -16,9 +16,12 @@ package com.google.cloud.tools.jib.maven; +import static com.google.cloud.tools.jib.plugins.common.PropertyNames.DISABLE_UPDATE_CHECKS; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth8.assertThat; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -1089,6 +1092,26 @@ public void testGetDependencies() throws URISyntaxException { testRepository.artifactPathOnDisk("com.test", "dependencyX", "1.0.0-SNAPSHOT")); } + @Test + public void testIsDisableCheck() { + final MavenProject project = new MavenProject(); + final MavenProjectProperties properties = + new MavenProjectProperties( + mockJibPluginDescriptor, + project, + mockMavenSession, + mockLog, + mockTempDirectoryProvider, + Collections.emptyList(), + mockExtensionLoader); + + project.getProperties().setProperty(DISABLE_UPDATE_CHECKS, "true"); + assertTrue(properties.isDisableUpdateCheck()); + + project.getProperties().setProperty(DISABLE_UPDATE_CHECKS, "false"); + assertFalse(mavenProjectProperties.isDisableUpdateCheck()); + } + private BuildContext setUpBuildContext() throws InvalidImageReferenceException, IOException, CacheDirectoryCreationException { return setUpBuildContext(ContainerizingMode.EXPLODED); diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MojoCommonTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MojoCommonTest.java index 88e3901f39..fbbdf6a8da 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MojoCommonTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MojoCommonTest.java @@ -16,6 +16,8 @@ package com.google.cloud.tools.jib.maven; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -24,7 +26,10 @@ import com.google.cloud.tools.jib.plugins.common.ProjectProperties; import com.google.common.util.concurrent.Futures; import java.util.Optional; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import org.apache.maven.monitor.logging.DefaultLog; +import org.codehaus.plexus.logging.console.ConsoleLogger; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -60,4 +65,18 @@ public void testFinishUpdateChecker_correctMessageLogged() { + ProjectInfo.GITHUB_URL + "/blob/master/docs/privacy.md for info on disabling this update check.")); } + + @Test + public void testUpdateChecker_disabledFromProjectProperties() + throws ExecutionException, InterruptedException { + when(mockProjectProperties.isDisableUpdateCheck()).thenReturn(true); + + final Future> future = + MojoCommon.newUpdateChecker( + mockProjectProperties, + null /* not tested since project property disable it before */, + new DefaultLog(new ConsoleLogger())); + assertEquals("com.google.common.util.concurrent.ImmediateFuture", future.getClass().getName()); + assertFalse(future.get().isPresent()); + } } diff --git a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/ProjectProperties.java b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/ProjectProperties.java index e8a0516226..bef4f64120 100644 --- a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/ProjectProperties.java +++ b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/ProjectProperties.java @@ -90,6 +90,8 @@ JibContainerBuilder createJibContainerBuilder( boolean isOffline(); + boolean isDisableUpdateCheck(); + JibContainerBuilder runPluginExtensions( List extensionConfigs, JibContainerBuilder jibContainerBuilder)