Permalink
Browse files

[FIXED JENKINS-7838] Supported a different quote set for each build r…

…esult.
  • Loading branch information...
1 parent 98f5ada commit ab57b8b3a750bf51b6a5c4f3843a21878f02bc79 @ssogabe ssogabe committed Feb 20, 2011
View
@@ -0,0 +1,8 @@
+target
+.classpath
+.settings
+.project
+*.iml
+*.ipr
+*.iws
+work
@@ -7,7 +7,8 @@
* @author Kohsuke Kawaguchi
*/
public class ProjectQuoteImpl extends AbstractQuoteImpl {
- public final AbstractProject<?,?> project;
+
+ public final AbstractProject<?, ?> project;
public ProjectQuoteImpl(SimplePersona persona, AbstractProject<?, ?> project) {
super(persona);
@@ -17,18 +18,27 @@ public ProjectQuoteImpl(SimplePersona persona, AbstractProject<?, ?> project) {
@Override
public String getQuote() {
QuoteImpl q = getLastQuote();
- return q!=null ? q.getQuote() : persona.getRandomQuoteText();
+
+ if (q != null) {
+ return q.getQuote();
+ }
+
+ AbstractBuild<?, ?> b = project.getLastBuild();
+
+ return persona.getRandomQuoteText(b);
}
@Override
public Image getImage() {
QuoteImpl q = getLastQuote();
- return q!=null ? q.getImage() : persona.getDefaultImage();
+ return q != null ? q.getImage() : persona.getDefaultImage();
}
private QuoteImpl getLastQuote() {
AbstractBuild<?, ?> b = project.getLastBuild();
- if (b==null) return null;
+ if (b == null) {
+ return null;
+ }
return b.getAction(QuoteImpl.class);
}
}
@@ -25,6 +25,7 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
+import hudson.model.Result;
import hudson.plugins.persona.Persona;
import hudson.plugins.persona.Quote;
@@ -37,22 +38,46 @@
* @author Kohsuke Kawaguchi
*/
public abstract class SimplePersona extends Persona {
+
private volatile List<String> quotes;
+
+ private volatile List<String> quotesSuccess;
+
+ private volatile List<String> quotesFailure;
+
+ private volatile List<String> quotesOther;
+
private final Random random = new Random();
/**
* @param id
* Unique identifier of this persona.
* @param quotes
* Collection of quotes.
+ * @param quotesSuccess
+ * Collection of success quotes.
+ * @param quotesFailure
+ * Collection of failure quotes.
+ * @param quotesOther
+ * Collection of other quotes.
*/
- protected SimplePersona(String id, List<String> quotes) {
+ protected SimplePersona(String id, List<String> quotes,
+ List<String> quotesSuccess, List<String> quotesFailure,
+ List<String> quotesOther) {
super(id);
this.quotes = quotes;
+ this.quotesSuccess = quotesSuccess;
+ this.quotesFailure = quotesFailure;
+ this.quotesOther = quotesOther;
}
- protected void setQuotes(List<String> quotes) {
+ protected void setQuotes(List<String> quotes,
+ List<String> quotesSuccess, List<String> quotesFailure,
+ List<String> quotesOther) {
this.quotes = quotes;
+ this.quotesSuccess = quotesSuccess;
+ this.quotesFailure = quotesFailure;
+ this.quotesOther = quotesOther;
}
/**
@@ -64,15 +89,29 @@ protected void setQuotes(List<String> quotes) {
@Override
public Quote generateQuote(AbstractBuild<?, ?> build) {
- return new QuoteImpl(build,this, getRandomQuoteText());
+ return new QuoteImpl(build, this, getRandomQuoteText(build));
}
- public synchronized String getRandomQuoteText() {
- return quotes.get(random.nextInt(quotes.size()));
+ public synchronized String getRandomQuoteText(AbstractBuild<?, ?> build) {
+ String choosenQuote = null;
+ Result r = build.getResult();
+ if ((r == Result.SUCCESS) && !quotesSuccess.isEmpty()) {
+ choosenQuote = quotesSuccess.get(random.nextInt(quotesSuccess.size()));
+ } else if ((r == Result.FAILURE) && !quotesFailure.isEmpty()) {
+ choosenQuote = quotesFailure.get(random.nextInt(quotesFailure.size()));
+ } else if (!quotesOther.isEmpty()) {
+ choosenQuote = quotesOther.get(random.nextInt(quotesOther.size()));
+ }
+
+ if (null == choosenQuote) {
+ choosenQuote = quotes.get(random.nextInt(quotes.size()));
+ }
+
+ return choosenQuote;
}
@Override
- public Quote generateProjectQuote(AbstractProject<?,?> project) {
- return new ProjectQuoteImpl(this,project);
+ public Quote generateProjectQuote(AbstractProject<?, ?> project) {
+ return new ProjectQuoteImpl(this, project);
}
}
@@ -4,6 +4,8 @@
import hudson.model.Result;
import hudson.plugins.persona.simple.Image;
import hudson.plugins.persona.simple.SimplePersona;
+
+import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
@@ -19,14 +21,21 @@
* @author Kohsuke Kawaguchi
*/
public class XmlBasedPersona extends SimplePersona {
+
public final URL xml;
+
private final URL imageBase;
+
private final String imageBasePath;
private String icon;
+
private String success;
+
private String failure;
+
private String other;
+
private String displayName;
/**
@@ -39,11 +48,11 @@
*/
public static XmlBasedPersona create(URL xml, URL imageBase, String imageBasePath) throws DocumentException, IOException {
Element r = new SAXReader().read(xml).getRootElement();
- return new XmlBasedPersona(r,xml,imageBase,imageBasePath);
+ return new XmlBasedPersona(r, xml, imageBase, imageBasePath);
}
private XmlBasedPersona(Element r, URL xml, URL imageBase, String imageBasePath) throws IOException, DocumentException {
- super(r.attributeValue("id"),new ArrayList<String>());
+ super(r.attributeValue("id"), new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>());
this.xml = xml;
this.imageBase = imageBase;
@@ -58,14 +67,14 @@ private XmlBasedPersona(Element r, URL xml, URL imageBase, String imageBasePath)
private String findImage(URL imageBase, String imageBasePath, String baseName) throws IOException {
for (String ext : EXTENSIONS) {
try {
- new URL(imageBase,baseName+ext).openStream().close();
+ new URL(imageBase, baseName + ext).openStream().close();
// found it.
- return imageBasePath+'/'+baseName+ext;
+ return imageBasePath + '/' + baseName + ext;
} catch (IOException e) {
// not found. try next
}
}
- throw new IOException("No image found that matches "+imageBase+"/"+baseName+".*");
+ throw new IOException("No image found that matches " + imageBase + "/" + baseName + ".*");
}
/**
@@ -81,19 +90,38 @@ public void reload() throws IOException, DocumentException {
this.displayName = r.attributeValue("displayName");
List<String> quotes = new ArrayList<String>();
- for (Element e : (List<Element>)r.elements("quote")) {
- quotes.add(e.getTextTrim());
+ List<String> quotesSuccess = new ArrayList<String>();
+ List<String> quotesFailure = new ArrayList<String>();
+ List<String> quotesOther = new ArrayList<String>();
+ for (Element e : (List<Element>) r.elements("quote")) {
+ Attribute attribute = e.attribute("type");
+ String quote = e.getTextTrim();
+
+ if (null == attribute) {
+ quotes.add(quote);
+ } else if ("success".equalsIgnoreCase(attribute.getText())) {
+ quotesSuccess.add(quote);
+ } else if ("failure".equalsIgnoreCase(attribute.getText())) {
+ quotesFailure.add(quote);
+ } else if ("other".equalsIgnoreCase(attribute.getText())) {
+ quotesOther.add(quote);
+ } else {
+ // unrecognized attribute type, use default quote
+ quotes.add(quote);
+ }
}
- setQuotes(quotes);
+ setQuotes(quotes, quotesSuccess, quotesFailure, quotesOther);
}
@Override
- public Image getImage(AbstractBuild<?,?> build) {
+ public Image getImage(AbstractBuild<?, ?> build) {
Result r = build.getResult();
- if (r== Result.SUCCESS)
+ if (r == Result.SUCCESS) {
return new Image(icon, success);
- if (r== Result.FAILURE)
+ }
+ if (r == Result.FAILURE) {
return new Image(icon, failure);
+ }
return new Image(icon, other);
}
@@ -106,6 +134,6 @@ public String getDisplayName() {
return displayName;
}
- private static final String[] EXTENSIONS = {".jpg",".jpeg",".png",".gif",
- ".JPG",".JPEG",".PNG",".GIF"};
+ private static final String[] EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif",
+ ".JPG", ".JPEG", ".PNG", ".GIF"};
}

0 comments on commit ab57b8b

Please sign in to comment.