├── .github
├── CODEOWNERS
└── workflows
│ └── jenkins-security-scan.yml
├── Jenkinsfile
├── README.md
├── .gitignore
├── LICENSE.txt
├── src
└── main
│ ├── webapp
│ ├── help-onlyHidden.html
│ ├── help-disableInfoInDesc.html
│ └── help
│ │ └── parameter
│ │ ├── admin-bool-param.html
│ │ └── admin-string-param.html
│ ├── resources
│ ├── index.jelly
│ └── io
│ │ └── jenkins
│ │ └── plugins
│ │ └── adminparameters
│ │ ├── AdminStringParameterValue
│ │ └── value.jelly
│ │ ├── AdminBooleanParameterValue
│ │ └── value.jelly
│ │ ├── AdminBooleanParameterDefinition
│ │ ├── index.jelly
│ │ └── config.jelly
│ │ └── AdminStringParameterDefinition
│ │ ├── index.jelly
│ │ └── config.jelly
│ └── java
│ └── io
│ └── jenkins
│ └── plugins
│ └── adminparameters
│ ├── AdminStringParameterValue.java
│ ├── AdminBooleanParameterValue.java
│ ├── Utils.java
│ ├── AdminBooleanParameterDefinition.java
│ └── AdminStringParameterDefinition.java
└── pom.xml
/.github/CODEOWNERS:
--------------------------------------------------------------------------------
1 | * @jenkinsci/admin-params-plugin-developers
--------------------------------------------------------------------------------
/Jenkinsfile:
--------------------------------------------------------------------------------
1 | buildPlugin(
2 | useContainerAgent: true,
3 | failFast: false,
4 | forkCount: '1C',
5 | configurations: [
6 | [platform: 'linux', jdk: 21],
7 | // [platform: 'windows', jdk: 17],
8 | ]
9 | )
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Adds two new parameter types called Admin String Parameter and
2 | Admin Boolean Parameter, which are visible only to users with
3 | admin permissions. Additionally, parameters can be set just hidden
4 | from non-admin users on UI.
5 |
6 | These parameter types are intended to prevent normal users from
7 | seeing and adjusting these parameter values, while the admin user
8 | can see them on the build screen. They should not be considered as a
9 | secret parameter.
10 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ### Java template
2 | # Compiled class file
3 | *.class
4 |
5 | # Log file
6 | *.log
7 |
8 | # BlueJ files
9 | *.ctxt
10 |
11 | # Mobile Tools for Java (J2ME)
12 | .mtj.tmp/
13 |
14 | # Package Files #
15 | *.jar
16 | *.war
17 | *.nar
18 | *.ear
19 | *.zip
20 | *.tar.gz
21 | *.rar
22 |
23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
24 | hs_err_pid*
25 | replay_pid*
26 |
27 | ### IntelliJ template
28 | .idea
29 | *.iml
30 | out
31 | gen
32 |
33 | ### VisualStudioCode template
34 | .vscode/*
35 | !.vscode/settings.json
36 | !.vscode/tasks.json
37 | !.vscode/launch.json
38 | !.vscode/extensions.json
39 | !.vscode/*.code-snippets
40 |
41 | # Local History for Visual Studio Code
42 | .history/
43 |
44 | # Built Visual Studio Code Extensions
45 | *.vsix
46 |
47 | ### Exclude Folders
48 | target
49 | work
50 |
--------------------------------------------------------------------------------
/.github/workflows/jenkins-security-scan.yml:
--------------------------------------------------------------------------------
1 | # More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/
2 | ---
3 | name: Jenkins Security Scan
4 | on:
5 | push:
6 | branches:
7 | - "master"
8 | - "main"
9 | pull_request:
10 | types: [opened, synchronize, reopened]
11 | workflow_dispatch:
12 |
13 | permissions:
14 | security-events: write
15 | contents: read
16 | actions: read
17 |
18 | jobs:
19 | security-scan:
20 | uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2
21 | with:
22 | java-cache: 'maven' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate.
23 | # java-version: 21 # Optionally specify what version of Java to set up for the build, or remove to use a recent default.
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2024-2025 Tunahan Sezen
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
--------------------------------------------------------------------------------
/src/main/webapp/help-onlyHidden.html:
--------------------------------------------------------------------------------
1 |
24 |
25 |
Only hide from non-admin users, default value will be used.
26 |
--------------------------------------------------------------------------------
/src/main/webapp/help-disableInfoInDesc.html:
--------------------------------------------------------------------------------
1 |
24 |
25 | Disable admin parameter information message in description.
26 |
--------------------------------------------------------------------------------
/src/main/resources/index.jelly:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 |
27 |
28 | Parameters that can be configured to be visible and editable only by Jenkins administrators.
29 |
30 |
31 |
--------------------------------------------------------------------------------
/src/main/webapp/help/parameter/admin-bool-param.html:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 |
Defines a boolean parameter, visible only to administrators,
27 | allowing them to select a true or false value. This value can be
28 | used during a build to control conditional logic, environment
29 | variables, or other configuration settings.
30 |
31 |
--------------------------------------------------------------------------------
/src/main/webapp/help/parameter/admin-string-param.html:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 |
Defines a simple text parameter, visible only to
27 | administrators, where they can enter a string value.
28 | This value can be used during a build, either as an
29 | environment variable or through variable substitution
30 | in other parts of the configuration.
31 |
32 |
--------------------------------------------------------------------------------
/src/main/resources/io/jenkins/plugins/adminparameters/AdminStringParameterValue/value.jelly:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/src/main/resources/io/jenkins/plugins/adminparameters/AdminBooleanParameterValue/value.jelly:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/src/main/java/io/jenkins/plugins/adminparameters/AdminStringParameterValue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2024-2025 Tunahan Sezen
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package io.jenkins.plugins.adminparameters;
26 |
27 | import hudson.model.StringParameterValue;
28 | import org.kohsuke.stapler.DataBoundConstructor;
29 |
30 | public class AdminStringParameterValue extends StringParameterValue {
31 |
32 | @DataBoundConstructor
33 | public AdminStringParameterValue(String name, String value) {
34 | this(name, value, null);
35 | }
36 |
37 | public AdminStringParameterValue(String name, String value, String description) {
38 | super(name, value, description);
39 | }
40 |
41 | public String getValue() {
42 | return value;
43 | }
44 |
45 | @Override
46 | public String toString() {
47 | return "(AdminStringParameterValue) " + getName() + "='" + value + "'";
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/io/jenkins/plugins/adminparameters/AdminBooleanParameterValue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2024-2025 Tunahan Sezen
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package io.jenkins.plugins.adminparameters;
26 |
27 | import hudson.model.BooleanParameterValue;
28 | import org.kohsuke.stapler.DataBoundConstructor;
29 |
30 | public class AdminBooleanParameterValue extends BooleanParameterValue {
31 |
32 | @DataBoundConstructor
33 | public AdminBooleanParameterValue(String name, boolean value) {
34 | this(name, value, null);
35 | }
36 |
37 | public AdminBooleanParameterValue(String name, boolean value, String description) {
38 | super(name, value, description);
39 | }
40 |
41 | public Boolean getValue() {
42 | return value;
43 | }
44 |
45 | @Override
46 | public String toString() {
47 | return "(AdminBooleanParameterValue) " + getName() + "='" + value + "'";
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/resources/io/jenkins/plugins/adminparameters/AdminBooleanParameterDefinition/index.jelly:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/src/main/resources/io/jenkins/plugins/adminparameters/AdminStringParameterDefinition/index.jelly:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/src/main/resources/io/jenkins/plugins/adminparameters/AdminBooleanParameterDefinition/config.jelly:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/src/main/java/io/jenkins/plugins/adminparameters/Utils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2024-2025 Tunahan Sezen
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package io.jenkins.plugins.adminparameters;
26 |
27 | import hudson.markup.MarkupFormatter;
28 | import jenkins.model.Jenkins;
29 |
30 | import java.util.logging.Level;
31 | import java.util.logging.Logger;
32 |
33 | public class Utils {
34 |
35 | private Utils() {
36 | }
37 |
38 | private static final Logger LOGGER = Logger.getLogger(Utils.class.getName());
39 |
40 | private static final String DESC_WARNING_MSG = "This is an admin parameter";
41 |
42 | public static boolean isUserAdmin() {
43 | return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
44 | }
45 |
46 | public static String adminFormattedDescription(String description) {
47 | StringBuilder sb = new StringBuilder();
48 | if (isHtmlFormatter()) {
49 | sb.append("");
50 | }
51 | sb.append(DESC_WARNING_MSG);
52 | if (isHtmlFormatter()) {
53 | sb.append(" ");
54 | }
55 | sb.append(" ");
56 | sb.append(description);
57 | return sb.toString();
58 | }
59 |
60 | private static boolean isHtmlFormatter() {
61 | MarkupFormatter formatter = Jenkins.get().getMarkupFormatter();
62 | String formatterClassName = formatter.getClass().getSimpleName();
63 | LOGGER.log(Level.CONFIG, "Formatter class name: {0}", formatterClassName);
64 | return formatterClassName.toLowerCase().contains("html");
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/resources/io/jenkins/plugins/adminparameters/AdminStringParameterDefinition/config.jelly:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | org.jenkins-ci.plugins
6 | plugin
7 | 5.9
8 |
9 |
10 | io.jenkins.plugins
11 | admin-params
12 | 1.0.1-SNAPSHOT
13 | hpi
14 | Admin Parameters
15 | A Jenkins plugin that enables parameters for only admin
16 |
17 |
18 | scm:git:https://github.com/${gitHubRepo}.git
19 | scm:git:git@github.com:${gitHubRepo}.git
20 | https://github.com/${gitHubRepo}
21 | ${scmTag}
22 |
23 |
24 |
25 |
26 | The MIT license
27 | https://opensource.org/licenses/MIT
28 | repo
29 |
30 |
31 |
32 |
33 | 2.479
34 | ${jenkins.baseline}.3
35 | jenkinsci/admin-params-plugin
36 |
37 |
38 |
39 |
40 |
41 | io.jenkins.tools.bom
42 | bom-${jenkins.baseline}.x
43 | 4545.v56392b_7ca_7b_a_
44 | import
45 | pom
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | org.apache.maven.plugins
54 | maven-antrun-plugin
55 | 3.1.0
56 |
57 |
58 | createTempDir
59 | generate-test-resources
60 |
61 | run
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 | repo.jenkins-ci.org
72 | https://repo.jenkins-ci.org/public/
73 |
74 |
75 |
76 |
77 | repo.jenkins-ci.org
78 | https://repo.jenkins-ci.org/public/
79 |
80 | true
81 |
82 |
83 | false
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/src/main/java/io/jenkins/plugins/adminparameters/AdminBooleanParameterDefinition.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2024-2025 Tunahan Sezen
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package io.jenkins.plugins.adminparameters;
26 |
27 | import edu.umd.cs.findbugs.annotations.NonNull;
28 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29 | import hudson.Extension;
30 | import hudson.model.BooleanParameterDefinition;
31 | import hudson.model.ParameterDefinition;
32 | import hudson.model.ParameterValue;
33 | import net.sf.json.JSONObject;
34 | import org.jenkinsci.Symbol;
35 | import org.kohsuke.stapler.DataBoundConstructor;
36 | import org.kohsuke.stapler.DataBoundSetter;
37 | import org.kohsuke.stapler.StaplerRequest2;
38 |
39 | import java.util.Objects;
40 |
41 | public class AdminBooleanParameterDefinition extends BooleanParameterDefinition {
42 |
43 | private boolean onlyHidden;
44 | private boolean disableInfoInDesc;
45 |
46 | @DataBoundConstructor
47 | public AdminBooleanParameterDefinition(String name) {
48 | super(name);
49 | }
50 |
51 | public AdminBooleanParameterDefinition(String name, boolean defaultValue, String description, boolean onlyHidden,
52 | boolean disableInfoInDesc) {
53 | this(name);
54 | this.setDefaultValue(defaultValue);
55 | this.setDescription(description);
56 | this.setOnlyHidden(onlyHidden);
57 | this.setDisableInfoInDesc(disableInfoInDesc);
58 | }
59 |
60 | public AdminBooleanParameterDefinition(String name, boolean defaultValue, String description) {
61 | this(name);
62 | this.setDefaultValue(defaultValue);
63 | this.setDescription(description);
64 | }
65 |
66 | public AdminBooleanParameterDefinition(String name, boolean defaultValue) {
67 | this(name);
68 | this.setDefaultValue(defaultValue);
69 | }
70 |
71 | public boolean isOnlyHidden() {
72 | return onlyHidden;
73 | }
74 |
75 | @DataBoundSetter
76 | public void setOnlyHidden(boolean onlyHidden) {
77 | this.onlyHidden = onlyHidden;
78 | }
79 |
80 | public boolean isDisableInfoInDesc() {
81 | return disableInfoInDesc;
82 | }
83 |
84 | @DataBoundSetter
85 | public void setDisableInfoInDesc(boolean disableInfoInDesc) {
86 | this.disableInfoInDesc = disableInfoInDesc;
87 | }
88 |
89 | @Override
90 | public AdminBooleanParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
91 | if (defaultValue instanceof AdminBooleanParameterValue value) {
92 | return new AdminBooleanParameterDefinition(this.getName(), value.value, this.getDescription());
93 | } else {
94 | return this;
95 | }
96 | }
97 |
98 | @Override
99 | public AdminBooleanParameterValue getDefaultParameterValue() {
100 | return new AdminBooleanParameterValue(this.getName(), this.isDefaultValue(), this.getDescription());
101 | }
102 |
103 | @Override
104 | public ParameterValue createValue(StaplerRequest2 req, JSONObject jo) {
105 | if (!isUserAdmin()) {
106 | return onlyHidden ? getDefaultParameterValue() : null;
107 | }
108 | AdminBooleanParameterValue value = req.bindJSON(AdminBooleanParameterValue.class, jo);
109 | value.setDescription(this.getDescription());
110 | return value;
111 | }
112 |
113 | @Override
114 | public ParameterValue createValue(String value) {
115 | if (!isUserAdmin()) {
116 | return onlyHidden ? getDefaultParameterValue() : null;
117 | }
118 | return new AdminBooleanParameterValue(this.getName(), Boolean.parseBoolean(value), this.getDescription());
119 | }
120 |
121 | @Override
122 | public int hashCode() {
123 | return AdminBooleanParameterDefinition.class != this.getClass() ? super.hashCode() :
124 | Objects.hash(this.getName(), this.getDescription(), this.isDefaultValue(), this.isOnlyHidden(),
125 | this.isDisableInfoInDesc());
126 | }
127 |
128 | @SuppressFBWarnings(
129 | value = {"EQ_GETCLASS_AND_CLASS_CONSTANT"},
130 | justification = "ParameterDefinitionTest tests that subclasses are not equal to their parent classes, so the behavior appears to be intentional"
131 | )
132 | @Override
133 | public boolean equals(Object obj) {
134 | if (AdminBooleanParameterDefinition.class != this.getClass()) {
135 | return super.equals(obj);
136 | } else if (this == obj) {
137 | return true;
138 | } else if (obj == null) {
139 | return false;
140 | } else if (this.getClass() != obj.getClass()) {
141 | return false;
142 | } else {
143 | AdminBooleanParameterDefinition other = (AdminBooleanParameterDefinition) obj;
144 | if (!Objects.equals(this.getName(), other.getName())) {
145 | return false;
146 | } else if (!Objects.equals(this.getDescription(), other.getDescription())) {
147 | return false;
148 | } else if (!Objects.equals(this.isDefaultValue(), other.isDefaultValue())) {
149 | return false;
150 | } else if (this.isOnlyHidden() != other.isOnlyHidden()) {
151 | return false;
152 | } else {
153 | return this.isDisableInfoInDesc() == other.isDisableInfoInDesc();
154 | }
155 | }
156 | }
157 |
158 | @Extension
159 | @Symbol({"adminBoolean", "adminBooleanParam"})
160 | public static class DescriptorImpl extends ParameterDefinition.ParameterDescriptor {
161 | public DescriptorImpl() {
162 | }
163 |
164 | @NonNull
165 | @Override
166 | public String getDisplayName() {
167 | return "Admin Boolean Parameter";
168 | }
169 |
170 | @Override
171 | public String getHelpFile() {
172 | return "/plugin/admin-params/help/parameter/admin-bool-param.html";
173 | }
174 | }
175 |
176 | public boolean isUserAdmin() {
177 | return Utils.isUserAdmin();
178 | }
179 |
180 | public String adminFormattedDescription() {
181 | if (disableInfoInDesc) {
182 | return getFormattedDescription();
183 | }
184 | return Utils.adminFormattedDescription(getFormattedDescription());
185 | }
186 | }
187 |
--------------------------------------------------------------------------------
/src/main/java/io/jenkins/plugins/adminparameters/AdminStringParameterDefinition.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2024-2025 Tunahan Sezen
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package io.jenkins.plugins.adminparameters;
26 |
27 | import edu.umd.cs.findbugs.annotations.NonNull;
28 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29 | import hudson.Extension;
30 | import hudson.model.ParameterDefinition;
31 | import hudson.model.ParameterValue;
32 | import hudson.model.StringParameterDefinition;
33 | import net.sf.json.JSONObject;
34 | import org.jenkinsci.Symbol;
35 | import org.kohsuke.stapler.DataBoundConstructor;
36 | import org.kohsuke.stapler.DataBoundSetter;
37 | import org.kohsuke.stapler.StaplerRequest2;
38 |
39 | import java.util.Objects;
40 |
41 | public class AdminStringParameterDefinition extends StringParameterDefinition {
42 |
43 | private boolean onlyHidden;
44 | private boolean disableInfoInDesc;
45 |
46 | @DataBoundConstructor
47 | public AdminStringParameterDefinition(String name) {
48 | super(name);
49 | }
50 |
51 | public AdminStringParameterDefinition(String name, String defaultValue, String description, boolean trim,
52 | boolean onlyHidden, boolean disableInfoInDesc) {
53 | this(name);
54 | this.setDefaultValue(defaultValue);
55 | this.setDescription(description);
56 | this.setTrim(trim);
57 | this.setOnlyHidden(onlyHidden);
58 | this.setDisableInfoInDesc(disableInfoInDesc);
59 | }
60 |
61 | public AdminStringParameterDefinition(String name, String defaultValue, String description) {
62 | this(name);
63 | this.setDefaultValue(defaultValue);
64 | this.setDescription(description);
65 | }
66 |
67 | public AdminStringParameterDefinition(String name, String defaultValue) {
68 | this(name);
69 | this.setDefaultValue(defaultValue);
70 | }
71 |
72 | public boolean isOnlyHidden() {
73 | return onlyHidden;
74 | }
75 |
76 | @DataBoundSetter
77 | public void setOnlyHidden(boolean onlyHidden) {
78 | this.onlyHidden = onlyHidden;
79 | }
80 |
81 | public boolean isDisableInfoInDesc() {
82 | return disableInfoInDesc;
83 | }
84 |
85 | @DataBoundSetter
86 | public void setDisableInfoInDesc(boolean disableInfoInDesc) {
87 | this.disableInfoInDesc = disableInfoInDesc;
88 | }
89 |
90 | @Override
91 | public AdminStringParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
92 | if (defaultValue instanceof AdminStringParameterValue value) {
93 | return new AdminStringParameterDefinition(this.getName(), value.value, this.getDescription());
94 | } else {
95 | return this;
96 | }
97 | }
98 |
99 | @Override
100 | public AdminStringParameterValue getDefaultParameterValue() {
101 | AdminStringParameterValue value = new AdminStringParameterValue(this.getName(), this.getDefaultValue(),
102 | this.getDescription());
103 | if (this.isTrim()) {
104 | value.doTrim();
105 | }
106 | return value;
107 | }
108 |
109 | @Override
110 | public ParameterValue createValue(StaplerRequest2 req, JSONObject jo) {
111 | if (!isUserAdmin()) {
112 | return onlyHidden ? getDefaultParameterValue() : null;
113 | }
114 | AdminStringParameterValue value = req.bindJSON(AdminStringParameterValue.class, jo);
115 | if (this.isTrim()) {
116 | value.doTrim();
117 | }
118 | value.setDescription(this.getDescription());
119 | return value;
120 | }
121 |
122 | @Override
123 | public ParameterValue createValue(String str) {
124 | if (!isUserAdmin()) {
125 | return onlyHidden ? getDefaultParameterValue() : null;
126 | }
127 | AdminStringParameterValue value = new AdminStringParameterValue(this.getName(), str, this.getDescription());
128 | if (this.isTrim()) {
129 | value.doTrim();
130 | }
131 | return value;
132 | }
133 |
134 | @Override
135 | public int hashCode() {
136 | return AdminStringParameterDefinition.class != this.getClass() ? super.hashCode() :
137 | Objects.hash(this.getName(), this.getDescription(), this.getDefaultValue(), this.isTrim(),
138 | this.isOnlyHidden(), this.isDisableInfoInDesc());
139 | }
140 |
141 | @SuppressFBWarnings(
142 | value = {"EQ_GETCLASS_AND_CLASS_CONSTANT"},
143 | justification = "ParameterDefinitionTest tests that subclasses are not equal to their parent classes, so the behavior appears to be intentional"
144 | )
145 | @Override
146 | public boolean equals(Object obj) {
147 | if (AdminStringParameterDefinition.class != this.getClass()) {
148 | return super.equals(obj);
149 | } else if (this == obj) {
150 | return true;
151 | } else if (obj == null) {
152 | return false;
153 | } else if (this.getClass() != obj.getClass()) {
154 | return false;
155 | } else {
156 | AdminStringParameterDefinition other = (AdminStringParameterDefinition) obj;
157 | if (!Objects.equals(this.getName(), other.getName())) {
158 | return false;
159 | } else if (!Objects.equals(this.getDescription(), other.getDescription())) {
160 | return false;
161 | } else if (!Objects.equals(this.getDefaultValue(), other.getDefaultValue())) {
162 | return false;
163 | } else if (!Objects.equals(this.isTrim(), other.isTrim())) {
164 | return this.isTrim() == other.isTrim();
165 | } else if (this.isOnlyHidden() != other.isOnlyHidden()) {
166 | return false;
167 | } else {
168 | return this.isDisableInfoInDesc() == other.isDisableInfoInDesc();
169 | }
170 | }
171 | }
172 |
173 | @Extension
174 | @Symbol({"adminString", "adminStringParam"})
175 | public static class DescriptorImpl extends ParameterDefinition.ParameterDescriptor {
176 | public DescriptorImpl() {
177 | }
178 |
179 | @NonNull
180 | @Override
181 | public String getDisplayName() {
182 | return "Admin String Parameter";
183 | }
184 |
185 | @Override
186 | public String getHelpFile() {
187 | return "/plugin/admin-params/help/parameter/admin-string-param.html";
188 | }
189 | }
190 |
191 | public boolean isUserAdmin() {
192 | return Utils.isUserAdmin();
193 | }
194 |
195 | public String adminFormattedDescription() {
196 | if (disableInfoInDesc) {
197 | return getFormattedDescription();
198 | }
199 | return Utils.adminFormattedDescription(getFormattedDescription());
200 | }
201 | }
202 |
--------------------------------------------------------------------------------