From a19110e638a5304e496fd0001f6dc45f6d409ca1 Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Mon, 22 Sep 2025 15:44:32 +1000 Subject: [PATCH] Improve logging with deprecation warnings --- .../plugin/PluginDeprecationLogger.java | 61 +++++++++++++++++++ .../PluginPullRequestWebhookProcessor.java | 6 ++ .../plugin/PluginPushWebhookProcessor.java | 2 + .../webhook/plugin/PluginWebhookManager.java | 1 + 4 files changed, 70 insertions(+) create mode 100644 src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginDeprecationLogger.java diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginDeprecationLogger.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginDeprecationLogger.java new file mode 100644 index 000000000..2d6ffd103 --- /dev/null +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginDeprecationLogger.java @@ -0,0 +1,61 @@ +/* + * The MIT License + * + * Copyright (c) 2025, Allan Burdajewicz + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.cloudbees.jenkins.plugins.bitbucket.impl.webhook.plugin; + +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Logs a deprecation warning for the Bitbucket Addons support in the plugin. + * If the logger is configured to log FINE messages, the warning is logged at FINE level. + * Otherwise, the warning is logged at WARNING level at most once every 5 minutes to avoid log flooding in very active environment. + */ +public class PluginDeprecationLogger { + + private static final long LOG_INTERVAL_MS = 5 * 60 * 1000; // 5 minutes + private static final java.util.concurrent.atomic.AtomicLong lastDeprecationLog = + new java.util.concurrent.atomic.AtomicLong(0); + + /** + * Logs the deprecation warning if the last log was more than LOG_INTERVAL_MS ago. + * + * @param logger the logger to use + */ + public static void log(Logger logger) { + if(logger.isLoggable(Level.FINE)) { + logger.fine("Bitbucket Addons support are deprecated in favor of native integrations. Please migrate to Bitbucket Server native webhook."); + } else { + long now = System.currentTimeMillis(); + long lastLog = lastDeprecationLog.get(); + if (now - lastLog > LOG_INTERVAL_MS) { + if (lastDeprecationLog.compareAndSet(lastLog, now)) { + logger.log( + Level.WARNING, + () -> + "Bitbucket Addons support are deprecated in favor of native integrations. Please migrate to Bitbucket Server native webhook."); + } + } + } + } +} diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginPullRequestWebhookProcessor.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginPullRequestWebhookProcessor.java index 91b80f187..ade661d2b 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginPullRequestWebhookProcessor.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginPullRequestWebhookProcessor.java @@ -34,6 +34,8 @@ import hudson.RestrictedSince; import java.util.List; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; import jenkins.scm.api.SCMEvent; import org.apache.commons.collections4.MultiValuedMap; import org.kohsuke.accmod.Restricted; @@ -45,6 +47,8 @@ @RestrictedSince("933.3.0") public class PluginPullRequestWebhookProcessor extends AbstractWebhookProcessor { + private static final Logger logger = Logger.getLogger(PluginPullRequestWebhookProcessor.class.getName()); + private static final List supportedEvents = List.of( HookEventType.PULL_REQUEST_CREATED.getKey(), // needed to create job HookEventType.PULL_REQUEST_DECLINED.getKey(), // needed to remove job @@ -61,6 +65,8 @@ public boolean canHandle(@NonNull Map headers, @NonNull MultiVal @Override public void process(@NonNull String hookEventType, @NonNull String payload, @NonNull Map context, @NonNull BitbucketEndpoint endpoint) { + PluginDeprecationLogger.log(logger); + logger.log(Level.FINE, () -> "Processing hook: " + hookEventType + " payload: " + " from: " + endpoint.getServerURL()); HookEventType hookEvent = HookEventType.fromString(hookEventType); BitbucketPullRequestEvent pull = BitbucketServerWebhookPayload.pullRequestEventFromPayload(payload); if (pull != null) { diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginPushWebhookProcessor.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginPushWebhookProcessor.java index cab45e6a5..04d8730f3 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginPushWebhookProcessor.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginPushWebhookProcessor.java @@ -62,6 +62,8 @@ public boolean canHandle(Map headers, MultiValuedMap context, @NonNull BitbucketEndpoint endpoint) { + PluginDeprecationLogger.log(logger); + logger.log(Level.FINE, () -> "Processing hook: " + eventType + " payload: " + " from: " + endpoint.getServerURL()); BitbucketPushEvent push = BitbucketServerWebhookPayload.pushEventFromPayload(payload); if (push != null) { if (push.getChanges().isEmpty()) { diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginWebhookManager.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginWebhookManager.java index c0b8055e6..a754816c9 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginWebhookManager.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/plugin/PluginWebhookManager.java @@ -193,6 +193,7 @@ public void remove(@NonNull String webhookId, @NonNull BitbucketAuthenticatedCli @Override public void register(@NonNull BitbucketAuthenticatedClient client) throws IOException { + PluginDeprecationLogger.log(logger); BitbucketPluginWebhook existingHook = (BitbucketPluginWebhook) read(client) .stream() .findFirst()