Skip to content

Commit 2d6df6d

Browse files
Support SVG icons from Jenkins core
1 parent 429bb77 commit 2d6df6d

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

src/main/java/com/jenkinsci/plugins/badge/action/AbstractBadgeAction.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import java.io.IOException;
3131
import java.io.Serial;
3232
import java.io.Serializable;
33+
import java.net.HttpURLConnection;
34+
import java.net.URI;
35+
import java.net.URISyntaxException;
3336
import java.util.UUID;
3437
import java.util.logging.Level;
3538
import java.util.logging.Logger;
@@ -136,10 +139,33 @@ public String getIcon() {
136139
case "text.gif" -> "symbol-document-text";
137140
case "warning.gif" -> "symbol-status-yellow";
138141
case "yellow.gif" -> Emojis.getIconClassName("yellow_square");
139-
default -> Jenkins.RESOURCE_PATH + "/images/16x16/" + icon;
142+
default -> {
143+
if (isJenkinsResource(Jenkins.RESOURCE_PATH + "/images/16x16/" + icon)) {
144+
yield Jenkins.RESOURCE_PATH + "/images/16x16/" + icon;
145+
} else if (isJenkinsResource(Jenkins.RESOURCE_PATH + "/images/svgs/" + icon)) {
146+
yield Jenkins.RESOURCE_PATH + "/images/svgs/" + icon;
147+
} else {
148+
LOGGER.log(Level.WARNING, () -> "Icon '" + icon + "' not found as Jenkins resource");
149+
yield icon;
150+
}
151+
}
140152
};
141153
}
142154

155+
private static boolean isJenkinsResource(String iconPath) {
156+
try {
157+
String url = Jenkins.get().getRootUrl() + iconPath;
158+
HttpURLConnection conn = (HttpURLConnection) new URI(url).toURL().openConnection();
159+
conn.setRequestMethod("HEAD");
160+
conn.setConnectTimeout(3000);
161+
conn.setReadTimeout(3000);
162+
return conn.getResponseCode() == 200;
163+
} catch (IOException | URISyntaxException ex) {
164+
LOGGER.log(Level.WARNING, ex, () -> "Unable to validate Jenkins resource '" + iconPath + "'.");
165+
return false;
166+
}
167+
}
168+
143169
@Whitelisted
144170
public void setText(String text) {
145171
this.text = text;

src/main/resources/com/jenkinsci/plugins/badge/dsl/AddBadgeStep/help-icon.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
</p>
2525

2626
<p>
27-
The plugin also supports the <a href="https://github.com/jenkinsci/jenkins/tree/master/war/src/main/webapp/images/16x16">16x16 icons</a> that are offered by Jenkins.
28-
These icons are listed in the <a href="https://plugins.jenkins.io/badge/#plugin-content-icons">plugin documentation</a> and are referenced as "<i>image-name</i>.gif".
29-
For example, the following are all valid references to icons included with the plugin:
27+
The plugin also supports the <a href="https://github.com/jenkinsci/jenkins/tree/master/war/src/main/webapp/images/16x16">16x16 icons</a> as well as the <a href="https://github.com/jenkinsci/jenkins/tree/master/war/src/main/webapp/images/svgs">SVG icons</a> that are offered by Jenkins.
28+
For example, the following are all valid references to icons included by Jenkins:
3029
<ul>
3130
<li><code>addBadge icon: 'aborted.gif', text: 'This is a aborted symbol'</code></li>
3231
<li><code>addBadge icon: 'folder-open.gif', text: 'This is a folder icon'</code></li>
3332
<li><code>addBadge icon: 'document_edit.gif', text: 'This is a document icon'</code></li>
33+
<li><code>addBadge icon: 'error.svg', text: 'This is an error'</code></li>
3434
</ul>
3535
</p>
3636
</div>

src/main/resources/com/jenkinsci/plugins/badge/dsl/AddSummaryStep/help-icon.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
</p>
2525

2626
<p>
27-
The plugin includes icons as GIF images and also supports the <a href="https://github.com/jenkinsci/jenkins/tree/master/war/src/main/webapp/images/16x16">16x16 icons</a> that are offered by Jenkins.
28-
These icons are listed in the <a href="https://plugins.jenkins.io/badge/#plugin-content-icons">plugin documentation</a> and are referenced as "<i>image-name</i>.gif".
29-
For example, the following are all valid references to icons that can be used with the plugin:
27+
The plugin also supports the <a href="https://github.com/jenkinsci/jenkins/tree/master/war/src/main/webapp/images/16x16">16x16 icons</a> as well as the <a href="https://github.com/jenkinsci/jenkins/tree/master/war/src/main/webapp/images/svgs">SVG icons</a> that are offered by Jenkins.
28+
For example, the following are all valid references to icons included by Jenkins:
3029
<ul>
3130
<li><code>addSummary icon: 'aborted.gif', text: 'This is a aborted symbol'</code></li>
3231
<li><code>addSummary icon: 'folder-open.gif', text: 'This is a folder icon'</code></li>
3332
<li><code>addSummary icon: 'document_edit.gif', text: 'This is a document icon'</code></li>
33+
<li><code>addSummary icon: 'error.svg', text: 'This is an error'</code></li>
3434
</ul>
3535
</p>
3636
</div>

src/test/java/com/jenkinsci/plugins/badge/action/AbstractBadgeActionTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ void icon() {
7979
action.setIcon("");
8080
assertThat(action.getIcon(), emptyString());
8181

82-
action.setIcon("icon.png");
83-
assertThat(action.getIcon(), is(Jenkins.RESOURCE_PATH + "/images/16x16/icon.png"));
84-
8582
action.setIcon("/relative/url/icon.png");
8683
assertThat(action.getIcon(), is("/relative/url/icon.png"));
8784

@@ -126,8 +123,21 @@ void icon() {
126123
action.setIcon("yellow.gif");
127124
assertThat(action.getIcon(), is(Emojis.getIconClassName("yellow_square")));
128125

126+
// does not exist in core
127+
action.setIcon("icon.png");
128+
assertThat(action.getIcon(), is("icon.png"));
129+
130+
// core resource in 16x16
129131
action.setIcon("blue.gif");
130132
assertThat(action.getIcon(), is(Jenkins.RESOURCE_PATH + "/images/16x16/blue.gif"));
133+
134+
// core resource in svgs
135+
action.setIcon("error.svg");
136+
assertThat(action.getIcon(), is(Jenkins.RESOURCE_PATH + "/images/svgs/error.svg"));
137+
138+
// can not be validated
139+
action.setIcon("[/]");
140+
assertThat(action.getIcon(), is("[/]"));
131141
}
132142

133143
@Test

0 commit comments

Comments
 (0)