Skip to content

Commit

Permalink
Add REST API endpoint for administrative monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-beck committed Nov 9, 2023
1 parent 41d09e0 commit 791b4d5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
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.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
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,25 @@
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 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.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
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 +46,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);

Check warning on line 72 in core/src/main/java/jenkins/management/AdministrativeMonitorsApi.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 60-72 are not covered by tests
}
}
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

0 comments on commit 791b4d5

Please sign in to comment.