Skip to content

Commit

Permalink
provide a way to extract last logged event via logging in duplicates …
Browse files Browse the repository at this point in the history
…admin monitor (checking if this is the best, if yes then will write a test for it)
  • Loading branch information
gbhat618 committed Feb 6, 2025
1 parent e77068d commit 188be21
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jenkinsci.plugins.github.admin;

import java.util.logging.Logger;

import hudson.Extension;
import hudson.model.AdministrativeMonitor;
import jenkins.model.Jenkins;
Expand All @@ -10,6 +12,9 @@
@Extension
public class GitHubDuplicateEventsMonitor extends AdministrativeMonitor {

private static final Logger LOGGER = Logger.getLogger(GitHubDuplicateEventsMonitor.class.getName());
private static String previouslyLoggedEventId;

@Override
public String getDisplayName() {
return Messages.duplicate_events_administrative_monitor_displayname();
Expand All @@ -25,7 +30,17 @@ public String getBlurb() {

@Override
public boolean isActivated() {
return DuplicateEventsSubscriber.isDuplicateEventSeen();
boolean isActivated = DuplicateEventsSubscriber.isDuplicateEventSeen();
if (isActivated) {
var curDuplicate = DuplicateEventsSubscriber.getLastDuplicate();
if (!curDuplicate.eventGuid().equals(previouslyLoggedEventId)) {

Check warning on line 36 in src/main/java/org/jenkinsci/plugins/github/admin/GitHubDuplicateEventsMonitor.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 36 is only partially covered, one branch is missing
LOGGER.finest(() -> {
previouslyLoggedEventId = curDuplicate.eventGuid();
return "Latest tracked event payload: " + curDuplicate.ghSubscriberEvent().getPayload();

Check warning on line 39 in src/main/java/org/jenkinsci/plugins/github/admin/GitHubDuplicateEventsMonitor.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 38-39 are not covered by tests
});
}
}
return isActivated;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public final class DuplicateEventsSubscriber extends GHEventsSubscriber {
private static final Map<String, Long> EVENT_TRACKER = new ConcurrentHashMap<>();
private static volatile TrackedDuplicateEvent lastDuplicate;

@VisibleForTesting
record TrackedDuplicateEvent(String eventGuid, long lastUpdated, GHSubscriberEvent ghSubscriberEvent) { }
public record TrackedDuplicateEvent(String eventGuid, long lastUpdated, GHSubscriberEvent ghSubscriberEvent) { }

/**
* This subscriber is not applicable to any item
Expand Down Expand Up @@ -113,9 +112,7 @@ static Map<String, Long> getEventCountsTracker() {
return Collections.unmodifiableMap(EVENT_TRACKER);
}

@VisibleForTesting
@Restricted(NoExternalUse.class)
static TrackedDuplicateEvent getLastDuplicate() {
public static TrackedDuplicateEvent getLastDuplicate() {
return lastDuplicate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ duplicate.events.administrative.monitor.displayname=GitHub Duplicate Events
duplicate.events.administrative.monitor.description=Warns about duplicate events received from GitHub.
duplicate.events.administrative.monitor.blurb=Duplicate events were received from GitHub, possibly due to \
misconfiguration (e.g., multiple webhooks targeting the same Jenkins controller at the repository or organization \
level), potentially causing redundant job executions.
level), potentially causing redundant job executions. To inspect the last tracked duplicate event payload, \
enable logging at the `FINEST` level for the class `org.jenkinsci.plugins.github.admin.GitHubDuplicateEventsMonitor`.

0 comments on commit 188be21

Please sign in to comment.