From 02502e28b6f400d8430ddee18a562e20ad0a97e9 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 29 Sep 2023 09:33:03 -0700 Subject: [PATCH] Automate Java version recommendation administrative monitor (#8526) * Automate Java version recommendation administrative monitor * Improve dates --- ...JavaVersionRecommendationAdminMonitor.java | 116 +++++++++++++++++- .../description.jelly | 2 +- .../description.properties | 2 +- .../description_pt_BR.properties | 2 +- .../description_ru.properties | 2 +- .../description_zh_TW.properties | 2 +- .../message.jelly | 7 +- .../message.properties | 8 +- .../message_pt_BR.properties | 10 +- .../message_ru.properties | 12 +- war/src/main/java/executable/Main.java | 4 + 11 files changed, 133 insertions(+), 34 deletions(-) diff --git a/core/src/main/java/jenkins/monitor/JavaVersionRecommendationAdminMonitor.java b/core/src/main/java/jenkins/monitor/JavaVersionRecommendationAdminMonitor.java index 133ea2b37731..9d32eb45a771 100644 --- a/core/src/main/java/jenkins/monitor/JavaVersionRecommendationAdminMonitor.java +++ b/core/src/main/java/jenkins/monitor/JavaVersionRecommendationAdminMonitor.java @@ -24,13 +24,21 @@ package jenkins.monitor; +import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; import hudson.model.AdministrativeMonitor; import hudson.security.Permission; import java.io.IOException; +import java.time.LocalDate; +import java.time.Period; +import java.time.ZoneId; +import java.util.Collections; +import java.util.Date; +import java.util.Locale; +import java.util.NavigableMap; +import java.util.TreeMap; import jenkins.model.Jenkins; import jenkins.util.SystemProperties; -import jenkins.util.java.JavaUtils; import org.jenkinsci.Symbol; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.DoNotUse; @@ -46,15 +54,68 @@ @Symbol("javaVersionRecommendation") public class JavaVersionRecommendationAdminMonitor extends AdministrativeMonitor { + /** + * The list of supported Java long-term support (LTS) releases. The key is the {@link + * Runtime.Version#feature() feature-release counter}. The value is the date the Jenkins project + * drops support for that release, which must be before the date the Eclipse Temurin project + * drops support for that release. This list must remain synchronized with the one in {@code + * executable.Main}. + * + *

To add support for a Java version: + * + *

+ * + * @see Eclipse Temurin End of Life + */ + private static final NavigableMap SUPPORTED_JAVA_VERSIONS; + + static { + NavigableMap supportedVersions = new TreeMap<>(); + supportedVersions.put(11, LocalDate.of(2024, 9, 30)); // Temurin: 2024-10-31 + supportedVersions.put(17, LocalDate.of(2026, 3, 31)); // Temurin: 2027-10-31 + supportedVersions.put(21, LocalDate.of(2027, 9, 30)); // Temurin: 2029-09-30 + SUPPORTED_JAVA_VERSIONS = Collections.unmodifiableNavigableMap(supportedVersions); + } + public JavaVersionRecommendationAdminMonitor() { - super(JavaVersionRecommendationAdminMonitor.class.getName() + "-3"); + super(getId()); + } + + /** + * Compute the ID for the administrative monitor. The ID includes the Java version, EOL date, + * and severity so that changes to the EOL date for a given Java version will invalidate + * previous dismissals of the administrative monitor and so that users who decline to upgrade + * after the first warning get a second warning when they are closer to the deadline. + * + * @return The computed ID. + */ + private static String getId() { + StringBuilder id = new StringBuilder(); + id.append(JavaVersionRecommendationAdminMonitor.class.getName()); + LocalDate endOfLife = getEndOfLife(); + if (endOfLife.isBefore(LocalDate.MAX)) { + id.append('-'); + id.append(Runtime.version().feature()); + id.append('-'); + id.append(endOfLife); + id.append('-'); + id.append(getSeverity()); + } + return id.toString(); } private static Boolean disabled = SystemProperties.getBoolean(JavaVersionRecommendationAdminMonitor.class.getName() + ".disabled", false); @Override public boolean isActivated() { - return !disabled && JavaUtils.isRunningWithJava8OrBelow(); + return !disabled && getDeprecationPeriod().getYears() < 2; } @Override @@ -78,8 +139,55 @@ public HttpResponse doAct(@QueryParameter String no) throws IOException { disable(true); return HttpResponses.forwardToPreviousPage(); } else { - return new HttpRedirect("https://www.jenkins.io/redirect/upgrading-jenkins-java-version-8-to-11"); + return new HttpRedirect("https://jenkins.io/redirect/java-support/"); } } + @NonNull + private static LocalDate getEndOfLife() { + LocalDate endOfLife = SUPPORTED_JAVA_VERSIONS.get(Runtime.version().feature()); + return endOfLife != null ? endOfLife : LocalDate.MAX; + } + + @NonNull + private static Period getDeprecationPeriod() { + return Period.between(LocalDate.now(), getEndOfLife()); + } + + @NonNull + private static Severity getSeverity() { + return getDeprecationPeriod().getYears() < 1 ? Severity.DANGER : Severity.WARNING; + } + + /** + * @return The current feature-release counter. + * @see Runtime#version() + */ + @Restricted(DoNotUse.class) + public int getJavaVersion() { + return Runtime.version().feature(); + } + + /** + * @return The end of life date for the current Java version in the system default time zone. + */ + @Restricted(DoNotUse.class) + public Date getEndOfLifeAsDate() { + return Date.from(getEndOfLife().atStartOfDay(ZoneId.systemDefault()).toInstant()); + } + + /** + * @return The severity of the administrative monitor, used to set the background color of the alert. + */ + @Restricted(DoNotUse.class) + public String getSeverityAsString() { + return getSeverity().toString().toLowerCase(Locale.US); + } + + private enum Severity { + SUCCESS, + INFO, + WARNING, + DANGER + } } diff --git a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description.jelly b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description.jelly index bb405e931933..f8a1ffe01683 100644 --- a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description.jelly +++ b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description.jelly @@ -1,4 +1,4 @@ - ${%blurb} + ${%blurb(it.javaVersion)} diff --git a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description.properties b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description.properties index 543b42b1caef..202d256764d8 100644 --- a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description.properties +++ b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description.properties @@ -1 +1 @@ -blurb = Recommends Java 11 for running Jenkins if an older version is used. +blurb = Recommends Java {0,number} for running Jenkins if an older version is used. diff --git a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_pt_BR.properties b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_pt_BR.properties index 1a1e3f070d5a..5f7de2576027 100644 --- a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_pt_BR.properties +++ b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_pt_BR.properties @@ -20,4 +20,4 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -blurb=Recomenda o Java 11 para executar o Jenkins se uma versão anterior está em uso. +blurb=Recomenda o Java {0,number} para executar o Jenkins se uma versão anterior está em uso. diff --git a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_ru.properties b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_ru.properties index 22cb72507294..58ae2d6173f4 100644 --- a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_ru.properties +++ b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_ru.properties @@ -1 +1 @@ -blurb = Рекомендует запускать Jenkins на Java 11, если используется более старая версия. \ No newline at end of file +blurb = Рекомендует запускать Jenkins на Java {0,number}, если используется более старая версия. diff --git a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_zh_TW.properties b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_zh_TW.properties index 2ed16e322ce4..ae4e29dcf6ec 100644 --- a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_zh_TW.properties +++ b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/description_zh_TW.properties @@ -1 +1 @@ -blurb=若您使用的 Java 版本低於 11,推薦您改用 Java 11 來執行 Jenkins。 +blurb=若您使用的 Java 版本低於 {0,number},推薦您改用 Java {0,number} 來執行 Jenkins。 diff --git a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message.jelly b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message.jelly index d11f46a6b540..0265cf145a72 100644 --- a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message.jelly +++ b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message.jelly @@ -24,15 +24,14 @@ THE SOFTWARE. - -
+
-

${%Recommended_Java_Version_Heading(currentRuntimeJavaVersion)}

- ${%Recommended_Java_Version(currentRuntimeJavaVersion)} +

${%Recommended_Java_Version_Heading(it.javaVersion)}

+ ${%Recommended_Java_Version(it.javaVersion, it.endOfLifeAsDate)}
diff --git a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message.properties b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message.properties index e3b0c4fe5f83..a2bb300c7843 100644 --- a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message.properties +++ b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message.properties @@ -1,5 +1,3 @@ -Recommended_Java_Version_Heading=Java {0} end of life in Jenkins -Recommended_Java_Version=

You are running Jenkins on Java {0}, support for which will end on or after June 21, 2022. \ - This is earlier than a previously announced date.

\ -

The Long Term Support (LTS) line of Jenkins will continue support till September 2022.

\ -

Please refer to the documentation for details on upgrading to Java 11.

+Recommended_Java_Version_Heading=Java {0,number} end of life in Jenkins +Recommended_Java_Version=You are running Jenkins on Java {0,number}, support for which will end on or after {1,date}. \ + Refer to the documentation for more details. diff --git a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message_pt_BR.properties b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message_pt_BR.properties index 25c54f9c654a..0c6a0bc82ea5 100644 --- a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message_pt_BR.properties +++ b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message_pt_BR.properties @@ -22,10 +22,6 @@ More\ Info=Mais informação Dismiss=Dispensar -Recommended_Java_Version=

Você está executando o Jenkins com o Java {0}, cujo \ - suporte será encerrado em 21 de junho de 2022. Isto é antes do que a data previamente anunciada.

\ -

A linha de suporte de longo período (LTS) do Jenkins continuará a suportar esta \ - versão até setembro de 2022.

Por favor se refira a \ - \ - a documentação para maiores detalhes sobre atualizar para o Java versão 11.

-Recommended_Java_Version_Heading=Fim de suporte ao Java {0} pelo Jenkins +Recommended_Java_Version=Você está executando o Jenkins com o Java {0,number}, cujo suporte será encerrado em {1,date}. \ + Se refira a a documentação para maiores detalhes. +Recommended_Java_Version_Heading=Fim de suporte ao Java {0,number} pelo Jenkins diff --git a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message_ru.properties b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message_ru.properties index b07389f4627b..75d24bd45678 100644 --- a/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message_ru.properties +++ b/core/src/main/resources/jenkins/monitor/JavaVersionRecommendationAdminMonitor/message_ru.properties @@ -1,11 +1,5 @@ -Recommended_Java_Version_Heading=Конец поддержки Java {0} в Jenkins -Recommended_Java_Version=

Вы запустили Jenkins на Java {0}. Её поддержка будет \ - остановлена 21 июня 2022. \ - Это раньше, чем дата, объявленная ранее.

\ -

Долгосрочно поддерживаемые (LTS) релизы Jenkins будут поддерживать её до сентября \ - 2022.

\ -

Пожалуйста, смотрите детали об обновлении до Java 11 в \ - документации.

+Recommended_Java_Version_Heading=Конец поддержки Java {0,number} в Jenkins +Recommended_Java_Version=Вы запустили Jenkins на Java {0,number}. Её поддержка будет остановлена {1,date}. \ + Смотрите детали в документации. More\ Info=Подробнее Dismiss=Убрать diff --git a/war/src/main/java/executable/Main.java b/war/src/main/java/executable/Main.java index 1a537e46825e..9812f3d5b9e1 100644 --- a/war/src/main/java/executable/Main.java +++ b/war/src/main/java/executable/Main.java @@ -72,6 +72,10 @@ */ public class Main { + /** + * This list must remain synchronized with the one in {@code + * JavaVersionRecommendationAdminMonitor}. + */ private static final NavigableSet SUPPORTED_JAVA_VERSIONS = new TreeSet<>(Arrays.asList(11, 17, 21));