Skip to content

Commit

Permalink
Add flat-styled images, make them default
Browse files Browse the repository at this point in the history
You can get the old "plastic" style badges by adding ?style=plastic to
the badge URL.

The new images were generated with

  wget https://img.shields.io/badge/build-failing-red.svg?style=flat         -O build-failing-red-flat.svg
  wget https://img.shields.io/badge/build-passing-brightgreen.svg?style=flat -O build-passing-brightgreen-flat.svg
  wget https://img.shields.io/badge/build-running-blue.svg?style=flat        -O build-running-blue-flat.svg
  wget https://img.shields.io/badge/build-unknown-lightgrey.svg?style=flat   -O build-unknown-lightgrey-flat.svg
  wget https://img.shields.io/badge/build-unstable-yellow.svg?style=flat     -O build-unstable-yellow-flat.svg

Fixes JENKINS-26705.
  • Loading branch information
mgedmin committed Jan 30, 2015
1 parent fe2441b commit f13eccb
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/jenkinsci/plugins/badge/BadgeAction.java
Expand Up @@ -8,6 +8,7 @@
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.QueryParameter;

import javax.servlet.ServletException;
import java.io.IOException;
Expand Down Expand Up @@ -43,7 +44,7 @@ public String getUrlName() {
/**
* Serves the badge image.
*/
public HttpResponse doIcon() {
return factory.getImage(project.getIconColor());
public HttpResponse doIcon(@QueryParameter String style) {
return factory.getImage(project.getIconColor(), style);
}
}
Expand Up @@ -17,7 +17,7 @@
public class BadgeActionFactory extends TransientProjectActionFactory {

private final ImageResolver iconResolver;

public BadgeActionFactory() throws IOException {
iconResolver = new ImageResolver();
}
Expand All @@ -31,4 +31,8 @@ public StatusImage getImage(BallColor color) {
return iconResolver.getImage(color);
}

public StatusImage getImage(BallColor color, String style) {
return iconResolver.getImage(color, style);
}

}
36 changes: 30 additions & 6 deletions src/main/java/org/jenkinsci/plugins/badge/ImageResolver.java
Expand Up @@ -26,22 +26,47 @@
import hudson.model.BallColor;

import java.io.IOException;
import java.util.HashMap;

public class ImageResolver {

private final StatusImage[] images;


private final HashMap<String, StatusImage[]> styles;
private final StatusImage[] defaultStyle;

public ImageResolver() throws IOException{
images = new StatusImage[] {
styles = new HashMap<String, StatusImage[]>();
// shields.io "plastic" style (aka the old default)
StatusImage[] plasticImages = new StatusImage[] {
new StatusImage("build-failing-red.svg"),
new StatusImage("build-unstable-yellow.svg"),
new StatusImage("build-passing-brightgreen.svg"),
new StatusImage("build-running-blue.svg"),
new StatusImage("build-unknown-lightgrey.svg")
};
styles.put("plastic", plasticImages);
// shields.io "flat" style (new default from Feb 1 2015)
StatusImage[] flatImages = new StatusImage[] {
new StatusImage("build-failing-red-flat.svg"),
new StatusImage("build-unstable-yellow-flat.svg"),
new StatusImage("build-passing-brightgreen-flat.svg"),
new StatusImage("build-running-blue-flat.svg"),
new StatusImage("build-unknown-lightgrey-flat.svg")
};
styles.put("flat", flatImages);
// Pick a default style
defaultStyle = flatImages;
styles.put("default", defaultStyle);
}

public StatusImage getImage(BallColor color) {
return getImage(color, "default");
}

public StatusImage getImage(BallColor color, String style) {
StatusImage[] images = styles.get(style);
if (images == null)
images = defaultStyle;

if (color.isAnimated())
return images[3];

Expand All @@ -58,5 +83,4 @@ public StatusImage getImage(BallColor color) {
}
}


}
Expand Up @@ -90,13 +90,13 @@ public String getDisplayName() {
/**
* Serves the badge image.
*/
public HttpResponse doIcon(StaplerRequest req, StaplerResponse rsp, @QueryParameter String job, @QueryParameter String build) {
public HttpResponse doIcon(StaplerRequest req, StaplerResponse rsp, @QueryParameter String job, @QueryParameter String build, @QueryParameter String style) {
if(build != null) {
Run run = getRun(job, build);
return iconResolver.getImage(run.getIconColor());
return iconResolver.getImage(run.getIconColor(), style);
} else {
AbstractProject<?, ?> project = getProject(job);
return iconResolver.getImage(project.getIconColor());
return iconResolver.getImage(project.getIconColor(), style);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jenkinsci/plugins/badge/RunBadgeAction.java
Expand Up @@ -5,6 +5,7 @@
import hudson.model.Run;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;

public class RunBadgeAction implements Action {
private final RunBadgeActionFactory factory;
Expand Down Expand Up @@ -32,7 +33,7 @@ public String getUrlName() {
/**
* Serves the badge image.
*/
public HttpResponse doIcon() {
return factory.getImage(run.getIconColor());
public HttpResponse doIcon(@QueryParameter String style) {
return factory.getImage(run.getIconColor(), style);
}
}
Expand Up @@ -24,4 +24,8 @@ public Collection<? extends Action> createFor(Run target) {
public StatusImage getImage(BallColor color) {
return iconResolver.getImage(color);
}

public StatusImage getImage(BallColor color, String style) {
return iconResolver.getImage(color, style);
}
}
Expand Up @@ -38,6 +38,8 @@ l.layout {
h3 {
text(_("Image"))
img(id:"badge",src:badge)
text(_(" or "))
img(src:badge + "?style=plastic")
}
b {text(_("protected"))}
input(type:"text",value:badge,class:"select-all")
Expand Down
Expand Up @@ -5,5 +5,10 @@ blurb=Jenkins exposes the current status of your build as an image in a fixed UR
<li><b>protected</b> exposes the badge to users having at least 'Read' permission on the job</li> \
<li><b>unprotected</b> exposes the badge to users having at least 'ViewStatus' permission on the job</li> \
</ul> \
If you want the status icons to be public readable/accessible, just grant the 'ViewStatus' permission globally to 'anonymous'.

If you want the status icons to be public readable/accessible, just grant the 'ViewStatus' permission globally to 'anonymous'. \
<br>There are two styles available: \
<ul> \
<li><b>flat</b> (default since version 1.7)\
<li><b>plastic</b> (the one used in version 1.5)\
</ul> \
You can choose the one you prefer by appending <tt>?style=plastic</tt> to the URL.
Expand Up @@ -38,6 +38,8 @@ l.layout {
h3 {
text(_("Image"))
img(id:"badge",src:badge)
text(_(" or "))
img(src:badge + "?style=plastic")
}
b {text(_("protected"))}
input(type:"text",value:badge,class:"select-all")
Expand Down
Expand Up @@ -5,5 +5,10 @@ blurb=Jenkins exposes the status of the specific build as an image in a fixed UR
<li><b>protected</b> exposes the badge to users having at least 'Read' permission on the job</li> \
<li><b>unprotected</b> exposes the badge to users having at least 'ViewStatus' permission on the job</li> \
</ul> \
If you want the status icons to be public readable/accessible, just grant the 'ViewStatus' permission globally to 'anonymous'.

If you want the status icons to be public readable/accessible, just grant the 'ViewStatus' permission globally to 'anonymous'. \
<br>There are two styles available: \
<ul> \
<li><b>flat</b> (default since version 1.7)\
<li><b>plastic</b> (the one used in version 1.5)\
</ul> \
You can choose the one you prefer by appending <tt>?style=plastic</tt> to the URL.
1 change: 1 addition & 0 deletions src/main/webapp/status/build-failing-red-flat.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/main/webapp/status/build-passing-brightgreen-flat.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/main/webapp/status/build-running-blue-flat.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/main/webapp/status/build-unknown-lightgrey-flat.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/main/webapp/status/build-unstable-yellow-flat.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -63,6 +63,8 @@ public void authenticatedAccess() throws Exception {
wc.goTo("buildStatus/icon?job=free", "image/svg+xml");
j.buildAndAssertSuccess(project);
wc.goTo("buildStatus/icon?job=free&build=1", "image/svg+xml");
wc.goTo("buildStatus/icon?job=free&build=1&style=plastic", "image/svg+xml");
wc.goTo("buildStatus/icon?job=free&build=1&style=unknown", "image/svg+xml");
}

@PresetData(PresetData.DataSet.NO_ANONYMOUS_READACCESS)
Expand Down

0 comments on commit f13eccb

Please sign in to comment.