Skip to content

Commit

Permalink
Add Appearance system configuration page
Browse files Browse the repository at this point in the history
  • Loading branch information
timja committed Aug 21, 2023
1 parent 7f3fa04 commit 3e12ca2
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 0 deletions.
45 changes: 45 additions & 0 deletions core/src/main/java/jenkins/appearance/AppearanceCategory.java
@@ -0,0 +1,45 @@
/*
* The MIT License
*
* Copyright (c) 2023, Tim Jacomb
*
* 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 jenkins.appearance;

import hudson.Extension;
import jenkins.model.GlobalConfigurationCategory;

/**
* Global configuration of appearance configuration
*
*/
@Extension
public class AppearanceCategory extends GlobalConfigurationCategory {
@Override
public String getShortDescription() {
return Messages.AppearanceCategory_DisplayName();

Check warning on line 38 in core/src/main/java/jenkins/appearance/AppearanceCategory.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 38 is not covered by tests
}

@Override
public String getDisplayName() {
return Messages.AppearanceCategory_Description();

Check warning on line 43 in core/src/main/java/jenkins/appearance/AppearanceCategory.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 43 is not covered by tests
}
}
@@ -0,0 +1,110 @@
/*
* The MIT License
*
* Copyright (c) 2023, Tim Jacomb
*
* 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 jenkins.appearance;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Functions;
import hudson.model.Descriptor;
import hudson.model.ManagementLink;
import hudson.util.FormApply;
import java.io.IOException;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
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.verb.POST;

@Extension
public class AppearanceGlobalConfiguration extends ManagementLink {

private static final Logger LOGGER = Logger.getLogger(AppearanceGlobalConfiguration.class.getName());

@Restricted(NoExternalUse.class)
public static final Predicate<Descriptor> FILTER = input -> input.getCategory() instanceof AppearanceCategory;

@Override
public String getIconFileName() {
if (Functions.getSortedDescriptorsForGlobalConfigByDescriptor(FILTER).isEmpty()) {

Check warning on line 56 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 56 is only partially covered, one branch is missing
return null;
}

return "symbol-brush-outline";

Check warning on line 60 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 60 is not covered by tests
}

@Override
public String getDisplayName() {
return Messages.AppearanceGlobalConfiguration_DisplayName();

Check warning on line 65 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 65 is not covered by tests
}

@Override
public String getDescription() {
return Messages.AppearanceGlobalConfiguration_Description();

Check warning on line 70 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 70 is not covered by tests
}

@Override
public String getUrlName() {
return "appearance";
}

@NonNull
@Override
public Category getCategory() {
return Category.CONFIGURATION;

Check warning on line 81 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 81 is not covered by tests
}

@POST
public synchronized void doConfigure(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException {
boolean result = configure(req, req.getSubmittedForm());

Check warning on line 86 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 86 is not covered by tests
LOGGER.log(Level.FINE, "appearance saved: " + result);

Check warning on line 87 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 87 is not covered by tests
FormApply.success(req.getContextPath() + "/manage").generateResponse(req, rsp, null);

Check warning on line 88 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 88 is not covered by tests
}

Check warning on line 89 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 89 is not covered by tests

private boolean configure(StaplerRequest req, JSONObject json) throws Descriptor.FormException, IOException {
Jenkins j = Jenkins.get();

Check warning on line 92 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 92 is not covered by tests
j.checkPermission(Jenkins.MANAGE);

Check warning on line 93 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 93 is not covered by tests

boolean result = true;

Check warning on line 95 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 95 is not covered by tests
for (Descriptor<?> d : Functions.getSortedDescriptorsForGlobalConfigByDescriptor(FILTER)) {

Check warning on line 96 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 96 is only partially covered, 2 branches are missing
result &= configureDescriptor(req, json, d);

Check warning on line 97 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 97 is not covered by tests
}

Check warning on line 98 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 98 is not covered by tests
j.save();

Check warning on line 99 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 99 is not covered by tests

return result;

Check warning on line 101 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 101 is not covered by tests
}

private boolean configureDescriptor(StaplerRequest req, JSONObject json, Descriptor<?> d) throws Descriptor.FormException {
String name = d.getJsonSafeClassName();

Check warning on line 105 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 105 is not covered by tests
JSONObject js = json.has(name) ? json.getJSONObject(name) : new JSONObject(); // if it doesn't have the property, the method returns invalid null object.

Check warning on line 106 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 106 is only partially covered, 2 branches are missing
json.putAll(js);

Check warning on line 107 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 107 is not covered by tests
return d.configure(req, js);

Check warning on line 108 in core/src/main/java/jenkins/appearance/AppearanceGlobalConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 108 is not covered by tests
}
}
@@ -0,0 +1,61 @@
<!--
The MIT License
Copyright (c) 2023, Tim Jacomb
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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout" xmlns:f="/lib/form">
<l:layout title="${%Appearance}" permissions="${app.MANAGE_AND_SYSTEM_READ}">
<j:set var="readOnlyMode" value="${!app.hasPermission(app.MANAGE)}"/>
<l:main-panel>
<l:app-bar title="${%Appearance}"/>

<div class="behavior-loading">
<l:spinner text="${%Loading}"/>
</div>

<f:form method="post" name="config" action="configure" class="jenkins-form">
<j:set var="instance" value="${it}" />
<j:set var="descriptor" value="${instance.descriptor}" />

<j:forEach var="descriptor" items="${h.getSortedDescriptorsForGlobalConfigByDescriptor(it.FILTER)}">
<j:set var="instance" value="${descriptor}" />
<f:rowSet name="${descriptor.jsonSafeClassName}">
<st:include page="${descriptor.globalConfigPage}" from="${descriptor}" />
</f:rowSet>
</j:forEach>

<l:hasAdministerOrManage>
<f:bottomButtonBar>
<f:submit value="${%Save}" />
<f:apply />
</f:bottomButtonBar>
</l:hasAdministerOrManage>
</f:form>

<l:hasAdministerOrManage>
<st:adjunct includes="lib.form.confirm" />
</l:hasAdministerOrManage>

</l:main-panel>
</l:layout>
</j:jelly>
@@ -0,0 +1,5 @@
AppearanceGlobalConfiguration.DisplayName=Appearance
AppearanceGlobalConfiguration.Description=Configure the look and feel of Jenkins

AppearanceCategory.DisplayName=Appearance
AppearanceCategory.Description=Configure the look and feel of Jenkins
1 change: 1 addition & 0 deletions war/src/main/resources/images/symbols/brush-outline.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3e12ca2

Please sign in to comment.