Skip to content

Commit

Permalink
add a user property to store configuration per user
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsymhoven committed May 3, 2021
1 parent e789cdf commit e92c384
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.jenkins.plugins.monitoring;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.User;
import hudson.model.UserProperty;
import hudson.model.UserPropertyDescriptor;

import java.util.ArrayList;
import java.util.Collection;

/**
* A {@link hudson.model.UserProperty} to store the json configuration per user as property.
*/
public class MonitorUserProperty extends hudson.model.UserProperty {

private Collection<MonitorProperty> properties;

public MonitorUserProperty() {

}

public MonitorUserProperty(MonitorProperty defaultProp) {
this.properties = new ArrayList<>();
this.properties.add(defaultProp);
}

public Collection<MonitorProperty> getProperties() {
return properties;
}

public MonitorProperty getView(String id) {
return this.getProperties().stream()
.filter(monitorProperty -> monitorProperty.getId().equals(id))
.findFirst().orElse(null);
}

public void setProperties(Collection<MonitorProperty> properties) {
this.properties = properties;
}

public void createOrUpdate(String id, String config) {
MonitorProperty property = this.getProperties().stream()
.filter(monitorProperty -> monitorProperty.getId().equals(id))
.findFirst().orElse(null);

if (property == null) {
this.getProperties().add(new MonitorProperty(id, config));
} else {
property.setConfig(config);
}
}

public static class MonitorProperty {

private final String id;
private String config;

public MonitorProperty(String id, String config) {
this.id = id;
this.config = config;
}

public String getId() {
return id;
}

public String getConfig() {
return config;
}

public void setConfig(String config) {
this.config = config;
}

}

@Extension
public static class MonitorPropertyDescriptor extends UserPropertyDescriptor {

@Override
public UserProperty newInstance(User user) {
return new MonitorUserProperty();
}

@NonNull
@Override
public String getDisplayName() {
return "Monitoring Configuration";
}
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package io.jenkins.plugins.monitoring;

import hudson.model.Run;
import hudson.model.User;
import jenkins.model.RunAction2;
import org.kohsuke.stapler.bind.JavaScriptMethod;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* This action displays a link on the side panel of a {@link Run}. The action is only displayed if the parent job
* is a pull request.
Expand All @@ -26,14 +31,49 @@ public class MonitoringBuildAction implements RunAction2 {
* @param monitor
* the {@link Monitor} to be add.
*/
public MonitoringBuildAction(Run<?, ?> run, Monitor monitor) {
public MonitoringBuildAction(Run<?, ?> run, Monitor monitor) throws IOException {
this.owner = run;
this.monitor = monitor;
}

@JavaScriptMethod
public String getConfiguration() {
return monitor.getConfiguration();
public void updateUserConfiguration(String id, String config) throws IOException {
User user = User.current();
MonitorUserProperty property = user.getProperty(MonitorUserProperty.class);
MonitorUserProperty.MonitorProperty defaultProp = new MonitorUserProperty.MonitorProperty("default",
this.monitor.getConfiguration());

if (property == null) {
property = new MonitorUserProperty(defaultProp);
user.addProperty(property);
}

if (property.getProperties() == null) {
List<MonitorUserProperty.MonitorProperty> views = new ArrayList<>();
views.add(defaultProp);
property.setProperties(views);
}

property.createOrUpdate(id, config);
user.save();
}

@JavaScriptMethod
public String getConfiguration(String id) throws IOException {
User user = User.current();
MonitorUserProperty property = user.getProperty(MonitorUserProperty.class);

if (property.getProperties() == null) {
updateUserConfiguration("default", this.monitor.getConfiguration());
}

MonitorUserProperty.MonitorProperty monitorProperty = property.getView(id);

if (monitorProperty == null) {
monitorProperty = property.getView("default");
}

return monitorProperty.getConfig();
}

@Override
Expand All @@ -51,14 +91,6 @@ public String getUrlName() {
return MonitoringMultibranchProjectAction.getURI();
}

public Run<?, ?> getRun() {
return owner;
}

public Monitor getMonitor() {
return monitor;
}

@Override
public void onAttached(Run<?, ?> run) {
this.owner = run;
Expand All @@ -69,4 +101,12 @@ public void onLoad(Run<?, ?> run) {
this.owner = run;
}

public Run<?, ?> getRun() {
return owner;
}

public Monitor getMonitor() {
return monitor;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?jelly escape-by-default='true'?>

<j:jelly xmlns:j="jelly:core">

<p>Nothing to configure here!</p>

</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<div class="container">

<button type="button" id="config-button" class="btn" data-toggle="modal" data-target="#configModal">
<button type="button" id="config-button" class="btn btn-success" data-toggle="modal" data-target="#configModal">

<i class="material-icons icon spin">&#xE8B8;</i>

Expand Down Expand Up @@ -70,7 +70,7 @@
<h5 class="modal-title" id="configModalLabel">

<img src="${resURL}/plugin/pull-request-monitoring/icons/washington.png" width="15%" height="15%"/>
Jenkinsfile Configuration
Current Dashboard Configuration

</h5>

Expand All @@ -82,13 +82,6 @@

<div class="modal-body">

<h4>
<span class="badge badge-pill" id="badge-config"/>
<span class="badge badge-pill" id="badge-config-type"/>
</h4>

<p id="config-text"/>

<div class="row">

<div class="col-9">
Expand Down Expand Up @@ -211,28 +204,38 @@

</div>

<div class="select">
<j:if test="${it.monitor.getAvailablePlugins(it.run).size() == 0}">

<input id="defaultInput" type="radio" name="plugin" value="default"/>
<p>No plugins were found that provide a view!</p>

<i class="material-icons toggle icon icon-plus">&#xe5c5;</i>
</j:if>

<i class="material-icons toggle icon icon-minus">&#xe5c7;</i>
<j:if test="${it.monitor.getAvailablePlugins(it.run).size() > 0}">

<span class="placeholder">Choose...</span>
<div class="select">

<j:forEach var="plugin" items="${it.monitor.getAvailablePlugins(it.run)}">
<input id="defaultInput" type="radio" name="plugin" value="default"/>

<label class="option">
<input type="radio" name="plugin" value="${plugin.id}"/>
<span class="title">
<i class="material-icons icon">&#xe433;</i>
${plugin.title}</span>
</label>
<i class="material-icons toggle icon icon-plus">&#xe5c5;</i>

</j:forEach>
<i class="material-icons toggle icon icon-minus">&#xe5c7;</i>

</div>
<span class="placeholder">Choose...</span>

<j:forEach var="plugin" items="${it.monitor.getAvailablePlugins(it.run)}">

<label class="option">
<input type="radio" name="plugin" value="${plugin.id}"/>
<span class="title">
<i class="material-icons icon">&#xe433;</i>
${plugin.title}</span>
</label>

</j:forEach>

</div>

</j:if>


</div>
Expand Down
30 changes: 15 additions & 15 deletions src/main/webapp/css/pull-request-monitoring.css
Original file line number Diff line number Diff line change
Expand Up @@ -141,48 +141,48 @@
}

.muuri-item.h1 {
height: 110px;
line-height: 110px;
height: 100px;
line-height: 100px;
}

.muuri-item.w1 {
width: 110px;
width: 100px;
}

.muuri-item.h2 {
height: 240px;
line-height: 240px;
height: 200px;
line-height: 200px;
}

.muuri-item.w2 {
width: 240px;
width: 200px;
}

.muuri-item.h3 {
height: 360px;
line-height: 360px;
height: 300px;
line-height: 300px;
}

.muuri-item.w3 {
width: 360px;
width: 300px;
}

.muuri-item.h4 {
height: 500px;
line-height: 500px;
height: 400px;
line-height: 400px;
}

.muuri-item.w4 {
width: 500px;
width: 400px;
}

.muuri-item.h5 {
height: 660px;
line-height: 660px;
height: 500px;
line-height: 500px;
}

.muuri-item.w5 {
width: 660px;
width: 500px;
}

.muuri-item-content {
Expand Down
Loading

0 comments on commit e92c384

Please sign in to comment.