Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REST API endpoint for administrative monitors #8688

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/src/main/java/hudson/model/AdministrativeMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.kohsuke.stapler.StaplerProxy;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.interceptor.RequirePOST;

/**
Expand Down Expand Up @@ -89,6 +91,7 @@
* @see Jenkins#administrativeMonitors
*/
@LegacyInstancesAreScopedToHudson
@ExportedBean
public abstract class AdministrativeMonitor extends AbstractModelObject implements ExtensionPoint, StaplerProxy {
/**
* Human-readable ID of this monitor, which needs to be unique within the system.
Expand All @@ -97,6 +100,7 @@ public abstract class AdministrativeMonitor extends AbstractModelObject implemen
* This ID is used to remember persisted setting for this monitor,
* so the ID should remain consistent beyond the Hudson JVM lifespan.
*/
@Exported
public final String id;

protected AdministrativeMonitor(String id) {
Expand Down Expand Up @@ -146,6 +150,7 @@ public void disable(boolean value) throws IOException {
* This flag implements the ability for the admin to say "no thank you" to the monitor that
* he wants to ignore.
*/
@Exported
public boolean isEnabled() {
return !Jenkins.get().getDisabledAdministrativeMonitors().contains(id);
}
Expand All @@ -158,6 +163,7 @@ public boolean isEnabled() {
* This method is called from the HTML rendering thread,
* so it should run efficiently.
*/
@Exported
public abstract boolean isActivated();

/**
Expand All @@ -167,6 +173,7 @@ public boolean isEnabled() {
*
* @since 2.267
*/
@Exported
public boolean isSecurity() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package jenkins.management;

import hudson.Extension;
import hudson.ExtensionList;
import hudson.model.AdministrativeMonitor;
import hudson.model.Api;
import hudson.model.PageDecorator;
import hudson.model.RootAction;
import jakarta.servlet.ServletException;
import java.io.IOException;
import java.util.Collection;
import javax.servlet.ServletException;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.verb.GET;

@Extension
@ExportedBean
@Restricted(NoExternalUse.class)
public class AdministrativeMonitorsApi implements RootAction {
@GET
Expand All @@ -39,12 +47,29 @@ public String getDisplayName() {

@Override
public String getUrlName() {
return "administrativeMonitorsApi";
return "administrative-monitors";
}

private AdministrativeMonitorsDecorator getDecorator() {
return Jenkins.get()
.getExtensionList(PageDecorator.class)
.get(AdministrativeMonitorsDecorator.class);
}

@Exported
public Collection<AdministrativeMonitor> getActiveAdministrativeMonitors() {
return ExtensionList.lookupSingleton(AdministrativeMonitorsDecorator.class).getAllActiveAdministrativeMonitors();
}

@Exported
public Collection<AdministrativeMonitor> getAdministrativeMonitors() {
return ExtensionList.lookup(AdministrativeMonitor.class);
}

public Api getApi() {
// Implement a permission check so failure to authenticate doesn't look like everything's OK when accessing /api.
// This check needs to match AdministrativeMonitorsDecorator#getMonitorsToDisplay.
Jenkins.get().checkPermission(Jenkins.SYSTEM_READ);
return new Api(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public List<AdministrativeMonitor> getSecurityAdministrativeMonitors() {
.collect(Collectors.toList());
}

private Collection<AdministrativeMonitor> getAllActiveAdministrativeMonitors() {
protected Collection<AdministrativeMonitor> getAllActiveAdministrativeMonitors() {
Collection<AdministrativeMonitor> active = new ArrayList<>();
for (AdministrativeMonitor am : Jenkins.get().getActiveAdministrativeMonitors()) {
if (am instanceof ReverseProxySetupMonitor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ THE SOFTWARE.
<a id="visible-am-button"
class="am-button"
href="#"
data-href="${rootURL}/administrativeMonitorsApi/nonSecurityPopupContent"
data-href="${rootURL}/administrative-monitors/nonSecurityPopupContent"
title="${%tooltip(activeNonSecurityAMCount)}">
<l:icon src="symbol-notifications" class="icon-md"/>
<div class="am-monitor__indicator-mobile"/>
Expand All @@ -55,7 +55,7 @@ THE SOFTWARE.
<a id="visible-sec-am-button"
class="am-button security-am"
href="#"
data-href="${rootURL}/administrativeMonitorsApi/securityPopupContent"
data-href="${rootURL}/administrative-monitors/securityPopupContent"
title="${%tooltipSec(activeSecurityAMCount)}">
<l:icon src="symbol-shield-warning" class="icon-md"/>
<div class="am-monitor__indicator-mobile"/>
Expand Down